"use client"; import { invoke } from "@tauri-apps/api/core"; import * as React from "react"; import { FiCheck } from "react-icons/fi"; import { toast } from "sonner"; import { FlagIcon } from "@/components/flag-icon"; import { Button } from "@/components/ui/button"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; import { formatRelativeTime } from "@/lib/flag-utils"; import type { ProxyCheckResult, StoredProxy } from "@/types"; interface ProxyCheckButtonProps { proxy: StoredProxy; profileId: string; checkingProfileId: string | null; cachedResult?: ProxyCheckResult; onCheckComplete?: (result: ProxyCheckResult) => void; onCheckFailed?: (result: ProxyCheckResult) => void; disabled?: boolean; setCheckingProfileId?: (id: string | null) => void; } export function ProxyCheckButton({ proxy, profileId, checkingProfileId, cachedResult, onCheckComplete, onCheckFailed, disabled = false, setCheckingProfileId, }: ProxyCheckButtonProps) { const [localResult, setLocalResult] = React.useState< ProxyCheckResult | undefined >(cachedResult); React.useEffect(() => { setLocalResult(cachedResult); }, [cachedResult]); const handleCheck = React.useCallback(async () => { if (checkingProfileId === profileId) return; setCheckingProfileId?.(profileId); try { const result = await invoke("check_proxy_validity", { proxyId: proxy.id, proxySettings: proxy.dynamic_proxy_url ? undefined : proxy.proxy_settings, }); setLocalResult(result); onCheckComplete?.(result); // Show toast with location const locationParts: string[] = []; if (result.city) locationParts.push(result.city); if (result.country) locationParts.push(result.country); const location = locationParts.length > 0 ? locationParts.join(", ") : "Unknown"; toast.success(
Your proxy location is:
{location} {result.country_code && ( )}
, ); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); toast.error(`Proxy check failed: ${errorMessage}`); // Save failed check result const failedResult: ProxyCheckResult = { ip: "", city: undefined, country: undefined, country_code: undefined, timestamp: Math.floor(Date.now() / 1000), is_valid: false, }; setLocalResult(failedResult); onCheckFailed?.(failedResult); } finally { setCheckingProfileId?.(null); } }, [ proxy, profileId, checkingProfileId, onCheckComplete, onCheckFailed, setCheckingProfileId, ]); const isCurrentlyChecking = checkingProfileId === profileId; const result = localResult; return ( {isCurrentlyChecking ? (

Checking proxy...

) : result?.is_valid ? (

{result.country_code && ( )} {[result.city, result.country].filter(Boolean).join(", ") || "Unknown"}

IP: {result.ip}

Checked {formatRelativeTime(result.timestamp)}

) : result && !result.is_valid ? (

Proxy check failed

Failed {formatRelativeTime(result.timestamp)}

) : (

Check proxy validity

)}
); }