@relipa/ai-flow-kit 0.0.3 → 0.0.4-beta.2

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.
@@ -1,567 +1,492 @@
1
- # React.js AI System Prompt
2
-
3
- You are an expert React.js developer. Follow these specific rules when generating or modifying code in this project.
4
-
5
- ---
6
-
7
- ## MANDATORY: Strict Gate Workflow
8
-
9
- > **Absolute Rule: Complete Gate N before entering Gate N+1.**
10
- > **Do not skip, shorten, or merge Gates.**
11
-
12
- You have superpowers. When a ticket context exists in `.claude/context/current.json`:
13
- - **AUTO-START Gate 1 immediately** — do NOT wait for the developer to ask.
14
- - Read `CLAUDE.md` (this file) and follow the gate sequence below — NO EXCEPTIONS.
15
-
16
- ---
17
-
18
- ### GATE 1 — AI Analyze Requirement (auto-start)
19
-
20
- **INVOKE:** `validate-ticket` skill
21
-
22
- AI actively reads ticket + source code to understand the requirement:
23
- 1. Read `.claude/context/current.json` — ticket info
24
- 2. Read source code — architecture, related files, data flow
25
- 3. If anything is unclear — ask ONE question at a time, wait for reply
26
- 4. Output `plan/[ticket-id]/requirement.md` with:
27
- - Requirements summary, source code analysis
28
- - Proposed solution and approach
29
- - Impact analysis, effort estimate, testing plan
30
- 5. Display "GATE 1: Requirement doc ready" → wait for **APPROVED**
31
-
32
- DO NOT just check format **understand the content and propose solutions**.
33
-
34
- ---
35
-
36
- ### GATE 2 — Implementation Plan (wait for APPROVED)
37
-
38
- **INVOKE:** `generate-spec` skill, then `superpowers:writing-plans`
39
-
40
- - Create a detailed TDD implementation plan based on the approved requirement.
41
- - Display: "GATE 2 PAUSED: type APPROVED to start coding".
42
- - CODE WILL NOT BE GENERATED until "APPROVED" is received.
43
-
44
- ---
45
-
46
- ### GATE 3 — Code Generation (TDD only)
47
-
48
- Only runs after Gate 2 has been APPROVED.
49
-
50
- **INVOKE:** `superpowers:test-driven-development`
51
- - Complex feature (3+ files): `superpowers:subagent-driven-development`
52
- - Write tests FIRST — run to confirm FAIL -> implement -> PASS.
53
- - Bug fix EXTRA: `superpowers:systematic-debugging` + `investigate-bug` skill first.
54
-
55
- ---
56
-
57
- ### GATE 4 — AI Self-Review (wait for APPROVED)
58
-
59
- **INVOKE:** `review-plan` skill
60
-
61
- Mandatory order:
62
- 1. `superpowers:verification-before-completion` — all tests must PASS
63
- 2. `impact-analysis` skill check for breaking changes
64
- 3. `custom/rules/review-checklist.md` — tick each item
65
- 4. Create `plan/[ticket-id]/summary.md`
66
-
67
- Then: "GATE 4 PAUSED: type APPROVED or BUG: [description]"
68
- - Coding bug -> fix -> repeat Gate 4.
69
- - Requirement bug -> return to Gate 1.
70
-
71
- ---
72
-
73
- ### GATE 5 Peer Review and Done
74
-
75
- Only runs after Gate 4 has been APPROVED.
76
-
77
- **INVOKE:** `superpowers:requesting-code-review`
78
- Guide on creating a Pull Request with the ticket link.
79
-
80
- ---
81
-
82
- ## Project Stack
83
-
84
- - **React:** 18+
85
- - **Language:** TypeScript
86
- - **Build Tool:** Vite (prefer) or Create React App
87
- - **State Management:** Zustand (local/global) or TanStack Query (server state)
88
- - **Routing:** React Router v6
89
- - **Styling:** Tailwind CSS + shadcn/ui (or CSS Modules if no Tailwind)
90
- - **Form:** React Hook Form + Zod validation
91
- - **HTTP Client:** Axios with interceptors
92
- - **Testing:** Vitest + React Testing Library
93
-
94
- ---
95
-
96
- ## Project Structure
97
-
98
- ```
99
- src/
100
- ├── components/
101
- │ ├── ui/ # Generic, reusable UI (Button, Input, Modal...)
102
- │ └── [feature]/ # Feature-specific components
103
- ├── pages/ # Route-level components (thin, delegate to features)
104
- ├── features/ # Self-contained feature modules
105
- │ └── [feature]/
106
- │ ├── components/ # UI specific to this feature
107
- │ ├── hooks/ # Custom hooks for this feature
108
- │ ├── api.ts # API calls for this feature
109
- │ ├── store.ts # Zustand slice (if needed)
110
- │ ├── types.ts # TypeScript types/interfaces
111
- │ └── index.ts # Public exports
112
- ├── hooks/ # Shared custom hooks
113
- ├── services/
114
- │ └── api.ts # Axios instance + interceptors
115
- ├── store/ # Global Zustand store
116
- ├── types/ # Shared TypeScript types
117
- ├── utils/ # Pure utility functions
118
- ├── constants/ # App-wide constants
119
- └── App.tsx
120
- ```
121
-
122
- ---
123
-
124
- ## Component Rules
125
-
126
- ### Always use Functional Components
127
-
128
- ```tsx
129
- // ✅ Good
130
- const UserCard = ({ user }: UserCardProps) => {
131
- return <div>{user.name}</div>;
132
- };
133
-
134
- // ❌ Bad — class component
135
- class UserCard extends React.Component {}
136
- ```
137
-
138
- ### Define Props with TypeScript interface
139
-
140
- ```tsx
141
- // ✅ Good
142
- interface UserCardProps {
143
- user: User;
144
- onDelete?: (id: number) => void;
145
- className?: string;
146
- }
147
-
148
- const UserCard = ({ user, onDelete, className }: UserCardProps) => {
149
- // ...
150
- };
151
-
152
- // Bad any, or no type
153
- const UserCard = ({ user }: any) => {};
154
- const UserCard = (props) => {};
155
- ```
156
-
157
- ### Keep components focused and small
158
-
159
- - One component = one responsibility
160
- - If a component exceeds ~150 lines, split it
161
- - Extract logic to custom hooks, keep JSX clean
162
-
163
- ```tsx
164
- // Good — logic extracted to hook
165
- const UserList = () => {
166
- const { users, isLoading, error, deleteUser } = useUserList();
167
-
168
- if (isLoading) return <Spinner />;
169
- if (error) return <ErrorMessage message={error.message} />;
170
-
171
- return (
172
- <ul>
173
- {users.map(user => (
174
- <UserCard key={user.id} user={user} onDelete={deleteUser} />
175
- ))}
176
- </ul>
177
- );
178
- };
179
-
180
- // ❌ Bad — logic mixed into component
181
- const UserList = () => {
182
- const [users, setUsers] = useState([]);
183
- const [loading, setLoading] = useState(true);
184
-
185
- useEffect(() => {
186
- fetch('/api/users')
187
- .then(r => r.json())
188
- .then(data => { setUsers(data); setLoading(false); });
189
- }, []);
190
-
191
- const handleDelete = async (id) => {
192
- await fetch(`/api/users/${id}`, { method: 'DELETE' });
193
- setUsers(prev => prev.filter(u => u.id !== id));
194
- };
195
- // ... long JSX
196
- };
197
- ```
198
-
199
- ### Export convention
200
-
201
- ```tsx
202
- // Named export for regular components
203
- export const UserCard = ({ user }: UserCardProps) => { ... };
204
-
205
- // Default export only for page-level components
206
- export default function UserPage() { ... }
207
- ```
208
-
209
- ---
210
-
211
- ## Custom Hook Rules
212
-
213
- - Prefix with `use`: `useUserList`, `useAuth`, `useModal`
214
- - Extract all side effects, API calls, and complex state from components
215
- - Return objects (not arrays) for multiple values — easier to destructure
216
-
217
- ```tsx
218
- // Good
219
- const useUserList = () => {
220
- const { data: users = [], isLoading, error } = useQuery({
221
- queryKey: ['users'],
222
- queryFn: userApi.getAll,
223
- });
224
-
225
- const { mutate: deleteUser } = useMutation({
226
- mutationFn: userApi.delete,
227
- onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }),
228
- });
229
-
230
- return { users, isLoading, error, deleteUser };
231
- };
232
-
233
- // Usage
234
- const { users, isLoading, deleteUser } = useUserList();
235
- ```
236
-
237
- ---
238
-
239
- ## State Management Rules
240
-
241
- ### Server state TanStack Query
242
-
243
- Use for all API data (fetching, caching, mutations):
244
-
245
- ```tsx
246
- // Fetching
247
- const { data, isLoading, error } = useQuery({
248
- queryKey: ['users', filters], // include deps in key
249
- queryFn: () => userApi.getAll(filters),
250
- staleTime: 5 * 60 * 1000, // 5 minutes
251
- });
252
-
253
- // Mutation
254
- const { mutate, isPending } = useMutation({
255
- mutationFn: userApi.create,
256
- onSuccess: () => {
257
- queryClient.invalidateQueries({ queryKey: ['users'] });
258
- toast.success('User created');
259
- },
260
- onError: (error) => toast.error(error.message),
261
- });
262
- ```
263
-
264
- ### Client/UI state → Zustand (or useState for local)
265
-
266
- ```tsx
267
- // Local state — use useState
268
- const [isOpen, setIsOpen] = useState(false);
269
-
270
- // Shared UI state — use Zustand
271
- const useAuthStore = create<AuthStore>((set) => ({
272
- user: null,
273
- token: null,
274
- login: (user, token) => set({ user, token }),
275
- logout: () => set({ user: null, token: null }),
276
- }));
277
- ```
278
-
279
- ### Rules:
280
- - **Never** use global state for server/API data — that's TanStack Query's job
281
- - **Never** fetch in `useEffect` manually — use TanStack Query
282
- - **Never** put loading/error state in Zustand — TanStack Query handles that
283
-
284
- ---
285
-
286
- ## Form Rules
287
-
288
- Use **React Hook Form + Zod** for all forms:
289
-
290
- ```tsx
291
- // 1. Define schema with Zod
292
- const createUserSchema = z.object({
293
- email: z.string().email('Invalid email'),
294
- fullName: z.string().min(2, 'Min 2 characters').max(100),
295
- password: z.string().min(8, 'Min 8 characters'),
296
- });
297
-
298
- type CreateUserForm = z.infer<typeof createUserSchema>;
299
-
300
- // 2. Use in component
301
- const CreateUserForm = () => {
302
- const { register, handleSubmit, formState: { errors, isSubmitting } } = useForm<CreateUserForm>({
303
- resolver: zodResolver(createUserSchema),
304
- });
305
-
306
- const onSubmit = async (data: CreateUserForm) => {
307
- await createUser(data);
308
- };
309
-
310
- return (
311
- <form onSubmit={handleSubmit(onSubmit)}>
312
- <input {...register('email')} />
313
- {errors.email && <span>{errors.email.message}</span>}
314
-
315
- <button type="submit" disabled={isSubmitting}>
316
- {isSubmitting ? 'Saving...' : 'Create'}
317
- </button>
318
- </form>
319
- );
320
- };
321
- ```
322
-
323
- ---
324
-
325
- ## API Layer Rules
326
-
327
- One `axios` instance shared across the app with interceptors:
328
-
329
- ```tsx
330
- // src/services/api.ts
331
- const api = axios.create({
332
- baseURL: import.meta.env.VITE_API_URL,
333
- timeout: 10000,
334
- headers: { 'Content-Type': 'application/json' },
335
- });
336
-
337
- // Attach token automatically
338
- api.interceptors.request.use((config) => {
339
- const token = useAuthStore.getState().token;
340
- if (token) config.headers.Authorization = `Bearer ${token}`;
341
- return config;
342
- });
343
-
344
- // Handle 401 globally
345
- api.interceptors.response.use(
346
- (response) => response,
347
- (error) => {
348
- if (error.response?.status === 401) {
349
- useAuthStore.getState().logout();
350
- window.location.href = '/login';
351
- }
352
- return Promise.reject(error);
353
- }
354
- );
355
- ```
356
-
357
- Feature API module:
358
-
359
- ```tsx
360
- // src/features/users/api.ts
361
- export const userApi = {
362
- getAll: (params?: UserListParams): Promise<User[]> =>
363
- api.get('/users', { params }).then(r => r.data),
364
-
365
- getById: (id: number): Promise<User> =>
366
- api.get(`/users/${id}`).then(r => r.data),
367
-
368
- create: (data: CreateUserRequest): Promise<User> =>
369
- api.post('/users', data).then(r => r.data),
370
-
371
- update: (id: number, data: UpdateUserRequest): Promise<User> =>
372
- api.put(`/users/${id}`, data).then(r => r.data),
373
-
374
- delete: (id: number): Promise<void> =>
375
- api.delete(`/users/${id}`).then(r => r.data),
376
- };
377
- ```
378
-
379
- ---
380
-
381
- ## TypeScript Rules
382
-
383
- - **No `any`** — use `unknown`, proper types, or generics
384
- - Define shared types in `types.ts` files
385
- - Use `interface` for object shapes, `type` for unions/intersections
386
- - Always type function return values for public hooks and API functions
387
-
388
- ```tsx
389
- // ✅ Good
390
- interface User {
391
- id: number;
392
- email: string;
393
- fullName: string;
394
- createdAt: string;
395
- }
396
-
397
- type UserStatus = 'active' | 'inactive' | 'banned';
398
-
399
- const getUser = async (id: number): Promise<User> => {
400
- return api.get(`/users/${id}`).then(r => r.data);
401
- };
402
-
403
- // ❌ Bad
404
- const getUser = async (id: any) => {
405
- return api.get(`/users/${id}`).then((r: any) => r.data);
406
- };
407
- ```
408
-
409
- ---
410
-
411
- ## Styling Rules (Tailwind CSS)
412
-
413
- - Use utility classes directly — avoid custom CSS unless necessary
414
- - Extract repeated class combinations into components or a `cn()` helper
415
- - Use `className` prop for external style overrides
416
-
417
- ```tsx
418
- // ✅ Good — cn() for conditional classes
419
- import { cn } from '@/utils/cn';
420
-
421
- const Button = ({ variant = 'primary', disabled, className, children }: ButtonProps) => (
422
- <button
423
- className={cn(
424
- 'px-4 py-2 rounded font-medium transition-colors',
425
- variant === 'primary' && 'bg-blue-600 text-white hover:bg-blue-700',
426
- variant === 'outline' && 'border border-gray-300 hover:bg-gray-50',
427
- disabled && 'opacity-50 cursor-not-allowed',
428
- className
429
- )}
430
- disabled={disabled}
431
- >
432
- {children}
433
- </button>
434
- );
435
-
436
- // Bad — inline style
437
- <button style={{ backgroundColor: 'blue', padding: '8px 16px' }}>
438
- ```
439
-
440
- ---
441
-
442
- ## Performance Rules
443
-
444
- - Use `React.memo` only when profiling shows a real problem — do not optimize prematurely
445
- - Use `useMemo` / `useCallback` for expensive computations or stable references passed to children
446
- - Lazy-load routes with `React.lazy` + `Suspense`
447
- - Avoid anonymous functions in JSX props when passing to memoized children
448
-
449
- ```tsx
450
- // Lazy routes
451
- const UserPage = React.lazy(() => import('./pages/UserPage'));
452
-
453
- <Suspense fallback={<PageSkeleton />}>
454
- <UserPage />
455
- </Suspense>
456
-
457
- // useMemo for expensive computation
458
- const sortedUsers = useMemo(
459
- () => [...users].sort((a, b) => a.fullName.localeCompare(b.fullName)),
460
- [users]
461
- );
462
- ```
463
-
464
- ---
465
-
466
- ## Error Handling Rules
467
-
468
- - Use React Error Boundary at the route level
469
- - Handle API errors in TanStack Query `onError` callbacks
470
- - Show user-friendly messages never raw error objects
471
- - Use toast notifications for async operation feedback
472
-
473
- ```tsx
474
- // Error boundary at route level
475
- <ErrorBoundary fallback={<ErrorPage />}>
476
- <Routes>
477
- <Route path="/users" element={<UserPage />} />
478
- </Routes>
479
- </ErrorBoundary>
480
-
481
- // Async error handling
482
- const { mutate } = useMutation({
483
- mutationFn: userApi.create,
484
- onSuccess: () => toast.success('User created successfully'),
485
- onError: (error: AxiosError<ApiError>) =>
486
- toast.error(error.response?.data?.message ?? 'Something went wrong'),
487
- });
488
- ```
489
-
490
- ---
491
-
492
- ## Testing Rules
493
-
494
- ### Unit/Component tests with React Testing Library
495
-
496
- ```tsx
497
- // Test behavior, not implementation
498
- describe('UserCard', () => {
499
- it('should display user name and email', () => {
500
- const user: User = { id: 1, email: 'test@example.com', fullName: 'Test User' };
501
- render(<UserCard user={user} />);
502
-
503
- expect(screen.getByText('Test User')).toBeInTheDocument();
504
- expect(screen.getByText('test@example.com')).toBeInTheDocument();
505
- });
506
-
507
- it('should call onDelete with user id when delete button clicked', async () => {
508
- const onDelete = vi.fn();
509
- const user: User = { id: 1, email: 'test@example.com', fullName: 'Test User' };
510
-
511
- render(<UserCard user={user} onDelete={onDelete} />);
512
- await userEvent.click(screen.getByRole('button', { name: /delete/i }));
513
-
514
- expect(onDelete).toHaveBeenCalledWith(1);
515
- });
516
- });
517
- ```
518
-
519
- ### Hook tests
520
-
521
- ```tsx
522
- import { renderHook, waitFor } from '@testing-library/react';
523
-
524
- it('should fetch users', async () => {
525
- const { result } = renderHook(() => useUserList(), { wrapper: QueryWrapper });
526
-
527
- await waitFor(() => expect(result.current.isLoading).toBe(false));
528
-
529
- expect(result.current.users).toHaveLength(2);
530
- });
531
- ```
532
-
533
- ---
534
-
535
- ## Naming Conventions
536
-
537
- | Element | Convention | Example |
538
- |---------|-----------|---------|
539
- | Component file | PascalCase | `UserCard.tsx` |
540
- | Hook file | camelCase | `useUserList.ts` |
541
- | Utility file | camelCase | `formatDate.ts` |
542
- | Type/Interface | PascalCase | `User`, `CreateUserRequest` |
543
- | Component | PascalCase | `UserCard`, `OrderList` |
544
- | Hook | `use` + PascalCase | `useUserList`, `useAuth` |
545
- | Event handler | `handle` + Action | `handleDelete`, `handleSubmit` |
546
- | Boolean variable | `is/has/can` prefix | `isLoading`, `hasError`, `canEdit` |
547
- | Constant | UPPER_SNAKE_CASE | `MAX_RETRY`, `API_TIMEOUT` |
548
- | CSS class (custom) | kebab-case | `user-card`, `nav-item` |
549
-
550
- ---
551
-
552
- ## Common Anti-Patterns to Avoid
553
-
554
- - ❌ Fetching in `useEffect` → use TanStack Query
555
- - ❌ Using `any` type → define proper TypeScript types
556
- - ❌ Storing server data in Zustand → that's TanStack Query's responsibility
557
- - ❌ Class components → use functional components
558
- - ❌ `index.js` everywhere → use named files for traceability
559
- - ❌ Direct DOM manipulation → use React state/refs
560
- - ❌ Mutating state directly → always use setter functions
561
- - ❌ Large components doing everything → split into smaller focused components
562
- - ❌ Prop drilling more than 2 levels → use context or Zustand
563
- - ❌ Hardcoding API URLs → use `import.meta.env.VITE_API_URL`
564
-
565
- ---
566
-
567
- When explaining changes, refer to the [React Official Documentation](https://react.dev), [TanStack Query](https://tanstack.com/query), and [React Hook Form](https://react-hook-form.com) conventions.
1
+ # React.js AI System Prompt
2
+
3
+ You are an expert React.js developer. Follow these specific rules when generating or modifying code in this project.
4
+
5
+ ---
6
+
7
+ ## Project Stack
8
+
9
+ - **React:** 18+
10
+ - **Language:** TypeScript
11
+ - **Build Tool:** Vite (prefer) or Create React App
12
+ - **State Management:** Zustand (local/global) or TanStack Query (server state)
13
+ - **Routing:** React Router v6
14
+ - **Styling:** Tailwind CSS + shadcn/ui (or CSS Modules if no Tailwind)
15
+ - **Form:** React Hook Form + Zod validation
16
+ - **HTTP Client:** Axios with interceptors
17
+ - **Testing:** Vitest + React Testing Library
18
+
19
+ ---
20
+
21
+ ## Project Structure
22
+
23
+ ```
24
+ src/
25
+ ├── components/
26
+ │ ├── ui/ # Generic, reusable UI (Button, Input, Modal...)
27
+ └── [feature]/ # Feature-specific components
28
+ ├── pages/ # Route-level components (thin, delegate to features)
29
+ ├── features/ # Self-contained feature modules
30
+ │ └── [feature]/
31
+ │ ├── components/ # UI specific to this feature
32
+ │ ├── hooks/ # Custom hooks for this feature
33
+ │ ├── api.ts # API calls for this feature
34
+ │ ├── store.ts # Zustand slice (if needed)
35
+ │ ├── types.ts # TypeScript types/interfaces
36
+ │ └── index.ts # Public exports
37
+ ├── hooks/ # Shared custom hooks
38
+ ├── services/
39
+ │ └── api.ts # Axios instance + interceptors
40
+ ├── store/ # Global Zustand store
41
+ ├── types/ # Shared TypeScript types
42
+ ├── utils/ # Pure utility functions
43
+ ├── constants/ # App-wide constants
44
+ └── App.tsx
45
+ ```
46
+
47
+ ---
48
+
49
+ ## Component Rules
50
+
51
+ ### Always use Functional Components
52
+
53
+ ```tsx
54
+ // ✅ Good
55
+ const UserCard = ({ user }: UserCardProps) => {
56
+ return <div>{user.name}</div>;
57
+ };
58
+
59
+ // Bad — class component
60
+ class UserCard extends React.Component {}
61
+ ```
62
+
63
+ ### Define Props with TypeScript interface
64
+
65
+ ```tsx
66
+ // ✅ Good
67
+ interface UserCardProps {
68
+ user: User;
69
+ onDelete?: (id: number) => void;
70
+ className?: string;
71
+ }
72
+
73
+ const UserCard = ({ user, onDelete, className }: UserCardProps) => {
74
+ // ...
75
+ };
76
+
77
+ // ❌ Bad — any, or no type
78
+ const UserCard = ({ user }: any) => {};
79
+ const UserCard = (props) => {};
80
+ ```
81
+
82
+ ### Keep components focused and small
83
+
84
+ - One component = one responsibility
85
+ - If a component exceeds ~150 lines, split it
86
+ - Extract logic to custom hooks, keep JSX clean
87
+
88
+ ```tsx
89
+ // Good logic extracted to hook
90
+ const UserList = () => {
91
+ const { users, isLoading, error, deleteUser } = useUserList();
92
+
93
+ if (isLoading) return <Spinner />;
94
+ if (error) return <ErrorMessage message={error.message} />;
95
+
96
+ return (
97
+ <ul>
98
+ {users.map(user => (
99
+ <UserCard key={user.id} user={user} onDelete={deleteUser} />
100
+ ))}
101
+ </ul>
102
+ );
103
+ };
104
+
105
+ // ❌ Bad — logic mixed into component
106
+ const UserList = () => {
107
+ const [users, setUsers] = useState([]);
108
+ const [loading, setLoading] = useState(true);
109
+
110
+ useEffect(() => {
111
+ fetch('/api/users')
112
+ .then(r => r.json())
113
+ .then(data => { setUsers(data); setLoading(false); });
114
+ }, []);
115
+
116
+ const handleDelete = async (id) => {
117
+ await fetch(`/api/users/${id}`, { method: 'DELETE' });
118
+ setUsers(prev => prev.filter(u => u.id !== id));
119
+ };
120
+ // ... long JSX
121
+ };
122
+ ```
123
+
124
+ ### Export convention
125
+
126
+ ```tsx
127
+ // Named export for regular components
128
+ export const UserCard = ({ user }: UserCardProps) => { ... };
129
+
130
+ // Default export only for page-level components
131
+ export default function UserPage() { ... }
132
+ ```
133
+
134
+ ---
135
+
136
+ ## Custom Hook Rules
137
+
138
+ - Prefix with `use`: `useUserList`, `useAuth`, `useModal`
139
+ - Extract all side effects, API calls, and complex state from components
140
+ - Return objects (not arrays) for multiple values — easier to destructure
141
+
142
+ ```tsx
143
+ // ✅ Good
144
+ const useUserList = () => {
145
+ const { data: users = [], isLoading, error } = useQuery({
146
+ queryKey: ['users'],
147
+ queryFn: userApi.getAll,
148
+ });
149
+
150
+ const { mutate: deleteUser } = useMutation({
151
+ mutationFn: userApi.delete,
152
+ onSuccess: () => queryClient.invalidateQueries({ queryKey: ['users'] }),
153
+ });
154
+
155
+ return { users, isLoading, error, deleteUser };
156
+ };
157
+
158
+ // Usage
159
+ const { users, isLoading, deleteUser } = useUserList();
160
+ ```
161
+
162
+ ---
163
+
164
+ ## State Management Rules
165
+
166
+ ### Server state TanStack Query
167
+
168
+ Use for all API data (fetching, caching, mutations):
169
+
170
+ ```tsx
171
+ // Fetching
172
+ const { data, isLoading, error } = useQuery({
173
+ queryKey: ['users', filters], // include deps in key
174
+ queryFn: () => userApi.getAll(filters),
175
+ staleTime: 5 * 60 * 1000, // 5 minutes
176
+ });
177
+
178
+ // Mutation
179
+ const { mutate, isPending } = useMutation({
180
+ mutationFn: userApi.create,
181
+ onSuccess: () => {
182
+ queryClient.invalidateQueries({ queryKey: ['users'] });
183
+ toast.success('User created');
184
+ },
185
+ onError: (error) => toast.error(error.message),
186
+ });
187
+ ```
188
+
189
+ ### Client/UI state → Zustand (or useState for local)
190
+
191
+ ```tsx
192
+ // Local state use useState
193
+ const [isOpen, setIsOpen] = useState(false);
194
+
195
+ // Shared UI state — use Zustand
196
+ const useAuthStore = create<AuthStore>((set) => ({
197
+ user: null,
198
+ token: null,
199
+ login: (user, token) => set({ user, token }),
200
+ logout: () => set({ user: null, token: null }),
201
+ }));
202
+ ```
203
+
204
+ ### Rules:
205
+ - **Never** use global state for server/API data — that's TanStack Query's job
206
+ - **Never** fetch in `useEffect` manually — use TanStack Query
207
+ - **Never** put loading/error state in Zustand — TanStack Query handles that
208
+
209
+ ---
210
+
211
+ ## Form Rules
212
+
213
+ Use **React Hook Form + Zod** for all forms:
214
+
215
+ ```tsx
216
+ // 1. Define schema with Zod
217
+ const createUserSchema = z.object({
218
+ email: z.string().email('Invalid email'),
219
+ fullName: z.string().min(2, 'Min 2 characters').max(100),
220
+ password: z.string().min(8, 'Min 8 characters'),
221
+ });
222
+
223
+ type CreateUserForm = z.infer<typeof createUserSchema>;
224
+
225
+ // 2. Use in component
226
+ const CreateUserForm = () => {
227
+ const { register, handleSubmit, formState: { errors, isSubmitting } } = useForm<CreateUserForm>({
228
+ resolver: zodResolver(createUserSchema),
229
+ });
230
+
231
+ const onSubmit = async (data: CreateUserForm) => {
232
+ await createUser(data);
233
+ };
234
+
235
+ return (
236
+ <form onSubmit={handleSubmit(onSubmit)}>
237
+ <input {...register('email')} />
238
+ {errors.email && <span>{errors.email.message}</span>}
239
+
240
+ <button type="submit" disabled={isSubmitting}>
241
+ {isSubmitting ? 'Saving...' : 'Create'}
242
+ </button>
243
+ </form>
244
+ );
245
+ };
246
+ ```
247
+
248
+ ---
249
+
250
+ ## API Layer Rules
251
+
252
+ One `axios` instance shared across the app with interceptors:
253
+
254
+ ```tsx
255
+ // src/services/api.ts
256
+ const api = axios.create({
257
+ baseURL: import.meta.env.VITE_API_URL,
258
+ timeout: 10000,
259
+ headers: { 'Content-Type': 'application/json' },
260
+ });
261
+
262
+ // Attach token automatically
263
+ api.interceptors.request.use((config) => {
264
+ const token = useAuthStore.getState().token;
265
+ if (token) config.headers.Authorization = `Bearer ${token}`;
266
+ return config;
267
+ });
268
+
269
+ // Handle 401 globally
270
+ api.interceptors.response.use(
271
+ (response) => response,
272
+ (error) => {
273
+ if (error.response?.status === 401) {
274
+ useAuthStore.getState().logout();
275
+ window.location.href = '/login';
276
+ }
277
+ return Promise.reject(error);
278
+ }
279
+ );
280
+ ```
281
+
282
+ Feature API module:
283
+
284
+ ```tsx
285
+ // src/features/users/api.ts
286
+ export const userApi = {
287
+ getAll: (params?: UserListParams): Promise<User[]> =>
288
+ api.get('/users', { params }).then(r => r.data),
289
+
290
+ getById: (id: number): Promise<User> =>
291
+ api.get(`/users/${id}`).then(r => r.data),
292
+
293
+ create: (data: CreateUserRequest): Promise<User> =>
294
+ api.post('/users', data).then(r => r.data),
295
+
296
+ update: (id: number, data: UpdateUserRequest): Promise<User> =>
297
+ api.put(`/users/${id}`, data).then(r => r.data),
298
+
299
+ delete: (id: number): Promise<void> =>
300
+ api.delete(`/users/${id}`).then(r => r.data),
301
+ };
302
+ ```
303
+
304
+ ---
305
+
306
+ ## TypeScript Rules
307
+
308
+ - **No `any`** — use `unknown`, proper types, or generics
309
+ - Define shared types in `types.ts` files
310
+ - Use `interface` for object shapes, `type` for unions/intersections
311
+ - Always type function return values for public hooks and API functions
312
+
313
+ ```tsx
314
+ // ✅ Good
315
+ interface User {
316
+ id: number;
317
+ email: string;
318
+ fullName: string;
319
+ createdAt: string;
320
+ }
321
+
322
+ type UserStatus = 'active' | 'inactive' | 'banned';
323
+
324
+ const getUser = async (id: number): Promise<User> => {
325
+ return api.get(`/users/${id}`).then(r => r.data);
326
+ };
327
+
328
+ // ❌ Bad
329
+ const getUser = async (id: any) => {
330
+ return api.get(`/users/${id}`).then((r: any) => r.data);
331
+ };
332
+ ```
333
+
334
+ ---
335
+
336
+ ## Styling Rules (Tailwind CSS)
337
+
338
+ - Use utility classes directly — avoid custom CSS unless necessary
339
+ - Extract repeated class combinations into components or a `cn()` helper
340
+ - Use `className` prop for external style overrides
341
+
342
+ ```tsx
343
+ // ✅ Good — cn() for conditional classes
344
+ import { cn } from '@/utils/cn';
345
+
346
+ const Button = ({ variant = 'primary', disabled, className, children }: ButtonProps) => (
347
+ <button
348
+ className={cn(
349
+ 'px-4 py-2 rounded font-medium transition-colors',
350
+ variant === 'primary' && 'bg-blue-600 text-white hover:bg-blue-700',
351
+ variant === 'outline' && 'border border-gray-300 hover:bg-gray-50',
352
+ disabled && 'opacity-50 cursor-not-allowed',
353
+ className
354
+ )}
355
+ disabled={disabled}
356
+ >
357
+ {children}
358
+ </button>
359
+ );
360
+
361
+ // Bad inline style
362
+ <button style={{ backgroundColor: 'blue', padding: '8px 16px' }}>
363
+ ```
364
+
365
+ ---
366
+
367
+ ## Performance Rules
368
+
369
+ - Use `React.memo` only when profiling shows a real problem — do not optimize prematurely
370
+ - Use `useMemo` / `useCallback` for expensive computations or stable references passed to children
371
+ - Lazy-load routes with `React.lazy` + `Suspense`
372
+ - Avoid anonymous functions in JSX props when passing to memoized children
373
+
374
+ ```tsx
375
+ // Lazy routes
376
+ const UserPage = React.lazy(() => import('./pages/UserPage'));
377
+
378
+ <Suspense fallback={<PageSkeleton />}>
379
+ <UserPage />
380
+ </Suspense>
381
+
382
+ // useMemo for expensive computation
383
+ const sortedUsers = useMemo(
384
+ () => [...users].sort((a, b) => a.fullName.localeCompare(b.fullName)),
385
+ [users]
386
+ );
387
+ ```
388
+
389
+ ---
390
+
391
+ ## Error Handling Rules
392
+
393
+ - Use React Error Boundary at the route level
394
+ - Handle API errors in TanStack Query `onError` callbacks
395
+ - Show user-friendly messages — never raw error objects
396
+ - Use toast notifications for async operation feedback
397
+
398
+ ```tsx
399
+ // Error boundary at route level
400
+ <ErrorBoundary fallback={<ErrorPage />}>
401
+ <Routes>
402
+ <Route path="/users" element={<UserPage />} />
403
+ </Routes>
404
+ </ErrorBoundary>
405
+
406
+ // Async error handling
407
+ const { mutate } = useMutation({
408
+ mutationFn: userApi.create,
409
+ onSuccess: () => toast.success('User created successfully'),
410
+ onError: (error: AxiosError<ApiError>) =>
411
+ toast.error(error.response?.data?.message ?? 'Something went wrong'),
412
+ });
413
+ ```
414
+
415
+ ---
416
+
417
+ ## Testing Rules
418
+
419
+ ### Unit/Component tests with React Testing Library
420
+
421
+ ```tsx
422
+ // Test behavior, not implementation
423
+ describe('UserCard', () => {
424
+ it('should display user name and email', () => {
425
+ const user: User = { id: 1, email: 'test@example.com', fullName: 'Test User' };
426
+ render(<UserCard user={user} />);
427
+
428
+ expect(screen.getByText('Test User')).toBeInTheDocument();
429
+ expect(screen.getByText('test@example.com')).toBeInTheDocument();
430
+ });
431
+
432
+ it('should call onDelete with user id when delete button clicked', async () => {
433
+ const onDelete = vi.fn();
434
+ const user: User = { id: 1, email: 'test@example.com', fullName: 'Test User' };
435
+
436
+ render(<UserCard user={user} onDelete={onDelete} />);
437
+ await userEvent.click(screen.getByRole('button', { name: /delete/i }));
438
+
439
+ expect(onDelete).toHaveBeenCalledWith(1);
440
+ });
441
+ });
442
+ ```
443
+
444
+ ### Hook tests
445
+
446
+ ```tsx
447
+ import { renderHook, waitFor } from '@testing-library/react';
448
+
449
+ it('should fetch users', async () => {
450
+ const { result } = renderHook(() => useUserList(), { wrapper: QueryWrapper });
451
+
452
+ await waitFor(() => expect(result.current.isLoading).toBe(false));
453
+
454
+ expect(result.current.users).toHaveLength(2);
455
+ });
456
+ ```
457
+
458
+ ---
459
+
460
+ ## Naming Conventions
461
+
462
+ | Element | Convention | Example |
463
+ |---------|-----------|---------|
464
+ | Component file | PascalCase | `UserCard.tsx` |
465
+ | Hook file | camelCase | `useUserList.ts` |
466
+ | Utility file | camelCase | `formatDate.ts` |
467
+ | Type/Interface | PascalCase | `User`, `CreateUserRequest` |
468
+ | Component | PascalCase | `UserCard`, `OrderList` |
469
+ | Hook | `use` + PascalCase | `useUserList`, `useAuth` |
470
+ | Event handler | `handle` + Action | `handleDelete`, `handleSubmit` |
471
+ | Boolean variable | `is/has/can` prefix | `isLoading`, `hasError`, `canEdit` |
472
+ | Constant | UPPER_SNAKE_CASE | `MAX_RETRY`, `API_TIMEOUT` |
473
+ | CSS class (custom) | kebab-case | `user-card`, `nav-item` |
474
+
475
+ ---
476
+
477
+ ## Common Anti-Patterns to Avoid
478
+
479
+ - ❌ Fetching in `useEffect` → use TanStack Query
480
+ - ❌ Using `any` type → define proper TypeScript types
481
+ - Storing server data in Zustand → that's TanStack Query's responsibility
482
+ - Class components use functional components
483
+ - ❌ `index.js` everywhere → use named files for traceability
484
+ - Direct DOM manipulation → use React state/refs
485
+ - Mutating state directly → always use setter functions
486
+ - Large components doing everything → split into smaller focused components
487
+ - ❌ Prop drilling more than 2 levels → use context or Zustand
488
+ - ❌ Hardcoding API URLs → use `import.meta.env.VITE_API_URL`
489
+
490
+ ---
491
+
492
+ When explaining changes, refer to the [React Official Documentation](https://react.dev), [TanStack Query](https://tanstack.com/query), and [React Hook Form](https://react-hook-form.com) conventions.