mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Merge branch 'main' of gitlab.com:soapbox-pub/act2 into consoleFramev2
This commit is contained in:
commit
6296fc4366
12 files changed
+802
-92
No files matched your search
@@ -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();
|
||||
|
||||
+103
-12
@@ -1,14 +1,21 @@
|
||||
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';
|
||||
import {
|
||||
isPersistentStorageSupported,
|
||||
isPersistentStorageGranted,
|
||||
requestPersistentStorage,
|
||||
getStorageInfo
|
||||
} from '@/lib/persistentStorage';
|
||||
import JSZip from 'jszip';
|
||||
|
||||
interface StorageUsageDetails {
|
||||
@@ -28,8 +35,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,27 +53,29 @@ 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 {
|
||||
// Check if StorageManager API is available
|
||||
if (!('storage' in navigator)) {
|
||||
if (!isPersistentStorageSupported()) {
|
||||
setStorageError('StorageManager API is not available in this browser');
|
||||
return;
|
||||
}
|
||||
|
||||
// Get storage estimate
|
||||
const estimate = await navigator.storage.estimate();
|
||||
const info = await getStorageInfo();
|
||||
if (info) {
|
||||
setStorageInfo({
|
||||
quota: info.quota,
|
||||
usage: info.usage,
|
||||
usageDetails: info.usageDetails as StorageUsageDetails,
|
||||
});
|
||||
}
|
||||
|
||||
// Extract usage details safely
|
||||
const usageDetails: StorageUsageDetails | undefined = (estimate as StorageEstimate & { usageDetails?: StorageUsageDetails }).usageDetails;
|
||||
|
||||
setStorageInfo({
|
||||
quota: estimate.quota || 0,
|
||||
usage: estimate.usage || 0,
|
||||
usageDetails,
|
||||
});
|
||||
// Check if storage is persistent
|
||||
const persistent = await isPersistentStorageGranted();
|
||||
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 +160,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 (!isPersistentStorageSupported()) {
|
||||
toast({
|
||||
title: t('persistentStorageNotSupported'),
|
||||
description: t('persistentStorageNotSupportedDescription'),
|
||||
variant: "destructive",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if already persistent
|
||||
const alreadyPersistent = await isPersistentStorageGranted();
|
||||
if (alreadyPersistent) {
|
||||
setIsPersistent(true);
|
||||
toast({
|
||||
title: t('persistentStorageAlreadyGranted'),
|
||||
description: t('persistentStorageAlreadyGrantedDescription'),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Request persistent storage
|
||||
const granted = await requestPersistentStorage();
|
||||
|
||||
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 +336,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>
|
||||
|
||||
Reference in new issue
Block a user