Open History to see the last commit
1/**
2 * Whether the app sheet is open lives in browser history, so Back/Forward,
3 * reloads and direct links all agree with what's on screen.
4 *
5 * The contract (small screens; the split layout ignores it):
6 *
7 * - Every history entry records `appSheet: boolean` in its history state
8 * (not the URL, so the fake address bar never shows it).
9 * - "Try it" pushes an entry with the sheet open and remembers that entry's
10 * index in `appSheetOpenedAt`.
11 * - Navigating without saying anything about the sheet (any link inside the
12 * app) inherits the current entry's state, so the sheet stays open while
13 * you use the app. See `withSheetState`.
14 * - Back/Forward land on entries that carry their own state, so backing out
15 * past the entry that opened the sheet closes it.
16 * - Closing (button, swipe, backdrop, Escape) goes back to the entry before
17 * the sheet opened if we know it; otherwise (the sheet opened on a direct
18 * load) it replaces the current entry with a closed one. See `closeSheetAction`.
19 * - An entry with no record (a direct load, a link from outside) is open on
20 * any app screen except the root, so a shared note link shows the note.
21 */
22
23export interface SheetHistoryState {
24 appSheet?: boolean;
25 /** History index of the entry that opened the sheet ("Try it"). */
26 appSheetOpenedAt?: number;
27}
28
29const ROOT_PATHNAME = "/";
30
31export function readSheetState(state: unknown): SheetHistoryState {
32 if (typeof state !== "object" || state === null) return {};
33 const { appSheet, appSheetOpenedAt } = state as Record<string, unknown>;
34 return {
35 appSheet: typeof appSheet === "boolean" ? appSheet : undefined,
36 appSheetOpenedAt: typeof appSheetOpenedAt === "number" ? appSheetOpenedAt : undefined,
37 };
38}
39
40export function isSheetOpen(state: unknown, pathname: string): boolean {
41 return readSheetState(state).appSheet ?? pathname !== ROOT_PATHNAME;
42}
43
44/**
45 * The state to store for a new entry. If the navigation says whether the
46 * sheet is open, that wins; otherwise the entry inherits the current one's.
47 */
48export function withSheetState<T extends object>(
49 current: { state: unknown; pathname: string },
50 next: T | undefined,
51): T & SheetHistoryState {
52 if (next && "appSheet" in next) return next as T & SheetHistoryState;
53 const appSheet = isSheetOpen(current.state, current.pathname);
54 const { appSheetOpenedAt } = readSheetState(current.state);
55 return {
56 ...(next as T),
57 appSheet,
58 ...(appSheet && appSheetOpenedAt !== undefined ? { appSheetOpenedAt } : {}),
59 };
60}
61
62/** State for the entry "Try it" pushes from the entry at `currentIndex`. */
63export function openSheetState(currentIndex: number): SheetHistoryState {
64 return { appSheet: true, appSheetOpenedAt: currentIndex + 1 };
65}
66
67export type CloseSheetAction =
68 | { type: "go"; delta: number }
69 | { type: "replace"; state: SheetHistoryState };
70
71export function closeSheetAction(state: unknown, currentIndex: number): CloseSheetAction {
72 const { appSheetOpenedAt } = readSheetState(state);
73 if (appSheetOpenedAt !== undefined && appSheetOpenedAt >= 1 && appSheetOpenedAt <= currentIndex) {
74 return { type: "go", delta: appSheetOpenedAt - 1 - currentIndex };
75 }
76 return { type: "replace", state: { appSheet: false, appSheetOpenedAt: undefined } };
77}