All files / pages/ApiKeys RevokeApiKeyDialog.tsx

86.66% Statements 13/15
50% Branches 3/6
100% Functions 2/2
92.85% Lines 13/14

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                                            2x           59x 59x   59x 2x   2x 2x 2x 2x       2x 2x               2x       59x                                                  
import {
    AlertDialog,
    AlertDialogAction,
    AlertDialogCancel,
    AlertDialogContent,
    AlertDialogDescription,
    AlertDialogFooter,
    AlertDialogHeader,
    AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { apiKeysService } from '@/services/api-keys.service';
import type { APIKeyListItem } from '@/services/api-keys.service';
import { useToast } from '@/hooks/use-toast';
import { useState } from 'react';
 
interface RevokeApiKeyDialogProps {
    apiKey: APIKeyListItem | null;
    open: boolean;
    onOpenChange: (open: boolean) => void;
    onRevoked: () => void;
}
 
export const RevokeApiKeyDialog = ({
    apiKey,
    open,
    onOpenChange,
    onRevoked,
}: RevokeApiKeyDialogProps) => {
    const [isRevoking, setIsRevoking] = useState(false);
    const { toast } = useToast();
 
    const handleRevoke = async () => {
        Iif (!apiKey) return;
 
        setIsRevoking(true);
        try {
            await apiKeysService.revokeApiKey(apiKey.id);
            toast({
                title: 'API key revoked',
                description: `Key "${apiKey.name}" has been successfully revoked.`,
            });
            onRevoked();
            onOpenChange(false);
        } catch (error: any) {
            toast({
                title: 'Error revoking API key',
                description: error.response?.data?.detail || 'Something went wrong',
                variant: 'destructive',
            });
        } finally {
            setIsRevoking(false);
        }
    };
 
    return (
        <AlertDialog open={open} onOpenChange={onOpenChange}>
            <AlertDialogContent>
                <AlertDialogHeader>
                    <AlertDialogTitle>Are you sure?</AlertDialogTitle>
                    <AlertDialogDescription>
                        This will permanently revoke the API key <strong>"{apiKey?.name}"</strong>.
                        Any applications or scripts using this key will immediately lose access.
                        This action cannot be undone.
                    </AlertDialogDescription>
                </AlertDialogHeader>
                <AlertDialogFooter>
                    <AlertDialogCancel disabled={isRevoking}>Cancel</AlertDialogCancel>
                    <AlertDialogAction
                        className="bg-red-600 hover:bg-red-700 focus:ring-red-600"
                        onClick={handleRevoke}
                        disabled={isRevoking}
                    >
                        {isRevoking ? 'Revoking...' : 'Revoke Key'}
                    </AlertDialogAction>
                </AlertDialogFooter>
            </AlertDialogContent>
        </AlertDialog>
    );
};