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

content.tsx

217 lines6.0 KB

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