"use client"; import { invoke } from "@tauri-apps/api/core"; import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { LoadingButton } from "@/components/loading-button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { ScrollArea } from "@/components/ui/scroll-area"; import type { BrowserProfile, ProfileGroup } from "@/types"; import { RippleButton } from "./ui/ripple"; interface DeleteGroupDialogProps { isOpen: boolean; onClose: () => void; group: ProfileGroup | null; onGroupDeleted: () => void; } export function DeleteGroupDialog({ isOpen, onClose, group, onGroupDeleted, }: DeleteGroupDialogProps) { const { t } = useTranslation(); const [associatedProfiles, setAssociatedProfiles] = useState< BrowserProfile[] >([]); const [deleteAction, setDeleteAction] = useState<"move" | "delete">("move"); const [isDeleting, setIsDeleting] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const loadAssociatedProfiles = useCallback(async () => { if (!group) return; setIsLoading(true); setError(null); try { const allProfiles = await invoke( "list_browser_profiles", ); const groupProfiles = allProfiles.filter( (profile) => profile.group_id === group.id, ); setAssociatedProfiles(groupProfiles); } catch (err) { console.error("Failed to load associated profiles:", err); setError(err instanceof Error ? err.message : "Failed to load profiles"); } finally { setIsLoading(false); } }, [group]); useEffect(() => { if (isOpen && group) { void loadAssociatedProfiles(); } }, [isOpen, group, loadAssociatedProfiles]); const handleDelete = useCallback(async () => { if (!group) return; setIsDeleting(true); setError(null); try { if (deleteAction === "delete" && associatedProfiles.length > 0) { // Delete all associated profiles first const profileIds = associatedProfiles.map((p) => p.id); await invoke("delete_selected_profiles", { profileIds }); } else if (deleteAction === "move" && associatedProfiles.length > 0) { // Move profiles to default group (null group_id) const profileIds = associatedProfiles.map((p) => p.id); await invoke("assign_profiles_to_group", { profileIds, groupId: null, }); } // Delete the group await invoke("delete_profile_group", { groupId: group.id }); toast.success("Group deleted successfully"); onGroupDeleted(); onClose(); } catch (err) { console.error("Failed to delete group:", err); const errorMessage = err instanceof Error ? err.message : "Failed to delete group"; setError(errorMessage); toast.error(errorMessage); } finally { setIsDeleting(false); } }, [group, deleteAction, associatedProfiles, onGroupDeleted, onClose]); const handleClose = useCallback(() => { setError(null); setDeleteAction("move"); setAssociatedProfiles([]); onClose(); }, [onClose]); return ( Delete Group This action cannot be undone. This will permanently delete the group "{group?.name}".
{isLoading ? (
Loading associated profiles...
) : ( <> {associatedProfiles.length > 0 && (
{associatedProfiles.map((profile) => (
• {profile.name}
))}
{ setDeleteAction(value as "move" | "delete"); }} >
)} {associatedProfiles.length === 0 && !isLoading && (
This group has no associated profiles.
)} )} {error && (
{error}
)}
Cancel void handleDelete()} disabled={isLoading} > Delete Group {deleteAction === "delete" && associatedProfiles.length > 0 && " & Profiles"}
); }