"use client"; import { invoke } from "@tauri-apps/api/core"; import { useCallback, useEffect, useState } from "react"; import { GoPlus } from "react-icons/go"; import { toast } from "sonner"; import { CreateGroupDialog } from "@/components/create-group-dialog"; import { LoadingButton } from "@/components/loading-button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import type { BrowserProfile, ProfileGroup } from "@/types"; import { RippleButton } from "./ui/ripple"; interface GroupAssignmentDialogProps { isOpen: boolean; onClose: () => void; selectedProfiles: string[]; onAssignmentComplete: () => void; profiles?: BrowserProfile[]; } export function GroupAssignmentDialog({ isOpen, onClose, selectedProfiles, onAssignmentComplete, profiles = [], }: GroupAssignmentDialogProps) { const [groups, setGroups] = useState([]); const [selectedGroupId, setSelectedGroupId] = useState(null); const [isLoading, setIsLoading] = useState(false); const [isAssigning, setIsAssigning] = useState(false); const [error, setError] = useState(null); const [createDialogOpen, setCreateDialogOpen] = useState(false); 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 handleAssign = useCallback(async () => { setIsAssigning(true); setError(null); try { await invoke("assign_profiles_to_group", { profileIds: selectedProfiles, groupId: selectedGroupId, }); const groupName = selectedGroupId ? groups.find((g) => g.id === selectedGroupId)?.name || "Unknown Group" : "Default"; toast.success( `Successfully assigned ${selectedProfiles.length} profile(s) to ${groupName}`, ); onAssignmentComplete(); onClose(); } catch (err) { console.error("Failed to assign profiles to group:", err); const errorMessage = err instanceof Error ? err.message : "Failed to assign profiles to group"; setError(errorMessage); toast.error(errorMessage); } finally { setIsAssigning(false); } }, [ selectedProfiles, selectedGroupId, groups, onAssignmentComplete, onClose, ]); useEffect(() => { if (isOpen) { void loadGroups(); setSelectedGroupId(null); setError(null); } }, [isOpen, loadGroups]); return ( Assign to Group Assign {selectedProfiles.length} selected profile(s) to a group.
    {selectedProfiles.map((profileId) => { // Find the profile name for display const profile = profiles.find( (p: BrowserProfile) => p.id === profileId, ); const displayName = profile ? profile.name : profileId; return (
  • • {displayName}
  • ); })}
setCreateDialogOpen(true)} > Create Group
{isLoading ? (
Loading groups...
) : ( )}
{error && (
{error}
)}
Cancel void handleAssign()} disabled={isLoading} > Assign
setCreateDialogOpen(false)} onGroupCreated={(group) => { setGroups((prev) => [...prev, group]); setSelectedGroupId(group.id); setCreateDialogOpen(false); }} />
); }