fix(activity panel): handle back navigation (#1487)

Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
Taylor Ho
2026-07-05 09:24:14 -07:00
committed by GitHub
parent 6716cd31a5
commit caf644eda7
12 changed files with 410 additions and 5 deletions

View File

@@ -0,0 +1,40 @@
import * as React from "react";
import {
createPanelReturnTargetStore,
type PanelReturnTargetStore,
} from "@/shared/lib/panelReturnTarget";
/**
* React binding for `createPanelReturnTargetStore`: a stable return-target
* breadcrumb for mutually-exclusive panels, plus a reactive `hasTarget` so
* back affordances can hide when there is nowhere to return to.
*
* The store identity is stable for the component's lifetime, so callbacks
* can list it as a dependency without churning. A change of `resetKey`
* (e.g. the active channel id) drops any recorded target, keeping
* breadcrumbs from leaking across contexts.
*/
export function usePanelReturnTarget<T>(resetKey: unknown = null): {
hasTarget: boolean;
store: PanelReturnTargetStore<T>;
} {
const storeRef = React.useRef<PanelReturnTargetStore<T> | null>(null);
storeRef.current ??= createPanelReturnTargetStore<T>();
const store = storeRef.current;
const previousResetKeyRef = React.useRef(resetKey);
if (previousResetKeyRef.current !== resetKey) {
previousResetKeyRef.current = resetKey;
// Render-safe silent drop: useSyncExternalStore re-reads the snapshot
// during this same render, so no notification is needed (or allowed).
store.reset();
}
const hasTarget = React.useSyncExternalStore(
store.subscribe,
() => store.peek() != null,
);
return { hasTarget, store };
}