| 1 | import { useCallback, useLayoutEffect, useSyncExternalStore } from "react"; |
| 2 | import { |
| 3 | fileNavigationKey, |
| 4 | type FileNavigationOwner, |
| 5 | type FileNavigationScope, |
| 6 | type FileNavigationScopeKey, |
| 7 | type FileNavigationSnapshot, |
| 8 | } from "../lib/fileNavigationOwner"; |
| 9 | |
| 10 | /** |
| 11 | * Read the committed navigation of one dock instance. Reading never starts a |
| 12 | * navigation: the panel renders what a command already decided, so a remount, |
| 13 | * a StrictMode replay or a parent re-render cannot open a file on its own. |
| 14 | */ |
| 15 | export function useFileNavigationRecord( |
| 16 | owner: FileNavigationOwner, |
| 17 | scope: FileNavigationScope, |
| 18 | key: FileNavigationScopeKey, |
| 19 | ): FileNavigationSnapshot | null { |
| 20 | const recordKey = fileNavigationKey(scope); |
| 21 | const subscribe = useCallback((listener: () => void) => owner.subscribe(listener), [owner]); |
| 22 | const read = useCallback(() => owner.getSnapshot(recordKey), [owner, recordKey]); |
| 23 | const snapshot = useSyncExternalStore(subscribe, read, read); |
| 24 | const { sessionTabId, dockTabId } = scope; |
| 25 | const { resource, session } = key; |
| 26 | useLayoutEffect( |
| 27 | () => { owner.bindScope({ sessionTabId, dockTabId }, { resource, session }); }, |
| 28 | [dockTabId, owner, resource, session, sessionTabId], |
| 29 | ); |
| 30 | return snapshot; |
| 31 | } |
| 32 |