import { useEffect, useState } from "react"; import { Icon } from "./Icon"; import { ConfirmDialog } from "./ConfirmDialog"; import { useBackup } from "../store/useBackup"; import { formatBackupTime, gdriveListBackups } from "../backup"; export function BackupSettings() { const open = useBackup((s) => s.settingsOpen); const close = useBackup((s) => s.closeSettings); const connected = useBackup((s) => s.connected); const email = useBackup((s) => s.email); const lastBackup = useBackup((s) => s.lastBackup); const pending = useBackup((s) => s.pending); const phase = useBackup((s) => s.phase); const connect = useBackup((s) => s.connect); const disconnect = useBackup((s) => s.disconnect); const backup = useBackup((s) => s.backup); const restore = useBackup((s) => s.restore); const cancelConnect = useBackup((s) => s.cancelConnect); const openAuthUrl = useBackup((s) => s.openAuthUrl); const copyAuthUrl = useBackup((s) => s.copyAuthUrl); const [remoteCount, setRemoteCount] = useState(null); const [confirmRestore, setConfirmRestore] = useState(false); const working = phase === "working"; useEffect(() => { if (!open || !connected) { setRemoteCount(null); return; } let active = true; gdriveListBackups() .then((list) => active && setRemoteCount(list.length)) .catch(() => active && setRemoteCount(null)); return () => { active = false; }; }, [open, connected, lastBackup]); if (!open) return null; return ( <>
e.stopPropagation()}>

Backup & Sync

{phase === "connecting" ? (
Waiting for Google authorization…

Finish in the browser tab that opened. If it didn't open, reopen the link or copy it into a browser.

·
) : !connected ? (

Connect Google Drive to keep a private backup of every book. margin only ever sees the files it creates in a margin folder, never the rest of your Drive.

) : ( <>
Connected account {email || "Google Drive"}
{pending ? "Changes not backed up" : formatBackupTime(lastBackup)}

Backs up automatically when you close margin, and every 15 minutes while there are changes.

)}
{confirmRestore && ( This replaces your local books with the copies in Google Drive. Any local changes that haven't been backed up will be lost. } confirmLabel="Restore" onConfirm={() => { setConfirmRestore(false); restore(); }} onClose={() => setConfirmRestore(false)} /> )} ); }