@bygghammar/cms-platform 0.12.0 → 0.13.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bygghammar/cms-platform",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Shared CMS platform: blocks, block types, i18n core, UI primitives and design-token contract used by every Bygghammar/Voyara site.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -94,7 +94,8 @@
94
94
  "./backoffice/seo/page-seo-form": "./src/backoffice/seo/page-seo-form.ts",
95
95
  "./platform/contract": "./src/platform/contract.ts",
96
96
  "./backoffice/media/ReviewImageUpload": "./src/backoffice/media/ReviewImageUpload.tsx",
97
- "./migrations/*": "./migrations/*"
97
+ "./migrations/*": "./migrations/*",
98
+ "./backoffice/seo/PageSeoPanelView": "./src/backoffice/seo/PageSeoPanelView.tsx"
98
99
  },
99
100
  "dependencies": {
100
101
  "fflate": "^0.8.3"
@@ -0,0 +1,406 @@
1
+ /**
2
+ * Host-agnostic page SEO editor UI.
3
+ *
4
+ * All data access is injected: the host builds the initial form from its own
5
+ * page registry + CMS overrides, supplies locale/domain facts, media metadata
6
+ * lookup and the media pickers, and performs save/reset. This component owns
7
+ * only editing state and presentation, so every site gets the same screen.
8
+ */
9
+ import { useEffect, useMemo, useState, type ReactNode } from "react";
10
+ import { Download, ImageIcon, Images, RotateCcw, Save, Sparkles, X } from "lucide-react";
11
+
12
+ import {
13
+ mergeMediaMetaIntoForm,
14
+ pageSeoPreviewCanonical,
15
+ pageSeoPreviewPath,
16
+ type PageSeoFormShape,
17
+ } from "./page-seo-form";
18
+
19
+ export type PageSeoImageCandidate = { url: string; hero?: boolean };
20
+
21
+ export type PageSeoMediaMeta = Parameters<typeof mergeMediaMetaIntoForm>[0]["meta"];
22
+
23
+ export type PageSeoPanelViewProps = {
24
+ /** Form values built by the host (registry defaults merged with overrides). */
25
+ initialForm: PageSeoFormShape;
26
+ locales: readonly string[];
27
+ defaultLocale: string;
28
+ /** True when the page exists only in the CMS (no code registry entry). */
29
+ isCmsPage?: boolean;
30
+ /** Current registry route for a locale, shown in the preview panel. */
31
+ registryRoute: (locale: string) => string;
32
+ /** Public origin used to build the canonical preview URL. */
33
+ originForLocale: (locale: string) => string;
34
+ /** Images found on this page, for the "use hero / pick from page" buttons. */
35
+ pageImages: readonly PageSeoImageCandidate[];
36
+ /** Saved media-library metadata for an image URL, or null when none exists. */
37
+ lookupMeta: (src: string) => PageSeoMediaMeta | null;
38
+ /** Persist the form. Return false when saving failed (host shows the error). */
39
+ onSave: (form: PageSeoFormShape) => Promise<boolean>;
40
+ /** Remove the override; only enabled when `canReset`. */
41
+ onReset: () => Promise<void>;
42
+ canReset?: boolean;
43
+ renderMediaPicker: (args: { onPick: (url: string) => void; onClose: () => void }) => ReactNode;
44
+ renderPageImagePicker: (args: {
45
+ onPick: (url: string) => void;
46
+ onClose: () => void;
47
+ }) => ReactNode;
48
+ notify: {
49
+ success: (message: string) => void;
50
+ error: (message: string) => void;
51
+ info: (message: string) => void;
52
+ };
53
+ confirm?: (message: string) => boolean;
54
+ };
55
+
56
+ export function PageSeoPanelView({
57
+ initialForm,
58
+ locales,
59
+ defaultLocale,
60
+ isCmsPage,
61
+ registryRoute,
62
+ originForLocale,
63
+ pageImages,
64
+ lookupMeta,
65
+ onSave,
66
+ onReset,
67
+ canReset,
68
+ renderMediaPicker,
69
+ renderPageImagePicker,
70
+ notify,
71
+ confirm,
72
+ }: PageSeoPanelViewProps) {
73
+ const [busy, setBusy] = useState(false);
74
+ const [dirty, setDirty] = useState(false);
75
+ const [activeLocale, setActiveLocale] = useState<string>(defaultLocale);
76
+ const [picking, setPicking] = useState(false);
77
+ const [pickingOnPage, setPickingOnPage] = useState(false);
78
+ const [form, setForm] = useState<PageSeoFormShape>(initialForm);
79
+
80
+ useEffect(() => {
81
+ setForm(initialForm);
82
+ setDirty(false);
83
+ }, [initialForm]);
84
+
85
+ const heroImage = useMemo(
86
+ () => pageImages.find((c) => c.hero) ?? pageImages[0],
87
+ [pageImages],
88
+ );
89
+
90
+ const updateLocalized = (
91
+ group: "slugs" | keyof PageSeoFormShape["seo"],
92
+ locale: string,
93
+ value: string,
94
+ ) => {
95
+ setDirty(true);
96
+ setForm((f) => {
97
+ if (group === "slugs") return { ...f, slugs: { ...f.slugs, [locale]: value } };
98
+ const seoField = f.seo[group];
99
+ if (typeof seoField === "string") return f;
100
+ return { ...f, seo: { ...f.seo, [group]: { ...seoField, [locale]: value } } };
101
+ });
102
+ };
103
+
104
+ const updateString = (field: "ogImageSrc" | "robots", value: string) => {
105
+ setDirty(true);
106
+ setForm((f) => ({ ...f, seo: { ...f.seo, [field]: value } }));
107
+ };
108
+
109
+ /** Copy saved media metadata into the SEO fields. */
110
+ const importFromMedia = (src: string, overwrite: boolean) => {
111
+ const meta = lookupMeta(src);
112
+ if (!meta) return false;
113
+ setDirty(true);
114
+ setForm((f) => mergeMediaMetaIntoForm({ form: f, locales, meta, overwrite }));
115
+ return true;
116
+ };
117
+
118
+ const onPickMedia = (url: string) => {
119
+ updateString("ogImageSrc", url);
120
+ setPicking(false);
121
+ setPickingOnPage(false);
122
+ const imported = importFromMedia(url, false);
123
+ notify.success(
124
+ imported
125
+ ? "Image selected — metadata imported from the media library"
126
+ : "Image selected (no metadata saved on this asset yet)",
127
+ );
128
+ };
129
+
130
+ const handleSave = async () => {
131
+ setBusy(true);
132
+ try {
133
+ if (await onSave(form)) setDirty(false);
134
+ } finally {
135
+ setBusy(false);
136
+ }
137
+ };
138
+
139
+ const handleReset = async () => {
140
+ if (!canReset) {
141
+ notify.info("Nothing to reset — this page already uses defaults.");
142
+ return;
143
+ }
144
+ const ask = confirm ?? ((m: string) => window.confirm(m));
145
+ if (!ask("Remove your changes and revert to the defaults from the codebase?")) return;
146
+ setBusy(true);
147
+ try {
148
+ await onReset();
149
+ } finally {
150
+ setBusy(false);
151
+ }
152
+ };
153
+
154
+ const previewPath = pageSeoPreviewPath({
155
+ slug: form.slugs[activeLocale] ?? "",
156
+ locale: activeLocale,
157
+ defaultLocale,
158
+ });
159
+ const previewCanonical = pageSeoPreviewCanonical({
160
+ path: previewPath,
161
+ origin: originForLocale(activeLocale),
162
+ });
163
+
164
+ return (
165
+ <div>
166
+ {isCmsPage && (
167
+ <p className="mb-4 text-xs text-ink/55">
168
+ This page was created in the page builder. Its URL and metadata are stored in the CMS —
169
+ set the slug below (empty = site root).
170
+ </p>
171
+ )}
172
+ {locales.length > 1 && (
173
+ <div className="flex gap-2 border-b border-ink/10">
174
+ {locales.map((l) => (
175
+ <button
176
+ key={l}
177
+ onClick={() => setActiveLocale(l)}
178
+ className={[
179
+ "px-4 py-2 text-xs uppercase tracking-[0.22em] border-b-2 -mb-px",
180
+ activeLocale === l
181
+ ? "border-ink text-ink"
182
+ : "border-transparent text-ink/50 hover:text-ink",
183
+ ].join(" ")}
184
+ >
185
+ {l}
186
+ </button>
187
+ ))}
188
+ </div>
189
+ )}
190
+
191
+ <div className="mt-8 grid grid-cols-1 md:grid-cols-2 gap-6">
192
+ <Field label="Slug" hint={`Empty = root for ${activeLocale}. E.g. "boka"`}>
193
+ <input
194
+ value={form.slugs[activeLocale] ?? ""}
195
+ onChange={(e) => updateLocalized("slugs", activeLocale, e.target.value)}
196
+ className="seo-input"
197
+ />
198
+ </Field>
199
+ <Field label="Robots" hint="E.g. noindex,nofollow — empty = default">
200
+ <input
201
+ value={form.seo.robots}
202
+ onChange={(e) => updateString("robots", e.target.value)}
203
+ className="seo-input"
204
+ />
205
+ </Field>
206
+ <Field label="Title" full>
207
+ <input
208
+ value={form.seo.title[activeLocale] ?? ""}
209
+ onChange={(e) => updateLocalized("title", activeLocale, e.target.value)}
210
+ className="seo-input"
211
+ />
212
+ </Field>
213
+ <Field label="Meta description" full>
214
+ <textarea
215
+ rows={3}
216
+ value={form.seo.description[activeLocale] ?? ""}
217
+ onChange={(e) => updateLocalized("description", activeLocale, e.target.value)}
218
+ className="seo-input"
219
+ />
220
+ </Field>
221
+ <Field label="OG title" hint="Share title (fallback: title)">
222
+ <input
223
+ value={form.seo.ogTitle[activeLocale] ?? ""}
224
+ onChange={(e) => updateLocalized("ogTitle", activeLocale, e.target.value)}
225
+ className="seo-input"
226
+ />
227
+ </Field>
228
+ <Field label="OG description" hint="Share text (fallback: meta description)">
229
+ <input
230
+ value={form.seo.ogDescription[activeLocale] ?? ""}
231
+ onChange={(e) => updateLocalized("ogDescription", activeLocale, e.target.value)}
232
+ className="seo-input"
233
+ />
234
+ </Field>
235
+ <Field label="OG image" hint="Pick from the media library or paste an absolute https URL">
236
+ <div className="space-y-2">
237
+ <div className="flex gap-2">
238
+ <input
239
+ value={form.seo.ogImageSrc}
240
+ onChange={(e) => updateString("ogImageSrc", e.target.value)}
241
+ className="seo-input"
242
+ placeholder="https://…"
243
+ />
244
+ <button
245
+ type="button"
246
+ onClick={() => setPicking(true)}
247
+ className="inline-flex shrink-0 items-center gap-1.5 border border-ink/20 px-3 text-[0.7rem] uppercase tracking-widest hover:border-sea"
248
+ >
249
+ <ImageIcon className="size-3.5" />
250
+ Choose
251
+ </button>
252
+ {form.seo.ogImageSrc ? (
253
+ <button
254
+ type="button"
255
+ onClick={() => updateString("ogImageSrc", "")}
256
+ className="inline-flex shrink-0 items-center border border-ink/20 px-2 text-ink/60 hover:border-sea"
257
+ title="Clear"
258
+ >
259
+ <X className="size-3.5" />
260
+ </button>
261
+ ) : null}
262
+ </div>
263
+ <div className="flex flex-wrap gap-2">
264
+ <button
265
+ type="button"
266
+ disabled={!heroImage}
267
+ onClick={() => heroImage && onPickMedia(heroImage.url)}
268
+ className="inline-flex items-center gap-1.5 border border-ink/20 px-3 py-1.5 text-[0.7rem] uppercase tracking-widest hover:border-sea disabled:opacity-40"
269
+ title={heroImage?.url}
270
+ >
271
+ <Sparkles className="size-3.5" />
272
+ Use hero image
273
+ </button>
274
+ <button
275
+ type="button"
276
+ disabled={pageImages.length === 0}
277
+ onClick={() => setPickingOnPage(true)}
278
+ className="inline-flex items-center gap-1.5 border border-ink/20 px-3 py-1.5 text-[0.7rem] uppercase tracking-widest hover:border-sea disabled:opacity-40"
279
+ >
280
+ <Images className="size-3.5" />
281
+ Pick from this page ({pageImages.length})
282
+ </button>
283
+ </div>
284
+ {form.seo.ogImageSrc ? (
285
+ <div className="flex items-center gap-3">
286
+ <img
287
+ src={form.seo.ogImageSrc}
288
+ alt=""
289
+ className="h-16 w-28 border border-ink/10 object-cover"
290
+ />
291
+ <button
292
+ type="button"
293
+ onClick={() => {
294
+ const ok = importFromMedia(form.seo.ogImageSrc, true);
295
+ (ok ? notify.success : notify.info)(
296
+ ok
297
+ ? "Imported alt, OG title and description from the media library"
298
+ : "This image has no metadata saved in the media library yet",
299
+ );
300
+ }}
301
+ className="inline-flex items-center gap-1.5 border border-ink/20 px-3 py-1.5 text-[0.7rem] uppercase tracking-widest hover:border-sea"
302
+ >
303
+ <Download className="size-3.5" />
304
+ Import image metadata
305
+ </button>
306
+ </div>
307
+ ) : null}
308
+ </div>
309
+ </Field>
310
+ <Field label={`OG image alt (${activeLocale})`}>
311
+ <input
312
+ value={form.seo.ogImageAlt[activeLocale] ?? ""}
313
+ onChange={(e) => updateLocalized("ogImageAlt", activeLocale, e.target.value)}
314
+ className="seo-input"
315
+ />
316
+ </Field>
317
+ <Field label="Keywords" full hint="Comma-separated list">
318
+ <input
319
+ value={form.seo.keywords[activeLocale] ?? ""}
320
+ onChange={(e) => updateLocalized("keywords", activeLocale, e.target.value)}
321
+ className="seo-input"
322
+ />
323
+ </Field>
324
+ </div>
325
+
326
+ <div className="mt-10 border border-ink/10 bg-white p-6">
327
+ <div className="text-[0.65rem] uppercase tracking-[0.22em] text-ink/55">Preview</div>
328
+ <dl className="mt-4 grid grid-cols-1 md:grid-cols-[140px_1fr] gap-x-6 gap-y-2 text-sm">
329
+ <dt className="text-ink/50">Canonical</dt>
330
+ <dd className="font-mono text-ink/80 break-all">{previewCanonical}</dd>
331
+ <dt className="text-ink/50">Route</dt>
332
+ <dd className="font-mono text-ink/80">
333
+ {previewPath} (current registry: {registryRoute(activeLocale)})
334
+ </dd>
335
+ <dt className="text-ink/50">&lt;title&gt;</dt>
336
+ <dd className="text-ink/80">
337
+ {form.seo.title[activeLocale] || <em className="text-ink/40">empty</em>}
338
+ </dd>
339
+ <dt className="text-ink/50">description</dt>
340
+ <dd className="text-ink/80">
341
+ {form.seo.description[activeLocale] || <em className="text-ink/40">empty</em>}
342
+ </dd>
343
+ </dl>
344
+ </div>
345
+
346
+ <div className="mt-8 flex flex-wrap items-center gap-3">
347
+ <button
348
+ onClick={handleSave}
349
+ disabled={busy || !dirty}
350
+ className="inline-flex items-center gap-2 bg-ink text-paper px-6 py-3 text-xs uppercase tracking-[0.22em] hover:bg-ink/85 disabled:opacity-50"
351
+ >
352
+ <Save className="size-4" />
353
+ {busy ? "Saving…" : "Save"}
354
+ </button>
355
+ <button
356
+ onClick={handleReset}
357
+ disabled={busy || !canReset}
358
+ className="inline-flex items-center gap-2 border border-ink/20 px-6 py-3 text-xs uppercase tracking-[0.22em] hover:border-ink disabled:opacity-40"
359
+ >
360
+ <RotateCcw className="size-4" />
361
+ Reset to default
362
+ </button>
363
+ {dirty && (
364
+ <span className="text-xs text-sea uppercase tracking-[0.22em]">unsaved changes</span>
365
+ )}
366
+ </div>
367
+
368
+ {picking && renderMediaPicker({ onPick: onPickMedia, onClose: () => setPicking(false) })}
369
+ {pickingOnPage &&
370
+ renderPageImagePicker({ onPick: onPickMedia, onClose: () => setPickingOnPage(false) })}
371
+
372
+ <style>{`
373
+ .seo-input {
374
+ width: 100%;
375
+ background: white;
376
+ border: 1px solid rgb(0 0 0 / 0.15);
377
+ padding: 0.65rem 0.85rem;
378
+ font-size: 0.9rem;
379
+ outline: none;
380
+ color: var(--ink);
381
+ }
382
+ .seo-input:focus { border-color: var(--sea); }
383
+ `}</style>
384
+ </div>
385
+ );
386
+ }
387
+
388
+ function Field({
389
+ label,
390
+ hint,
391
+ full,
392
+ children,
393
+ }: {
394
+ label: string;
395
+ hint?: string;
396
+ full?: boolean;
397
+ children: ReactNode;
398
+ }) {
399
+ return (
400
+ <label className={full ? "md:col-span-2" : ""}>
401
+ <div className="text-[0.65rem] uppercase tracking-[0.22em] text-ink/55">{label}</div>
402
+ <div className="mt-2">{children}</div>
403
+ {hint && <div className="mt-1 text-xs text-ink/45">{hint}</div>}
404
+ </label>
405
+ );
406
+ }