import { type RefObject, useEffect, useRef } from "react"; import { Alert, AlertDescription, AlertTitle } from "@/components/shadcn/alert"; import { Button } from "@/components/shadcn/button/button"; import { Modal } from "@/components/shadcn/modal/modal"; import { REGISTRY_ARTIFACT_REMOVAL, type RegistryRemoveDialogError, } from "@/types/registry"; interface RegistryRemoveDialogProps { artifactName?: string; error?: RegistryRemoveDialogError; isPending: boolean; onConfirm: () => void; onOpenChange: (open: boolean) => void; onViewProviders: () => void; open: boolean; returnFocusRef: RefObject; } export function RegistryRemoveDialog({ artifactName, error, isPending, onConfirm, onOpenChange, onViewProviders, open, returnFocusRef, }: RegistryRemoveDialogProps) { const cancelButtonRef = useRef(null); const isInUse = error?.status === REGISTRY_ARTIFACT_REMOVAL.IN_USE; // Busy keeps Confirm Remove: waiting is the remedy, not deleting providers. const isBusy = error?.status === REGISTRY_ARTIFACT_REMOVAL.BUSY; // Disabling the submit button can lose focus; restore it inside the dialog // when a failure re-enables the actions or replaces them with recovery actions. useEffect(() => { if (open && error) cancelButtonRef.current?.focus(); }, [error, open]); return ( { event.preventDefault(); cancelButtonRef.current?.focus(); }} onCloseAutoFocus={(event) => { event.preventDefault(); returnFocusRef.current?.focus(); }} onOpenChange={onOpenChange} open={open} size="sm" title="Remove artifact" > {error && ( {isInUse && Artifact in use} {isBusy && A scan is using this artifact} {isInUse ? "This artifact cannot be removed because one or more providers use it. Review the associated providers before trying again." : isBusy ? "A scan is running the checks this artifact adds. It clears by itself when the scan finishes, so try again shortly." : error.message} )}
{isInUse ? ( ) : ( )}
); }