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

spotlight-targets.ts

69 lines2.7 KB

1import type { LinkOptions } from "@tanstack/react-router";
2​
3/**
4 * Parts of the app the guide can point at ("Show me"). Each is marked in the
5 * app with `data-spotlight="<target>"`, in both the macOS and the iPhone
6 * layout.
7 */
8export const SPOTLIGHT_TARGETS = {
9 "folder-counts": { label: "Folder counts", hint: "They're next to each folder in the app." },
10 "note-list": { label: "Note list", hint: "Open a folder in the app to see it." },
11 editor: { label: "Editor", hint: "There are no notes to open. Create one in the app first." },
12 "network-controls": {
13 label: "Network controls",
14 hint: "They're in the browser toolbar, or under Network on a phone.",
15 },
16 "request-inspector": {
17 label: "Request inspector",
18 hint: "Open it with Requests in the browser toolbar, or under Network on a phone.",
19 },
20 analytics: { label: "Analytics", hint: "Open Analytics at the bottom of the folder list." },
21} as const satisfies Record<string, { label: string; hint: string }>;
22​
23export type SpotlightTarget = keyof typeof SPOTLIGHT_TARGETS;
24​
25/** The network targets live next to the app (toolbar, drawer, the sheet's Network view). */
26export function isNetworkTarget(target: SpotlightTarget): boolean {
27 return target === "network-controls" || target === "request-inspector";
29​
30/** The folder the app is in (All iCloud outside a folder). */
31export function folderIdFromPath(pathname: string): string {
32 return /^\/folders\/([^/]+)/.exec(pathname)?.[1] ?? "all";
34​
35/**
36 * Where to take the app so `target` is on screen, when it isn't already:
37 * the folder you're in (or All iCloud) for the list, a note for the editor
38 * (`noteToOpen`: the caller picks it), and so on. `null`: it's not about the
39 * route, or there's nowhere to go (no notes at all).
40 */
41export function routeForTarget(
42 target: SpotlightTarget,
43 pathname: string,
44 noteToOpen?: { folderId: string; noteId: string },
45): LinkOptions | null {
46 switch (target) {
47 case "folder-counts":
48 return { to: "/" };
49 case "note-list":
50 return { to: "/folders/$folderId", params: { folderId: folderIdFromPath(pathname) } };
51 case "editor":
52 return noteToOpen ? { to: "/folders/$folderId/notes/$noteId", params: noteToOpen } : null;
53 case "analytics":
54 return { to: "/analytics" };
55 case "network-controls":
56 case "request-inspector":
57 return null;
58 }
60​
61/** The element for `target` that's showing, if any (layouts can render a hidden copy). */
62export function findSpotlightElement(target: SpotlightTarget): HTMLElement | null {
63 const candidates = document.querySelectorAll<HTMLElement>(`[data-spotlight="${target}"]`);
64 for (const element of candidates) {
65 const rect = element.getBoundingClientRect();
66 if (rect.width > 0 && rect.height > 0) return element;
67 }
68 return null;