Skip to content

Commit

Permalink
tracer: Add “Latest Event” button
Browse files Browse the repository at this point in the history
To scroll the event view to the latest event emitted by the current
handler, and highlight it.
  • Loading branch information
oleavr committed Sep 19, 2024
1 parent ada76d8 commit 3ca0e56
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
27 changes: 26 additions & 1 deletion apps/tracer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default function App() {
const [draftedCode, setDraftedCode] = useState("");
const [addingTargets, setAddingTargets] = useState(false);
const [events, setEvents] = useState<Event[]>([]);
const [latestMatchingEventIndex, setLatestMatchingEventIndex] = useState<number | null>(null);
const [highlightedEventIndex, setHighlightedEventIndex] = useState<number | null>(null);
const [stagedItems, setStagedItems] = useState<StagedItem[]>([]);
const { sendJsonMessage, lastJsonMessage, readyState } = useWebSocket<TracerMessage>(
(import.meta.env.MODE === "development")
Expand All @@ -39,6 +41,7 @@ export default function App() {

function handleHandlerSelection(id: HandlerId) {
setSelectedHandler(id);
setHighlightedEventIndex(null);
sendJsonMessage({ type: "handler:load", id });
}

Expand Down Expand Up @@ -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)
? <Callout
title="Lost connection to frida-trace"
Expand Down Expand Up @@ -145,6 +159,13 @@ export default function App() {
>
Deploy
</Button>
<Button
icon="arrow-down"
disabled={latestMatchingEventIndex === null}
onClick={() => setHighlightedEventIndex(latestMatchingEventIndex)}
>
Latest Event
</Button>
</ButtonGroup>
<HandlerEditor
handlerId={selectedHandler}
Expand All @@ -156,7 +177,11 @@ export default function App() {
</Resplit.Pane>
<Resplit.Splitter className="app-splitter" order={1} size="5px" />
<Resplit.Pane className="bottom-area" order={2} initialSize="0.3fr">
<EventView events={events} onActivate={handleEventActivation} />
<EventView
events={events}
highlightedIndex={highlightedEventIndex}
onActivate={handleEventActivation}
/>
</Resplit.Pane>
</Resplit.Root>
<AddTargetsDialog
Expand Down
12 changes: 12 additions & 0 deletions apps/tracer/src/EventView.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@
font-weight: bold;
white-space: pre-line;
}

.event-highlighted {
background-color: #EF6456;
}

.event-highlighted .event-timestamp {
color: white !important;
}

.event-highlighted .event-message {
color: white !important;
}
17 changes: 15 additions & 2 deletions apps/tracer/src/EventView.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import "./EventView.css";
import { Event, HandlerId } from "./model.js";
import { Button } from "@blueprintjs/core";
import { useEffect, useRef } from "react";
import ScrollToBottom from "react-scroll-to-bottom";

export interface EventViewProps {
events: Event[];
highlightedIndex: number | null;
onActivate: ActivateEventHandler;
}

Expand All @@ -13,9 +15,14 @@ export type ActivateEventHandler = (id: HandlerId) => 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<HTMLDivElement>(null);
let lastTid: number | null = null;

useEffect(() => {
highlightedRef.current?.scrollIntoView({ block: "center" });
}, [highlightedRef, highlightedIndex]);

return (
<ScrollToBottom className="event-view">
<div className="event-items">
Expand All @@ -38,8 +45,14 @@ export default function EventView({ events, onActivate }: EventViewProps) {
lastTid = threadId;
}

const isHighlighted = i === highlightedIndex;

result.push(
<div key={i}>
<div
key={i}
ref={isHighlighted ? highlightedRef : undefined}
className={isHighlighted ? "event-highlighted" : ""}
>
<span className="event-timestamp">{timestampStr} ms</span>
<span className={"event-indent " + colorClass}>{INDENT.repeat(depth)}</span>
<Button
Expand Down

0 comments on commit 3ca0e56

Please sign in to comment.