mirror of
https://github.com/block/buzz.git
synced 2026-12-01 13:42:45 +00:00
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:
40
desktop/src/shared/hooks/usePanelReturnTarget.ts
Normal file
40
desktop/src/shared/hooks/usePanelReturnTarget.ts
Normal 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 };
|
||||
}
|
||||
Reference in New Issue
Block a user