chore: clean up and restructure repository
This commit is contained in:
@@ -1,229 +0,0 @@
|
||||
import React from 'react';
|
||||
import { Sparkles, Upload } from 'lucide-react';
|
||||
import { Button, Field, Input, Modal, Textarea } from '@/components/ds';
|
||||
import { usePreferences, useUpdatePreferences } from '@/lib/krowHooks';
|
||||
import { PAGE_KEYS, validateSkillSource } from '@/lib/skills/registry';
|
||||
import { skillTemplate, upsertCustomSkill } from '@/lib/skills/customSkills';
|
||||
import {
|
||||
isReadableDefinition, normalizeUpload, owliverFieldsFromSource, patchFrontmatter,
|
||||
} from '@/lib/skills/skillFields';
|
||||
import { reportSave } from '@/lib/skills/saveFeedback';
|
||||
|
||||
/**
|
||||
* Add an Owliver skill.
|
||||
*
|
||||
* One dialog, opened from Settings and from Owliver's own header, writing to
|
||||
* one registry — a second registration path would be a second source of truth
|
||||
* by another name.
|
||||
*
|
||||
* What is stored is the Markdown itself, not a parsed record. A custom skill is
|
||||
* then exactly the artefact a file on disk is, read by the same parser, and can
|
||||
* be copied into `src/skills/` later without conversion.
|
||||
*
|
||||
* The fields above the editor are a starting point, not a second schema: typing
|
||||
* in them composes frontmatter in the existing format, which the author can
|
||||
* then edit directly. The Markdown is always what gets saved.
|
||||
*/
|
||||
|
||||
export function AddSkillDialog({ open, onOpenChange }) {
|
||||
const preferences = usePreferences();
|
||||
const updatePreferences = useUpdatePreferences();
|
||||
|
||||
const [meta, setMeta] = React.useState({ id: '', name: '', description: '', pages: '' });
|
||||
const [source, setSource] = React.useState('');
|
||||
const [touched, setTouched] = React.useState(false);
|
||||
const [error, setError] = React.useState('');
|
||||
const fileRef = React.useRef(null);
|
||||
|
||||
/* A fresh sheet each time it opens, so an abandoned draft is not waiting the
|
||||
next time someone adds a skill. */
|
||||
React.useEffect(() => {
|
||||
if (!open) return;
|
||||
setMeta({ id: '', name: '', description: '', pages: '' });
|
||||
setSource('');
|
||||
setTouched(false);
|
||||
setError('');
|
||||
}, [open]);
|
||||
|
||||
/**
|
||||
* A field edit, written into the definition.
|
||||
*
|
||||
* A fresh draft is composed from the template; once there is a definition to
|
||||
* preserve — an uploaded file, or Markdown typed by hand — the field patches
|
||||
* the key it owns and leaves everything else alone. It used to stop here
|
||||
* entirely, so after an upload these four boxes edited nothing that was saved.
|
||||
*/
|
||||
const syncFromMeta = (next) => {
|
||||
setMeta(next);
|
||||
const pages = next.pages.split(',').map((p) => p.trim()).filter(Boolean);
|
||||
|
||||
if (!touched) {
|
||||
setSource(skillTemplate({
|
||||
id: next.id,
|
||||
name: next.name,
|
||||
description: next.description,
|
||||
pages,
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
setSource(patchFrontmatter(source, {
|
||||
id: next.id || undefined,
|
||||
name: next.name || undefined,
|
||||
description: next.description || undefined,
|
||||
pages: pages.length ? pages : undefined,
|
||||
}));
|
||||
};
|
||||
|
||||
/**
|
||||
* A definition uploaded from a file.
|
||||
*
|
||||
* Filled the Markdown box and left every field beside it empty, and said
|
||||
* nothing about whether the file was usable until Add was pressed. Both are
|
||||
* fixed here: the frontmatter fills the fields, and an unusable file says so
|
||||
* at the moment it arrives.
|
||||
*/
|
||||
const readFile = (event) => {
|
||||
const file = event.target.files?.[0];
|
||||
if (!file) return;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onerror = () => setError('That file could not be read.');
|
||||
reader.onload = () => {
|
||||
const raw = normalizeUpload(reader.result);
|
||||
setSource(raw);
|
||||
setTouched(true);
|
||||
|
||||
/* Nothing readable: the fields stay as they were rather than emptying to
|
||||
a row of placeholders that read as values. */
|
||||
if (!isReadableDefinition(raw)) {
|
||||
setError('That file has no frontmatter block, so there is nothing to read into the fields. '
|
||||
+ 'A definition starts with `---` on its own line.');
|
||||
return;
|
||||
}
|
||||
|
||||
setError(validateSkillSource(raw) || '');
|
||||
const fields = owliverFieldsFromSource(raw);
|
||||
setMeta({
|
||||
id: fields.id,
|
||||
name: fields.name,
|
||||
description: fields.description,
|
||||
pages: fields.pages.join(', '),
|
||||
});
|
||||
};
|
||||
reader.readAsText(file);
|
||||
event.target.value = '';
|
||||
};
|
||||
|
||||
const save = () => {
|
||||
const problem = validateSkillSource(source);
|
||||
if (problem) {
|
||||
setError(problem);
|
||||
return;
|
||||
}
|
||||
|
||||
const { skill, next } = upsertCustomSkill(preferences.customSkills || [], source);
|
||||
|
||||
updatePreferences.mutate({ customSkills: next }, reportSave(`${skill.name} added`));
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
title="Add Owliver skill"
|
||||
description="Define a skill in Markdown. It attaches itself to the pages it names."
|
||||
icon={Sparkles}
|
||||
size="lg"
|
||||
footer={
|
||||
<>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>Cancel</Button>
|
||||
<Button onClick={save} loading={updatePreferences.isPending}>Add skill</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<div className="space-y-4">
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<Field label="Skill name">
|
||||
<Input
|
||||
value={meta.name}
|
||||
placeholder="e.g. Chef Training"
|
||||
onChange={(e) => syncFromMeta({ ...meta, name: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Skill ID" hint="Lower-case, dashes.">
|
||||
<Input
|
||||
value={meta.id}
|
||||
placeholder="e.g. chef-training"
|
||||
onChange={(e) => syncFromMeta({ ...meta, id: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<Field label="Description">
|
||||
<Input
|
||||
value={meta.description}
|
||||
placeholder="e.g. Helps Owliver explain chef training paths."
|
||||
onChange={(e) => syncFromMeta({ ...meta, description: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="Pages" hint={`Comma separated. Known pages: ${PAGE_KEYS.join(', ')}`}>
|
||||
<Input
|
||||
value={meta.pages}
|
||||
placeholder="university"
|
||||
onChange={(e) => syncFromMeta({ ...meta, pages: e.target.value })}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label="Markdown definition"
|
||||
hint="This is what gets saved. Edit it directly if you prefer."
|
||||
error={error}
|
||||
>
|
||||
<Textarea
|
||||
value={source}
|
||||
rows={12}
|
||||
spellCheck={false}
|
||||
className="font-mono text-[12px] leading-relaxed"
|
||||
placeholder="--- id: my-skill name: My Skill pages: - positions ---"
|
||||
onChange={(e) => {
|
||||
const next = e.target.value;
|
||||
setSource(next);
|
||||
setTouched(true);
|
||||
|
||||
/* Markdown → fields, the direction that was missing. Same
|
||||
extractor the upload path uses; guarded so a half-typed
|
||||
definition cannot wipe fields that are already filled in.
|
||||
See the fuller note in the two skill editors. */
|
||||
if (isReadableDefinition(next)) {
|
||||
const fields = owliverFieldsFromSource(next);
|
||||
if (fields.name) {
|
||||
setMeta({
|
||||
id: fields.id,
|
||||
name: fields.name,
|
||||
description: fields.description,
|
||||
pages: fields.pages.join(', '),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (error) setError('');
|
||||
}}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<input ref={fileRef} type="file" accept=".md,text/markdown" onChange={readFile} className="hidden" />
|
||||
<Button variant="outline" size="sm" onClick={() => fileRef.current?.click()}>
|
||||
<Upload aria-hidden="true" /> Upload .md
|
||||
</Button>
|
||||
<p className="text-caption text-ink-4">
|
||||
Stored as data in this browser, for this workspace. It is never executed.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user