All files / web/src/components/agents delete-agent-dialog.tsx

75.32% Statements 58/77
33.33% Branches 1/3
20% Functions 1/5
75.32% Lines 58/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111      1x                   1x 1x 1x   1x                 1x 9x 9x 9x 9x 9x 9x 9x 9x   9x                             9x         9x   9x 9x 9x 9x 9x 9x   9x 9x 9x   9x   9x 9x 9x 9x 9x   9x 9x 9x 9x 9x 9x 9x 9x 9x         9x 9x 9x 9x 9x 9x   9x 9x 9x 9x 9x   9x 9x 9x 9x 9x   9x  
'use client';
 
import type { Agent } from '@shared/types/data';
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from '@web/components/ui/alert-dialog';
import { Input } from '@web/components/ui/input';
import { Label } from '@web/components/ui/label';
import { AlertTriangle } from 'lucide-react';
import type { ReactElement } from 'react';
import { useId, useState } from 'react';
 
interface DeleteAgentDialogProps {
  agent: Agent | null;
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onConfirm: () => Promise<void>;
}
 
export function DeleteAgentDialog({
  agent,
  open,
  onOpenChange,
  onConfirm,
}: DeleteAgentDialogProps): ReactElement {
  const [confirmName, setConfirmName] = useState('');
  const [isDeleting, setIsDeleting] = useState(false);
  const inputId = useId();
 
  const handleConfirm = async () => {
    if (!agent || confirmName !== agent.name) return;
 
    setIsDeleting(true);
    try {
      await onConfirm();
      onOpenChange(false);
      setConfirmName('');
    } catch (error) {
      console.error('Error deleting agent:', error);
    } finally {
      setIsDeleting(false);
    }
  };
 
  const handleCancel = () => {
    setConfirmName('');
    onOpenChange(false);
  };
 
  const isConfirmDisabled = !agent || confirmName !== agent.name || isDeleting;
 
  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle className="flex items-center gap-2">
            <AlertTriangle className="h-5 w-5 text-destructive" />
            Delete Agent
          </AlertDialogTitle>
          <AlertDialogDescription className="space-y-4 pt-4">
            <p>
              This action cannot be undone. This will permanently delete the
              agent <span className="font-semibold">{agent?.name}</span> and all
              of its skills.
            </p>
            <div className="space-y-2">
              <Label htmlFor={inputId}>
                Type{' '}
                <span className="font-mono font-semibold">{agent?.name}</span>{' '}
                to confirm:
              </Label>
              <Input
                id={inputId}
                value={confirmName}
                onChange={(e) => setConfirmName(e.target.value)}
                placeholder="Enter agent name"
                className="font-mono"
                disabled={isDeleting}
                onKeyDown={(e) => {
                  if (e.key === 'Enter' && !isConfirmDisabled) {
                    handleConfirm();
                  }
                }}
              />
            </div>
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel onClick={handleCancel} disabled={isDeleting}>
            Cancel
          </AlertDialogCancel>
          <AlertDialogAction
            onClick={handleConfirm}
            variant={'destructive'}
            disabled={isConfirmDisabled}
          >
            {isDeleting ? 'Deleting...' : 'Delete Agent'}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}