core/workbench-ui/src/app/evidenceUrlSync.tsx
Shay 41f4435dc1 feat(workbench): evidence addresses — deep-linkable subjects + inspector URL state
URL = subject: one codec (evidenceAddress.ts) owns the address grammar for
every EvidenceSubject kind, including kinds whose routes are placeholders.
urlToSubject is a total inverse — malformed input yields null, never throws.

- Route params: /trace/:turnId?, /proposals/:proposalId?, /evals/:laneId?,
  /replay/:artifactId? (placeholders keep flat paths)
- ?inspect= carries inspector subject + open state; EvidenceUrlSync restores
  deep links and write-through syncs with replace (selection churn never
  pollutes history); malformed values are dropped from the URL
- Proposals/Evals/Replay restore selection from their param on load, write it
  on selection change (replace), and publish the selected subject to the
  evidence context (identity at once, detail when the query resolves)
- EvidenceSubject.data now optional; inspectors render an honest
  'detail not loaded' state for identity-only subjects
- Cmd+Shift+C copies origin + subjectToUrl(subject); no-op without subject;
  input-focus guard; transient inline 'Copied' on the inspector header
- Fix pre-existing unbounded re-render loop in RightInspector.test.tsx
  (render-phase setSubject); subjects now set from effects
2026-06-12 12:17:57 -07:00

58 lines
1.7 KiB
TypeScript

import { useEffect, useRef } from "react";
import { useSearchParams } from "react-router-dom";
import { useEvidenceSubject } from "./evidenceContext";
import {
INSPECT_PARAM,
inspectValueToSubject,
isAddressable,
subjectToInspectValue,
} from "./evidenceAddress";
// Keeps `?inspect=` and the evidence context in sync.
// On first mount a valid `?inspect=` deep link restores the inspector
// (identity-only subject; the owning route loads detail). After that the
// URL follows state: param present iff the inspector is open on an
// addressable subject. All writes use replace — selection churn must not
// pollute history. A malformed `?inspect=` is dropped from the URL.
export function EvidenceUrlSync() {
const { subject, setSubject, inspectorOpen, setInspectorOpen } =
useEvidenceSubject();
const [searchParams, setSearchParams] = useSearchParams();
const restoredRef = useRef(false);
useEffect(() => {
if (!restoredRef.current) {
restoredRef.current = true;
const restored = inspectValueToSubject(searchParams.get(INSPECT_PARAM));
if (restored) {
setSubject(restored);
setInspectorOpen(true);
return;
}
}
const desired =
inspectorOpen && isAddressable(subject)
? subjectToInspectValue(subject)
: null;
const current = searchParams.get(INSPECT_PARAM);
if (desired === current) return;
const next = new URLSearchParams(searchParams);
if (desired === null) {
next.delete(INSPECT_PARAM);
} else {
next.set(INSPECT_PARAM, desired);
}
setSearchParams(next, { replace: true });
}, [
subject,
inspectorOpen,
searchParams,
setSearchParams,
setSubject,
setInspectorOpen,
]);
return null;
}