"use client"; import { invoke } from "@tauri-apps/api/core"; import { useCallback, useEffect, useState } from "react"; import { GoPlus } from "react-icons/go"; import { LuPencil, LuTrash2 } from "react-icons/lu"; import { CreateGroupDialog } from "@/components/create-group-dialog"; import { DeleteGroupDialog } from "@/components/delete-group-dialog"; import { EditGroupDialog } from "@/components/edit-group-dialog"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import type { ProfileGroup } from "@/types"; import { RippleButton } from "./ui/ripple"; interface GroupManagementDialogProps { isOpen: boolean; onClose: () => void; onGroupManagementComplete: () => void; } export function GroupManagementDialog({ isOpen, onClose, onGroupManagementComplete, }: GroupManagementDialogProps) { const [groups, setGroups] = useState([]); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // Dialog states const [createDialogOpen, setCreateDialogOpen] = useState(false); const [editDialogOpen, setEditDialogOpen] = useState(false); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [selectedGroup, setSelectedGroup] = useState(null); const loadGroups = useCallback(async () => { setIsLoading(true); setError(null); try { const groupList = await invoke("get_profile_groups"); setGroups(groupList); } catch (err) { console.error("Failed to load groups:", err); setError(err instanceof Error ? err.message : "Failed to load groups"); } finally { setIsLoading(false); } }, []); const handleGroupCreated = useCallback( (newGroup: ProfileGroup) => { setGroups((prev) => [...prev, newGroup]); onGroupManagementComplete(); }, [onGroupManagementComplete], ); const handleGroupUpdated = useCallback( (updatedGroup: ProfileGroup) => { setGroups((prev) => prev.map((group) => group.id === updatedGroup.id ? updatedGroup : group, ), ); onGroupManagementComplete(); }, [onGroupManagementComplete], ); const handleGroupDeleted = useCallback(() => { void loadGroups(); onGroupManagementComplete(); }, [loadGroups, onGroupManagementComplete]); const handleEditGroup = useCallback((group: ProfileGroup) => { setSelectedGroup(group); setEditDialogOpen(true); }, []); const handleDeleteGroup = useCallback((group: ProfileGroup) => { setSelectedGroup(group); setDeleteDialogOpen(true); }, []); useEffect(() => { if (isOpen) { void loadGroups(); } }, [isOpen, loadGroups]); return ( <> Manage Profile Groups Create, edit, and delete profile groups. Profiles without a group will appear in the "Default" group.
{/* Create new group button */}
setCreateDialogOpen(true)} className="flex gap-2 items-center" > Create
{error && (
{error}
)} {/* Groups list */} {isLoading ? (
Loading groups...
) : groups.length === 0 ? (
No groups created yet. Create your first group using the button above.
) : (
Name Actions {groups.map((group) => ( {group.name}
))}
)}
Close
setCreateDialogOpen(false)} onGroupCreated={handleGroupCreated} /> setEditDialogOpen(false)} group={selectedGroup} onGroupUpdated={handleGroupUpdated} /> setDeleteDialogOpen(false)} group={selectedGroup} onGroupDeleted={handleGroupDeleted} /> ); }