"use client"; import { invoke } from "@tauri-apps/api/core"; import { useCallback, useState } from "react"; import { LoadingButton } from "@/components/loading-button"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { showErrorToast, showSuccessToast } from "@/lib/toast-utils"; import type { BrowserProfile, SyncSettings } from "@/types"; interface ProfileSyncDialogProps { isOpen: boolean; onClose: () => void; profile: BrowserProfile | null; onSyncConfigOpen: () => void; } export function ProfileSyncDialog({ isOpen, onClose, profile, onSyncConfigOpen, }: ProfileSyncDialogProps) { const [isSaving, setIsSaving] = useState(false); const [isSyncing, setIsSyncing] = useState(false); const [syncEnabled, setSyncEnabled] = useState( profile?.sync_enabled ?? false, ); const [hasConfig, setHasConfig] = useState(false); const [isCheckingConfig, setIsCheckingConfig] = useState(false); const checkSyncConfig = useCallback(async () => { setIsCheckingConfig(true); try { const settings = await invoke("get_sync_settings"); setHasConfig(Boolean(settings.sync_server_url && settings.sync_token)); } catch { setHasConfig(false); } finally { setIsCheckingConfig(false); } }, []); const handleOpenChange = useCallback( (open: boolean) => { if (open && profile) { setSyncEnabled(profile.sync_enabled ?? false); void checkSyncConfig(); } if (!open) { onClose(); } }, [profile, onClose, checkSyncConfig], ); const handleToggleSync = useCallback(async () => { if (!profile) return; if (!hasConfig) { showErrorToast("Please configure sync service first"); onSyncConfigOpen(); onClose(); return; } setIsSaving(true); try { await invoke("set_profile_sync_enabled", { profileId: profile.id, enabled: !syncEnabled, }); setSyncEnabled(!syncEnabled); showSuccessToast( !syncEnabled ? "Sync enabled - syncing now..." : "Sync disabled", ); } catch (error) { console.error("Failed to toggle sync:", error); showErrorToast("Failed to update sync settings"); } finally { setIsSaving(false); } }, [profile, syncEnabled, hasConfig, onSyncConfigOpen, onClose]); const handleSyncNow = useCallback(async () => { if (!profile) return; if (!hasConfig) { showErrorToast("Please configure sync service first"); onSyncConfigOpen(); onClose(); return; } setIsSyncing(true); try { await invoke("request_profile_sync", { profileId: profile.id }); showSuccessToast("Sync queued"); } catch (error) { console.error("Failed to queue sync:", error); showErrorToast("Failed to queue sync"); } finally { setIsSyncing(false); } }, [profile, hasConfig, onSyncConfigOpen, onClose]); const formatLastSync = (timestamp?: number) => { if (!timestamp) return "Never"; const date = new Date(timestamp * 1000); return date.toLocaleString(); }; if (!profile) return null; return ( Profile Sync Manage sync settings for "{profile.name}" {isCheckingConfig ? (
) : (
{!hasConfig && (

Sync service not configured.

)} {hasConfig && ( <>

Sync this profile across devices

{formatLastSync(profile.last_sync)} {syncEnabled && ( {profile.last_sync ? "Synced" : "Pending"} )}
)}
)} {hasConfig && syncEnabled && ( Sync Now )}
); }