Files
codecraft/web/src/components/ChatSkills.tsx
T

81 lines
3.3 KiB
TypeScript

import { useState } from "react";
import { patch } from "../api";
import { toast } from "../dialogs";
import { useFetch } from "../hooks";
import { useT } from "../i18n";
import { Spinner } from "./Activity";
import { Icon } from "./Icon";
interface SkillInfo {
name: string;
description: string;
files: string[];
user?: boolean;
}
/** Switch skills on for one chat: their full instructions go into every turn. */
export function ChatSkillsDialog({ sessionId, cwd, active, onClose }: { sessionId: string; cwd: string; active: string[]; onClose: () => void }) {
const t = useT();
const { data } = useFetch<{ skills: SkillInfo[] }>(`/commands?cwd=${encodeURIComponent(cwd)}`);
const [picked, setPicked] = useState<string[]>(active);
const [filter, setFilter] = useState("");
const [busy, setBusy] = useState(false);
const all = data?.skills ?? [];
// Chosen skills first, then the rest.
const shown = all
.filter((s) => !filter.trim() || `${s.name} ${s.description}`.toLowerCase().includes(filter.trim().toLowerCase()))
.sort((a, b) => Number(picked.includes(b.name)) - Number(picked.includes(a.name)) || a.name.localeCompare(b.name));
const toggle = (n: string) => setPicked((p) => (p.includes(n) ? p.filter((x) => x !== n) : [...p, n]));
const save = async () => {
setBusy(true);
try {
await patch(`/sessions/${sessionId}`, { skills: picked });
onClose();
} catch (e: any) {
toast(e.message);
setBusy(false);
}
};
return (
<div className="modal-backdrop" onClick={onClose}>
<div className="modal draft-modal" role="dialog" aria-label={t("skills.chatTitle")} onClick={(e) => e.stopPropagation()}>
<h3>{t("skills.chatTitle")}</h3>
<div className="hint" style={{ marginBottom: 8 }}>
{t("skills.chatHint")}
</div>
{!data ? (
<Spinner />
) : all.length === 0 ? (
<div className="empty">{t("skills.empty")}</div>
) : (
<>
{all.length > 6 && <input autoFocus style={{ width: "100%" }} placeholder={t("skills.filter")} value={filter} onChange={(e) => setFilter(e.target.value)} />}
<div className="issue-list">
{shown.map((s) => (
<label key={s.name} className="row" style={{ gap: 8, alignItems: "flex-start", padding: "2px 0" }}>
<input type="checkbox" checked={picked.includes(s.name)} onChange={() => toggle(s.name)} />
<span>
<code>{s.name}</code>
{s.files.length > 0 && <span className="hint"> · {t("skills.files", { n: s.files.length })}</span>}
<div className="hint">{s.description}</div>
</span>
</label>
))}
</div>
{picked.length > 4 && <div className="notice">{t("skills.manyActive")}</div>}
</>
)}
<div className="row" style={{ marginTop: 10, justifyContent: "flex-end", gap: 6 }}>
<button onClick={onClose} disabled={busy}>
{t("common.cancel")}
</button>
<button className="primary" onClick={() => void save()} disabled={busy}>
{busy ? <Spinner small /> : <Icon name="check" />} {t("common.save")}
</button>
</div>
</div>
</div>
);
}