MCPcopy Create free account
hub / github.com/block/buzz / useBackForwardControls

Function useBackForwardControls

desktop/src/app/navigation/useBackForwardControls.ts:30–137  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28}
29
30export function useBackForwardControls() {
31 const router = useRouter();
32 const canGoBack = useCanGoBack();
33 const locationState = useRouterState({
34 select: (state) => state.location.state,
35 }) as RouterHistoryState;
36 const locationIndex = locationState.__TSR_index ?? 0;
37 const locationKey =
38 locationState.__TSR_key ?? locationState.key ?? String(locationIndex);
39 const keysByIndexRef = React.useRef(new Map<number, string>());
40 const [maxIndex, setMaxIndex] = React.useState(locationIndex);
41
42 React.useEffect(() => {
43 const keysByIndex = keysByIndexRef.current;
44 const currentKey = keysByIndex.get(locationIndex);
45
46 if (currentKey && currentKey !== locationKey) {
47 for (const storedIndex of [...keysByIndex.keys()]) {
48 if (storedIndex >= locationIndex) {
49 keysByIndex.delete(storedIndex);
50 }
51 }
52 }
53
54 keysByIndex.set(locationIndex, locationKey);
55 trimMapToSize(keysByIndex, 200);
56 setMaxIndex((current: number) => {
57 if (currentKey && currentKey !== locationKey) {
58 return locationIndex;
59 }
60
61 return Math.max(current, locationIndex);
62 });
63 }, [locationIndex, locationKey]);
64
65 const canGoForward = locationIndex < maxIndex;
66
67 const goBack = React.useCallback(() => {
68 if (!canGoBack) {
69 return;
70 }
71
72 router.history.back();
73 }, [canGoBack, router.history]);
74
75 const goForward = React.useCallback(() => {
76 if (!canGoForward) {
77 return;
78 }
79
80 router.history.forward();
81 }, [canGoForward, router.history]);
82
83 const handleKeyDown = React.useEffectEvent((event: KeyboardEvent) => {
84 if (isEditableTarget(event.target)) {
85 return;
86 }
87

Callers 1

AppShellFunction · 0.90

Calls 7

trimMapToSizeFunction · 0.90
isMacPlatformFunction · 0.90
isEditableTargetFunction · 0.85
keysMethod · 0.80
setMethod · 0.80
getMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected