Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2

Phase 4: Interactivity #7

Mergedfukomerged 16 commits intomain fromphase-4-interactivitymerged by fuko

fukocommented

Phase 4 connects the guide and the app. Using the app moves the guide, and the guide can point at the app. The browser toolbar sets latency and failures, and every request shows up live in the inspector.

1. Section highlighting from app state

  • lib/app-state.ts keeps a snapshot of the app: the screen and its params, the last action, and the open panel. Each part records when it last changed.
  • The section whose appStates match lights up. When several match, the one about the newest change wins, then the more specific state, then the one earlier in the guide.
  • Rules that keep it from jumping around:
    • A rolled-back change only matches Rollbacks.
    • A confirmed save only counts while it's still the last action, so a save that lands after you've moved on doesn't move the guide.
    • Navigating forgets the last action, unless it was the click that opened the folder or note you landed on.
    • When nothing matches, the last highlight stays.
    • Route changes only count while the app is on screen. On phones, closing the sheet (Close, a swipe, Back) is a history move back to the guide, not app navigation, so the highlight stays. Opening it again (Try it, Forward) counts.
    • A panel closing (the inspector drawer, the Network view) isn't news: the highlight stays.
  • New mappings: setting the latency → Optimistic updates; arming "fail next" → Rollbacks; opening the request inspector → How collections sync.
  • On macOS, opening a folder lands on its first note. Collections and Live queries also match that note screen when it was reached by opening a folder, so the guide keeps following the app with the inspector left open.
  • Don't fight the reader: the guide scrolls to the section only if the reader hasn't scrolled it in the last 2 s, and only if less than 30% of the section is visible. With reduced motion it jumps instead of scrolling smoothly.
  • The guide header says again that the matching section lights up.

2. "Show me" buttons (spotlight)

  • Inline ShowMe buttons in the guide can point at:
    • the folder counts, the note list and the editor (Collections);
    • the network controls and the request inspector (Intro, Optimistic updates, Rollbacks, Syncing);
    • Analytics (Aggregations).
  • If the target isn't on screen, the button first takes the app there. The editor case opens the newest note in your folder, picked with a one-shot live query (queryOnce(newestNoteQuery)).
  • Hidden targets are revealed first. A hidden macOS sidebar and a collapsed iPhone iCloud section register a "reveal" for the folder counts. When the toolbar is narrow, the network popover carries the target. On narrow desktops (iPhone layout), the app navigates to the screen that has the target.
  • If a target still doesn't show up within 2 s (for example, there are no notes to open), a short toast says what couldn't be shown and where to find it.
  • Accessibility:
    • The spotlight ignores the pointer and never takes focus.
    • Escape, any click or tap, or 8 s dismiss it. Escape is consumed by the spotlight, so it doesn't also close the sheet.
    • It's announced through a polite status region.
    • With prefers-reduced-motion, it doesn't fade in or pulse.

3. Latency and failure controls

  • The browser toolbar has the presets (0 ms, 500 ms, 2 s, 5 s) and a "Fail next" toggle. When the toolbar is narrow, they move into a popover (shadcn popover).
  • The existing Network panel is now enabled, and the mobile "Network" button in the sheet still opens it. It shows a pending-request badge.
  • "Fail next" is decided when a request is sent: it catches the next request the app sends, not one already in flight, and that request still fails only after its latency. If the request that took it is aborted, "Fail next" is armed again.
  • Rollbacks now also tells readers to use "Fail next". The duplicate-folder-name and long-title triggers are still there.

4. Request inspector

  • Desktop: a drawer under the app, opened with "Requests" in the toolbar or from the guide. Phones: the list in the sheet's Network view.
  • Each row shows method, URL, status and time. While a request is pending, its time counts up, with a bar filling towards its latency. Expand a row to see the request and response bodies.
  • The log now records each request's latencyMs, so a latency change is visible on the very next request.
  • Requests from the query-driven sync demo appear too, through the shared log.

5. Analytics with TanStack Charts

  • @tanstack/charts 1.2.1 (pinned) with its React adapter, @tanstack/charts/react.
  • Package note: both @tanstack/charts and @tanstack/react-charts exist at 1.2.1. The README of @tanstack/react-charts says it's a compatibility package and that new apps should use @tanstack/charts/react, so only @tanstack/charts is installed.
  • Three charts, from the existing live groupBy queries:
    • notes per folder;
    • words per folder;
    • notes by day of last edit (the last 14 days, plus how many notes were last edited before that).
  • They follow the text colour in light and dark mode. The bars use a darker Notes yellow in light mode for contrast. Each chart has an accessible name and a description listing its values, plus a "Show as table" disclosure with a semantic table of the same rows.
  • The Aggregations section also draws words per folder from the same query. With the app beside the guide, you can type and watch it change.
  • Bundle: charts.tsx is the only module that imports @tanstack/charts, and it's loaded lazily. The guide's chart starts loading when it's within a screen of the guide's scroll container; the Analytics page loads it when it shows. Client chunks, before → after:
    • index: 521.21 → 398.94 kB (main: 387.37);
    • queries: 573.69 → 474.98 kB (main: 386.17);
    • charts: new, 121.88 kB, lazy.
    • The 500 kB warning is gone. The remaining sourcemap warning also comes from main.
  • If the chart chunk fails to load, each chart shows "Couldn't load the chart." with a "Try again" button in its own place, through a chart-local error boundary. The page, the guide and the "Show as table" tables stay. "Try again" retries every chart. It re-imports the chunk under a ?retry= URL, because browsers remember a failed module URL; Chromium and Firefox put that URL in the error message.
  • That URL is only used if it's what a failed dynamic import names, on the page's origin, in the same directory as the loader's module (the build's assets, or the source folder in dev), and named like the chart chunk (charts-<hash>.js, or charts.tsx in dev). Otherwise the retry uses the plain import (charts-loader.ts).

Phone behaviour (the choices)

  • Highlighting while the sheet is open: the section lights up behind the sheet and in the header chip right away. The guide doesn't scroll while the sheet covers it. With the sheet closed, the guide alone doesn't light anything up.
  • When the sheet closes: the guide first returns to the exact scroll position (as the mobile brief asks). If the highlight moved while the sheet was open and that section isn't visible, it then scrolls there after about 350 ms.
  • Toasts: at the bottom, just above the iPhone layout's floating bottom bar. At the top they covered the sheet's Close and Network buttons. Wide screens keep bottom-right.
  • "Show me": opens the sheet first, on the right screen (the Network view for the network targets), then spotlights the target once it has stopped moving.
  • Inspector: the "tab" is the existing Network view, with the controls on top and the live request list below.

Tests

  • App state → section: the snapshot reducer, the matching rules, and scenarios played against the real GUIDE_SECTIONS.
  • The workspace's real highlighting hook (happy-dom, memory history, the real sheet store): Close, Back and Forward keep the highlight; closing the inspector doesn't move it.
  • Show me reveals a hidden sidebar without navigating, and the spotlight reports a target that never shows up.
  • The "Show as table" table is semantic, with column and row headers.
  • A chart import that rejects leaves the page and its tables in place, and "Try again" loads the charts.
  • The retry URL: negative tests (another origin, same-origin non-chunk paths, errors that aren't failed imports), the ?retry= branch, and one import for concurrent loads and for concurrent retries.
  • The inspector left open: Analytics → Requests → Personal → All iCloud, as app-state sequences and through the real hook (with a macOS-style folder → first-note redirect), plus the phone Network view.
  • "Fail next" turned on while a request is in flight doesn't fail it; the next request sent does, after its latency.
  • The inspector reacts to the log: RequestLog rendered to static markup while requests are pending and after they settle.
  • Each latency preset reaches the fake API, and "fail next" fails exactly one request.
  • Chart data comes from the live folderStats and editsPerDay queries and follows an optimistic edit and move.
  • Also checked by hand: at 5 s latency, typing and pinning the same note quickly. The note list went A → AB → unpinned → ABC and never reverted.

Open decisions

  • Keep @tanstack/react-charts out (it's a compatibility package), or add it anyway because the brief names it?
  • "Notes by day of last edit" shows the last 14 days; older notes are summarised in a caption. An alternative would be a time axis over the whole year.
  • On desktop the Analytics page replaces the note list and editor, so you can't type and watch that page at the same time. The live chart in the guide covers "updates as you type".
  • The spotlight dims the whole page, the guide included, and dismisses itself after 8 s.

🤖 Generated with Claude Code

    1. fuko merged this0007401

    2. fuko closed this

Sign in to join this conversation.