ai-flow-dev 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +408 -0
  3. package/dist/cli.d.ts +3 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +791 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/fs-utils.d.ts +2 -0
  8. package/dist/fs-utils.d.ts.map +1 -0
  9. package/dist/fs-utils.js +46 -0
  10. package/dist/fs-utils.js.map +1 -0
  11. package/package.json +71 -0
  12. package/prompts/backend/flow-dev-feature.md +1318 -0
  13. package/prompts/backend/flow-dev-fix.md +903 -0
  14. package/prompts/backend/flow-dev-refactor.md +715 -0
  15. package/prompts/backend/flow-dev-review.md +401 -0
  16. package/prompts/backend/flow-dev-work.md +1129 -0
  17. package/prompts/backend/flow-docs-gen-phase-0.md +1840 -0
  18. package/prompts/backend/flow-docs-gen-phase-1.md +435 -0
  19. package/prompts/backend/flow-docs-gen-phase-2.md +460 -0
  20. package/prompts/backend/flow-docs-gen-phase-3.md +684 -0
  21. package/prompts/backend/flow-docs-gen-phase-4.md +516 -0
  22. package/prompts/backend/flow-docs-gen-phase-5.md +637 -0
  23. package/prompts/backend/flow-docs-gen-phase-6.md +465 -0
  24. package/prompts/backend/flow-docs-gen-phase-7.md +1207 -0
  25. package/prompts/backend/flow-docs-gen.md +820 -0
  26. package/prompts/backend/flow-docs-sync.md +526 -0
  27. package/prompts/backend/flow-project-init.md +248 -0
  28. package/prompts/backend/flow-project-roadmap.md +1159 -0
  29. package/prompts/frontend/flow-docs-gen-phase-0.md +494 -0
  30. package/prompts/frontend/flow-docs-gen-phase-1.md +449 -0
  31. package/prompts/frontend/flow-docs-gen-phase-2.md +983 -0
  32. package/prompts/frontend/flow-docs-gen-phase-3.md +685 -0
  33. package/prompts/frontend/flow-docs-gen-phase-4.md +480 -0
  34. package/prompts/frontend/flow-docs-gen-phase-5.md +483 -0
  35. package/prompts/frontend/flow-docs-gen-phase-6.md +570 -0
  36. package/prompts/frontend/flow-docs-gen-phase-7.md +582 -0
  37. package/prompts/frontend/flow-docs-gen.md +413 -0
  38. package/prompts/frontend/flow-docs-sync.md +561 -0
  39. package/prompts/mobile/flow-docs-gen-phase-0.md +387 -0
  40. package/prompts/mobile/flow-docs-gen-phase-1.md +530 -0
  41. package/prompts/mobile/flow-docs-gen-phase-2.md +584 -0
  42. package/prompts/mobile/flow-docs-gen-phase-3.md +659 -0
  43. package/prompts/mobile/flow-docs-gen-phase-4.md +363 -0
  44. package/prompts/mobile/flow-docs-gen-phase-5.md +369 -0
  45. package/prompts/mobile/flow-docs-gen-phase-6.md +490 -0
  46. package/prompts/mobile/flow-docs-gen-phase-7.md +407 -0
  47. package/prompts/mobile/flow-docs-gen.md +430 -0
  48. package/prompts/mobile/flow-docs-sync.md +634 -0
  49. package/templates/backend/.clauderules.template +111 -0
  50. package/templates/backend/.cursorrules.template +102 -0
  51. package/templates/backend/.env.example.template +122 -0
  52. package/templates/backend/README.template.md +200 -0
  53. package/templates/backend/ai-instructions.template.md +354 -0
  54. package/templates/backend/copilot-instructions.template.md +160 -0
  55. package/templates/backend/docs/api.template.md +251 -0
  56. package/templates/backend/docs/architecture.template.md +612 -0
  57. package/templates/backend/docs/business-flows.template.md +109 -0
  58. package/templates/backend/docs/code-standards.template.md +828 -0
  59. package/templates/backend/docs/contributing.template.md +163 -0
  60. package/templates/backend/docs/data-model.template.md +416 -0
  61. package/templates/backend/docs/operations.template.md +591 -0
  62. package/templates/backend/docs/testing.template.md +762 -0
  63. package/templates/backend/project-brief.template.md +176 -0
  64. package/templates/backend/specs/configuration.template.md +133 -0
  65. package/templates/backend/specs/security.template.md +422 -0
  66. package/templates/frontend/README.template.md +121 -0
  67. package/templates/frontend/ai-instructions.template.md +368 -0
  68. package/templates/frontend/docs/api-integration.template.md +390 -0
  69. package/templates/frontend/docs/components.template.md +567 -0
  70. package/templates/frontend/docs/error-handling.template.md +385 -0
  71. package/templates/frontend/docs/operations.template.md +123 -0
  72. package/templates/frontend/docs/performance.template.md +140 -0
  73. package/templates/frontend/docs/pwa.template.md +135 -0
  74. package/templates/frontend/docs/state-management.template.md +394 -0
  75. package/templates/frontend/docs/styling.template.md +779 -0
  76. package/templates/frontend/docs/testing.template.md +736 -0
  77. package/templates/frontend/project-brief.template.md +55 -0
  78. package/templates/frontend/specs/accessibility.template.md +111 -0
  79. package/templates/frontend/specs/configuration.template.md +520 -0
  80. package/templates/frontend/specs/security.template.md +197 -0
  81. package/templates/fullstack/README.template.md +282 -0
  82. package/templates/fullstack/ai-instructions.template.md +487 -0
  83. package/templates/fullstack/project-brief.template.md +197 -0
  84. package/templates/fullstack/specs/configuration.template.md +380 -0
  85. package/templates/mobile/AGENT.template.md +251 -0
  86. package/templates/mobile/README.template.md +195 -0
  87. package/templates/mobile/ai-instructions.template.md +221 -0
  88. package/templates/mobile/docs/app-store.template.md +163 -0
  89. package/templates/mobile/docs/architecture.template.md +100 -0
  90. package/templates/mobile/docs/native-features.template.md +137 -0
  91. package/templates/mobile/docs/navigation.template.md +81 -0
  92. package/templates/mobile/docs/offline-strategy.template.md +90 -0
  93. package/templates/mobile/docs/permissions.template.md +70 -0
  94. package/templates/mobile/docs/state-management.template.md +116 -0
  95. package/templates/mobile/docs/testing.template.md +146 -0
  96. package/templates/mobile/project-brief.template.md +97 -0
  97. package/templates/mobile/specs/build-configuration.template.md +116 -0
  98. package/templates/mobile/specs/deployment.template.md +114 -0
  99. package/templates/shared/AGENT.template.md +252 -0
@@ -0,0 +1,685 @@
1
+ # Phase 3: State Management
2
+
3
+ **Duration:** 15-20 minutes
4
+ **Questions:** ~10 questions
5
+ **Output:** docs/state-management.md, parts of ai-instructions.md
6
+
7
+ ---
8
+
9
+ ## 🎯 Objective
10
+
11
+ Define how your application will manage state across four categories:
12
+
13
+ 1. **Server State** - Remote data from APIs
14
+ 2. **Client State** - Local UI state
15
+ 3. **URL State** - Query params and routes
16
+ 4. **Form State** - Form data and validation
17
+
18
+ ---
19
+
20
+ ## 📋 Questions
21
+
22
+ ### Question 3.1: Server State Management
23
+
24
+ **How will you fetch and cache data from APIs?**
25
+
26
+ #### React Options
27
+
28
+ A) ⭐ **TanStack Query (React Query)** (Recommended)
29
+ - Features: Auto caching, background refetching, optimistic updates
30
+ - Best for: REST APIs, GraphQL (via custom fetcher)
31
+ - Bundle: ~12KB (gzipped)
32
+
33
+ B) 🔥 **SWR (Vercel)**
34
+ - Features: Stale-while-revalidate, automatic deduplication
35
+ - Best for: REST APIs, simple data fetching
36
+ - Bundle: ~5KB (gzipped)
37
+
38
+ C) **Apollo Client**
39
+ - Features: GraphQL-specific, normalized cache, subscriptions
40
+ - Best for: GraphQL APIs only
41
+ - Bundle: ~33KB (gzipped)
42
+
43
+ D) **urql**
44
+ - Features: Lightweight GraphQL client, extensible
45
+ - Best for: GraphQL, smaller bundle than Apollo
46
+ - Bundle: ~16KB (gzipped)
47
+
48
+ E) **Redux Toolkit + RTK Query**
49
+ - Features: Integrated with Redux, powerful caching
50
+ - Best for: Complex apps already using Redux
51
+ - Bundle: ~15KB (gzipped, with Redux)
52
+
53
+ F) **Plain fetch + Custom Hooks**
54
+ - Features: Full control, no dependencies
55
+ - Best for: Very simple apps
56
+ - Bundle: 0KB (native)
57
+
58
+ #### Vue Options
59
+
60
+ A) ⭐ **TanStack Query (Vue Query)** (Recommended)
61
+ - Same as React version, Vue adapter
62
+
63
+ B) **Pinia + Custom Composables**
64
+ - Features: Reactive state management
65
+ - Best for: Vue 3 apps with complex state
66
+
67
+ C) **Villus (GraphQL)**
68
+ - Features: Lightweight GraphQL client for Vue
69
+ - Best for: Vue + GraphQL
70
+
71
+ #### Angular Options
72
+
73
+ A) ⭐ **TanStack Query (Angular Query)**
74
+ - Same as React version, Angular adapter
75
+
76
+ B) **NgRx + Custom Effects**
77
+ - Features: Redux pattern for Angular
78
+ - Best for: Enterprise Angular apps
79
+
80
+ C) **Apollo Angular**
81
+ - Features: GraphQL client for Angular
82
+ - Best for: Angular + GraphQL
83
+
84
+ **Your answer:**
85
+
86
+ ---
87
+
88
+ ### Question 3.2: Client State Management
89
+
90
+ **How will you manage local UI state?** (modals, themes, UI flags)
91
+
92
+ #### React Options
93
+
94
+ A) ⭐ **Zustand** (Recommended for most)
95
+ - Features: Minimal boilerplate, hooks-based, simple API
96
+ - Best for: Small to medium apps, simple global state
97
+ - Bundle: ~1KB (gzipped)
98
+
99
+ B) 🔥 **Redux Toolkit**
100
+ - Features: Redux with less boilerplate, DevTools, middleware
101
+ - Best for: Large apps, complex state logic, time-travel debugging
102
+ - Bundle: ~14KB (gzipped)
103
+
104
+ C) **Jotai**
105
+ - Features: Atomic state management, bottom-up approach
106
+ - Best for: Fine-grained reactivity, derived state
107
+ - Bundle: ~3KB (gzipped)
108
+
109
+ D) **Recoil**
110
+ - Features: Facebook's state library, atom-based
111
+ - Best for: Complex dependency graphs
112
+ - Bundle: ~21KB (gzipped)
113
+
114
+ E) **Context API + useReducer**
115
+ - Features: Built-in React, no dependencies
116
+ - Best for: Simple apps, small global state
117
+ - Bundle: 0KB (native)
118
+
119
+ F) **XState**
120
+ - Features: State machines, predictable state transitions
121
+ - Best for: Complex workflows, multi-step forms
122
+ - Bundle: ~18KB (gzipped)
123
+
124
+ #### Vue Options
125
+
126
+ A) ⭐ **Pinia** (Recommended)
127
+ - Features: Official Vue state library, TypeScript support
128
+ - Best for: Vue 3 apps
129
+ - Bundle: ~1.5KB (gzipped)
130
+
131
+ B) **Vuex**
132
+ - Features: Traditional Vue state management
133
+ - Best for: Vue 2 apps, migration from Vue 2
134
+ - Bundle: ~2KB (gzipped)
135
+
136
+ C) **Composition API (Built-in)**
137
+ - Features: `reactive()`, `ref()`, composables
138
+ - Best for: Simple apps
139
+ - Bundle: 0KB (native)
140
+
141
+ #### Angular Options
142
+
143
+ A) ⭐ **NgRx** (Recommended for large apps)
144
+ - Features: Redux pattern, RxJS integration
145
+ - Best for: Enterprise Angular apps
146
+ - Bundle: ~18KB (gzipped)
147
+
148
+ B) **Akita**
149
+ - Features: Simplified state management, RxJS-based
150
+ - Best for: Medium Angular apps
151
+ - Bundle: ~12KB (gzipped)
152
+
153
+ C) **Elf**
154
+ - Features: Reactive state management, modular
155
+ - Best for: Flexible architecture
156
+ - Bundle: ~8KB (gzipped)
157
+
158
+ D) **Services + RxJS (Built-in)**
159
+ - Features: Angular services with BehaviorSubject
160
+ - Best for: Simple apps
161
+ - Bundle: 0KB (native)
162
+
163
+ #### Svelte Options
164
+
165
+ A) ⭐ **Svelte Stores** (Recommended)
166
+ - Features: Built-in, writable/readable/derived stores
167
+ - Best for: All Svelte apps
168
+ - Bundle: 0KB (built-in)
169
+
170
+ B) **XState + Svelte**
171
+ - Features: State machines for complex flows
172
+ - Best for: Complex workflows
173
+
174
+ #### Solid Options
175
+
176
+ A) ⭐ **Solid Store** (Recommended)
177
+ - Features: Built-in, reactive store
178
+ - Best for: All Solid apps
179
+ - Bundle: 0KB (built-in)
180
+
181
+ **Your answer:**
182
+
183
+ ---
184
+
185
+ ### Question 3.3: State Persistence
186
+
187
+ **Do you need to persist state to localStorage/sessionStorage?**
188
+
189
+ A) ⭐ **No Persistence**
190
+ - State reset on page reload
191
+ - Best for: Most apps
192
+
193
+ B) **Selective Persistence**
194
+ - Persist specific state (e.g., theme, user preferences)
195
+ - Example: Zustand persist middleware, Pinia persist plugin
196
+ - Best for: User preferences, shopping cart
197
+
198
+ C) **Full Persistence**
199
+ - Persist entire state tree
200
+ - Best for: Offline-first apps, complex forms
201
+
202
+ **Your answer:**
203
+
204
+ **If persistence selected, what should be persisted?**
205
+
206
+ ---
207
+
208
+ ### Question 3.4: Form State Management
209
+
210
+ **How will you handle forms?**
211
+
212
+ #### React Options
213
+
214
+ A) ⭐ **React Hook Form** (Recommended)
215
+ - Features: Uncontrolled forms, minimal re-renders, validation
216
+ - Best for: Performance-critical forms, complex validation
217
+ - Bundle: ~9KB (gzipped)
218
+
219
+ B) **Formik**
220
+ - Features: Controlled forms, popular, mature
221
+ - Best for: Traditional React forms
222
+ - Bundle: ~15KB (gzipped)
223
+
224
+ C) **TanStack Form**
225
+ - Features: Framework-agnostic, type-safe
226
+ - Best for: Advanced form logic
227
+ - Bundle: ~12KB (gzipped)
228
+
229
+ D) **Controlled Components (Built-in)**
230
+ - Features: Manual useState management
231
+ - Best for: Simple forms (1-3 fields)
232
+ - Bundle: 0KB (native)
233
+
234
+ #### Vue Options
235
+
236
+ A) ⭐ **VeeValidate** (Recommended)
237
+ - Features: Composition API, Yup/Zod integration
238
+ - Best for: Vue 3 forms with validation
239
+ - Bundle: ~7KB (gzipped)
240
+
241
+ B) **FormKit**
242
+ - Features: All-in-one forms + UI components
243
+ - Best for: Rapid form development
244
+ - Bundle: ~25KB (gzipped)
245
+
246
+ C) **v-model (Built-in)**
247
+ - Features: Two-way binding
248
+ - Best for: Simple forms
249
+ - Bundle: 0KB (native)
250
+
251
+ #### Angular Options
252
+
253
+ A) ⭐ **Reactive Forms** (Recommended)
254
+ - Features: Built-in, powerful validation
255
+ - Best for: Complex Angular forms
256
+ - Bundle: 0KB (built-in)
257
+
258
+ B) **Template-Driven Forms**
259
+ - Features: Built-in, simpler API
260
+ - Best for: Simple Angular forms
261
+ - Bundle: 0KB (built-in)
262
+
263
+ #### Svelte/Solid Options
264
+
265
+ A) **Two-way binding (Built-in)**
266
+ - Features: Simple bind:value syntax
267
+ - Best for: Most forms
268
+ - Bundle: 0KB (built-in)
269
+
270
+ B) **Felte (Svelte) / Modular Forms (Solid)**
271
+ - Features: Advanced form libraries
272
+ - Best for: Complex validation
273
+
274
+ **Your answer:**
275
+
276
+ ---
277
+
278
+ ### Question 3.5: Form Validation
279
+
280
+ **What validation strategy will you use?**
281
+
282
+ A) ⭐ **Schema Validation (Zod/Yup)**
283
+ - Features: Type-safe, reusable schemas
284
+ - Best for: Complex validation, type safety
285
+ - Example: Zod for TypeScript projects
286
+
287
+ B) **Built-in HTML5 Validation**
288
+ - Features: Native browser validation
289
+ - Best for: Simple forms
290
+ - Example: `required`, `pattern`, `min`, `max`
291
+
292
+ C) **Custom Validation Functions**
293
+ - Features: Full control, no dependencies
294
+ - Best for: Unique validation rules
295
+
296
+ D) **Backend Validation Only**
297
+ - Features: Server-side validation
298
+ - Best for: Security-critical forms
299
+ - Note: Still need client-side for UX
300
+
301
+ E) **Combined (Client + Server)**
302
+ - Features: Client for UX, server for security
303
+ - Best for: Most production apps
304
+
305
+ **Your answer:**
306
+
307
+ ---
308
+
309
+ ### Question 3.6: Optimistic Updates
310
+
311
+ **Will you use optimistic UI updates?**
312
+
313
+ Optimistic updates = Update UI before server confirms success
314
+
315
+ A) ⭐ **Yes, for common actions**
316
+ - Example: Like button, delete item, mark complete
317
+ - Best for: Interactive apps, real-time feel
318
+ - Requires: Rollback logic on error
319
+
320
+ B) **No, wait for server confirmation**
321
+ - Best for: Critical actions (payments, deletions)
322
+ - Safer but slower UX
323
+
324
+ C) **Selective (case-by-case)**
325
+ - Optimistic for safe actions, wait for critical ones
326
+
327
+ **Your answer:**
328
+
329
+ ---
330
+
331
+ ### Question 3.7: Real-Time Updates
332
+
333
+ **Do you need real-time data updates?**
334
+
335
+ A) ⭐ **No Real-Time**
336
+ - Data fetched on demand or on interval
337
+ - Best for: Most apps
338
+
339
+ B) **Polling (Short Intervals)**
340
+ - Refetch data every X seconds
341
+ - Best for: Near real-time, simple implementation
342
+ - Example: Every 10-30 seconds
343
+
344
+ C) **WebSockets**
345
+ - Bi-directional, persistent connection
346
+ - Best for: Chat, live dashboards, collaboration
347
+ - Technologies: Socket.io, native WebSockets
348
+
349
+ D) **Server-Sent Events (SSE)**
350
+ - Unidirectional, server pushes updates
351
+ - Best for: Live notifications, feeds
352
+ - Technologies: EventSource API
353
+
354
+ E) **GraphQL Subscriptions**
355
+ - Real-time GraphQL queries
356
+ - Best for: GraphQL apps with live data
357
+
358
+ **Your answer:**
359
+
360
+ ---
361
+
362
+ ### Question 3.8: State Normalization
363
+
364
+ **Will you normalize relational data?**
365
+
366
+ Normalization = Flatten nested data into lookup tables (like a database)
367
+
368
+ **Example:**
369
+ ```javascript
370
+ // ❌ Nested (Not normalized)
371
+ {
372
+ posts: [
373
+ { id: 1, title: 'Post 1', author: { id: 10, name: 'Alice' } },
374
+ { id: 2, title: 'Post 2', author: { id: 10, name: 'Alice' } }
375
+ ]
376
+ }
377
+
378
+ // ✅ Normalized
379
+ {
380
+ posts: { 1: { id: 1, title: 'Post 1', authorId: 10 }, 2: { ... } },
381
+ authors: { 10: { id: 10, name: 'Alice' } }
382
+ }
383
+ ```
384
+
385
+ A) ⭐ **Yes, normalize relational data**
386
+ - Best for: Complex data models, many-to-many relationships
387
+ - Tools: normalizr, Redux Toolkit (createEntityAdapter)
388
+
389
+ B) **No, keep data nested**
390
+ - Best for: Simple data structures, tree-like data
391
+ - Easier to reason about
392
+
393
+ **Your answer:**
394
+
395
+ ---
396
+
397
+ ### Question 3.9: Global vs Local State
398
+
399
+ **What's your default strategy for state placement?**
400
+
401
+ A) ⭐ **Local by default, global when shared** (Recommended)
402
+ - Start with component state (useState/reactive)
403
+ - Lift to global only when multiple components need it
404
+ - Best for: Most apps, avoid over-engineering
405
+
406
+ B) **Global by default**
407
+ - Most state in global store
408
+ - Best for: Complex apps with heavy state sharing
409
+
410
+ C) **URL-driven state**
411
+ - Prefer URL params over global state
412
+ - Best for: Multi-page apps, shareable state
413
+
414
+ **Your answer:**
415
+
416
+ ---
417
+
418
+ ### Question 3.10: Error Handling Strategy
419
+
420
+ **How will you handle API errors and failed requests?**
421
+
422
+ A) ⭐ **Toast Notifications**
423
+ - Show temporary error message
424
+ - Best for: Non-critical errors
425
+ - Libraries: react-hot-toast, vue-toastification
426
+
427
+ B) **Inline Error Messages**
428
+ - Display errors next to forms/components
429
+ - Best for: Form validation, input errors
430
+
431
+ C) **Error Boundary (Global)**
432
+ - Catch all errors, show fallback UI
433
+ - Best for: Unhandled errors, app crashes
434
+ - React: Error Boundaries, Vue: errorHandler
435
+
436
+ D) **Modal Dialogs**
437
+ - Show error in modal
438
+ - Best for: Critical errors requiring user action
439
+
440
+ E) **Retry Mechanism**
441
+ - Auto-retry failed requests
442
+ - Best for: Network errors, transient failures
443
+ - TanStack Query has built-in retry
444
+
445
+ F) **Combined Approach**
446
+ - Different strategies for different error types
447
+
448
+ **Your answer:**
449
+
450
+ ---
451
+
452
+ #### 🎨 MERMAID STATE MANAGEMENT DIAGRAM FORMATS - CRITICAL
453
+
454
+ **Use these exact formats** for state management diagrams mentioned in documentation:
455
+
456
+ ---
457
+
458
+ ##### 1️⃣ State Categories & Store Organization
459
+
460
+ Use `graph LR` to show different state types and their tools:
461
+
462
+ ````markdown
463
+ ```mermaid
464
+ graph LR
465
+ subgraph "State Types"
466
+ SS[Server State<br/>TanStack Query<br/>React Query]
467
+ CS[Client State<br/>Zustand/Redux<br/>Context]
468
+ US[URL State<br/>React Router<br/>Query Params]
469
+ FS[Form State<br/>React Hook Form<br/>Formik]
470
+ LS[Local State<br/>useState<br/>useReducer]
471
+ end
472
+
473
+ subgraph "Components"
474
+ C1[ProductList]
475
+ C2[UserProfile]
476
+ C3[SearchPage]
477
+ C4[CheckoutForm]
478
+ C5[DropdownMenu]
479
+ end
480
+
481
+ SS -.->|products, users| C1
482
+ SS -.->|user data| C2
483
+ CS -.->|theme, auth| C1
484
+ CS -.->|auth| C2
485
+ US -.->|search query| C3
486
+ FS -.->|form data| C4
487
+ LS -.->|isOpen| C5
488
+
489
+ style SS fill:#e1f5ff
490
+ style CS fill:#fff4e6
491
+ style US fill:#e8f5e9
492
+ style FS fill:#fce4ec
493
+ style LS fill:#f3e5f5
494
+ ```
495
+ ````
496
+
497
+ **Use for:** Deciding where to store state, showing state architecture, documenting state tools
498
+
499
+ ---
500
+
501
+ ##### 2️⃣ Data Flow Sequence (API to UI)
502
+
503
+ Use `sequenceDiagram` to show step-by-step data flow:
504
+
505
+ ````markdown
506
+ ```mermaid
507
+ sequenceDiagram
508
+ participant U as User
509
+ participant C as Component
510
+ participant S as Store/Hook
511
+ participant API as API Service
512
+ participant BE as Backend
513
+
514
+ U->>C: Click "Load Products"
515
+ C->>S: useProducts() hook
516
+ S->>API: fetchProducts()
517
+ API->>BE: GET /api/products
518
+ BE-->>API: 200 OK + data
519
+ API-->>S: Return products[]
520
+ S->>S: Update cache
521
+ S-->>C: products data
522
+ C->>U: Render UI
523
+
524
+ Note over S: TanStack Query<br/>caches for 5min
525
+
526
+ U->>C: Click "Add to Cart"
527
+ C->>S: addToCart(productId)
528
+ S->>S: Update client state
529
+ S-->>C: cart updated
530
+ C->>U: Show cart badge
531
+ ```
532
+ ````
533
+
534
+ **Use for:** Showing request/response cycles, async operations, state updates, caching behavior
535
+
536
+ ---
537
+
538
+ ##### 3️⃣ State Location Decision Tree
539
+
540
+ Use `graph TD` to help developers choose where to store state:
541
+
542
+ ````markdown
543
+ ```mermaid
544
+ graph TD
545
+ A[New State Needed] --> B{From Server?}
546
+ B -->|Yes| C[Use TanStack Query<br/>Server State<br/>Auto-caching]
547
+ B -->|No| D{Shared Across<br/>Components?}
548
+ D -->|Yes| E{Needs to<br/>Persist?}
549
+ E -->|Yes| F[Use Zustand +<br/>localStorage<br/>Client State]
550
+ E -->|No| G[Use Zustand or<br/>Context<br/>Client State]
551
+ D -->|No| H{In URL?}
552
+ H -->|Yes| I[Use React Router<br/>searchParams<br/>URL State]
553
+ H -->|No| J{Form Data?}
554
+ J -->|Yes| K[Use React Hook Form<br/>Form State]
555
+ J -->|No| L[Use useState/useReducer<br/>Local Component State]
556
+
557
+ style C fill:#e1f5ff
558
+ style F fill:#fff4e6
559
+ style G fill:#fff4e6
560
+ style I fill:#e8f5e9
561
+ style K fill:#fce4ec
562
+ style L fill:#f3e5f5
563
+ ```
564
+ ````
565
+
566
+ **Use for:** Decision-making guide, onboarding new developers, documenting state strategy
567
+
568
+ ---
569
+
570
+ **Best Practices for State Management Diagrams:**
571
+
572
+ 1. **Use Sequence Diagrams for Time-Based Flows:**
573
+ - Show async operations step-by-step
574
+ - Include caching behavior
575
+ - Document timing (e.g., "caches for 5min")
576
+
577
+ 2. **Use Graph Diagrams for Relationships:**
578
+ - State categories and their connections
579
+ - Decision trees for choosing state location
580
+ - Component-to-store relationships
581
+
582
+ 3. **Color Code by State Type:**
583
+ - Server State: `#e1f5ff` (light blue)
584
+ - Client State: `#fff4e6` (light orange)
585
+ - URL State: `#e8f5e9` (light green)
586
+ - Form State: `#fce4ec` (light pink)
587
+ - Local State: `#f3e5f5` (light purple)
588
+
589
+ 4. **Include Tool Names:** Always specify actual tools (TanStack Query, Zustand, React Hook Form, etc.)
590
+
591
+ 5. **Show Data Flow Direction:** Use solid arrows for data flow, dotted for state updates
592
+
593
+ **Common Formatting Rules:**
594
+ - Code fence: ` ```mermaid ` (lowercase, no spaces, three backticks)
595
+ - Use `sequenceDiagram` for API calls and async flows
596
+ - Use `graph TD` for decision trees
597
+ - Use `graph LR` for category relationships
598
+ - Preview at https://mermaid.live/ before saving
599
+
600
+ ---
601
+
602
+ ## 📊 Phase 3 Summary
603
+
604
+ ```
605
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
606
+ 📋 PHASE 3 SUMMARY: STATE MANAGEMENT
607
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
608
+
609
+ Server State: [Answer from 3.1]
610
+ Client State: [Answer from 3.2]
611
+ State Persistence: [Answer from 3.3]
612
+ Form Management: [Answer from 3.4]
613
+ Form Validation: [Answer from 3.5]
614
+ Optimistic Updates: [Answer from 3.6]
615
+ Real-Time Updates: [Answer from 3.7]
616
+ State Normalization: [Answer from 3.8]
617
+ State Placement: [Answer from 3.9]
618
+ Error Handling: [Answer from 3.10]
619
+ API Error Handling: [Answer from 3.11]
620
+ Retry Strategy: [Answer from 3.12]
621
+
622
+ Is this correct? (Y/n)
623
+ ```
624
+
625
+ ---
626
+
627
+ ## 📝 Document Generation
628
+
629
+ Generate `docs/state-management.md` using the template:
630
+
631
+ Replace these placeholders:
632
+
633
+ - `{{STATE_MANAGEMENT}}` → Client state solution (e.g., "Zustand")
634
+ - `{{DATA_FETCHING}}` → Server state solution (e.g., "TanStack Query")
635
+ - `{{FORM_LIBRARY}}` → Form library (e.g., "React Hook Form")
636
+ - `{{FORM_VALIDATION}}` → Validation strategy (e.g., "Zod")
637
+ - `{{STATE_PERSISTENCE}}` → Persistence approach
638
+ - `{{OPTIMISTIC_UPDATES}}` → Yes/No
639
+ - `{{REAL_TIME_STRATEGY}}` → Real-time approach
640
+ - `{{STATE_NORMALIZATION}}` → Yes/No
641
+ - `{{ERROR_HANDLING_STRATEGY}}` → Error handling approach
642
+
643
+ Also update `ai-instructions.md`:
644
+
645
+ ```markdown
646
+ ## State Management
647
+
648
+ - **Server State:** {{DATA_FETCHING}}
649
+ - **Client State:** {{STATE_MANAGEMENT}}
650
+ - **Forms:** {{FORM_LIBRARY}} with {{FORM_VALIDATION}}
651
+ - **Real-Time:** {{REAL_TIME_STRATEGY}}
652
+
653
+ ### Rules
654
+
655
+ - ✅ ALWAYS use {{DATA_FETCHING}} for server data
656
+ - ✅ ALWAYS validate forms with {{FORM_VALIDATION}}
657
+ - ❌ NEVER store derived data in state
658
+ - ❌ NEVER mutate state directly (use immutable updates)
659
+ {{#IF_OPTIMISTIC_UPDATES}}
660
+ - ✅ ALWAYS implement rollback for optimistic updates
661
+ {{/IF_OPTIMISTIC_UPDATES}}
662
+ ```
663
+
664
+ ---
665
+
666
+ ## 🚀 Next Steps
667
+
668
+ ```
669
+ ✅ Phase 3 Complete!
670
+
671
+ Documents Generated:
672
+ - docs/state-management.md
673
+ - ai-instructions.md (updated)
674
+
675
+ Next: Phase 4 - Styling & Design
676
+
677
+ Read: .ai-flow/prompts/frontend/flow-docs-gen-phase-4-styling.md
678
+ ```
679
+
680
+ ---
681
+
682
+ **Last Updated:** 2025-01-XX
683
+
684
+ **Version:** 1.2.0
685
+