-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
314 lines (299 loc) · 8.14 KB
/
App.tsx
File metadata and controls
314 lines (299 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* React Application - Zustand Integration
*
* This example shows how Zustand stores can be managed as Braided resources.
* The stores live in the system, providing centralized state management.
*/
import React, { useState } from "react";
import { useResource } from "./hooks";
/**
* Counter Component
*
* Uses the Zustand store from the counterStore resource.
*/
function Counter() {
const { useCounterStore } = useResource("counterStore");
const { count, increment, decrement, reset } = useCounterStore();
return (
<div
style={{
padding: "20px",
border: "2px solid #3b82f6",
borderRadius: "8px",
}}
>
<h2>🔢 Counter (Zustand Store)</h2>
<p style={{ fontSize: "48px", margin: "20px 0" }}>{count}</p>
<div style={{ display: "flex", gap: "10px" }}>
<button
onClick={increment}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#3b82f6",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
+
</button>
<button
onClick={decrement}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#8b5cf6",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
-
</button>
<button
onClick={reset}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#ef4444",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Reset
</button>
</div>
</div>
);
}
/**
* Todo List Component
*
* Uses the Zustand store from the todoStore resource.
*/
function TodoList() {
const todoStore = useResource("todoStore");
const { todos, addTodo, toggleTodo, removeTodo, clearCompleted } =
todoStore.useTodoStore();
const [input, setInput] = useState("");
const handleAdd = () => {
if (input.trim()) {
addTodo(input);
setInput("");
}
};
const handleKeyPress = (e: React.KeyboardEvent) => {
if (e.key === "Enter") {
handleAdd();
}
};
return (
<div
style={{
padding: "20px",
border: "2px solid #10b981",
borderRadius: "8px",
}}
>
<h2>📝 Todos (Zustand Store)</h2>
<div style={{ display: "flex", gap: "10px", marginBottom: "20px" }}>
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={handleKeyPress}
placeholder="Add a todo..."
style={{
flex: 1,
padding: "10px",
fontSize: "16px",
border: "1px solid #d1d5db",
borderRadius: "4px",
}}
/>
<button
onClick={handleAdd}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#10b981",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Add
</button>
</div>
<div style={{ marginBottom: "10px" }}>
<p style={{ color: "#6b7280", margin: "0 0 10px 0" }}>
{todos.length} total, {todos.filter((t) => t.completed).length}{" "}
completed
</p>
{todos.filter((t) => t.completed).length > 0 && (
<button
onClick={clearCompleted}
style={{
padding: "5px 10px",
fontSize: "14px",
cursor: "pointer",
backgroundColor: "#ef4444",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Clear Completed
</button>
)}
</div>
<div style={{ display: "flex", flexDirection: "column", gap: "10px" }}>
{todos.length === 0 ? (
<p style={{ color: "#6b7280" }}>No todos yet. Add one above!</p>
) : (
todos.map((todo) => (
<div
key={todo.id}
style={{
display: "flex",
alignItems: "center",
gap: "10px",
padding: "10px",
backgroundColor: todo.completed ? "#f3f4f6" : "white",
border: "1px solid #d1d5db",
borderRadius: "4px",
}}
>
<input
type="checkbox"
checked={todo.completed}
onChange={() => toggleTodo(todo.id)}
style={{ cursor: "pointer" }}
/>
<span
style={{
flex: 1,
textDecoration: todo.completed ? "line-through" : "none",
color: todo.completed ? "#6b7280" : "inherit",
}}
>
{todo.text}
</span>
<button
onClick={() => removeTodo(todo.id)}
style={{
padding: "5px 10px",
fontSize: "14px",
cursor: "pointer",
backgroundColor: "#ef4444",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Delete
</button>
</div>
))
)}
</div>
</div>
);
}
/**
* Logger Controls Component
*
* Demonstrates accessing the logger resource that observes stores.
*/
function LoggerControls() {
const logger = useResource("logger");
return (
<div
style={{
padding: "20px",
border: "2px solid #8b5cf6",
borderRadius: "8px",
}}
>
<h2>📊 Logger</h2>
<p style={{ color: "#6b7280", marginBottom: "10px" }}>
The logger resource subscribes to store changes and logs them to the
console.
</p>
<button
onClick={() => logger.logCurrentState()}
style={{
padding: "10px 20px",
fontSize: "16px",
cursor: "pointer",
backgroundColor: "#8b5cf6",
color: "white",
border: "none",
borderRadius: "4px",
}}
>
Log Current State
</button>
</div>
);
}
/**
* Main App Component
*/
export function App() {
return (
<div style={{ padding: "40px", maxWidth: "800px", margin: "0 auto" }}>
<h1 style={{ marginBottom: "10px" }}>
🧶 Braided React - Zustand Integration
</h1>
<p style={{ color: "#6b7280", marginBottom: "40px" }}>
Zustand stores managed as Braided resources. Stores persist across React
remounts and can be observed by other resources.
</p>
<div style={{ display: "flex", flexDirection: "column", gap: "20px" }}>
<Counter />
<TodoList />
<LoggerControls />
</div>
<div
style={{
marginTop: "40px",
padding: "20px",
backgroundColor: "#fef3c7",
borderRadius: "8px",
}}
>
<h3 style={{ marginTop: 0 }}>💡 Key Concepts:</h3>
<ol style={{ marginBottom: 0 }}>
<li>
<strong>Stores as Resources:</strong> Zustand stores are created in
the system, not in components
</li>
<li>
<strong>Centralized State:</strong> All stores live in one place,
easy to coordinate
</li>
<li>
<strong>Cross-Resource Observation:</strong> Logger resource
subscribes to store changes
</li>
<li>
<strong>Persistence:</strong> Stores survive React remounts and
StrictMode
</li>
<li>
<strong>Type Safety:</strong> Full TypeScript inference from system
to components
</li>
<li>Open DevTools console to see store change logs</li>
</ol>
</div>
</div>
);
}