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

use-section-highlighting.ts

93 lines3.3 KB

1import { useRouterState } from "@tanstack/react-router";
2import { useEffect, useRef } from "react";
3​
4import { useAppSheetView, useIsAppSheetOpen } from "@/components/app-sheet/app-sheet-store";
5import { useIsInspectorOpen } from "@/components/network/inspector-store";
6import { GUIDE_SECTIONS } from "@/guide/content";
7import { subscribeToAppActions } from "@/lib/app-actions";
8import {
9 INITIAL_APP_SNAPSHOT,
10 isNews,
11 nextAppSnapshot,
12 screenFromRouteId,
13 sectionForAppState,
14} from "@/lib/app-state";
15import type { AppPanel, AppSnapshot, AppStateInput } from "@/lib/app-state";
16​
17import { setActiveSection } from "./guide-store";
18​
19/**
20 * The workspace's highlighting: the app is beside the guide (`isSplit`) or in
21 * the sheet, and the request inspector is a drawer or the sheet's Network view.
22 */
23export function useWorkspaceSectionHighlighting(isSplit: boolean) {
24 const inspectorOpen = useIsInspectorOpen();
25 const sheetOpen = useIsAppSheetOpen();
26 const sheetView = useAppSheetView();
27 const inspectorShown = isSplit ? inspectorOpen : sheetOpen && sheetView === "network";
28 useSectionHighlighting({
29 panel: inspectorShown ? "inspector" : null,
30 appOnScreen: isSplit || sheetOpen,
31 });
33​
34/**
35 * Lights up the guide section that matches what the reader is doing in the
36 * app: the screen they're on, what they just did, and the open panel. When
37 * nothing matches, the last highlight stays, so wandering around the app
38 * doesn't keep switching it off.
39 *
40 * Route changes only count while the app is on screen (`appOnScreen`). On
41 * phones, closing the sheet (Close, a swipe, Back) is a history navigation
42 * back to where the guide was; that's the reader leaving the app, not using
43 * it, so the highlight from the app stays. Opening the sheet again (Try it,
44 * Forward) counts.
45 */
46export function useSectionHighlighting({
47 panel,
48 appOnScreen,
49}: {
50 panel: AppPanel | null;
51 appOnScreen: boolean;
52}) {
53 const snapshotRef = useRef<AppSnapshot>(INITIAL_APP_SNAPSHOT);
54​
55 const applyRef = useRef((input: AppStateInput) => {
56 const next = nextAppSnapshot(snapshotRef.current, input);
57 if (next === snapshotRef.current) return;
58 snapshotRef.current = next;
59 if (!isNews(input)) return;
60 const sectionId = sectionForAppState(next, GUIDE_SECTIONS);
61 if (sectionId) setActiveSection(sectionId);
62 });
63​
64 // The screen, as `routeId|folderId|noteId` so it only changes when one of them does.
65 const screenKey = useRouterState({
66 select: (state) => {
67 const match = state.matches.at(-1);
68 if (!match) return "";
69 const params = match.params as { folderId?: string; noteId?: string };
70 return [match.routeId, params.folderId ?? "", params.noteId ?? ""].join("|");
71 },
72 });
73​
74 // Actions first: a click reports `folder.opened` before the route changes.
75 useEffect(
76 () => subscribeToAppActions((event) => applyRef.current({ kind: "action", event })),
77 [],
78 );
79​
80 useEffect(() => {
81 if (!appOnScreen) return;
82 const [routeId = "", folderId, noteId] = screenKey.split("|");
83 applyRef.current({
84 kind: "navigated",
85 screen: screenFromRouteId(routeId),
86 params: { ...(folderId ? { folderId } : {}), ...(noteId ? { noteId } : {}) },
87 });
88 }, [screenKey, appOnScreen]);
89​
90 useEffect(() => {
91 applyRef.current({ kind: "panel", panel });
92 }, [panel]);