Open History to see the last commit
1/**
2 * Guide content. Phase 1 ships placeholder copy so the layout can be reviewed;
3 * phase 3 rewrites each section against the working app. Section ids are
4 * stable: phase 4 maps app state (route, last action…) to them.
5 */
6import { linkOptions } from "@tanstack/react-router";
7import type { LinkOptions } from "@tanstack/react-router";
8import type { ReactNode } from "react";
9
10import { Callout } from "@/components/guide/callout";
11import { CodeBlock } from "@/components/guide/code-block";
12import type { AppActionMatcher } from "@/lib/app-actions";
13
14export interface GuideSectionContent {
15 id: string;
16 eyebrow: string;
17 title: string;
18 /** Label for the active-section chip in the mobile header. Keep it short: it's never truncated. */
19 shortTitle: string;
20 /**
21 * The app screen this section's hands-on step happens on. Sections with one
22 * get a "Try it" button on mobile that opens the app there.
23 */
24 tryIt?: LinkOptions;
25 /**
26 * The app actions that count as doing this section's hands-on step; the
27 * first one that happens marks the section done (see `section-progress.ts`).
28 */
29 doneWhen?: AppActionMatcher[];
30 content: ReactNode;
31}
32
33export const GUIDE_SECTIONS: GuideSectionContent[] = [
34 {
35 id: "introduction",
36 eyebrow: "Start here",
37 title: "A notes app where every view is a query",
38 shortTitle: "Intro",
39 tryIt: linkOptions({ to: "/" }),
40 doneWhen: [{ type: "note.opened" }],
41 content: (
42 <>
43 <p>
44 Next to this guide is a small clone of Apple Notes running in a fake browser. It talks to
45 a fake API, so you can slow the network down or make requests fail and see how the UI
46 reacts.
47 </p>
48 <p>
49 This guide walks you through how it's built with <strong>TanStack DB</strong>. Each
50 section tells you what to do in the app and what to look for.
51 </p>
52 <Callout variant="tip">
53 Click around first: open a folder, pick a note, open settings from the account at the
54 bottom of the sidebar.
55 </Callout>
56 </>
57 ),
58 },
59 {
60 id: "collections",
61 eyebrow: "Step 1",
62 title: "What are collections?",
63 shortTitle: "Collections",
64 tryIt: linkOptions({ to: "/" }),
65 doneWhen: [{ type: "folder.opened", smart: false }],
66 content: (
67 <>
68 <p>
69 A collection is a typed, in-memory set of records that stays in sync with your backend.
70 The note list, the editor and the folder counts all read from the same{" "}
71 <code>notesCollection</code>.
72 </p>
73 <CodeBlock
74 filename="src/collections/notes.ts"
75 code={`
76import { createCollection } from "@tanstack/react-db";
77import { queryCollectionOptions } from "@tanstack/query-db-collection";
78
79export const notesCollection = createCollection(
80 queryCollectionOptions({
81 queryKey: ["notes"],
82 queryFn: async () => {
83 const response = await fakeFetch("/api/notes");
84 return response.json();
85 },
86 queryClient,
87 getKey: (note) => note.id,
88 }),
89);
90`}
91 />
92 <p>
93 Because there is only one copy of each note, an edit shows up everywhere it's displayed at
94 the same time.
95 </p>
96 </>
97 ),
98 },
99 {
100 id: "live-queries",
101 eyebrow: "Step 2",
102 title: "Live queries",
103 shortTitle: "Live queries",
104 tryIt: linkOptions({ to: "/folders/$folderId", params: { folderId: "all" } }),
105 doneWhen: [{ type: "folder.opened", smart: true }, { type: "note.pinned" }],
106 content: (
107 <>
108 <p>
109 Each folder is a live query over notes: filter by folder, put pinned notes first, sort by
110 the last edit. When a note changes, only the affected rows are recomputed.
111 </p>
112 <CodeBlock
113 filename="src/routes/folders.$folderId.tsx"
114 code={`
115const { data: notes } = useLiveQuery({
116 query: (q) =>
117 q
118 .from({ note: notesCollection })
119 .where(({ note }) => eq(note.folderId, folderId))
120 .orderBy(({ note }) => note.updatedAt, "desc"),
121});
122`}
123 />
124 <Callout variant="note">
125 Smart folders like <code>All iCloud</code> and <code>Shared</code> aren't stored anywhere.
126 They're just different <code>where</code> clauses.
127 </Callout>
128 </>
129 ),
130 },
131 {
132 id: "optimistic-updates",
133 eyebrow: "Step 3",
134 title: "Optimistic updates",
135 shortTitle: "Optimistic updates",
136 tryIt: linkOptions({ to: "/folders/$folderId", params: { folderId: "work" } }),
137 doneWhen: [{ type: "note.edited" }, { type: "folder.renamed", status: "confirmed" }],
138 content: (
139 <>
140 <p>
141 Type in a note and the list updates instantly, before the server has answered. Turn the
142 latency up to two seconds and try again: typing still feels instant.
143 </p>
144 <CodeBlock
145 filename="src/components/editor.tsx"
146 code={`
147notesCollection.update(note.id, (draft) => {
148 draft.title = title;
149 draft.updatedAt = new Date().toISOString();
150});
151`}
152 />
153 </>
154 ),
155 },
156 {
157 id: "rollbacks",
158 eyebrow: "Step 4",
159 title: "Rollbacks",
160 shortTitle: "Rollbacks",
161 tryIt: linkOptions({ to: "/" }),
162 doneWhen: [{ status: "rolled-back" }],
163 content: (
164 <>
165 <p>
166 If the server rejects a change, TanStack DB drops the optimistic state and the UI goes
167 back to what the server has. Rename a folder to a name that already exists to see it.
168 </p>
169 <Callout variant="warning">
170 Rollbacks only work if your mutation handler throws when the request fails. Check{" "}
171 <code>response.ok</code>.
172 </Callout>
173 </>
174 ),
175 },
176 {
177 id: "sync",
178 eyebrow: "Step 5",
179 title: "How collections sync",
180 shortTitle: "Syncing",
181 content: (
182 <>
183 <p>
184 Query collections load data with a <code>queryFn</code>, and after each write they store
185 what the server sent back. Data flows one way: server → collection → live queries →
186 components.
187 </p>
188 <ul>
189 <li>Query collections: any REST or RPC API</li>
190 <li>Electric and Neon Realtime: streaming sync from Postgres</li>
191 <li>Local-only and localStorage collections for UI state</li>
192 </ul>
193 </>
194 ),
195 },
196 {
197 id: "aggregations",
198 eyebrow: "Step 6",
199 title: "Aggregations",
200 shortTitle: "Aggregations",
201 tryIt: linkOptions({ to: "/analytics" }),
202 content: (
203 <>
204 <p>
205 The analytics page groups notes by folder and counts words, all with live queries, so the
206 charts move while you type.
207 </p>
208 <CodeBlock
209 filename="src/routes/analytics.tsx"
210 code={`
211const { data: perFolder } = useLiveQuery({
212 query: (q) =>
213 q
214 .from({ note: notesCollection })
215 .groupBy(({ note }) => note.folderId)
216 .select(({ note }) => ({
217 folderId: note.folderId,
218 notes: count(note.id),
219 })),
220});
221`}
222 />
223 </>
224 ),
225 },
226 {
227 id: "whats-next",
228 eyebrow: "Wrap-up",
229 title: "What's next",
230 shortTitle: "What's next",
231 content: (
232 <>
233 <p>
234 You've seen collections, live queries, optimistic updates and rollbacks. The deep dives
235 cover query-driven sync and realtime sync with Neon.
236 </p>
237 <ul>
238 <li>
239 <a href="https://tanstack.com/db/latest/docs/overview" target="_blank" rel="noreferrer">
240 TanStack DB docs
241 </a>
242 </li>
243 <li>
244 <a href="https://github.com/TanStack/db" target="_blank" rel="noreferrer">
245 TanStack DB on GitHub
246 </a>
247 </li>
248 </ul>
249 </>
250 ),
251 },
252];