create-nextblock 0.14.2 → 0.14.5

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 (68) hide show
  1. package/package.json +1 -1
  2. package/templates/nextblock-template/app/[slug]/page.tsx +7 -2
  3. package/templates/nextblock-template/app/[slug]/page.utils.ts +8 -3
  4. package/templates/nextblock-template/app/actions/postActions.ts +3 -0
  5. package/templates/nextblock-template/app/actions/visibilityActions.ts +210 -0
  6. package/templates/nextblock-template/app/actions/visualEditingActions.test.ts +83 -3
  7. package/templates/nextblock-template/app/actions/visualEditingActions.ts +34 -14
  8. package/templates/nextblock-template/app/api/ai/global-agent/route.ts +45 -0
  9. package/templates/nextblock-template/app/api/cron/reset-sandbox/sandboxResetSql.ts +736 -117
  10. package/templates/nextblock-template/app/api/view/route.ts +114 -0
  11. package/templates/nextblock-template/app/article/[slug]/page.utils.ts +1 -2
  12. package/templates/nextblock-template/app/cms/CmsClientLayout.tsx +2 -2
  13. package/templates/nextblock-template/app/cms/blocks/components/ColumnEditor.tsx +17 -22
  14. package/templates/nextblock-template/app/cms/blocks/components/EditableBlock.tsx +13 -19
  15. package/templates/nextblock-template/app/cms/blocks/editors/HeadingBlockEditor.tsx +45 -34
  16. package/templates/nextblock-template/app/cms/components/DraftStatusActions.tsx +10 -0
  17. package/templates/nextblock-template/app/cms/components/VisibilityBadge.tsx +62 -0
  18. package/templates/nextblock-template/app/cms/components/VisibilityControl.tsx +528 -0
  19. package/templates/nextblock-template/app/cms/pages/[id]/edit/EditPageClient.tsx +33 -17
  20. package/templates/nextblock-template/app/cms/pages/[id]/edit/page.tsx +19 -7
  21. package/templates/nextblock-template/app/cms/pages/actions.ts +17 -10
  22. package/templates/nextblock-template/app/cms/pages/components/PageForm.tsx +7 -29
  23. package/templates/nextblock-template/app/cms/pages/page.tsx +6 -19
  24. package/templates/nextblock-template/app/cms/posts/[id]/edit/page.tsx +42 -18
  25. package/templates/nextblock-template/app/cms/posts/actions.ts +16 -27
  26. package/templates/nextblock-template/app/cms/posts/components/PostForm.tsx +3 -60
  27. package/templates/nextblock-template/app/cms/posts/page.tsx +6 -13
  28. package/templates/nextblock-template/app/cms/products/[id]/edit/page.tsx +63 -9
  29. package/templates/nextblock-template/app/cms/revisions/RevisionHistoryButton.tsx +66 -32
  30. package/templates/nextblock-template/app/cms/revisions/actions.ts +332 -285
  31. package/templates/nextblock-template/app/cms/revisions/service.test.ts +498 -0
  32. package/templates/nextblock-template/app/cms/revisions/service.ts +549 -471
  33. package/templates/nextblock-template/app/cms/revisions/utils.ts +304 -132
  34. package/templates/nextblock-template/app/cms/settings/global-css/components/ThemeEditor.tsx +382 -0
  35. package/templates/nextblock-template/app/cms/settings/global-css/components/ThemeManager.tsx +267 -0
  36. package/templates/nextblock-template/app/cms/settings/global-css/page.tsx +40 -24
  37. package/templates/nextblock-template/app/cms/settings/global-css/theme-actions.ts +259 -0
  38. package/templates/nextblock-template/app/layout.tsx +49 -0
  39. package/templates/nextblock-template/app/lib/sitemap-utils.ts +6 -4
  40. package/templates/nextblock-template/app/lib/ucp/server.ts +4 -1
  41. package/templates/nextblock-template/app/page.tsx +6 -3
  42. package/templates/nextblock-template/app/product/[slug]/page.tsx +27 -3
  43. package/templates/nextblock-template/app/providers.tsx +16 -4
  44. package/templates/nextblock-template/components/blocks/renderers/HeadingBlockRenderer.tsx +7 -10
  45. package/templates/nextblock-template/components/theme-icon.tsx +78 -0
  46. package/templates/nextblock-template/components/theme-switcher.tsx +85 -90
  47. package/templates/nextblock-template/components/visual-editing/NextblockVisualEditing.tsx +4 -1
  48. package/templates/nextblock-template/context/ThemeCatalogContext.tsx +44 -0
  49. package/templates/nextblock-template/docs/03-CMS-AND-EDITOR.md +77 -0
  50. package/templates/nextblock-template/docs/04-DATABASE-AND-AUTH.md +7 -2
  51. package/templates/nextblock-template/lib/blocks/blockColors.test.ts +114 -0
  52. package/templates/nextblock-template/lib/blocks/blockColors.ts +132 -0
  53. package/templates/nextblock-template/lib/blocks/blockRegistry.ts +17 -1
  54. package/templates/nextblock-template/lib/cms-transfer/server.ts +13 -0
  55. package/templates/nextblock-template/lib/full-backup/server.ts +1 -0
  56. package/templates/nextblock-template/lib/publishing/viewUrl.ts +26 -0
  57. package/templates/nextblock-template/lib/search/server.ts +3 -0
  58. package/templates/nextblock-template/lib/setup/migrations-bundle.ts +102 -87
  59. package/templates/nextblock-template/lib/themes/buildThemeCss.test.ts +163 -0
  60. package/templates/nextblock-template/lib/themes/buildThemeCss.ts +124 -0
  61. package/templates/nextblock-template/lib/themes/tokenColor.ts +31 -0
  62. package/templates/nextblock-template/lib/themes/tokens.ts +143 -0
  63. package/templates/nextblock-template/lib/visual-editing/mutations.ts +4 -1
  64. package/templates/nextblock-template/lib/visual-editing/product-drafts.ts +46 -1
  65. package/templates/nextblock-template/next-env.d.ts +2 -2
  66. package/templates/nextblock-template/package.json +1 -1
  67. package/templates/nextblock-template/scripts/verify-site-themes.ts +63 -0
  68. package/templates/nextblock-template/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,382 @@
1
+ 'use client';
2
+
3
+ import React from 'react';
4
+ import {
5
+ Button,
6
+ ColorField,
7
+ Input,
8
+ Label,
9
+ Select,
10
+ SelectContent,
11
+ SelectItem,
12
+ SelectTrigger,
13
+ SelectValue,
14
+ } from '@nextblock-cms/ui';
15
+ import { contrastRatio, parseCssColor, rateContrast } from '@nextblock-cms/utils';
16
+ import { AlertTriangle, Check } from 'lucide-react';
17
+ import type { SiteTheme } from '../../../../../lib/themes/buildThemeCss';
18
+ import { THEME_TOKEN_GROUPS, type ThemeTokenDef } from '../../../../../lib/themes/tokens';
19
+ import {
20
+ cssColorToTokenValue,
21
+ tokenValueToCss,
22
+ tokenValueToHex,
23
+ } from '../../../../../lib/themes/tokenColor';
24
+ import { THEME_ICON_NAMES, ThemeIcon } from '../../../../../components/theme-icon';
25
+
26
+ export interface ThemeDraft {
27
+ name: string;
28
+ description: string;
29
+ icon: string;
30
+ color_scheme: 'light' | 'dark';
31
+ tokens: Record<string, string>;
32
+ extra_css: string;
33
+ is_active: boolean;
34
+ }
35
+
36
+ export function themeToDraft(theme: SiteTheme): ThemeDraft {
37
+ return {
38
+ name: theme.name,
39
+ description: theme.description ?? '',
40
+ icon: theme.icon || 'Palette',
41
+ color_scheme: theme.color_scheme,
42
+ tokens: { ...(theme.tokens ?? {}) },
43
+ extra_css: theme.extra_css ?? '',
44
+ is_active: theme.is_active,
45
+ };
46
+ }
47
+
48
+ /**
49
+ * Live contrast readout for a foreground/background token pair. Headings and
50
+ * body copy both come from these tokens, so a bad pair here is a site-wide
51
+ * legibility bug — surfacing it at edit time is the whole point.
52
+ */
53
+ function ContrastBadge({ fg, bg }: { fg?: string; bg?: string }) {
54
+ const result = React.useMemo(() => {
55
+ if (!fg || !bg) return null;
56
+ const f = parseCssColor(tokenValueToCss(fg));
57
+ const b = parseCssColor(tokenValueToCss(bg));
58
+ if (!f || !b) return null;
59
+ const ratio = contrastRatio(f, b);
60
+ return { ratio, rating: rateContrast(ratio) };
61
+ }, [fg, bg]);
62
+
63
+ if (!result) return null;
64
+ const failing = result.rating === 'Fail';
65
+ return (
66
+ <span
67
+ title={`Contrast against its paired surface: ${result.ratio}:1 (WCAG ${result.rating})`}
68
+ className={
69
+ failing
70
+ ? 'inline-flex items-center gap-1 rounded px-1.5 py-0.5 text-[10px] font-medium bg-destructive/10 text-destructive'
71
+ : 'inline-flex items-center gap-1 rounded px-1.5 py-0.5 text-[10px] font-medium bg-muted text-muted-foreground'
72
+ }
73
+ >
74
+ {failing ? <AlertTriangle className="h-3 w-3" /> : <Check className="h-3 w-3" />}
75
+ {result.ratio}:1 {result.rating}
76
+ </span>
77
+ );
78
+ }
79
+
80
+ function TokenRow({
81
+ token,
82
+ draft,
83
+ onTokenChange,
84
+ }: {
85
+ token: ThemeTokenDef;
86
+ draft: ThemeDraft;
87
+ onTokenChange: (key: string, value: string | undefined) => void;
88
+ }) {
89
+ const raw = draft.tokens[token.key];
90
+
91
+ if (token.kind === 'length') {
92
+ return (
93
+ <div className="space-y-1.5">
94
+ <Label htmlFor={`token-${token.key}`} className="text-sm">
95
+ {token.label}
96
+ </Label>
97
+ <Input
98
+ id={`token-${token.key}`}
99
+ value={raw ?? ''}
100
+ onChange={(event) => onTokenChange(token.key, event.target.value || undefined)}
101
+ placeholder="0.75rem"
102
+ className="h-9 font-mono text-xs"
103
+ />
104
+ {token.hint ? <p className="text-[11px] text-muted-foreground">{token.hint}</p> : null}
105
+ </div>
106
+ );
107
+ }
108
+
109
+ const hex = raw ? tokenValueToHex(raw) : null;
110
+ const pairedValue = token.pairedWith ? draft.tokens[token.pairedWith] : undefined;
111
+
112
+ return (
113
+ <div className="space-y-1">
114
+ <div className="flex items-center justify-between gap-2">
115
+ <Label htmlFor={`token-${token.key}`} className="text-sm">
116
+ {token.label}
117
+ </Label>
118
+ {token.pairedWith ? <ContrastBadge fg={raw} bg={pairedValue} /> : null}
119
+ </div>
120
+ <ColorField
121
+ id={`token-${token.key}`}
122
+ value={hex ?? undefined}
123
+ placeholder="Not set"
124
+ clearLabel="Unset"
125
+ onChange={(next) => {
126
+ if (!next) {
127
+ onTokenChange(token.key, undefined);
128
+ return;
129
+ }
130
+ const triplet = cssColorToTokenValue(next);
131
+ onTokenChange(token.key, triplet ?? undefined);
132
+ }}
133
+ // Theme tokens ARE the palette, so there is nothing to offer as a token
134
+ // shortcut here — this is the custom picker only.
135
+ tokens={[]}
136
+ contrastAgainst={pairedValue ? tokenValueToCss(pairedValue) : undefined}
137
+ />
138
+ <p className="font-mono text-[10px] text-muted-foreground">
139
+ --{token.key}: {raw ?? <span className="italic">inherited</span>}
140
+ </p>
141
+ </div>
142
+ );
143
+ }
144
+
145
+ export function ThemeEditor({
146
+ theme,
147
+ draft,
148
+ onDraftChange,
149
+ }: {
150
+ theme: SiteTheme;
151
+ draft: ThemeDraft;
152
+ onDraftChange: (next: ThemeDraft) => void;
153
+ }) {
154
+ const set = <K extends keyof ThemeDraft>(key: K, value: ThemeDraft[K]) =>
155
+ onDraftChange({ ...draft, [key]: value });
156
+
157
+ const onTokenChange = (key: string, value: string | undefined) => {
158
+ const tokens = { ...draft.tokens };
159
+ if (value === undefined) delete tokens[key];
160
+ else tokens[key] = value;
161
+ onDraftChange({ ...draft, tokens });
162
+ };
163
+
164
+ return (
165
+ <div className="space-y-6">
166
+ <div className="grid gap-4 sm:grid-cols-2">
167
+ <div className="space-y-1.5">
168
+ <Label htmlFor="theme-name">Name</Label>
169
+ <Input
170
+ id="theme-name"
171
+ value={draft.name}
172
+ onChange={(event) => set('name', event.target.value)}
173
+ placeholder="Midnight"
174
+ />
175
+ </div>
176
+ <div className="space-y-1.5">
177
+ <Label htmlFor="theme-icon">Switcher icon</Label>
178
+ <Select value={draft.icon} onValueChange={(value) => set('icon', value)}>
179
+ <SelectTrigger id="theme-icon">
180
+ <SelectValue />
181
+ </SelectTrigger>
182
+ <SelectContent className="max-h-64">
183
+ {THEME_ICON_NAMES.map((name) => (
184
+ <SelectItem key={name} value={name}>
185
+ <span className="flex items-center gap-2">
186
+ <ThemeIcon name={name} size={14} />
187
+ {name}
188
+ </span>
189
+ </SelectItem>
190
+ ))}
191
+ </SelectContent>
192
+ </Select>
193
+ </div>
194
+ <div className="space-y-1.5">
195
+ <Label htmlFor="theme-description">Description</Label>
196
+ <Input
197
+ id="theme-description"
198
+ value={draft.description}
199
+ onChange={(event) => set('description', event.target.value)}
200
+ placeholder="What this theme is for"
201
+ />
202
+ </div>
203
+ <div className="space-y-1.5">
204
+ <Label htmlFor="theme-scheme">Colour scheme</Label>
205
+ <Select
206
+ value={draft.color_scheme}
207
+ onValueChange={(value) => set('color_scheme', value as 'light' | 'dark')}
208
+ >
209
+ <SelectTrigger id="theme-scheme">
210
+ <SelectValue />
211
+ </SelectTrigger>
212
+ <SelectContent>
213
+ <SelectItem value="light">Light</SelectItem>
214
+ <SelectItem value="dark">Dark</SelectItem>
215
+ </SelectContent>
216
+ </Select>
217
+ <p className="text-[11px] text-muted-foreground">
218
+ Sets the CSS <code>color-scheme</code> for native controls. Dark also applies Tailwind&apos;s{' '}
219
+ <code>dark:</code> variants.
220
+ </p>
221
+ </div>
222
+ </div>
223
+
224
+ <div className="space-y-1.5">
225
+ <div className="flex items-center gap-2">
226
+ <input
227
+ id="theme-active"
228
+ type="checkbox"
229
+ className="h-4 w-4 rounded border-input"
230
+ checked={draft.is_active}
231
+ onChange={(event) => set('is_active', event.target.checked)}
232
+ disabled={theme.is_default}
233
+ />
234
+ <Label htmlFor="theme-active" className="text-sm font-normal">
235
+ Show in the theme switcher
236
+ </Label>
237
+ </div>
238
+ {theme.is_default ? (
239
+ <p className="text-[11px] text-muted-foreground">The default theme is always visible.</p>
240
+ ) : null}
241
+ </div>
242
+
243
+ {THEME_TOKEN_GROUPS.map((group) => (
244
+ <section key={group.id} className="space-y-3">
245
+ <div>
246
+ <h3 className="text-sm font-semibold">{group.label}</h3>
247
+ <p className="text-[11px] text-muted-foreground">{group.description}</p>
248
+ </div>
249
+ <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
250
+ {group.tokens.map((token) => (
251
+ <TokenRow key={token.key} token={token} draft={draft} onTokenChange={onTokenChange} />
252
+ ))}
253
+ </div>
254
+ </section>
255
+ ))}
256
+
257
+ <section className="space-y-2">
258
+ <Label htmlFor="theme-extra-css">Extra CSS for this theme</Label>
259
+ <p className="text-[11px] text-muted-foreground">
260
+ Scoped automatically to this theme — start selectors with <code>&amp;</code>, e.g.{' '}
261
+ <code>&amp; h1 {'{'} text-shadow: 0 0 5px hsl(var(--primary)); {'}'}</code>
262
+ </p>
263
+ <textarea
264
+ id="theme-extra-css"
265
+ value={draft.extra_css}
266
+ onChange={(event) => set('extra_css', event.target.value)}
267
+ spellCheck={false}
268
+ className="flex min-h-[140px] w-full rounded-md border border-input bg-background px-3 py-2 font-mono text-xs ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2"
269
+ placeholder={'& h1 {\n letter-spacing: -0.02em;\n}'}
270
+ />
271
+ </section>
272
+ </div>
273
+ );
274
+ }
275
+
276
+ /** Renders the draft as a self-contained preview card, scoped by inline vars. */
277
+ export function ThemePreview({ draft }: { draft: ThemeDraft }) {
278
+ const style = React.useMemo(() => {
279
+ const vars: Record<string, string> = {};
280
+ for (const [key, value] of Object.entries(draft.tokens)) {
281
+ vars[`--${key}`] = value;
282
+ }
283
+ return vars as React.CSSProperties;
284
+ }, [draft.tokens]);
285
+
286
+ return (
287
+ <div
288
+ style={style}
289
+ className="rounded-lg border p-4"
290
+ // The preview paints with the draft's own tokens, so it updates live
291
+ // without saving and without touching the surrounding CMS chrome.
292
+ >
293
+ <div className="rounded-md p-4" style={{ background: 'hsl(var(--background))' }}>
294
+ <p className="text-sm font-semibold" style={{ color: 'hsl(var(--foreground))' }}>
295
+ {draft.name || 'Untitled theme'}
296
+ </p>
297
+ <p className="mt-1 text-xs" style={{ color: 'hsl(var(--muted-foreground))' }}>
298
+ {draft.description || 'The quick brown fox jumps over the lazy dog.'}
299
+ </p>
300
+ <div className="mt-3 flex flex-wrap gap-2">
301
+ <span
302
+ className="rounded px-2.5 py-1 text-xs font-medium"
303
+ style={{
304
+ background: 'hsl(var(--primary))',
305
+ color: 'hsl(var(--primary-foreground))',
306
+ borderRadius: 'var(--radius)',
307
+ }}
308
+ >
309
+ Primary
310
+ </span>
311
+ <span
312
+ className="rounded px-2.5 py-1 text-xs font-medium"
313
+ style={{
314
+ background: 'hsl(var(--secondary))',
315
+ color: 'hsl(var(--secondary-foreground))',
316
+ borderRadius: 'var(--radius)',
317
+ }}
318
+ >
319
+ Secondary
320
+ </span>
321
+ <span
322
+ className="rounded px-2.5 py-1 text-xs font-medium"
323
+ style={{
324
+ background: 'hsl(var(--accent))',
325
+ color: 'hsl(var(--accent-foreground))',
326
+ borderRadius: 'var(--radius)',
327
+ }}
328
+ >
329
+ Accent
330
+ </span>
331
+ <span
332
+ className="rounded px-2.5 py-1 text-xs font-medium"
333
+ style={{
334
+ background: 'hsl(var(--destructive))',
335
+ color: 'hsl(var(--destructive-foreground))',
336
+ borderRadius: 'var(--radius)',
337
+ }}
338
+ >
339
+ Destructive
340
+ </span>
341
+ <span
342
+ className="rounded px-2.5 py-1 text-xs font-medium"
343
+ style={{
344
+ background: 'hsl(var(--warning))',
345
+ color: 'hsl(var(--warning-foreground))',
346
+ borderRadius: 'var(--radius)',
347
+ }}
348
+ >
349
+ Warning
350
+ </span>
351
+ </div>
352
+ <div
353
+ className="mt-3 rounded-md border p-3"
354
+ style={{
355
+ background: 'hsl(var(--card))',
356
+ color: 'hsl(var(--card-foreground))',
357
+ borderColor: 'hsl(var(--border))',
358
+ borderRadius: 'var(--radius)',
359
+ }}
360
+ >
361
+ <p className="text-xs font-medium">Card surface</p>
362
+ <div
363
+ className="mt-2 h-8 rounded-md border px-2 text-xs leading-8"
364
+ style={{
365
+ borderColor: 'hsl(var(--input))',
366
+ color: 'hsl(var(--muted-foreground))',
367
+ borderRadius: 'var(--radius)',
368
+ }}
369
+ >
370
+ Input field
371
+ </div>
372
+ </div>
373
+ </div>
374
+ </div>
375
+ );
376
+ }
377
+
378
+ export function ThemePreviewActions({ children }: { children: React.ReactNode }) {
379
+ return <div className="flex flex-wrap items-center gap-2">{children}</div>;
380
+ }
381
+
382
+ export { Button };
@@ -0,0 +1,267 @@
1
+ 'use client';
2
+
3
+ import React from 'react';
4
+ import { useRouter } from 'next/navigation';
5
+ import {
6
+ Button,
7
+ Input,
8
+ Label,
9
+ Dialog,
10
+ DialogContent,
11
+ DialogDescription,
12
+ DialogFooter,
13
+ DialogHeader,
14
+ DialogTitle,
15
+ } from '@nextblock-cms/ui';
16
+ import { toast } from 'sonner';
17
+ import { Copy, Loader2, Plus, Star, Trash2 } from 'lucide-react';
18
+ import type { SiteTheme } from '../../../../../lib/themes/buildThemeCss';
19
+ import {
20
+ createTheme,
21
+ deleteTheme,
22
+ duplicateTheme,
23
+ setDefaultTheme,
24
+ updateTheme,
25
+ } from '../theme-actions';
26
+ import { ThemeEditor, ThemePreview, themeToDraft, type ThemeDraft } from './ThemeEditor';
27
+ import { ThemeIcon } from '../../../../../components/theme-icon';
28
+
29
+ export default function ThemeManager({ initialThemes }: { initialThemes: SiteTheme[] }) {
30
+ const router = useRouter();
31
+ const [themes, setThemes] = React.useState(initialThemes);
32
+ const [selectedId, setSelectedId] = React.useState(initialThemes[0]?.id ?? '');
33
+ const [draft, setDraft] = React.useState<ThemeDraft | null>(
34
+ initialThemes[0] ? themeToDraft(initialThemes[0]) : null,
35
+ );
36
+ const [pending, setPending] = React.useState<string | null>(null);
37
+ const [createOpen, setCreateOpen] = React.useState(false);
38
+ const [newName, setNewName] = React.useState('');
39
+
40
+ React.useEffect(() => {
41
+ setThemes(initialThemes);
42
+ }, [initialThemes]);
43
+
44
+ const selected = themes.find((theme) => theme.id === selectedId) ?? null;
45
+
46
+ // Switching themes discards nothing silently: the draft is per-theme and only
47
+ // written back on Save, so we reseed whenever the selection changes.
48
+ const selectTheme = (theme: SiteTheme) => {
49
+ setSelectedId(theme.id);
50
+ setDraft(themeToDraft(theme));
51
+ };
52
+
53
+ const isDirty = React.useMemo(() => {
54
+ if (!selected || !draft) return false;
55
+ return JSON.stringify(draft) !== JSON.stringify(themeToDraft(selected));
56
+ }, [selected, draft]);
57
+
58
+ const run = async (key: string, fn: () => Promise<{ ok: boolean; message?: string; error?: string }>) => {
59
+ setPending(key);
60
+ try {
61
+ const result = await fn();
62
+ if (result.ok) {
63
+ toast.success(result.message ?? 'Done.');
64
+ router.refresh();
65
+ return true;
66
+ }
67
+ toast.error(result.error ?? 'Something went wrong.');
68
+ return false;
69
+ } catch (error) {
70
+ toast.error(error instanceof Error ? error.message : 'Something went wrong.');
71
+ return false;
72
+ } finally {
73
+ setPending(null);
74
+ }
75
+ };
76
+
77
+ const handleSave = async () => {
78
+ if (!selected || !draft) return;
79
+ await run('save', () =>
80
+ updateTheme(selected.id, {
81
+ name: draft.name,
82
+ description: draft.description,
83
+ icon: draft.icon,
84
+ color_scheme: draft.color_scheme,
85
+ tokens: draft.tokens,
86
+ extra_css: draft.extra_css,
87
+ is_active: selected.is_default ? true : draft.is_active,
88
+ }),
89
+ );
90
+ };
91
+
92
+ const handleCreate = async () => {
93
+ const name = newName.trim();
94
+ if (!name) return;
95
+ const ok = await run('create', () =>
96
+ createTheme({
97
+ name,
98
+ // Start from the currently selected theme's palette so a new theme is a
99
+ // usable variation rather than an unreadable blank slate.
100
+ tokens: draft?.tokens ?? {},
101
+ color_scheme: draft?.color_scheme ?? 'light',
102
+ icon: 'Palette',
103
+ sort_order: (themes[themes.length - 1]?.sort_order ?? 0) + 10,
104
+ }),
105
+ );
106
+ if (ok) {
107
+ setCreateOpen(false);
108
+ setNewName('');
109
+ }
110
+ };
111
+
112
+ if (!selected || !draft) {
113
+ return (
114
+ <div className="rounded-lg border border-dashed p-8 text-center">
115
+ <p className="text-sm text-muted-foreground">
116
+ No themes found. Run the pending database migration to seed Light, Dark and Vibrant.
117
+ </p>
118
+ </div>
119
+ );
120
+ }
121
+
122
+ return (
123
+ <div className="space-y-4">
124
+ <div className="grid gap-6 lg:grid-cols-[220px_minmax(0,1fr)]">
125
+ {/* Theme list */}
126
+ <aside className="space-y-2">
127
+ <div className="flex items-center justify-between">
128
+ <h2 className="text-sm font-semibold">Themes</h2>
129
+ <Button type="button" variant="ghost" size="sm" className="h-7 px-2" onClick={() => setCreateOpen(true)}>
130
+ <Plus className="h-3.5 w-3.5" />
131
+ </Button>
132
+ </div>
133
+ <ul className="space-y-1">
134
+ {themes.map((theme) => {
135
+ const active = theme.id === selectedId;
136
+ return (
137
+ <li key={theme.id}>
138
+ <button
139
+ type="button"
140
+ onClick={() => selectTheme(theme)}
141
+ aria-current={active}
142
+ className={
143
+ 'flex w-full items-center gap-2 rounded-md border px-2.5 py-2 text-left text-sm transition-colors ' +
144
+ (active ? 'border-ring bg-accent/50' : 'border-transparent hover:bg-accent/30')
145
+ }
146
+ >
147
+ <ThemeIcon name={theme.icon} size={15} className="shrink-0 text-muted-foreground" />
148
+ <span className="min-w-0 flex-1 truncate">{theme.name}</span>
149
+ {theme.is_default ? (
150
+ <Star className="h-3.5 w-3.5 shrink-0 fill-current text-amber-500" aria-label="Default theme" />
151
+ ) : null}
152
+ {!theme.is_active ? (
153
+ <span className="shrink-0 text-[10px] text-muted-foreground">hidden</span>
154
+ ) : null}
155
+ </button>
156
+ </li>
157
+ );
158
+ })}
159
+ </ul>
160
+ </aside>
161
+
162
+ {/* Editor */}
163
+ <div className="min-w-0 space-y-4">
164
+ <div className="flex flex-wrap items-center gap-2 border-b pb-3">
165
+ <Button type="button" onClick={handleSave} disabled={!isDirty || pending !== null}>
166
+ {pending === 'save' ? <Loader2 className="mr-1.5 h-3.5 w-3.5 animate-spin" /> : null}
167
+ {isDirty ? 'Save changes' : 'Saved'}
168
+ </Button>
169
+ <Button
170
+ type="button"
171
+ variant="outline"
172
+ size="sm"
173
+ disabled={pending !== null}
174
+ onClick={() => run('duplicate', () => duplicateTheme(selected.id))}
175
+ >
176
+ <Copy className="mr-1.5 h-3.5 w-3.5" />
177
+ Duplicate
178
+ </Button>
179
+ {!selected.is_default ? (
180
+ <Button
181
+ type="button"
182
+ variant="outline"
183
+ size="sm"
184
+ disabled={pending !== null}
185
+ onClick={() => run('default', () => setDefaultTheme(selected.id))}
186
+ >
187
+ <Star className="mr-1.5 h-3.5 w-3.5" />
188
+ Make default
189
+ </Button>
190
+ ) : null}
191
+ {!selected.is_system && !selected.is_default ? (
192
+ <Button
193
+ type="button"
194
+ variant="outline"
195
+ size="sm"
196
+ disabled={pending !== null}
197
+ className="text-destructive hover:text-destructive"
198
+ onClick={() => {
199
+ if (
200
+ !window.confirm(
201
+ `Delete "${selected.name}"? Visitors currently using it will fall back to the default theme.`,
202
+ )
203
+ ) {
204
+ return;
205
+ }
206
+ void run('delete', () => deleteTheme(selected.id)).then((ok) => {
207
+ if (ok) {
208
+ const next = themes.find((theme) => theme.id !== selected.id);
209
+ if (next) selectTheme(next);
210
+ }
211
+ });
212
+ }}
213
+ >
214
+ <Trash2 className="mr-1.5 h-3.5 w-3.5" />
215
+ Delete
216
+ </Button>
217
+ ) : null}
218
+ {selected.is_system ? (
219
+ <span className="text-[11px] text-muted-foreground">
220
+ System theme — fully editable, but cannot be deleted because &quot;System&quot; resolves to it.
221
+ </span>
222
+ ) : null}
223
+ </div>
224
+
225
+ <ThemePreview draft={draft} />
226
+
227
+ <ThemeEditor theme={selected} draft={draft} onDraftChange={setDraft} />
228
+ </div>
229
+ </div>
230
+
231
+ <Dialog open={createOpen} onOpenChange={setCreateOpen}>
232
+ <DialogContent>
233
+ <DialogHeader>
234
+ <DialogTitle>New theme</DialogTitle>
235
+ <DialogDescription>
236
+ Starts as a copy of &quot;{selected.name}&quot; so you can adjust from a working palette.
237
+ </DialogDescription>
238
+ </DialogHeader>
239
+ <div className="space-y-1.5">
240
+ <Label htmlFor="new-theme-name">Name</Label>
241
+ <Input
242
+ id="new-theme-name"
243
+ value={newName}
244
+ onChange={(event) => setNewName(event.target.value)}
245
+ placeholder="Midnight"
246
+ onKeyDown={(event) => {
247
+ if (event.key === 'Enter') {
248
+ event.preventDefault();
249
+ void handleCreate();
250
+ }
251
+ }}
252
+ />
253
+ </div>
254
+ <DialogFooter>
255
+ <Button type="button" variant="outline" onClick={() => setCreateOpen(false)}>
256
+ Cancel
257
+ </Button>
258
+ <Button type="button" onClick={handleCreate} disabled={!newName.trim() || pending !== null}>
259
+ {pending === 'create' ? <Loader2 className="mr-1.5 h-3.5 w-3.5 animate-spin" /> : null}
260
+ Create theme
261
+ </Button>
262
+ </DialogFooter>
263
+ </DialogContent>
264
+ </Dialog>
265
+ </div>
266
+ );
267
+ }