fix(dashboard): derive a slug when importing a single profile file #10
1 changed files with 29 additions and 1 deletions
|
|
@ -20,6 +20,33 @@ const THUMBNAIL_ROLES = ['curiosity', 'emotion', 'technical'];
|
||||||
// Same slug contract as the server: no separators, no traversal segments.
|
// Same slug contract as the server: no separators, no traversal segments.
|
||||||
const SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
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
|
// A fresh profile. A function, not a constant, so nested arrays are never
|
||||||
// shared between two opens of the form.
|
// shared between two opens of the form.
|
||||||
const emptyForm = () => ({
|
const emptyForm = () => ({
|
||||||
|
|
@ -503,7 +530,7 @@ export default function ChannelProfiles() {
|
||||||
const data = await apiJson('/api/channels/import', {
|
const data = await apiJson('/api/channels/import', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(parsed),
|
body: JSON.stringify(withImportedSlugs(parsed, file.name)),
|
||||||
});
|
});
|
||||||
setImportResult({
|
setImportResult({
|
||||||
imported: data?.imported ?? 0,
|
imported: data?.imported ?? 0,
|
||||||
|
|
@ -1015,6 +1042,7 @@ export default function ChannelProfiles() {
|
||||||
)}
|
)}
|
||||||
<p className="text-xs text-muted mt-3 leading-relaxed">
|
<p className="text-xs text-muted mt-3 leading-relaxed">
|
||||||
Import accepts an export bundle, a list of profiles, or a single profile.
|
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>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue