Skip to content

Commit

Permalink
make logs active when block is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
breeg554 committed Dec 13, 2024
1 parent 87f6890 commit c96ffdb
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useMemo, useState } from 'react';
import { useFetcher } from '@remix-run/react';
import { useStore } from '@xyflow/react';
import { ClientOnly } from 'remix-utils/client-only';
import { useLocalStorage } from 'usehooks-ts';

Expand Down Expand Up @@ -187,6 +188,10 @@ function SidebarLogs({
const fetcher = useFetcher<typeof logsLoader>();
const [key, setKey] = useState(Date.now());

const selectedNode = useStore((state) =>
state.nodes.find((node) => node.selected),
);

useEffect(() => {
if (runId) {
fetcher.load(
Expand All @@ -203,6 +208,35 @@ function SidebarLogs({
}
}, [fetcher.state, blockName]);

const clearActiveLogs = () => {
const rows = [
...document.querySelectorAll(`[data-logactive="true"]`),
] as HTMLDivElement[];

rows.forEach((row) => {
row.removeAttribute('data-logactive');
});
};

const setActiveLogs = (id: string) => {
const rows = [
...document.querySelectorAll(`[data-log="${buildLogRowKey(id)}"]`),
] as HTMLDivElement[];

rows.forEach((row) => {
row.setAttribute('data-logactive', 'true');
});
};

useEffect(() => {
if (selectedNode) {
clearActiveLogs();
setActiveLogs(selectedNode.id);
} else {
clearActiveLogs();
}
}, [selectedNode?.id]);

if (isStarting) {
return (
<EmptyMessage className="text-xs mx-auto block w-fit mt-4">
Expand Down Expand Up @@ -237,7 +271,13 @@ function SidebarLogs({
<ItemList
items={logs}
renderItem={(log) => (
<Log log={log} className={cn('py-1')}>
<Log
log={log}
className={cn(
'py-1 data-[logactive=true]:bg-primary/5 data-[logactive=true]:hover:bg-muted',
)}
data-log={buildLogRowKey(log.block_name)}
>
<LogDate className="mr-2">{log.created_at}</LogDate>
<LogTopic className="mr-2">{log.context}</LogTopic>
<LogBlockName className="mr-2">{log.block_name}</LogBlockName>
Expand Down Expand Up @@ -267,3 +307,7 @@ function SidebarLogs({
function buildLSKey(organizationId: string) {
return hashString('builder-bottom-sidebar-' + organizationId).toString();
}

function buildLogRowKey(blockName: string) {
return `${blockName}-log-row`;
}

0 comments on commit c96ffdb

Please sign in to comment.