import { useNavigate } from "react-router"; import { Button, CircularProgress, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, } from "@mui/material"; import { DIALOG_BLUR_BACKDROP_SX } from "@/components/ui/dialog-blur-backdrop"; import { useDeleteLocation } from "@/domain/locations/use-cases/use-delete-location"; import { useSiteOpenWorkOrders } from "@/domain/locations/use-cases/use-site-open-work-orders"; import { buildSiteOpenWorkOrdersHref } from "@/domain/work-orders/utils/dashboard-drilldown-params"; import { buildWorkOrderIdsHref } from "@/domain/work-orders/utils/work-order-ids-filter"; interface SiteDeleteDialogProps { open: boolean; siteId: string; siteCode: string; onClose: () => void; onDeleted: () => void; } const CHECK_FAILED_MESSAGE = "Couldn't check this site for open work orders. Try again before deleting it."; function describeDeletion(siteCode: string, openCount: number): string { if (openCount === 0) { return `This will permanently remove ${siteCode} from Sites. This cannot be undone.`; } const noun = openCount === 1 ? "work order" : "work orders"; return `This will permanently remove ${siteCode} from Sites. It still has ${openCount} open ${noun} — deleting the site won't cancel them automatically. This cannot be undone.`; } /** Delete confirmation; warns with the open work-order count and links to those work orders. */ export function SiteDeleteDialog({ open, siteId, siteCode, onClose, onDeleted, }: SiteDeleteDialogProps) { const navigate = useNavigate(); const deleteLocation = useDeleteLocation(); const { data: openWorkOrders, isLoading, isError, isFetching, refetch, } = useSiteOpenWorkOrders(siteId, open); const openCount = openWorkOrders?.count ?? 0; // Without a current count the warning cannot be trusted, so Delete waits for a successful check. // `isFetching` covers reopening: the count cached from the last open is stale until the refetch lands. const countUnknown = isLoading || isFetching || isError; // The server caps the id list; when it doesn't cover the stated count, open the site's whole board. const openIds = openWorkOrders?.workOrderIds ?? []; const openWorkOrdersHref = openIds.length > 0 && openIds.length >= openCount ? buildWorkOrderIdsHref(openIds) : buildSiteOpenWorkOrdersHref(siteId); return ( { if (!deleteLocation.isPending) { onClose(); } }} aria-labelledby="site-delete-title" slotProps={{ backdrop: { sx: DIALOG_BLUR_BACKDROP_SX } }} > Delete this site? {isFetching && !isError && ( )} {isError && {CHECK_FAILED_MESSAGE}} {!countUnknown && ( {describeDeletion(siteCode, openCount)} )} {isError && ( )} {!countUnknown && openCount > 0 && ( )} ); }