import { useCallback, useEffect, useState } from "react"; import { ArrowsClockwise, FolderSimple, GithubLogo, Globe, Link, PuzzlePiece, Trash, } from "../ui/icons"; import type { AgentRecord, SkillRecord, SkillUpdateStatus } from "@shared/types"; import { useAppStore } from "../../state/store"; import { ActionButton, StatusPill, Eyebrow } from "../ui"; import { SkillAgentToggles } from "./SkillAgentToggles"; import { buildTree, SkillFileTree } from "./SkillFileTree"; import { useT, type TranslationFn } from "../../i18n"; import { formatDesktopResultError } from "../../lib/desktop"; const sourceIconMap: Record = { github: GithubLogo, local_path: FolderSimple, url: Link, skills_sh: Globe, managed: PuzzlePiece, }; function sourceLabel(kind: string, t: TranslationFn): string { switch (kind) { case "github": return t("skill.source.github"); case "local_path": return t("skill.source.localPath"); case "url": return t("skill.source.url"); case "skills_sh": return t("skill.source.skillsSh"); case "managed": return t("skill.source.managed"); default: return kind; } } const trustToneMap: Record = { markdown_only: "ready", assets: "pending", scripts_executables: "error", }; function trustLabel(level: string, t: TranslationFn): string { switch (level) { case "markdown_only": return t("skill.trust.markdownOnly"); case "assets": return t("skill.trust.assets"); case "scripts_executables": return t("skill.trust.scripts"); default: return level; } } function stripFrontmatter(markdown: string): string { const normalized = markdown.replace(/\r\n/g, "\n"); if (!normalized.startsWith("---\n")) return normalized.trim(); const closing = normalized.indexOf("\n---\n", 4); if (closing < 0) return normalized.trim(); return normalized.slice(closing + 5).trim(); } function shortRef(ref: string | null | undefined, t: TranslationFn): string { if (!ref) return t("skill.untracked"); return ref.length > 7 ? ref.slice(0, 7) : ref; } export function SkillDetail({ companyId, skillId, agents, onDeleted, onRefresh: _onRefresh, }: { companyId: string; skillId: string; agents: AgentRecord[]; onDeleted: () => void; onRefresh: () => void; }) { const t = useT(); const addToast = useAppStore((s) => s.addToast); const selectedSkillFilePath = useAppStore((s) => s.selectedSkillFilePath); const setSelectedSkillFilePath = useAppStore((s) => s.setSelectedSkillFilePath); const [detail, setDetail] = useState(null); const [loading, setLoading] = useState(true); const [updateStatus, setUpdateStatus] = useState(null); const [checkingUpdate, setCheckingUpdate] = useState(false); const [confirmingDelete, setConfirmingDelete] = useState(false); const [expandedDirs, setExpandedDirs] = useState>(new Set()); const [skillMarkdown, setSkillMarkdown] = useState(null); const [markdownError, setMarkdownError] = useState(null); useEffect(() => { let cancelled = false; setLoading(true); setDetail(null); setUpdateStatus(null); setSelectedSkillFilePath("SKILL.md"); setExpandedDirs(new Set()); async function load() { const result = await window.agentCompany.getSkillDetail(companyId, skillId); if (cancelled) return; setLoading(false); if (result.ok) { setDetail(result.data); } else { addToast(formatDesktopResultError(result.error), "danger"); } } void load(); return () => { cancelled = true; }; }, [companyId, skillId, addToast, setSelectedSkillFilePath]); useEffect(() => { if (!detail) return; let cancelled = false; setSkillMarkdown(null); setMarkdownError(null); async function load(companyIdArg: string, slugArg: string) { const result = await window.agentCompany.getSkillMarkdown(companyIdArg, slugArg); if (cancelled) return; if (result.ok) { setSkillMarkdown(result.data); } else { setMarkdownError(result.error.message); } } void load(detail.companyId, detail.slug); return () => { cancelled = true; }; }, [detail]); const handleCheckUpdate = useCallback(async () => { if (!detail) return; setCheckingUpdate(true); const result = await window.agentCompany.checkSkillUpdate(companyId, skillId); setCheckingUpdate(false); if (result.ok) { setUpdateStatus(result.data); } else { addToast(formatDesktopResultError(result.error), "danger"); } }, [companyId, skillId, detail, addToast]); const handleDelete = useCallback(async () => { const result = await window.agentCompany.deleteSkill(companyId, skillId); if (result.ok) { addToast(t("skill.deleted"), "success"); onDeleted(); } else { addToast(formatDesktopResultError(result.error), "danger"); } }, [companyId, skillId, addToast, onDeleted, t]); const handleToggleDir = useCallback((path: string) => { setExpandedDirs((prev) => { const next = new Set(prev); if (next.has(path)) next.delete(path); else next.add(path); return next; }); }, []); if (loading) { return (
{t("skill.loading")}
); } if (!detail) { return (
{t("skill.notFound")}
); } const SourceIcon = sourceIconMap[detail.sourceType] ?? PuzzlePiece; const treeNodes = buildTree(detail.fileInventory ?? []); const isGitHubSource = detail.sourceType === "github" || detail.sourceType === "skills_sh"; // Determine content for the file viewer const isSkillMd = selectedSkillFilePath === "SKILL.md"; const fileContent = isSkillMd ? (markdownError ? markdownError : skillMarkdown === null ? t("skill.loading") : stripFrontmatter(skillMarkdown)) : null; const selectedFileEntry = !isSkillMd ? (detail.fileInventory ?? []).find((f) => f.path === selectedSkillFilePath) : null; return (
{/* Header section */}

{detail.name}

{detail.description ? (

{detail.description}

) : null}
{/* Metadata row */}
{/* Source */}
{t("skill.source")} {sourceLabel(detail.sourceType, t)}
{/* Trust level */}
{t("skill.trust")} {trustLabel(detail.trustLevel, t)}
{/* Compatibility */}
{t("skill.compat")} {detail.compatibility || "all"}
{/* Ref (for GitHub sources) */} {isGitHubSource ? (
{t("skill.ref")} {shortRef(detail.sourceRef, t)}
) : null}
{/* Update check row */} {isGitHubSource ? (
void handleCheckUpdate()} /> {updateStatus?.hasUpdate ? ( {t("skill.updateAvailable")}: {shortRef(updateStatus.currentRef, t)} → {shortRef(updateStatus.latestRef, t)} ) : updateStatus && !updateStatus.hasUpdate ? ( {t("skill.upToDate")} ) : null}
) : null}
{/* File viewer section */}
{/* File tree sidebar + content */}
{/* File tree sidebar */}
{treeNodes.length > 0 ? ( ) : (

{t("skill.noFiles")}

)}
{/* Content area */}
{/* File path bar */}
{selectedSkillFilePath.split("/").map((segment, i, arr) => ( {i > 0 ? / : null} {segment} ))}
{/* Content */} {isSkillMd ? (
                {fileContent}
              
) : selectedFileEntry ? (

{selectedFileEntry.path}

{t("skill.kind")}: {selectedFileEntry.kind}

{t("skill.previewUnavailable")}

) : (

{t("skill.selectFile")}

)}
{/* Agent assignments section */}
{t("skill.agentAssignments")}
{/* Delete section */}
{confirmingDelete ? (

{t("skill.deleteConfirm").replace("{name}", detail.name)}

void handleDelete()} /> setConfirmingDelete(false)} />
) : ( setConfirmingDelete(true)} /> )}
); }