"use client"; import { useEffect, useState } from "react"; import { SharedCamoufoxConfigForm } from "@/components/shared-camoufox-config-form"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { ScrollArea } from "@/components/ui/scroll-area"; import { WayfernConfigForm } from "@/components/wayfern-config-form"; import type { BrowserProfile, CamoufoxConfig, CamoufoxOS, WayfernConfig, } from "@/types"; const getCurrentOS = (): CamoufoxOS => { if (typeof navigator === "undefined") return "linux"; const platform = navigator.platform.toLowerCase(); if (platform.includes("win")) return "windows"; if (platform.includes("mac")) return "macos"; return "linux"; }; import { LoadingButton } from "./loading-button"; import { RippleButton } from "./ui/ripple"; interface CamoufoxConfigDialogProps { isOpen: boolean; onClose: () => void; profile: BrowserProfile | null; onSave: (profile: BrowserProfile, config: CamoufoxConfig) => Promise; onSaveWayfern?: ( profile: BrowserProfile, config: CamoufoxConfig, ) => Promise; isRunning?: boolean; crossOsUnlocked?: boolean; } export function CamoufoxConfigDialog({ isOpen, onClose, profile, onSave, onSaveWayfern, isRunning = false, crossOsUnlocked = false, }: CamoufoxConfigDialogProps) { // Use union type to support both Camoufox and Wayfern configs const [config, setConfig] = useState(() => ({ geoip: true, os: getCurrentOS(), })); const [isSaving, setIsSaving] = useState(false); const isAntiDetectBrowser = profile?.browser === "camoufox" || profile?.browser === "wayfern"; // Initialize config when profile changes useEffect(() => { if (profile && isAntiDetectBrowser) { const profileConfig = profile.browser === "wayfern" ? profile.wayfern_config : profile.camoufox_config; setConfig( profileConfig || { geoip: true, os: getCurrentOS(), }, ); } }, [profile, isAntiDetectBrowser]); const updateConfig = ( key: keyof CamoufoxConfig | keyof WayfernConfig, value: unknown, ) => { setConfig((prev) => ({ ...prev, [key]: value })); }; const handleSave = async () => { if (!profile) return; // Validate fingerprint JSON if it exists if (config.fingerprint) { try { JSON.parse(config.fingerprint); } catch (_error) { const { toast } = await import("sonner"); toast.error("Invalid fingerprint configuration", { description: "The fingerprint configuration contains invalid JSON. Please check your advanced settings.", }); return; } } setIsSaving(true); try { if (profile.browser === "wayfern" && onSaveWayfern) { await onSaveWayfern(profile, config as CamoufoxConfig); } else { await onSave(profile, config as CamoufoxConfig); } onClose(); } catch (error) { console.error("Failed to save config:", error); const { toast } = await import("sonner"); toast.error("Failed to save configuration", { description: error instanceof Error ? error.message : "Unknown error occurred", }); } finally { setIsSaving(false); } }; const handleClose = () => { // Reset config to original when closing without saving if (profile && isAntiDetectBrowser) { const profileConfig = profile.browser === "wayfern" ? profile.wayfern_config : profile.camoufox_config; setConfig( profileConfig || { geoip: true, os: getCurrentOS(), }, ); } onClose(); }; if (!profile || !isAntiDetectBrowser) { return null; } const browserName = profile.browser === "wayfern" ? "Wayfern" : "Camoufox"; return ( {isRunning ? "View" : "Configure"} Fingerprint Settings -{" "} {profile.name} ({browserName})
{profile.browser === "wayfern" ? ( ) : ( )}
{isRunning ? "Close" : "Cancel"} {!isRunning && ( Save )}
); }