File size: 6,322 Bytes
f56a29b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import { useState, useCallback } from 'react';
import { Label } from '@/components/ui/label';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import {
AlertDialog,
AlertDialogContent,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogCancel,
} from '@/components/ui/alert-dialog';
import { Loader2, Trash2, AlertTriangle } from 'lucide-react';
import { useI18n } from '@/lib/hooks/use-i18n';
import { clearDatabase } from '@/lib/utils/database';
import { toast } from 'sonner';
import { createLogger } from '@/lib/logger';
const log = createLogger('GeneralSettings');
export function GeneralSettings() {
const { t } = useI18n();
// Clear cache state
const [showClearDialog, setShowClearDialog] = useState(false);
const [confirmInput, setConfirmInput] = useState('');
const [clearing, setClearing] = useState(false);
const confirmPhrase = t('settings.clearCacheConfirmPhrase');
const isConfirmValid = confirmInput === confirmPhrase;
const handleClearCache = useCallback(async () => {
if (!isConfirmValid) return;
setClearing(true);
try {
// 1. Clear IndexedDB
await clearDatabase();
// 2. Clear localStorage
localStorage.clear();
// 3. Clear sessionStorage
sessionStorage.clear();
toast.success(t('settings.clearCacheSuccess'));
// Reload page after a short delay
setTimeout(() => {
window.location.reload();
}, 1000);
} catch (error) {
log.error('Failed to clear cache:', error);
toast.error(t('settings.clearCacheFailed'));
setClearing(false);
}
}, [isConfirmValid, t]);
const clearCacheItems =
t('settings.clearCacheConfirmItems').split('、').length > 1
? t('settings.clearCacheConfirmItems').split('、')
: t('settings.clearCacheConfirmItems').split(', ');
return (
<div className="flex flex-col gap-8">
{/* Danger Zone - Clear Cache */}
<div className="relative rounded-xl border border-destructive/30 bg-destructive/[0.03] dark:bg-destructive/[0.06] overflow-hidden">
{/* Subtle diagonal stripe pattern for danger emphasis */}
<div
className="absolute inset-0 opacity-[0.015] dark:opacity-[0.03] pointer-events-none"
style={{
backgroundImage: `repeating-linear-gradient(
-45deg,
transparent,
transparent 10px,
currentColor 10px,
currentColor 11px
)`,
}}
/>
<div className="relative p-4 space-y-4">
{/* Header */}
<div className="flex items-center gap-2.5">
<div className="p-1.5 rounded-md bg-destructive/10 text-destructive">
<AlertTriangle className="w-4 h-4" />
</div>
<h3 className="text-sm font-semibold text-destructive">{t('settings.dangerZone')}</h3>
</div>
{/* Content */}
<div className="flex items-center justify-between gap-4">
<div className="flex-1 min-w-0">
<p className="text-sm font-medium">{t('settings.clearCache')}</p>
<p className="text-xs text-muted-foreground mt-0.5 leading-relaxed">
{t('settings.clearCacheDescription')}
</p>
</div>
<Button
variant="destructive"
size="sm"
className="shrink-0"
onClick={() => {
setConfirmInput('');
setShowClearDialog(true);
}}
>
<Trash2 className="w-3.5 h-3.5 mr-1.5" />
{t('settings.clearCache')}
</Button>
</div>
</div>
</div>
{/* Clear Cache Confirmation Dialog */}
<AlertDialog
open={showClearDialog}
onOpenChange={(open) => {
if (!clearing) {
setShowClearDialog(open);
if (!open) setConfirmInput('');
}
}}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle className="flex items-center gap-2 text-destructive">
<AlertTriangle className="w-5 h-5" />
{t('settings.clearCacheConfirmTitle')}
</AlertDialogTitle>
<AlertDialogDescription asChild>
<div className="space-y-3">
<p>{t('settings.clearCacheConfirmDescription')}</p>
<ul className="space-y-1.5 ml-1">
{clearCacheItems.map((item, i) => (
<li key={i} className="flex items-center gap-2 text-sm">
<span className="w-1.5 h-1.5 rounded-full bg-destructive/60 shrink-0" />
{item.trim()}
</li>
))}
</ul>
<div className="pt-1">
<Label className="text-xs font-medium text-foreground">
{t('settings.clearCacheConfirmInput')}
</Label>
<Input
className="mt-1.5 h-9 text-sm"
placeholder={confirmPhrase}
value={confirmInput}
onChange={(e) => setConfirmInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && isConfirmValid) {
handleClearCache();
}
}}
autoFocus
/>
</div>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={clearing}>{t('common.cancel')}</AlertDialogCancel>
<Button
variant="destructive"
disabled={!isConfirmValid || clearing}
onClick={handleClearCache}
>
{clearing ? (
<Loader2 className="w-4 h-4 mr-1.5 animate-spin" />
) : (
<Trash2 className="w-4 h-4 mr-1.5" />
)}
{t('settings.clearCacheButton')}
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
);
}
|