Compare commits

..

2 commits

Author SHA1 Message Date
a213d11b2b Merge remote-tracking branch 'origin/main' into feat/youtube-studio-handoff
All checks were successful
CI / unit (pull_request) Successful in 48s
CI / backend-e2e (pull_request) Successful in 1m23s
CI / dashboard-e2e (pull_request) Successful in 2m27s
2026-10-06 13:27:41 +01:00
47add55c5f fix(dashboard): derive a slug when importing a single profile file (#10)
All checks were successful
CI / unit (push) Successful in 45s
CI / backend-e2e (push) Successful in 1m23s
CI / dashboard-e2e (push) Successful in 2m26s
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.

Reviewed-on: #10
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-06 13:27:09 +01:00

View file

@ -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() {
)}
<p className="text-xs text-muted mt-3 leading-relaxed">
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.
</p>
</div>
</aside>