import { tr } from '../lib/i18n'; /** Quiet skill rows; all editing lives in one focused dialog. */ import { useState } from 'react'; import { Btn, Dialog, Empty, Icon, IconBtn, Select, ErrorNotice } from '../components/ui'; import { Screen } from '../components/layout'; import { api } from '../lib/client'; import { pageIsOnThisComputer, postWithFolderFallback } from '../lib/folder-pick'; import { editSkill, loadSkills, toggleSkill, useStore } from '../lib/store'; import type { Skill } from '../lib/types'; export function SkillsScreen() { const skills = useStore(s => s.skills); const workspaces = useStore(s => s.workspaces); const workspace = useStore(s => s.activeWorkspace); const [query, setQuery] = useState(''); const [pending, setPending] = useState(null); const [error, setError] = useState(''); const [tab, setTab] = useState<'details' | 'instructions'>('details'); const [editing, setEditing] = useState(null); const [triggers, setTriggers] = useState(''); const visible = skills.filter(s => `${s.source === 'bundled' ? tr(s.name) : s.name} ${s.source === 'bundled' ? tr(s.description) : s.description} ${s.triggers.join(' ')}`.toLowerCase().includes(query.toLowerCase())); async function action(id: string, fn: () => Promise) { setPending(id); setError(''); try { await fn(); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(null); } } async function add() { // The folder window opens on the computer that runs Strata. From a page on another computer there is nothing to see, so say so instead of waiting. if (!pageIsOnThisComputer(window.location.hostname)) throw new Error(tr("Adding a skill opens a folder window on the computer that runs Strata. Open Strata on that computer to add one.")); const result = await postWithFolderFallback<{ cancelled?: boolean; added?: string; }>('/api/skills/add') as { cancelled?: boolean; added?: string; }; if (!result.cancelled) await loadSkills(); } /** A skill written here: a name, what it is for, and the instructions. Saved as a folder the assistant reads; it is there for the next message. */ function openNew() { setTab('details'); setEditing({ id: '', name: '', description: '', body: '', triggers: [], enabled: true, source: 'created', onlyIn: null, sizeKb: 0, useCount: 0, lastUsedAt: null } as unknown as Skill); setTriggers(''); setError(''); } function openEditor(skill: Skill) { setTab('details'); setEditing({ ...skill }); setTriggers(skill.triggers.join(', ')); setError(''); } function closeEditor() { if (!pending) { setEditing(null); setError(''); } } async function save() { if (!editing) return; if (!editing.id) { await api.post('/api/skills', { name: editing.name.trim(), description: editing.description.trim(), body: editing.body ?? '', enabled: editing.enabled, triggers: triggers.split(',').map(t => t.trim()).filter(Boolean) }); await loadSkills(); setEditing(null); return; } await editSkill(editing.id, { name: editing.name.trim(), description: editing.description.trim(), body: editing.body, triggers: triggers.split(',').map(t => t.trim()).filter(Boolean), enabled: editing.enabled, onlyIn: editing.onlyIn ?? null }); setEditing(null); } return {tr("New skill")} void action('add', add)}>{tr("Add skill")} setQuery(e.target.value)}/> void action('refresh', loadSkills)}/>}>
{tr(error && !editing ? void action('refresh', loadSkills)}/> : null)} {tr((['bundled', 'added'] as const).map(group => { const rows = visible.filter(s => group === 'bundled' ? s.source === 'bundled' : s.source !== 'bundled'); return rows.length ?
{tr(group === 'added' ?

{tr("Added skills")}

: null)}
{tr(rows.map(s =>

{s.source === 'bundled' ? tr(s.name) : s.name}

{s.source === 'bundled' ? tr(s.description) : s.description}

{tr(s.onlyIn ? {tr(s.onlyIn === workspace ? 'This workspace' : `Only in ${workspaces.find(w => w.id === s.onlyIn)?.name ?? 'another workspace'}`)} : null)}
openEditor(s)}/>
))}
: null; }))} {tr(!visible.length ? skills.length ? setQuery('') : void action('add', add)}>{tr(skills.length ? 'Clear search' : 'Add skill')})}/> : null)}
{tr("Cancel")} void action(editing!.id, save)}>{tr(pending ? 'Saving…' : editing && !editing.id ? 'Save skill' : 'Save changes')}}> {tr(editing ? <>
{tr((['details', 'instructions'] as const).map(t => ))}
{tr(tab === 'details' ? <>
{tr("Enabled")}
{tr("Available in")} setTriggers(e.target.value)} placeholder={tr("review this, check my code")}/>{tr("Separate phrases with commas.")}
: