(resetKey: unknown = null)
| 16 | * breadcrumbs from leaking across contexts. |
| 17 | */ |
| 18 | export function usePanelReturnTarget<T>(resetKey: unknown = null): { |
| 19 | hasTarget: boolean; |
| 20 | store: PanelReturnTargetStore<T>; |
| 21 | } { |
| 22 | const storeRef = React.useRef<PanelReturnTargetStore<T> | null>(null); |
| 23 | storeRef.current ??= createPanelReturnTargetStore<T>(); |
| 24 | const store = storeRef.current; |
| 25 | |
| 26 | const previousResetKeyRef = React.useRef(resetKey); |
| 27 | if (previousResetKeyRef.current !== resetKey) { |
| 28 | previousResetKeyRef.current = resetKey; |
| 29 | // Render-safe silent drop: useSyncExternalStore re-reads the snapshot |
| 30 | // during this same render, so no notification is needed (or allowed). |
| 31 | store.reset(); |
| 32 | } |
| 33 | |
| 34 | const hasTarget = React.useSyncExternalStore( |
| 35 | store.subscribe, |
| 36 | () => store.peek() != null, |
| 37 | ); |
| 38 | |
| 39 | return { hasTarget, store }; |
| 40 | } |
no test coverage detected