import { useState } from 'react'; import { Loader2, Copy, Check, RotateCcw } from 'lucide-react'; import { Button } from '@components/ui/button'; import { Input } from '@components/ui/input'; import { Card, CardContent, CardHeader, CardTitle } from '@components/ui/card'; import { Alert, AlertDescription } from '@components/ui/alert'; import { useStorage } from '@utils/useStorage'; import { useGlobalState } from '@contexts/state/StateContext'; export function LoadingSpinner() { return ; } export default function BackupControls() { const [key, setKey] = useState(''); const [backupKey, setBackupKey] = useState(''); const [isBackingUp, setIsBackingUp] = useState(false); const [isRestoring, setIsRestoring] = useState(false); const [copied, setCopied] = useState(false); const [error, setError] = useState(null); const { state, dispatch, resetState } = useGlobalState(); const { backupState, restoreState } = useStorage(); const handleBackup = async () => { setIsBackingUp(true); setError(null); try { const result = await backupState(state); if (result?.key) { setBackupKey(result.key); } } catch (error) { console.error(error); setError('Failed to backup data. Please try again.'); } finally { setIsBackingUp(false); } }; const handleRestore = async () => { if (!key) { setError('Please enter a key'); return; } setIsRestoring(true); setError(null); try { const restored = await restoreState(key); if (restored) { dispatch({ type: 'SET_STATE', payload: restored }); setKey(''); } else { setError('No data found for this key'); } } catch (error) { console.error(error); setError('Failed to restore data. Please check your key and try again.'); } finally { setIsRestoring(false); } }; const copyKey = async () => { try { await navigator.clipboard.writeText(backupKey); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (error) { console.error(error); setError('Failed to copy key to clipboard'); } }; return ( Backup Controls {error && ( {error} )}
{backupKey && (
Your backup key: {backupKey}
)}
setKey(e.target.value)} disabled={isRestoring} />
); }