Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

app-sheet-store.ts

127 lines4.1 KB

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,
16 * - where the app was when the sheet last closed, so "Try it" can pick up
17 * there instead of starting over.
18 */
19export type AppSheetView = "app" | "network";
20​
21interface AppSheetState {
22 view: AppSheetView;
23 sectionId: string | null;
25​
26let state: AppSheetState = { view: "app", sectionId: null };
27let lastSheetHref: string | null = null;
28const listeners = new Set<() => void>();
29​
30function update(next: AppSheetState) {
31 state = next;
32 for (const listener of listeners) listener();
34​
35function subscribe(listener: () => void) {
36 listeners.add(listener);
37 return () => listeners.delete(listener);
39​
40export function setAppSheetView(view: AppSheetView) {
41 if (view !== state.view) update({ ...state, view });
43​
44export function useAppSheetView(): AppSheetView {
45 return useSyncExternalStore(
46 subscribe,
47 () => state.view,
48 () => state.view,
49 );
51​
52/** Called while the sheet is open, with the app's current location. */
53export function rememberSheetLocation(href: string) {
54 lastSheetHref = href;
56​
57export function useIsAppSheetOpen(): boolean {
58 return useLocation({ select: (location) => isSheetOpen(location.state, location.pathname) });
60​
61/** Opens the app sheet on a guide section's screen. */
62export function useOpenAppSheet() {
63 const router = useRouter();
64 return useCallback(
65 (sectionId: string, target: LinkOptions) => {
66 const screen = router.buildLocation(target).pathname;
67 const { location } = router.history;
68 // Already on that screen (or deeper in it), now or when the sheet last
69 // closed? Pick up there; otherwise open the section's screen.
70 const resumeHref = [location.href, lastSheetHref].find(
71 (href) =>
72 href !== null && isOnScreen(new URL(href, window.location.origin).pathname, screen),
73 );
74 update({ view: "app", sectionId });
75 void router.navigate({
76 ...(resumeHref ? { href: resumeHref } : target),
77 state: (previous) => ({ ...previous, ...openSheetState(location.state.__TSR_index) }),
78 });
79 },
80 [router],
81 );
83​
84/** Closes the sheet by going back to before it opened, or by recording it closed. */
85export function useCloseAppSheet() {
86 const router = useRouter();
87 return useCallback(() => {
88 const { location } = router.history;
89 const action = closeSheetAction(location.state, location.state.__TSR_index);
90 if (action.type === "go") {
91 router.history.go(action.delta);
92 } else {
93 void router.navigate({
94 href: location.href,
95 replace: true,
96 resetScroll: false,
97 state: (previous) => ({ ...previous, ...action.state }),
98 });
99 }
100 }, [router]);
102​
103/**
104 * Shows something in the app sheet, for the guide's "show me" buttons: opens
105 * the sheet if it's closed, on `target` (or wherever the app is, if `null`),
106 * with `view` on top.
107 */
108export function useShowInAppSheet() {
109 const router = useRouter();
110 return useCallback(
111 (target: LinkOptions | null, view: AppSheetView) => {
112 const { location } = router.history;
113 const alreadyOpen = isSheetOpen(location.state, router.state.location.pathname);
114 update({ ...state, view });
115 if (alreadyOpen) {
116 // Links inside the open sheet keep it open.
117 if (target) void router.navigate(target);
118 return;
119 }
120 void router.navigate({
121 ...(target ?? { href: location.href }),
122 state: (previous) => ({ ...previous, ...openSheetState(location.state.__TSR_index) }),
123 });
124 },
125 [router],
126 );