import { useState } from "react" import { toast } from "sonner" import { notifyApiError } from "../../../lib/apiErrors" import { bulkDeletePractices, bulkUnlinkPractices, formatBulkPracticeActionToast, type PracticeUnlinkContext, } from "../../../lib/practiceBulkActions" import { usePracticeListSelection } from "../../../hooks/usePracticeListSelection" import type { ParentContextPractice } from "../../../types/course.types" import { Button } from "../../../components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../../../components/ui/dialog" import { ModulePracticeCard } from "./ModulePracticeCard" type PracticeSelectableGridProps = { practices: ParentContextPractice[] searchQuery?: string locationLabel: string unlinkContext: PracticeUnlinkContext isExamPrep?: boolean publishStatusUpdatingId?: number | null onReload: () => Promise onEdit: (practice: ParentContextPractice) => void onPublish: (practiceId: number) => void onSaveAsDraft: (practiceId: number) => void onUnlink: (practice: ParentContextPractice) => void onDelete: (practice: ParentContextPractice) => void unlinkActionLabel?: string deleteActionLabel?: string } function summarizePracticeTitles(practices: ParentContextPractice[]): string { const titles = practices .slice(0, 3) .map((practice) => practice.title?.trim() || `Practice #${practice.id}`) if (practices.length <= 3) { return titles.join(", ") } return `${titles.join(", ")}, and ${practices.length - 3} more` } export function PracticeSelectableGrid({ practices, searchQuery = "", locationLabel, unlinkContext, isExamPrep = false, publishStatusUpdatingId = null, onReload, onEdit, onPublish, onSaveAsDraft, onUnlink, onDelete, unlinkActionLabel = "Remove from location", deleteActionLabel = "Delete permanently", }: PracticeSelectableGridProps) { const selection = usePracticeListSelection(practices) const [bulkUnlinkOpen, setBulkUnlinkOpen] = useState(false) const [bulkDeleteOpen, setBulkDeleteOpen] = useState(false) const [bulkUnlinking, setBulkUnlinking] = useState(false) const [bulkDeleting, setBulkDeleting] = useState(false) const runBulkUnlink = async () => { if (selection.selectedPractices.length === 0) return setBulkUnlinking(true) try { const result = await bulkUnlinkPractices({ practices: selection.selectedPractices, context: unlinkContext, isExamPrep, }) const toastResult = formatBulkPracticeActionToast( "unlink", locationLabel, result, ) if (toastResult.kind === "success") { toast.success(toastResult.message) } else if (toastResult.kind === "info") { toast.info(toastResult.message) } else { toast.error(toastResult.message) } setBulkUnlinkOpen(false) selection.clear() await onReload() } catch (error) { notifyApiError(error, "Could not unlink selected practices") } finally { setBulkUnlinking(false) } } const runBulkDelete = async () => { if (selection.selectedPractices.length === 0) return setBulkDeleting(true) try { const result = await bulkDeletePractices({ practices: selection.selectedPractices, isExamPrep, }) const toastResult = formatBulkPracticeActionToast( "delete", locationLabel, result, ) if (toastResult.kind === "success") { toast.success(toastResult.message) } else if (toastResult.kind === "info") { toast.info(toastResult.message) } else { toast.error(toastResult.message) } setBulkDeleteOpen(false) selection.clear() await onReload() } catch (error) { notifyApiError(error, "Could not delete selected practices") } finally { setBulkDeleting(false) } } return (
{practices.map((practice) => ( selection.toggle(practice.id)} onEdit={() => onEdit(practice)} onPublish={() => onPublish(practice.id)} onSaveAsDraft={() => onSaveAsDraft(practice.id)} onUnlink={() => onUnlink(practice)} onDelete={() => onDelete(practice)} /> ))}
{ if (!open && !bulkUnlinking) setBulkUnlinkOpen(false) }} > Remove {selection.selectedCount} practice {selection.selectedCount === 1 ? "" : "s"} from {locationLabel}? {summarizePracticeTitles(selection.selectedPractices)} will be detached from this location. Questions are kept unless you delete the practices afterward. { if (!open && !bulkDeleting) setBulkDeleteOpen(false) }} > Delete {selection.selectedCount} practice {selection.selectedCount === 1 ? "" : "s"} permanently? {summarizePracticeTitles(selection.selectedPractices)} and all of their questions will be deleted. This cannot be undone.
) }