start-vibing-stacks 2.0.1 → 2.0.3
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.
package/dist/ui.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Start Vibing Stacks — Terminal UI
|
|
3
3
|
*/
|
|
4
4
|
import chalk from 'chalk';
|
|
5
|
-
const VERSION = '2.0.
|
|
5
|
+
const VERSION = '2.0.3';
|
|
6
6
|
const gradient = (text) => {
|
|
7
7
|
const colors = [chalk.hex('#FF6B6B'), chalk.hex('#FF8E53'), chalk.hex('#FFBD2E'), chalk.hex('#48BB78'), chalk.hex('#4299E1'), chalk.hex('#9F7AEA')];
|
|
8
8
|
return text.split('').map((c, i) => colors[i % colors.length](c)).join('');
|
package/package.json
CHANGED
|
@@ -104,10 +104,121 @@ const Heavy = lazy(() => import('./HeavyComponent'));
|
|
|
104
104
|
<Suspense fallback={<Loading />}><Heavy /></Suspense>
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
## React 19 Hooks
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
// useActionState — form submission state
|
|
111
|
+
import { useActionState } from 'react';
|
|
112
|
+
|
|
113
|
+
function LoginForm() {
|
|
114
|
+
const [state, formAction, isPending] = useActionState(
|
|
115
|
+
async (prev, formData: FormData) => {
|
|
116
|
+
const result = await login(formData);
|
|
117
|
+
if (result.error) return { error: result.error };
|
|
118
|
+
redirect('/dashboard');
|
|
119
|
+
},
|
|
120
|
+
{ error: null }
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<form action={formAction}>
|
|
125
|
+
<input name="email" type="email" />
|
|
126
|
+
{state.error && <p className="text-destructive">{state.error}</p>}
|
|
127
|
+
<button disabled={isPending}>
|
|
128
|
+
{isPending ? 'Signing in...' : 'Sign In'}
|
|
129
|
+
</button>
|
|
130
|
+
</form>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// useOptimistic — instant UI feedback
|
|
135
|
+
import { useOptimistic } from 'react';
|
|
136
|
+
|
|
137
|
+
function TodoList({ todos }: { todos: Todo[] }) {
|
|
138
|
+
const [optimisticTodos, addOptimistic] = useOptimistic(
|
|
139
|
+
todos,
|
|
140
|
+
(state, newTodo: Todo) => [...state, newTodo]
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const addTodo = async (formData: FormData) => {
|
|
144
|
+
const todo = { id: crypto.randomUUID(), title: formData.get('title') as string, done: false };
|
|
145
|
+
addOptimistic(todo); // Instant UI
|
|
146
|
+
await saveTodo(todo); // Server sync
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
return <ul>{optimisticTodos.map(t => <li key={t.id}>{t.title}</li>)}</ul>;
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## State Management Selection
|
|
154
|
+
|
|
155
|
+
| Complexity | Solution | When |
|
|
156
|
+
|---|---|---|
|
|
157
|
+
| Simple local | `useState` | Single component, simple values |
|
|
158
|
+
| Complex local | `useReducer` | Multiple related state transitions |
|
|
159
|
+
| Parent-child | Lift state up | 1-2 levels |
|
|
160
|
+
| Subtree | Context + `useReducer` | Theme, auth, 3+ levels |
|
|
161
|
+
| Server state | React Query / SWR | API data, caching, refetch |
|
|
162
|
+
| Complex global | Zustand | Cross-feature, many consumers |
|
|
163
|
+
|
|
164
|
+
## Error Boundaries
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
import { Component, type ReactNode } from 'react';
|
|
168
|
+
|
|
169
|
+
class ErrorBoundary extends Component<
|
|
170
|
+
{ children: ReactNode; fallback?: ReactNode },
|
|
171
|
+
{ hasError: boolean; error?: Error }
|
|
172
|
+
> {
|
|
173
|
+
state = { hasError: false, error: undefined as Error | undefined };
|
|
174
|
+
|
|
175
|
+
static getDerivedStateFromError(error: Error) {
|
|
176
|
+
return { hasError: true, error };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
componentDidCatch(error: Error, info: React.ErrorInfo) {
|
|
180
|
+
console.error('ErrorBoundary caught:', error, info);
|
|
181
|
+
// Send to Sentry/logging
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
render() {
|
|
185
|
+
if (this.state.hasError) {
|
|
186
|
+
return this.props.fallback ?? (
|
|
187
|
+
<div className="p-8 text-center">
|
|
188
|
+
<h2 className="text-lg font-semibold text-foreground">Something went wrong</h2>
|
|
189
|
+
<button onClick={() => this.setState({ hasError: false })}
|
|
190
|
+
className="mt-4 px-4 py-2 bg-primary text-primary-foreground rounded-lg">
|
|
191
|
+
Try Again
|
|
192
|
+
</button>
|
|
193
|
+
</div>
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
return this.props.children;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Usage: wrap routes/features
|
|
201
|
+
<ErrorBoundary fallback={<ErrorPage />}>
|
|
202
|
+
<DashboardFeature />
|
|
203
|
+
</ErrorBoundary>
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Component Types
|
|
207
|
+
|
|
208
|
+
| Type | Use | Example |
|
|
209
|
+
|---|---|---|
|
|
210
|
+
| **Server** (RSC) | Data fetching, static content | Page-level components |
|
|
211
|
+
| **Client** (`'use client'`) | Interactivity, hooks, browser APIs | Forms, modals, dropdowns |
|
|
212
|
+
| **Presentational** | Pure display, props only | `<Badge>`, `<Avatar>` |
|
|
213
|
+
| **Container** | Logic + state, renders presentational | `<UserListContainer>` |
|
|
214
|
+
|
|
107
215
|
## FORBIDDEN
|
|
108
216
|
|
|
109
|
-
1. **Class components** — function components only
|
|
217
|
+
1. **Class components** — function components only (except ErrorBoundary)
|
|
110
218
|
2. **Prop drilling** — use context or composition
|
|
111
219
|
3. **Inline objects/functions in JSX** — causes re-renders
|
|
112
220
|
4. **useEffect for derived state** — use useMemo
|
|
113
221
|
5. **Mutating state directly** — always setState/dispatch
|
|
222
|
+
6. **Index as key** — use stable unique ID (`uuid`, `id`)
|
|
223
|
+
7. **Premature optimization** — profile first with React DevTools
|
|
224
|
+
8. **useEffect for everything** — prefer server components for data
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# React UI Patterns — Loading, Errors, Empty States & Forms
|
|
2
|
+
|
|
3
|
+
**ALWAYS invoke when handling async UI states, forms, or user feedback.**
|
|
4
|
+
|
|
5
|
+
## Core Principles
|
|
6
|
+
|
|
7
|
+
1. **Never show stale UI** — loading only when actually loading
|
|
8
|
+
2. **Always surface errors** — users must KNOW when something fails
|
|
9
|
+
3. **Optimistic updates** — make UI feel instant
|
|
10
|
+
4. **Progressive disclosure** — show content as it becomes available
|
|
11
|
+
5. **Disable during operations** — prevent double-submit
|
|
12
|
+
|
|
13
|
+
## Loading State Decision Tree
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
Error?
|
|
17
|
+
→ Yes: Show ErrorState with retry
|
|
18
|
+
→ No ↓
|
|
19
|
+
|
|
20
|
+
Loading AND no data?
|
|
21
|
+
→ Yes: Show Skeleton or Spinner
|
|
22
|
+
→ No ↓
|
|
23
|
+
|
|
24
|
+
Has data?
|
|
25
|
+
→ Yes + items: Render data
|
|
26
|
+
→ Yes + empty: Show EmptyState
|
|
27
|
+
→ No: Show Spinner (fallback)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
// ✅ CORRECT — only loading when no data
|
|
32
|
+
const { data, isLoading, error, refetch } = useQuery(...);
|
|
33
|
+
|
|
34
|
+
if (error) return <ErrorState error={error} onRetry={refetch} />;
|
|
35
|
+
if (isLoading && !data) return <Skeleton />;
|
|
36
|
+
if (!data?.items.length) return <EmptyState />;
|
|
37
|
+
return <ItemList items={data.items} />;
|
|
38
|
+
|
|
39
|
+
// ❌ WRONG — flashes spinner on refetch when cached data exists
|
|
40
|
+
if (isLoading) return <Spinner />;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Skeleton vs Spinner
|
|
44
|
+
|
|
45
|
+
| Use Skeleton | Use Spinner |
|
|
46
|
+
|---|---|
|
|
47
|
+
| Known content shape (cards, tables, lists) | Unknown shape (modals, inline actions) |
|
|
48
|
+
| Initial page load | Button submissions |
|
|
49
|
+
| Content placeholders | Small inline operations |
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
// Skeleton component
|
|
53
|
+
function CardSkeleton() {
|
|
54
|
+
return (
|
|
55
|
+
<div className="bg-card border border-card-line rounded-xl p-6 animate-pulse">
|
|
56
|
+
<div className="h-4 w-3/4 bg-muted rounded" />
|
|
57
|
+
<div className="mt-3 h-3 w-1/2 bg-muted rounded" />
|
|
58
|
+
<div className="mt-6 h-10 w-full bg-muted rounded" />
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Skeleton grid
|
|
64
|
+
function ListSkeleton({ count = 6 }: { count?: number }) {
|
|
65
|
+
return (
|
|
66
|
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
|
67
|
+
{Array.from({ length: count }, (_, i) => <CardSkeleton key={i} />)}
|
|
68
|
+
</div>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Error Handling Hierarchy
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
Level 1 — Inline error → Field validation (under input)
|
|
77
|
+
Level 2 — Toast notification → Recoverable, user can retry
|
|
78
|
+
Level 3 — Error banner → Page-level, data partially usable
|
|
79
|
+
Level 4 — Full error screen → Unrecoverable, needs user action
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
// Reusable ErrorState
|
|
84
|
+
interface ErrorStateProps {
|
|
85
|
+
error: Error | string;
|
|
86
|
+
onRetry?: () => void;
|
|
87
|
+
title?: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function ErrorState({ error, onRetry, title }: ErrorStateProps) {
|
|
91
|
+
const message = typeof error === 'string' ? error : error.message;
|
|
92
|
+
return (
|
|
93
|
+
<div className="flex flex-col items-center justify-center py-12 text-center">
|
|
94
|
+
<div className="h-12 w-12 rounded-full bg-destructive/10 flex items-center justify-center mb-4">
|
|
95
|
+
<AlertCircle className="h-6 w-6 text-destructive" />
|
|
96
|
+
</div>
|
|
97
|
+
<h3 className="text-lg font-semibold text-foreground">{title ?? 'Something went wrong'}</h3>
|
|
98
|
+
<p className="mt-1 text-sm text-muted-foreground max-w-md">{message}</p>
|
|
99
|
+
{onRetry && (
|
|
100
|
+
<button onClick={onRetry}
|
|
101
|
+
className="mt-4 px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary-hover transition-colors">
|
|
102
|
+
Try Again
|
|
103
|
+
</button>
|
|
104
|
+
)}
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### NEVER Swallow Errors
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
// ✅ CORRECT — error surfaced to user
|
|
114
|
+
const mutation = useMutation({
|
|
115
|
+
mutationFn: createItem,
|
|
116
|
+
onSuccess: () => toast.success('Item created!'),
|
|
117
|
+
onError: (error) => {
|
|
118
|
+
console.error('createItem failed:', error);
|
|
119
|
+
toast.error('Failed to create item');
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// ❌ WRONG — user sees nothing
|
|
124
|
+
try { await createItem(data); }
|
|
125
|
+
catch (e) { console.log(e); } // Silent failure!
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Empty States
|
|
129
|
+
|
|
130
|
+
**Every list/collection MUST have an empty state.**
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
// ✅ With empty state
|
|
134
|
+
function UserList({ users }: { users: User[] }) {
|
|
135
|
+
if (!users.length) {
|
|
136
|
+
return (
|
|
137
|
+
<EmptyState
|
|
138
|
+
icon={<Users className="h-8 w-8" />}
|
|
139
|
+
title="No users yet"
|
|
140
|
+
description="Invite your first team member"
|
|
141
|
+
action={{ label: 'Invite User', onClick: () => router.visit('/users/invite') }}
|
|
142
|
+
/>
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return <div className="space-y-2">{users.map(u => <UserCard key={u.id} user={u} />)}</div>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Reusable EmptyState
|
|
149
|
+
function EmptyState({ icon, title, description, action }: {
|
|
150
|
+
icon: ReactNode;
|
|
151
|
+
title: string;
|
|
152
|
+
description: string;
|
|
153
|
+
action?: { label: string; onClick: () => void };
|
|
154
|
+
}) {
|
|
155
|
+
return (
|
|
156
|
+
<div className="flex flex-col items-center justify-center py-16 text-center">
|
|
157
|
+
<div className="text-muted-foreground mb-4">{icon}</div>
|
|
158
|
+
<h3 className="text-lg font-semibold text-foreground">{title}</h3>
|
|
159
|
+
<p className="mt-1 text-sm text-muted-foreground max-w-sm">{description}</p>
|
|
160
|
+
{action && (
|
|
161
|
+
<button onClick={action.onClick}
|
|
162
|
+
className="mt-4 px-4 py-2 bg-primary text-primary-foreground rounded-lg hover:bg-primary-hover transition-colors">
|
|
163
|
+
{action.label}
|
|
164
|
+
</button>
|
|
165
|
+
)}
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Button States
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
// ✅ CORRECT — disabled + loading indicator
|
|
175
|
+
<button
|
|
176
|
+
onClick={handleSubmit}
|
|
177
|
+
disabled={!isValid || isSubmitting}
|
|
178
|
+
className="bg-primary text-primary-foreground px-4 py-2 rounded-lg hover:bg-primary-hover disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
|
179
|
+
>
|
|
180
|
+
{isSubmitting ? (
|
|
181
|
+
<span className="flex items-center gap-2">
|
|
182
|
+
<Loader className="h-4 w-4 animate-spin" />
|
|
183
|
+
Saving...
|
|
184
|
+
</span>
|
|
185
|
+
) : 'Save'}
|
|
186
|
+
</button>
|
|
187
|
+
|
|
188
|
+
// ❌ WRONG — user can click multiple times
|
|
189
|
+
<button onClick={handleSubmit}>
|
|
190
|
+
{isSubmitting ? 'Submitting...' : 'Submit'}
|
|
191
|
+
</button>
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Form Pattern (Inertia.js)
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
import { useForm } from '@inertiajs/react';
|
|
198
|
+
|
|
199
|
+
export default function CreateUser() {
|
|
200
|
+
const { data, setData, post, processing, errors, reset } = useForm({
|
|
201
|
+
name: '',
|
|
202
|
+
email: '',
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const submit = (e: React.FormEvent) => {
|
|
206
|
+
e.preventDefault();
|
|
207
|
+
post('/users', {
|
|
208
|
+
onSuccess: () => {
|
|
209
|
+
toast.success('User created!');
|
|
210
|
+
reset();
|
|
211
|
+
},
|
|
212
|
+
onError: () => toast.error('Failed to create user'),
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
return (
|
|
217
|
+
<form onSubmit={submit} className="space-y-4">
|
|
218
|
+
<div>
|
|
219
|
+
<label className="block text-sm font-medium text-foreground mb-1">Name</label>
|
|
220
|
+
<input
|
|
221
|
+
value={data.name}
|
|
222
|
+
onChange={e => setData('name', e.target.value)}
|
|
223
|
+
className="w-full h-10 px-3 rounded-md border border-border bg-background text-foreground"
|
|
224
|
+
/>
|
|
225
|
+
{errors.name && <p className="mt-1 text-sm text-destructive">{errors.name}</p>}
|
|
226
|
+
</div>
|
|
227
|
+
|
|
228
|
+
<div>
|
|
229
|
+
<label className="block text-sm font-medium text-foreground mb-1">Email</label>
|
|
230
|
+
<input
|
|
231
|
+
type="email"
|
|
232
|
+
value={data.email}
|
|
233
|
+
onChange={e => setData('email', e.target.value)}
|
|
234
|
+
className="w-full h-10 px-3 rounded-md border border-border bg-background text-foreground"
|
|
235
|
+
/>
|
|
236
|
+
{errors.email && <p className="mt-1 text-sm text-destructive">{errors.email}</p>}
|
|
237
|
+
</div>
|
|
238
|
+
|
|
239
|
+
<button
|
|
240
|
+
type="submit"
|
|
241
|
+
disabled={processing}
|
|
242
|
+
className="bg-primary text-primary-foreground px-6 py-2 rounded-lg hover:bg-primary-hover disabled:opacity-50 transition-colors"
|
|
243
|
+
>
|
|
244
|
+
{processing ? (
|
|
245
|
+
<span className="flex items-center gap-2">
|
|
246
|
+
<Loader className="h-4 w-4 animate-spin" /> Creating...
|
|
247
|
+
</span>
|
|
248
|
+
) : 'Create User'}
|
|
249
|
+
</button>
|
|
250
|
+
</form>
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## Optimistic Updates
|
|
256
|
+
|
|
257
|
+
```tsx
|
|
258
|
+
// Show result immediately, rollback on error
|
|
259
|
+
function ToggleFavorite({ item }: { item: Item }) {
|
|
260
|
+
const [optimistic, setOptimistic] = useState(item.isFavorite);
|
|
261
|
+
|
|
262
|
+
const toggle = () => {
|
|
263
|
+
setOptimistic(!optimistic); // Instant UI
|
|
264
|
+
router.post(`/items/${item.id}/favorite`, {}, {
|
|
265
|
+
preserveState: true,
|
|
266
|
+
onError: () => {
|
|
267
|
+
setOptimistic(item.isFavorite); // Rollback
|
|
268
|
+
toast.error('Failed to update');
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
return (
|
|
274
|
+
<button onClick={toggle} className="text-xl">
|
|
275
|
+
{optimistic ? '❤️' : '🤍'}
|
|
276
|
+
</button>
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Checklist — Before Shipping Any UI Component
|
|
282
|
+
|
|
283
|
+
- [ ] Error state handled and shown to user
|
|
284
|
+
- [ ] Loading state only when no data exists (no flash on refetch)
|
|
285
|
+
- [ ] Empty state for every collection/list
|
|
286
|
+
- [ ] Buttons disabled during async operations
|
|
287
|
+
- [ ] Buttons show loading indicator
|
|
288
|
+
- [ ] Form errors shown inline under fields
|
|
289
|
+
- [ ] Mutations have onError with user feedback
|
|
290
|
+
- [ ] Skeleton matches content layout shape
|
|
291
|
+
|
|
292
|
+
## FORBIDDEN
|
|
293
|
+
|
|
294
|
+
1. **`if (loading) return <Spinner />`** — check `loading && !data` instead
|
|
295
|
+
2. **Silent catch** — always toast/display errors to user
|
|
296
|
+
3. **No empty state** — every list needs one
|
|
297
|
+
4. **Clickable button during submit** — always `disabled={processing}`
|
|
298
|
+
5. **Console.log-only errors** — user must see feedback
|