From 3ca0e569ed38dea0a179935986ac64e5852b772b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Andr=C3=A9=20Vadla=20Ravn=C3=A5s?= Date: Thu, 19 Sep 2024 15:11:50 +0200 Subject: [PATCH] =?UTF-8?q?tracer:=20Add=20=E2=80=9CLatest=20Event?= =?UTF-8?q?=E2=80=9D=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To scroll the event view to the latest event emitted by the current handler, and highlight it. --- apps/tracer/src/App.tsx | 27 ++++++++++++++++++++++++++- apps/tracer/src/EventView.css | 12 ++++++++++++ apps/tracer/src/EventView.tsx | 17 +++++++++++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/apps/tracer/src/App.tsx b/apps/tracer/src/App.tsx index 3839971d..68e612b2 100644 --- a/apps/tracer/src/App.tsx +++ b/apps/tracer/src/App.tsx @@ -31,6 +31,8 @@ export default function App() { const [draftedCode, setDraftedCode] = useState(""); const [addingTargets, setAddingTargets] = useState(false); const [events, setEvents] = useState([]); + const [latestMatchingEventIndex, setLatestMatchingEventIndex] = useState(null); + const [highlightedEventIndex, setHighlightedEventIndex] = useState(null); const [stagedItems, setStagedItems] = useState([]); const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket( (import.meta.env.MODE === "development") @@ -39,6 +41,7 @@ export default function App() { function handleHandlerSelection(id: HandlerId) { setSelectedHandler(id); + setHighlightedEventIndex(null); sendJsonMessage({ type: "handler:load", id }); } @@ -111,6 +114,17 @@ export default function App() { }, [lastJsonMessage]); + useEffect(() => { + for (let i = events.length - 1; i !== -1; i--) { + const event = events[i]; + if (event[0] === selectedHandler) { + setLatestMatchingEventIndex(i); + return; + } + } + setLatestMatchingEventIndex(null); + }, [selectedHandler, events]); + const connectionError = (readyState === ReadyState.CLOSED) ? Deploy + - + void; const NON_BLOCKING_SPACE = "\u00A0"; const INDENT = NON_BLOCKING_SPACE.repeat(3) + "|" + NON_BLOCKING_SPACE; -export default function EventView({ events, onActivate }: EventViewProps) { +export default function EventView({ events, highlightedIndex = null, onActivate }: EventViewProps) { + const highlightedRef = useRef(null); let lastTid: number | null = null; + useEffect(() => { + highlightedRef.current?.scrollIntoView({ block: "center" }); + }, [highlightedRef, highlightedIndex]); + return (
@@ -38,8 +45,14 @@ export default function EventView({ events, onActivate }: EventViewProps) { lastTid = threadId; } + const isHighlighted = i === highlightedIndex; + result.push( -
+
{timestampStr} ms {INDENT.repeat(depth)}