Open History to see the last commit
1import { useLocation, useRouter } from "@tanstack/react-router";
2import type { LinkOptions } from "@tanstack/react-router";
3import { useCallback, useSyncExternalStore } from "react";
4
5import { closeSheetAction, isSheetOpen, openSheetState } from "@/lib/app-sheet-history";
6import { isOnScreen } from "@/lib/app-screen";
7
8/**
9 * On small screens the app lives in a sheet over the guide. Whether the sheet
10 * is open is part of browser history (see `lib/app-sheet-history.ts`); this
11 * store only holds what's on top of that:
12 *
13 * - which view the sheet shows: the app, or the latency controls and request
14 * inspector over it,
15 * - the guide section it was last opened from (phase 4 watches for that
16 * section's action),
17 * - where the app was when the sheet last closed, so "Try it" can pick up
18 * there instead of starting over.
19 */
20export type AppSheetView = "app" | "network";
21
22interface AppSheetState {
23 view: AppSheetView;
24 sectionId: string | null;
25}
26
27let state: AppSheetState = { view: "app", sectionId: null };
28let lastSheetHref: string | null = null;
29const listeners = new Set<() => void>();
30
31function update(next: AppSheetState) {
32 state = next;
33 for (const listener of listeners) listener();
34}
35
36function subscribe(listener: () => void) {
37 listeners.add(listener);
38 return () => listeners.delete(listener);
39}
40
41export function setAppSheetView(view: AppSheetView) {
42 if (view !== state.view) update({ ...state, view });
43}
44
45export function useAppSheetView(): AppSheetView {
46 return useSyncExternalStore(
47 subscribe,
48 () => state.view,
49 () => state.view,
50 );
51}
52
53/** Called while the sheet is open, with the app's current location. */
54export function rememberSheetLocation(href: string) {
55 lastSheetHref = href;
56}
57
58export function useIsAppSheetOpen(): boolean {
59 return useLocation({ select: (location) => isSheetOpen(location.state, location.pathname) });
60}
61
62/** Opens the app sheet on a guide section's screen. */
63export function useOpenAppSheet() {
64 const router = useRouter();
65 return useCallback(
66 (sectionId: string, target: LinkOptions) => {
67 const screen = router.buildLocation(target).pathname;
68 const { location } = router.history;
69 // Already on that screen (or deeper in it), now or when the sheet last
70 // closed? Pick up there; otherwise open the section's screen.
71 const resumeHref = [location.href, lastSheetHref].find(
72 (href) =>
73 href !== null && isOnScreen(new URL(href, window.location.origin).pathname, screen),
74 );
75 update({ view: "app", sectionId });
76 void router.navigate({
77 ...(resumeHref ? { href: resumeHref } : target),
78 state: (previous) => ({ ...previous, ...openSheetState(location.state.__TSR_index) }),
79 });
80 },
81 [router],
82 );
83}
84
85/** Closes the sheet by going back to before it opened, or by recording it closed. */
86export function useCloseAppSheet() {
87 const router = useRouter();
88 return useCallback(() => {
89 const { location } = router.history;
90 const action = closeSheetAction(location.state, location.state.__TSR_index);
91 if (action.type === "go") {
92 router.history.go(action.delta);
93 } else {
94 void router.navigate({
95 href: location.href,
96 replace: true,
97 resetScroll: false,
98 state: (previous) => ({ ...previous, ...action.state }),
99 });
100 }
101 }, [router]);
102}