"use client"; import { invoke } from "@tauri-apps/api/core"; import { emit } from "@tauri-apps/api/event"; import { useCallback, useEffect, useState } from "react"; 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import type { BrowserProfile, StoredProxy } from "@/types"; import { RippleButton } from "./ui/ripple"; interface ProxyAssignmentDialogProps { isOpen: boolean; onClose: () => void; selectedProfiles: string[]; onAssignmentComplete: () => void; profiles?: BrowserProfile[]; storedProxies?: StoredProxy[]; } export function ProxyAssignmentDialog({ isOpen, onClose, selectedProfiles, onAssignmentComplete, profiles = [], storedProxies = [], }: ProxyAssignmentDialogProps) { const [selectedProxyId, setSelectedProxyId] = useState(null); const [isAssigning, setIsAssigning] = useState(false); const [error, setError] = useState(null); const handleAssign = useCallback(async () => { setIsAssigning(true); setError(null); try { const validProfiles = selectedProfiles.filter((profileId) => { const profile = profiles.find((p) => p.id === profileId); return profile; }); if (validProfiles.length === 0) { setError("No valid profiles selected."); setIsAssigning(false); return; } // Update each profile's proxy sequentially to avoid file locking issues for (const profileId of validProfiles) { await invoke("update_profile_proxy", { profileId, proxyId: selectedProxyId, }); } // Notify other parts of the app so usage counts and lists refresh await emit("profile-updated"); onAssignmentComplete(); onClose(); } catch (err) { console.error("Failed to assign proxies to profiles:", err); const errorMessage = err instanceof Error ? err.message : "Failed to assign proxies to profiles"; setError(errorMessage); toast.error(errorMessage); } finally { setIsAssigning(false); } }, [ selectedProfiles, selectedProxyId, profiles, onAssignmentComplete, onClose, ]); useEffect(() => { if (isOpen) { setSelectedProxyId(null); setError(null); } }, [isOpen]); return ( Assign Proxy Assign a proxy to {selectedProfiles.length} selected profile(s).
    {selectedProfiles.map((profileId) => { const profile = profiles.find( (p: BrowserProfile) => p.id === profileId, ); const displayName = profile ? profile.name : profileId; return (
  • • {displayName}
  • ); })}
{error && (
{error}
)}
Cancel void handleAssign()} > Assign
); }