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