From 3e6ad5fbe7c0f662de005bece363f2e721058043 Mon Sep 17 00:00:00 2001 From: pedro-bento Date: Tue, 6 Oct 2026 12:38:17 +0100 Subject: [PATCH] fix(dashboard): derive a slug when importing a single profile file A single exported profile may omit `slug` (the old channels/*.json used the file name as the id), so picking one in the UI failed with "profile entry is missing a slug". Derive the slug from the file name (falling back to the profile name) for a single-profile file; bundle/list entries derive from their own name only, so a shared file name cannot collide. --- dashboard/src/components/ChannelProfiles.jsx | 30 +++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/dashboard/src/components/ChannelProfiles.jsx b/dashboard/src/components/ChannelProfiles.jsx index 2766867..884f4b0 100644 --- a/dashboard/src/components/ChannelProfiles.jsx +++ b/dashboard/src/components/ChannelProfiles.jsx @@ -20,6 +20,33 @@ const THUMBNAIL_ROLES = ['curiosity', 'emotion', 'technical']; // Same slug contract as the server: no separators, no traversal segments. const SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9_-]*$/; +// A single exported profile file may omit `slug` (the old channels/*.json used +// the file name as the id). Derive one so selecting such a file just works: +// the file name wins, then the profile's own name. Bundle/list entries derive +// from their own name only — a shared file name would collide. +const slugify = (value) => String(value ?? '') + .normalize('NFKD') + .replace(/[\u0300-\u036f]/g, '') + .trim() + .toLowerCase() + .replace(/[^a-z0-9_-]+/g, '-') + .replace(/^[-_]+|[-_]+$/g, ''); + +const deriveSlug = (item, fallback) => { + if (!item || typeof item !== 'object' || Array.isArray(item) || item.slug) return item; + const candidate = slugify(fallback) || slugify(item.name); + return SLUG_RE.test(candidate) ? { ...item, slug: candidate } : item; +}; + +const withImportedSlugs = (parsed, filename) => { + if (Array.isArray(parsed)) return parsed.map((item) => deriveSlug(item, '')); + if (parsed && typeof parsed === 'object' && Array.isArray(parsed.profiles)) { + return { ...parsed, profiles: parsed.profiles.map((item) => deriveSlug(item, '')) }; + } + const stem = String(filename || '').replace(/\.[^.]+$/, ''); + return deriveSlug(parsed, stem); +}; + // A fresh profile. A function, not a constant, so nested arrays are never // shared between two opens of the form. const emptyForm = () => ({ @@ -503,7 +530,7 @@ export default function ChannelProfiles() { const data = await apiJson('/api/channels/import', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(parsed), + body: JSON.stringify(withImportedSlugs(parsed, file.name)), }); setImportResult({ imported: data?.imported ?? 0, @@ -1015,6 +1042,7 @@ export default function ChannelProfiles() { )}

Import accepts an export bundle, a list of profiles, or a single profile. + A single file may omit its slug — it is derived from the file name.

-- 2.45.2