@bygghammar/cms-platform 0.12.0 → 0.14.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 +3 -2
- package/src/backoffice/seo/PageSeoPanelView.tsx +406 -0
- package/src/platform/index.ts +173 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bygghammar/cms-platform",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.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"><title></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
|
+
}
|
package/src/platform/index.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* Host platform contract.
|
|
3
3
|
*
|
|
4
4
|
* Blocks and inspectors already reach the host through `../host`. Backoffice
|
|
5
|
-
* *screens* additionally need data: pages, forms,
|
|
6
|
-
* Those calls are per-site (each site
|
|
7
|
-
* and auth), while the screens
|
|
8
|
-
*
|
|
9
|
-
* startup.
|
|
5
|
+
* *screens* additionally need data: pages, global sections, forms, submissions,
|
|
6
|
+
* media, SEO, redirects and audit access. Those calls are per-site (each site
|
|
7
|
+
* owns its own database, server functions and auth), while the screens
|
|
8
|
+
* themselves are identical everywhere — so the package declares the contract
|
|
9
|
+
* here and each site registers an adapter at startup.
|
|
10
10
|
*
|
|
11
11
|
* Adapters return plain data. No Supabase client, request or response object
|
|
12
12
|
* ever crosses this boundary, which keeps the package usable by a site that is
|
|
@@ -15,17 +15,60 @@
|
|
|
15
15
|
|
|
16
16
|
import type { Json } from "../i18n/json";
|
|
17
17
|
|
|
18
|
+
/** Contract revision. Bump when a required member is added or changed. */
|
|
19
|
+
export const PLATFORM_DATA_VERSION = 2;
|
|
20
|
+
|
|
21
|
+
export type BlockInstanceLike = {
|
|
22
|
+
id: string;
|
|
23
|
+
type: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
props?: Record<string, unknown>;
|
|
26
|
+
visible?: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
18
29
|
export type PageSummary = {
|
|
30
|
+
id: string;
|
|
19
31
|
key: string;
|
|
20
|
-
|
|
32
|
+
name: string;
|
|
33
|
+
isSystem: boolean;
|
|
34
|
+
status: string;
|
|
21
35
|
updatedAt: string | null;
|
|
22
|
-
|
|
36
|
+
currentVersionId: string | null;
|
|
23
37
|
};
|
|
24
38
|
|
|
25
|
-
export type
|
|
26
|
-
|
|
39
|
+
export type PageEditState = {
|
|
40
|
+
page: {
|
|
41
|
+
id: string;
|
|
42
|
+
key: string;
|
|
43
|
+
name: string;
|
|
44
|
+
isSystem: boolean;
|
|
45
|
+
status: string;
|
|
46
|
+
currentVersionId: string | null;
|
|
47
|
+
};
|
|
48
|
+
blocks: BlockInstanceLike[];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type PageVersionSummary = {
|
|
52
|
+
id: string;
|
|
27
53
|
versionNo: number;
|
|
28
|
-
|
|
54
|
+
note: string | null;
|
|
55
|
+
createdAt: string | null;
|
|
56
|
+
createdByEmail: string | null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type SavePageVersionInput = {
|
|
60
|
+
pageId: string;
|
|
61
|
+
blocks: BlockInstanceLike[];
|
|
62
|
+
publish?: boolean;
|
|
63
|
+
note?: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type GlobalSectionSummary = {
|
|
67
|
+
id: string;
|
|
68
|
+
key: string;
|
|
69
|
+
name: string;
|
|
70
|
+
kind: string | null;
|
|
71
|
+
updatedAt: string | null;
|
|
29
72
|
};
|
|
30
73
|
|
|
31
74
|
export type MediaEntry = {
|
|
@@ -36,17 +79,102 @@ export type MediaEntry = {
|
|
|
36
79
|
updatedAt: string | null;
|
|
37
80
|
};
|
|
38
81
|
|
|
82
|
+
/** A file in the site's media bin (soft-deleted, restorable by id). */
|
|
83
|
+
export type MediaTrashEntry = {
|
|
84
|
+
id: string;
|
|
85
|
+
originalPath: string;
|
|
86
|
+
size: number;
|
|
87
|
+
contentType: string | null;
|
|
88
|
+
deletedAt: string | null;
|
|
89
|
+
url: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export type RedirectEntry = {
|
|
93
|
+
id?: string;
|
|
94
|
+
fromPath: string;
|
|
95
|
+
toPath: string;
|
|
96
|
+
/** 301 or 302. */
|
|
97
|
+
status: number;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* SEO overrides are stored per page key, with per-locale slug and meta maps —
|
|
102
|
+
* one row covers every locale of a page.
|
|
103
|
+
*/
|
|
104
|
+
export type PageSeoRow = {
|
|
105
|
+
pageKey: string;
|
|
106
|
+
/** locale -> slug */
|
|
107
|
+
slugs: Json;
|
|
108
|
+
/** locale -> meta values */
|
|
109
|
+
seo: Json;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type FormSummary = {
|
|
113
|
+
id: string;
|
|
114
|
+
key: string;
|
|
115
|
+
name: string;
|
|
116
|
+
updatedAt: string | null;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export type SubmissionSummary = {
|
|
120
|
+
id: string;
|
|
121
|
+
formKey: string;
|
|
122
|
+
status: string | null;
|
|
123
|
+
createdAt: string | null;
|
|
124
|
+
payload: Json;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export type SubmissionQuery = {
|
|
128
|
+
formKey?: string;
|
|
129
|
+
status?: string;
|
|
130
|
+
page?: number;
|
|
131
|
+
pageSize?: number;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export type SubmissionPage = {
|
|
135
|
+
rows: SubmissionSummary[];
|
|
136
|
+
total: number;
|
|
137
|
+
};
|
|
138
|
+
|
|
39
139
|
/** Data access the shared backoffice screens need from the host site. */
|
|
40
140
|
export type PlatformData = {
|
|
41
141
|
pages: {
|
|
42
142
|
list: () => Promise<PageSummary[]>;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
143
|
+
loadForEdit: (key: string) => Promise<PageEditState>;
|
|
144
|
+
saveVersion: (input: SavePageVersionInput) => Promise<{ versionId: string; versionNo: number }>;
|
|
145
|
+
listVersions?: (pageId: string) => Promise<PageVersionSummary[]>;
|
|
146
|
+
restoreVersion?: (input: { pageId: string; versionId: string }) => Promise<void>;
|
|
147
|
+
create?: (input: { key: string; name: string }) => Promise<{ id: string }>;
|
|
148
|
+
rename?: (input: { id: string; name: string }) => Promise<void>;
|
|
149
|
+
remove?: (id: string) => Promise<void>;
|
|
150
|
+
};
|
|
151
|
+
globalSections?: {
|
|
152
|
+
list: () => Promise<GlobalSectionSummary[]>;
|
|
153
|
+
loadForEdit: (key: string) => Promise<PageEditState>;
|
|
154
|
+
saveVersion: (input: SavePageVersionInput) => Promise<{ versionId: string; versionNo: number }>;
|
|
46
155
|
};
|
|
47
156
|
media: {
|
|
48
157
|
list: () => Promise<MediaEntry[]>;
|
|
49
|
-
|
|
158
|
+
/** Soft delete a single library path (variants included by the host). */
|
|
159
|
+
remove: (path: string) => Promise<void>;
|
|
160
|
+
listTrash?: () => Promise<MediaTrashEntry[]>;
|
|
161
|
+
restore?: (trashId: string) => Promise<void>;
|
|
162
|
+
purge?: (trashId: string) => Promise<void>;
|
|
163
|
+
};
|
|
164
|
+
seo?: {
|
|
165
|
+
list: () => Promise<PageSeoRow[]>;
|
|
166
|
+
save: (row: PageSeoRow) => Promise<void>;
|
|
167
|
+
removeOverride: (input: { pageKey: string }) => Promise<void>;
|
|
168
|
+
};
|
|
169
|
+
redirects?: {
|
|
170
|
+
list: () => Promise<RedirectEntry[]>;
|
|
171
|
+
save: (entry: RedirectEntry) => Promise<void>;
|
|
172
|
+
remove: (id: string) => Promise<void>;
|
|
173
|
+
};
|
|
174
|
+
forms?: {
|
|
175
|
+
list: () => Promise<FormSummary[]>;
|
|
176
|
+
listSubmissions: (query?: SubmissionQuery) => Promise<SubmissionPage>;
|
|
177
|
+
setSubmissionStatus: (input: { ids: string[]; status: string }) => Promise<void>;
|
|
50
178
|
};
|
|
51
179
|
audit?: {
|
|
52
180
|
log: (event: { action: string; target: string; detail?: Json }) => Promise<void>;
|
|
@@ -61,6 +189,8 @@ export type PlatformSite = {
|
|
|
61
189
|
backofficeBasePath: string;
|
|
62
190
|
/** Public site origin, used for preview links. */
|
|
63
191
|
publicOrigin?: string;
|
|
192
|
+
/** Locales the site authors content in; first entry is the default. */
|
|
193
|
+
locales?: string[];
|
|
64
194
|
};
|
|
65
195
|
|
|
66
196
|
export type HostPlatform = {
|
|
@@ -88,3 +218,32 @@ export function getHostPlatform(): HostPlatform {
|
|
|
88
218
|
export function hasHostPlatform(): boolean {
|
|
89
219
|
return platform !== null;
|
|
90
220
|
}
|
|
221
|
+
|
|
222
|
+
/** Required members every adapter must implement, used by host-side tests. */
|
|
223
|
+
export const REQUIRED_PLATFORM_DATA: Record<string, readonly string[]> = {
|
|
224
|
+
pages: ["list", "loadForEdit", "saveVersion"],
|
|
225
|
+
media: ["list", "remove"],
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Assert an adapter implements every required member. Sites call this in a test
|
|
230
|
+
* (or at startup) so a contract bump fails loudly instead of at click time.
|
|
231
|
+
*/
|
|
232
|
+
export function assertPlatformData(data: PlatformData): void {
|
|
233
|
+
const missing: string[] = [];
|
|
234
|
+
for (const [group, members] of Object.entries(REQUIRED_PLATFORM_DATA)) {
|
|
235
|
+
const bag = (data as unknown as Record<string, Record<string, unknown> | undefined>)[group];
|
|
236
|
+
if (!bag) {
|
|
237
|
+
missing.push(group);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
for (const member of members) {
|
|
241
|
+
if (typeof bag[member] !== "function") missing.push(`${group}.${member}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (missing.length > 0) {
|
|
245
|
+
throw new Error(
|
|
246
|
+
`@bygghammar/cms-platform: host platform adapter is missing ${missing.join(", ")}`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|