From 492542df915af96a9bca32fb7265a22e44f6306a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 13 Sep 2025 20:24:56 -0500 Subject: [PATCH] Add Storage Usage section to Data Settings page --- src/pages/DataSettings.tsx | 172 ++++++++++++++++++++++++++++++++++++- 1 file changed, 170 insertions(+), 2 deletions(-) diff --git a/src/pages/DataSettings.tsx b/src/pages/DataSettings.tsx index add3880f..133332d3 100644 --- a/src/pages/DataSettings.tsx +++ b/src/pages/DataSettings.tsx @@ -1,20 +1,86 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; -import { Download, Trash2, AlertTriangle, Loader2, Database } from 'lucide-react'; +import { Download, Trash2, AlertTriangle, Loader2, Database, HardDrive, Info } 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 { Badge } from '@/components/ui/badge'; import { useToast } from '@/hooks/useToast'; import { useFS } from '@/hooks/useFS'; import JSZip from 'jszip'; +interface StorageUsageDetails { + indexedDB?: number; + caches?: number; + serviceWorkerRegistrations?: number; + [key: string]: number | undefined; +} + +interface StorageInfo { + quota: number; + usage: number; + usageDetails?: StorageUsageDetails; + isPersistent?: boolean; +} + export function DataSettings() { const [isExporting, setIsExporting] = useState(false); const [isClearing, setIsClearing] = useState(false); + const [storageInfo, setStorageInfo] = useState(null); + const [storageError, setStorageError] = useState(null); const { toast } = useToast(); const { fs } = useFS(); const navigate = useNavigate(); + // Format bytes to human readable format + const formatBytes = (bytes: number): string => { + if (bytes === 0) return '0 B'; + const k = 1024; + const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; + const i = Math.floor(Math.log(bytes) / Math.log(k)); + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; + }; + + // Load storage information + useEffect(() => { + const loadStorageInfo = async () => { + try { + // Check if StorageManager API is available + if (!('storage' in navigator)) { + setStorageError('StorageManager API is not available in this browser'); + return; + } + + // Get storage estimate + const estimate = await navigator.storage.estimate(); + + // Get persistence status + let isPersistent = false; + try { + isPersistent = await navigator.storage.persisted(); + } catch (error) { + console.warn('Failed to check persistence status:', error); + } + + // Extract usage details safely + const usageDetails: StorageUsageDetails | undefined = (estimate as StorageEstimate & { usageDetails?: StorageUsageDetails }).usageDetails; + + setStorageInfo({ + quota: estimate.quota || 0, + usage: estimate.usage || 0, + usageDetails, + isPersistent, + }); + } catch (error) { + console.error('Failed to get storage information:', error); + setStorageError(error instanceof Error ? error.message : 'Failed to get storage information'); + } + }; + + loadStorageInfo(); + }, []); + const exportFilesAsZip = async () => { const zip = new JSZip(); @@ -150,6 +216,108 @@ export function DataSettings() {
+ {/* Storage Information */} + + + + + Storage Usage + + + Information about local storage usage in this browser. + + + + {storageError ? ( +
+ + {storageError} +
+ ) : storageInfo ? ( +
+ {/* Usage Progress */} +
+
+ Used: {formatBytes(storageInfo.usage)} + Available: {formatBytes(storageInfo.quota)} +
+ 0 ? (storageInfo.usage / storageInfo.quota) * 100 : 0} + className="h-2" + /> +
+ {storageInfo.quota > 0 + ? `${((storageInfo.usage / storageInfo.quota) * 100).toFixed(1)}% used` + : 'Usage percentage unavailable' + } +
+
+ + {/* Storage Details */} +
+
+

Storage Details

+
+
+ Total Used: + {formatBytes(storageInfo.usage)} +
+
+ Available: + {formatBytes(storageInfo.quota)} +
+ {storageInfo.usageDetails && ( + <> + {storageInfo.usageDetails.indexedDB !== undefined && ( +
+ IndexedDB: + {formatBytes(storageInfo.usageDetails.indexedDB)} +
+ )} + {storageInfo.usageDetails.caches !== undefined && ( +
+ Cache: + {formatBytes(storageInfo.usageDetails.caches)} +
+ )} + {storageInfo.usageDetails.serviceWorkerRegistrations !== undefined && ( +
+ Service Workers: + {formatBytes(storageInfo.usageDetails.serviceWorkerRegistrations)} +
+ )} + + )} +
+
+ +
+

Storage Status

+
+
+ + {storageInfo.isPersistent ? "Persistent" : "Not Persistent"} + +
+

+ {storageInfo.isPersistent + ? "Data is protected from eviction by the browser." + : "Data may be cleared by the browser when storage is low." + } +

+
+
+
+
+ ) : ( +
+ + Loading storage information... +
+ )} +
+
+ {/* Export Files */}