Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

5.4 KB
Raw Blame
import { useRef, useState } from 'react';
import { MoreHorizontal, Trash2 } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { Switch } from '@/components/ui/switch';
import { Button } from '@/components/ui/button';
import { Tip } from '@/components/ui/tooltip';
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
import { extractIpcError } from '@/utils/ipcError';
import { toast } from '@/lib/toast';
import { refresh } from '../hooks/useSkillhub';
import { showUninstallCleanup } from '../lib/uninstallCleanupNotifications';
import { useSkillhubTarget } from '../SkillhubTargetContext';

/** Local management for Skill details, independent of cloud ownership. */
export function LocalSkillControls({ skill, disabled = false, onUninstalled }: {
  skill: SkillhubSkill;
  disabled?: boolean;
  onUninstalled?: () => void;
}) {
  const { t } = useTranslation();
  const target = useSkillhubTarget();
  disabled = disabled || (!!target && !target.writable);
  const busyRef = useRef(false);
  const [busy, setBusy] = useState(false);
  const [pendingEnabled, setPendingEnabled] = useState<boolean | null>(null);
  if (skill.kind !== 'skill') return null;
  const enabled = pendingEnabled ?? skill.cindyEnabled !== false;
  const reportError = (error: unknown, operation: 'toggleFailed' | 'uninstallFailed') => {
    if (target && !target.current()) return;
    const code = extractIpcError(error)?.code;
    toast.error(t(`skillhub.management.${code === 'PRECONDITION_FAILED' || code === 'PERMISSION_DENIED' ? 'refreshRequired' : operation}`));
  };
  const toggle = async (value: boolean) => {
    if (busyRef.current || disabled || skill.managedByPlugin) return;
    busyRef.current = true;
    setBusy(true);
    setPendingEnabled(value);
    try {
      if (target) {
        const result = await target.mutate(value ? 'enable' : 'disable', skill);
        if (!target.current()) return;
        if (result.phase !== 'succeeded') throw new Error('UNAVAILABLE');
      } else await window.electronAPI.skillhub.setEnabled({ absolutePath: skill.absolutePath, skillId: skill.id, enabled: value });
      await (target?.refresh ?? refresh)();
      if (target && !target.current()) return;
      toast.success(t(value ? 'skillhub.management.enabledToast' : 'skillhub.management.disabledToast', { name: skill.name }));
    } catch (error) { reportError(error, 'toggleFailed'); }
    finally {
      setPendingEnabled(null);
      busyRef.current = false;
      setBusy(false);
    }
  };
  const uninstall = async () => {
    if (busyRef.current || disabled || !skill.canUninstall) return;
    busyRef.current = true;
    setBusy(true);
    try {
      if (target) {
        const result = await target.mutate('uninstall', skill);
        if (!target.current() || result.phase === 'cancelled') return;
        if (result.phase !== 'succeeded') throw new Error('UNAVAILABLE');
        toast.success(t('skillhub.detail.uninstalledToast', { name: skill.name }));
        await target.refresh();
        if (target.current()) onUninstalled?.();
        return;
      }
      const result = await window.electronAPI.skillhub.uninstall(skill.absolutePath, skill.id);
      if (!result.success) {
        if (result.errorCode === 'CANCELLED') return;
        toast.error(t('skillhub.management.uninstallFailed'));
        return;
      }
      if (result.cleanupToken) {
        showUninstallCleanup(result.cleanupToken, skill.name, refresh);
      } else {
        toast.success(t('skillhub.detail.uninstalledToast', { name: skill.name }));
      }
      onUninstalled?.();
      await refresh();
    } catch (error) { reportError(error, 'uninstallFailed'); }
    finally { busyRef.current = false; setBusy(false); }
  };

  return (
    <div className="flex shrink-0 items-center gap-2" onClick={(event) => event.stopPropagation()}>
      <Tip text={`${target?.displayPath(skill.absolutePath) ?? skill.absolutePath}\n${t(skill.managedByPlugin ? 'skillhub.management.managedByPlugin' : 'skillhub.management.activationHint')}`}>
        {/* Keep Tooltip's open/closed data-state off the Switch's checked/unchecked root. */}
        <span className="inline-flex">
          <Switch checked={enabled} disabled={busy || disabled || skill.managedByPlugin}
            aria-label={t('skillhub.management.enabledLabel', { name: skill.name })}
            onCheckedChange={(value) => { void toggle(value); }} />
        </span>
      </Tip>
      <DropdownMenu>
        <DropdownMenuTrigger asChild>
          <Button variant="secondary" disabled={busy || disabled}
            aria-label={t('skillhub.management.moreLabel', { name: skill.name })}
            className="w-8 p-0">
            <MoreHorizontal size={14} />
          </Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent align="end" className="p-1.5">
          <DropdownMenuItem variant="danger" disabled={!skill.canUninstall} onSelect={() => { void uninstall(); }}
            className="gap-2">
            <Trash2 size={14} />{t('skillhub.detail.uninstall')}
          </DropdownMenuItem>
          {!skill.canUninstall && <p className="max-w-56 px-2 py-1 text-11 text-[var(--text-secondary)]">{t(skill.managedByPlugin
            ? 'skillhub.management.managedByPlugin'
            : skill.builtIn
              ? 'skillhub.management.builtInManaged'
              : 'skillhub.management.managedElsewhere')}</p>}
        </DropdownMenuContent>
      </DropdownMenu>
    </div>
  );
}