Skip to content

Commit

Permalink
tracer: Scroll handler tree node into view when needed
Browse files Browse the repository at this point in the history
Like when clicking on an event to select the handler that emitted it.
  • Loading branch information
oleavr committed Sep 19, 2024
1 parent 58759c4 commit ada76d8
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions apps/tracer/src/HandlerList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./HandlerList.css";
import { Handler, ScopeId, HandlerId } from "./model.js";
import { Tree, TreeNodeInfo } from "@blueprintjs/core";
import { useEffect, useRef, useState } from "react";

export interface HandlerListProps {
handlers: Handler[];
Expand All @@ -14,6 +15,9 @@ export type ScopeEventHandler = (id: ScopeId) => void;
export type HandlerEventHandler = (id: HandlerId) => void;

export default function HandlerList({ handlers, selectedScope, onScopeSelect, selectedHandler, onHandlerSelect }: HandlerListProps) {
const treeRef = useRef<Tree>(null);
const [mouseInsideNode, setMouseInsideNode] = useState(false);

const scopes = handlers.reduce((result, { scope }) => result.add(scope), new Set<string>());
const handlerNodes: TreeNodeInfo[] = Array.from(scopes).map(scope => {
const isExpanded = scope === selectedScope;
Expand Down Expand Up @@ -51,10 +55,41 @@ export default function HandlerList({ handlers, selectedScope, onScopeSelect, se
onScopeSelect("");
}

useEffect(() => {
if (mouseInsideNode) {
return;
}

const tree = treeRef.current;
if (tree === null) {
return;
}

const scopeElement = tree.getNodeContentElement(selectedScope);
if (scopeElement === undefined) {
return;
}

scopeElement.addEventListener("transitionend", scrollIntoView);

requestAnimationFrame(scrollIntoView);

function scrollIntoView() {
tree!.getNodeContentElement(selectedHandler)?.scrollIntoView({ block: "center" });
}

return () => {
scopeElement.removeEventListener("transitionend", scrollIntoView);
};
}, [treeRef, selectedScope, selectedHandler, mouseInsideNode]);

return (
<Tree
ref={treeRef}
className="handler-list"
contents={handlerNodes}
onNodeMouseEnter={() => setMouseInsideNode(true)}
onNodeMouseLeave={() => setMouseInsideNode(false)}
onNodeClick={handleNodeClick}
onNodeExpand={handleNodeExpand}
onNodeCollapse={handleNodeCollapse}
Expand Down

0 comments on commit ada76d8

Please sign in to comment.