"use client"; import { invoke } from "@tauri-apps/api/core"; import { useCallback, useEffect, useState } from "react"; import { toast } from "sonner"; import { LoadingButton } from "@/components/loading-button"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import type { ProfileGroup } from "@/types"; interface EditGroupDialogProps { isOpen: boolean; onClose: () => void; group: ProfileGroup | null; onGroupUpdated: (group: ProfileGroup) => void; } export function EditGroupDialog({ isOpen, onClose, group, onGroupUpdated, }: EditGroupDialogProps) { const [groupName, setGroupName] = useState(""); const [isUpdating, setIsUpdating] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (group) { setGroupName(group.name); } else { setGroupName(""); } setError(null); }, [group]); const handleUpdate = useCallback(async () => { if (!group || !groupName.trim()) return; setIsUpdating(true); setError(null); try { const updatedGroup = await invoke("update_profile_group", { groupId: group.id, name: groupName.trim(), }); toast.success("Group updated successfully"); onGroupUpdated(updatedGroup); onClose(); } catch (err) { console.error("Failed to update group:", err); const errorMessage = err instanceof Error ? err.message : "Failed to update group"; setError(errorMessage); toast.error(errorMessage); } finally { setIsUpdating(false); } }, [group, groupName, onGroupUpdated, onClose]); const handleClose = useCallback(() => { setError(null); onClose(); }, [onClose]); return ( Edit Group Update the name of the group "{group?.name}".
setGroupName(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter" && groupName.trim()) { void handleUpdate(); } }} disabled={isUpdating} />
{error && (
{error}
)}
void handleUpdate()} disabled={!groupName.trim() || groupName === group?.name} > Update Group
); }