Add Persist Data toggle to Data Settings

This commit is contained in:
Alex Gleason committed 2025-09-24 22:59:37 -05:00
1 parent 2956c86f88
commit 980cb60f1b
3 files changed
+165 -3

No files matched your search

+66
View File
@@ -242,6 +242,17 @@ const resources = {
dataClearedSuccessfully: 'Data cleared successfully',
dataClearedDescription: 'All local data has been removed. Redirecting to homepage...',
failedToClearData: 'Failed to clear data',
persistData: 'Persist Data',
persistDataDescription: 'Request persistent storage to prevent data loss when browser storage is low. This helps protect your projects and settings.',
persistentStorageGranted: 'Persistent storage granted',
persistentStorageGrantedDescription: 'Your browser will now protect your data from automatic cleanup.',
persistentStorageAlreadyGranted: 'Persistent storage already granted',
persistentStorageAlreadyGrantedDescription: 'Your data is already protected from automatic cleanup.',
persistentStorageDenied: 'Persistent storage request denied',
persistentStorageDeniedDescription: 'Your browser declined the request. Data may still be cleared automatically when storage is low.',
persistentStorageNotSupported: 'Persistent storage not supported',
persistentStorageNotSupportedDescription: 'Your browser does not support persistent storage requests.',
failedToRequestPersistentStorage: 'Failed to request persistent storage',
// 404 Page
pageNotFound: 'Oops! Page not found',
@@ -534,6 +545,17 @@ const resources = {
dataClearedSuccessfully: 'Dados limpos com sucesso',
dataClearedDescription: 'Todos os dados locais foram removidos. Redirecionando para a página inicial...',
failedToClearData: 'Falha ao limpar dados',
persistData: 'Persistir Dados',
persistDataDescription: 'Solicitar armazenamento persistente para evitar perda de dados quando o armazenamento do navegador estiver baixo. Isso ajuda a proteger seus projetos e configurações.',
persistentStorageGranted: 'Armazenamento persistente concedido',
persistentStorageGrantedDescription: 'Seu navegador agora protegerá seus dados da limpeza automática.',
persistentStorageAlreadyGranted: 'Armazenamento persistente já concedido',
persistentStorageAlreadyGrantedDescription: 'Seus dados já estão protegidos da limpeza automática.',
persistentStorageDenied: 'Solicitação de armazenamento persistente negada',
persistentStorageDeniedDescription: 'Seu navegador recusou a solicitação. Os dados ainda podem ser limpos automaticamente quando o armazenamento estiver baixo.',
persistentStorageNotSupported: 'Armazenamento persistente não suportado',
persistentStorageNotSupportedDescription: 'Seu navegador não suporta solicitações de armazenamento persistente.',
failedToRequestPersistentStorage: 'Falha ao solicitar armazenamento persistente',
// About Settings
aboutShakespeare: 'Sobre o Shakespeare',
@@ -836,6 +858,17 @@ const resources = {
dataClearedSuccessfully: '数据清除成功',
dataClearedDescription: '所有本地数据已删除。正在重定向到主页...',
failedToClearData: '清除数据失败',
persistData: '持久化数据',
persistDataDescription: '请求持久存储以防止浏览器存储不足时数据丢失。这有助于保护您的项目和设置。',
persistentStorageGranted: '已授予持久存储',
persistentStorageGrantedDescription: '您的浏览器现在将保护您的数据免受自动清理。',
persistentStorageAlreadyGranted: '已授予持久存储',
persistentStorageAlreadyGrantedDescription: '您的数据已受到保护,不会被自动清理。',
persistentStorageDenied: '持久存储请求被拒绝',
persistentStorageDeniedDescription: '您的浏览器拒绝了请求。当存储不足时,数据仍可能被自动清理。',
persistentStorageNotSupported: '不支持持久存储',
persistentStorageNotSupportedDescription: '您的浏览器不支持持久存储请求。',
failedToRequestPersistentStorage: '请求持久存储失败',
// About Settings
aboutShakespeare: '关于Shakespeare',
@@ -1138,6 +1171,17 @@ const resources = {
dataClearedSuccessfully: 'An share bayanai cikin nasara',
dataClearedDescription: 'An cire duk bayanan gida. Ana turawa zuwa shafin gida...',
failedToClearData: 'Ya gaza share bayanai',
persistData: 'Kiyaye Bayanai',
persistDataDescription: 'Nemi ajiyar bayanai ta dindindin don hana asarar bayanai lokacin da ajiyar burauzar ta ragu. Wannan yana taimakawa wajen kare ayyukan ku da saitunan.',
persistentStorageGranted: 'An ba da ajiyar ta dindindin',
persistentStorageGrantedDescription: 'Burauzar ku yanzu za ta kare bayananku daga share mai sarrafa kansa.',
persistentStorageAlreadyGranted: 'An riga an ba da ajiyar ta dindindin',
persistentStorageAlreadyGrantedDescription: 'An riga an kare bayananku daga share mai sarrafa kansa.',
persistentStorageDenied: 'An ki neman ajiyar ta dindindin',
persistentStorageDeniedDescription: 'Burauzar ku ta ki bukatar. Ana iya share bayanai ta atomatik lokacin da ajiya ta ragu.',
persistentStorageNotSupported: 'Ba a goyan bayan ajiyar ta dindindin ba',
persistentStorageNotSupportedDescription: 'Burauzar ku ba ta goyan bayan bukatun ajiyar ta dindindin ba.',
failedToRequestPersistentStorage: 'Ya gaza neman ajiyar ta dindindin',
// About Settings
aboutShakespeare: 'Game da Shakespeare',
@@ -1440,6 +1484,17 @@ const resources = {
dataClearedSuccessfully: 'Data ti nu ni aṣeyọri',
dataClearedDescription: 'Gbogbo data agbegbe ti yọ kuro. N ṣe atunṣe si oju-iwe ile...',
failedToClearData: 'O kuna lati nu data',
persistData: 'Fi Data Pamọ',
persistDataDescription: 'Beere ibi ipamọ ti o wa nigbagbogbo lati yago fun isonu data nigbati ibi ipamọ aṣawakiri ba kere. Eyi ṣe iranlọwọ lati daabobo awọn iṣẹ akanṣe ati awọn eto rẹ.',
persistentStorageGranted: 'Ibi ipamọ ti o wa nigbagbogbo ti funni',
persistentStorageGrantedDescription: 'Aṣawakiri rẹ yoo daabobo data rẹ lati imukuro adaṣe.',
persistentStorageAlreadyGranted: 'Ibi ipamọ ti o wa nigbagbogbo ti funni tẹlẹ',
persistentStorageAlreadyGrantedDescription: 'Data rẹ ti wa ni aabo lati imukuro adaṣe.',
persistentStorageDenied: 'Ibeere ibi ipamọ ti o wa nigbagbogbo ti kọ',
persistentStorageDeniedDescription: 'Aṣawakiri rẹ ti kọ ibeere naa. Data le tun jẹ imukuro adaṣe nigbati ibi ipamọ ba kere.',
persistentStorageNotSupported: 'Ibi ipamọ ti o wa nigbagbogbo ko ni atilẹyin',
persistentStorageNotSupportedDescription: 'Aṣawakiri rẹ ko ni atilẹyin awọn ibeere ibi ipamọ ti o wa nigbagbogbo.',
failedToRequestPersistentStorage: 'O kuna lati beere ibi ipamọ ti o wa nigbagbogbo',
// About Settings
aboutShakespeare: 'Nipa Shakespeare',
@@ -1737,6 +1792,17 @@ const resources = {
dataClearedSuccessfully: 'Ehichapụrụ data nke ọma',
dataClearedDescription: 'Ewepụrụ data mpaghara niile. Na-atụgharị gaa na ibe ụlọ...',
failedToClearData: 'O dara ihichapụ data',
persistData: 'Chekwaa Data Ogologo Oge',
persistDataDescription: 'Rịọ nchekwa na-adịgide adịgide iji gbochie mfu data mgbe nchekwa browser dị ala. Nke a na-enyere aka ichebe ọrụ gị na ntọala.',
persistentStorageGranted: 'Enyere nchekwa na-adịgide adịgide',
persistentStorageGrantedDescription: 'Browser gị ga-echebe data gị ugbu a site na nhicha akpaaka.',
persistentStorageAlreadyGranted: 'Enyerela nchekwa na-adịgide adịgide',
persistentStorageAlreadyGrantedDescription: 'Echebeela data gị site na nhicha akpaaka.',
persistentStorageDenied: 'Ajụrụ arịrịọ nchekwa na-adịgide adịgide',
persistentStorageDeniedDescription: 'Browser gị jụrụ arịrịọ ahụ. Enwere ike ihichapụ data akpaaka mgbe nchekwa dị ala.',
persistentStorageNotSupported: 'Akwadoghị nchekwa na-adịgide adịgide',
persistentStorageNotSupportedDescription: 'Browser gị akwadoghị arịrịọ nchekwa na-adịgide adịgide.',
failedToRequestPersistentStorage: 'O dara ịrịọ nchekwa na-adịgide adịgide',
// About Settings
aboutShakespeare: 'Gbasara Shakespeare',
+8 -1
View File
@@ -17,6 +17,9 @@ describe('DataSettings', () => {
// Check for description
expect(screen.getByText('Export files and manage local data.')).toBeInTheDocument();
// Check for Persist Data section
expect(screen.getByText('Persist Data')).toBeInTheDocument();
// Check for Export Files section
expect(screen.getByText('Export Files')).toBeInTheDocument();
expect(screen.getByText('Export All Files')).toBeInTheDocument();
@@ -26,13 +29,17 @@ describe('DataSettings', () => {
expect(screen.getByRole('button', { name: /clear all data/i })).toBeInTheDocument();
});
it('shows export and clear buttons', () => {
it('shows persist data toggle, export and clear buttons', () => {
render(
<TestApp>
<DataSettings />
</TestApp>
);
// Persist storage toggle should be visible
const persistToggle = screen.getByRole('switch');
expect(persistToggle).toBeInTheDocument();
// Export button should be visible
const exportButton = screen.getByRole('button', { name: /export all files/i });
expect(exportButton).toBeInTheDocument();
+91 -2
View File
@@ -1,11 +1,12 @@
import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Download, Trash2, AlertTriangle, Loader2, Database, Info, ArrowLeft } from 'lucide-react';
import { Download, Trash2, AlertTriangle, Loader2, Database, Info, ArrowLeft, Shield } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from '@/components/ui/alert-dialog';
import { Progress } from '@/components/ui/progress';
import { Switch } from '@/components/ui/switch';
import { useToast } from '@/hooks/useToast';
import { useFS } from '@/hooks/useFS';
import { useIsMobile } from '@/hooks/useIsMobile';
@@ -28,8 +29,10 @@ export function DataSettings() {
const { t } = useTranslation();
const [isExporting, setIsExporting] = useState(false);
const [isClearing, setIsClearing] = useState(false);
const [isRequestingPersistent, setIsRequestingPersistent] = useState(false);
const [storageInfo, setStorageInfo] = useState<StorageInfo | null>(null);
const [storageError, setStorageError] = useState<string | null>(null);
const [isPersistent, setIsPersistent] = useState<boolean | null>(null);
const { toast } = useToast();
const { fs } = useFS();
const navigate = useNavigate();
@@ -44,7 +47,7 @@ export function DataSettings() {
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};
// Load storage information
// Load storage information and persistent status
useEffect(() => {
const loadStorageInfo = async () => {
try {
@@ -65,6 +68,12 @@ export function DataSettings() {
usage: estimate.usage || 0,
usageDetails,
});
// Check if storage is persistent
if ('persist' in navigator.storage) {
const persistent = await navigator.storage.persisted();
setIsPersistent(persistent);
}
} catch (error) {
console.error('Failed to get storage information:', error);
setStorageError(error instanceof Error ? error.message : 'Failed to get storage information');
@@ -149,6 +158,63 @@ export function DataSettings() {
}
};
const handlePersistentStorageToggle = async (checked: boolean) => {
// If trying to turn off, we can't actually do that - just ignore
if (!checked) {
return;
}
setIsRequestingPersistent(true);
try {
// Check if persist method is available
if (!('storage' in navigator) || !('persist' in navigator.storage)) {
toast({
title: t('persistentStorageNotSupported'),
description: t('persistentStorageNotSupportedDescription'),
variant: "destructive",
});
return;
}
// Check if already persistent
const alreadyPersistent = await navigator.storage.persisted();
if (alreadyPersistent) {
setIsPersistent(true);
toast({
title: t('persistentStorageAlreadyGranted'),
description: t('persistentStorageAlreadyGrantedDescription'),
});
return;
}
// Request persistent storage
const granted = await navigator.storage.persist();
if (granted) {
setIsPersistent(true);
toast({
title: t('persistentStorageGranted'),
description: t('persistentStorageGrantedDescription'),
});
} else {
toast({
title: t('persistentStorageDenied'),
description: t('persistentStorageDeniedDescription'),
variant: "destructive",
});
}
} catch (error) {
console.error('Failed to request persistent storage:', error);
toast({
title: t('failedToRequestPersistentStorage'),
description: error instanceof Error ? error.message : t('error'),
variant: "destructive",
});
} finally {
setIsRequestingPersistent(false);
}
};
const handleClearAllData = async () => {
setIsClearing(true);
try {
@@ -268,6 +334,29 @@ export function DataSettings() {
</div>
)}
{/* Persist Data */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Shield className="h-5 w-5" />
{t('persistData')}
</CardTitle>
<CardDescription>
{t('persistDataDescription')}
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex items-center justify-start">
<Switch
id="persistent-storage"
checked={isPersistent === true}
onCheckedChange={handlePersistentStorageToggle}
disabled={isRequestingPersistent || isPersistent === null}
/>
</div>
</CardContent>
</Card>
{/* Export Files */}
<Card>
<CardHeader>