mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-11-17 09:31:45 +00:00
Switching from a run's Overview tab to another tab and back reset the graph zoom and position to the default. Now it holds. ## Why The viewport (pan and zoom) lived in `RunOverview` component state. Overview and Stages are sibling routes under `runs/:id`, so switching tabs unmounts Overview and drops that state. ## Fix `apps/fabro-web/app/routes/run-overview.tsx`: cache the viewport per run outside the component so it survives the remount, and reset it when the run id changes, since the route instance is reused when only the id changes. Added two tests: viewport restores on remount for the same run, and does not carry across runs. Does not persist across a full page reload (in-memory only). --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Bryan Helmkamp <bryan@brynary.com>
39 lines
1.9 KiB
TypeScript
39 lines
1.9 KiB
TypeScript
import { useEffect, useRef, useState } from "react";
|
|
import type { Dispatch, SetStateAction } from "react";
|
|
|
|
import { DEFAULT_GRAPH_VIEW, type GraphView } from "../lib/graph-viewport";
|
|
|
|
// Remember each run's graph pan/zoom so it survives leaving and returning to the Overview
|
|
// tab — that route unmounts when you switch to Stages/Files/etc. and remounts on return, so
|
|
// the viewport can't live only in component state. In-memory for the session; a full reload
|
|
// starts fresh. Tradeoff: unpruned Map, entries are three numbers each and a session views
|
|
// few runs — swap for an LRU if that ever stops holding.
|
|
const graphViewByRun = new Map<string, GraphView>();
|
|
|
|
const loadGraphView = (runId: string | undefined): GraphView =>
|
|
(runId ? graphViewByRun.get(runId) : undefined) ?? DEFAULT_GRAPH_VIEW;
|
|
|
|
/**
|
|
* Synchronizes a run's graph viewport with the session-scoped store above, so the
|
|
* viewport is remembered per run across unmounts. `runId` controls which entry the
|
|
* state binds to: when it changes, the returned view resets to that run's remembered
|
|
* (or default) viewport instead of carrying the previous run's over. Every update is
|
|
* written back to the store; the store itself needs no cleanup. Safe under Strict
|
|
* Mode — the write is idempotent and re-runs persist the same committed value.
|
|
*/
|
|
export function useRememberedGraphView(
|
|
runId: string | undefined,
|
|
): [GraphView, Dispatch<SetStateAction<GraphView>>] {
|
|
const [view, setView] = useState<GraphView>(() => loadGraphView(runId));
|
|
// Callers keep their component instance when only the route param changes, so the
|
|
// rebind is a render-phase reset rather than a mount.
|
|
const viewedRunId = useRef(runId);
|
|
if (viewedRunId.current !== runId) {
|
|
viewedRunId.current = runId;
|
|
setView(loadGraphView(runId));
|
|
}
|
|
useEffect(() => {
|
|
if (runId) graphViewByRun.set(runId, view);
|
|
}, [runId, view]);
|
|
return [view, setView];
|
|
}
|