Request persistent storage when creating a project

This commit is contained in:
Alex Gleason committed 2025-09-24 23:13:29 -05:00
1 parent 980cb60f1b
commit a20bbfdf29
3 files changed
+148 -18

No files matched your search

+17
View File
@@ -1,6 +1,7 @@
import { Buffer } from 'buffer';
import type { Git } from '@/lib/git';
import type { JSRuntimeFS } from '@/lib/JSRuntime';
import { ensurePersistentStorage } from '@/lib/persistentStorage';
// Polyfill Buffer for browser
if (typeof window !== 'undefined') {
@@ -92,6 +93,14 @@ export class ProjectsManager {
},
});
// Automatically request persistent storage after project creation
try {
await ensurePersistentStorage();
} catch (error) {
// Don't fail project creation if persistent storage request fails
console.warn('Failed to request persistent storage after project creation:', error);
}
return project;
}
@@ -119,6 +128,14 @@ export class ProjectsManager {
const stats = await this.fs.stat(projectPath);
const timestamp = stats.mtimeMs ? new Date(stats.mtimeMs) : new Date();
// Automatically request persistent storage after project cloning
try {
await ensurePersistentStorage();
} catch (error) {
// Don't fail project cloning if persistent storage request fails
console.warn('Failed to request persistent storage after project cloning:', error);
}
// Return the full project object with dynamically generated properties
return {
id,
+111
View File
@@ -0,0 +1,111 @@
/**
* Utility functions for managing persistent storage in the browser
*/
/**
* Checks if persistent storage is supported by the browser
*/
export function isPersistentStorageSupported(): boolean {
return 'storage' in navigator && 'persist' in navigator.storage;
}
/**
* Checks if persistent storage is already granted
*/
export async function isPersistentStorageGranted(): Promise<boolean> {
if (!isPersistentStorageSupported()) {
return false;
}
try {
return await navigator.storage.persisted();
} catch (error) {
console.warn('Failed to check persistent storage status:', error);
return false;
}
}
/**
* Requests persistent storage from the browser
* Returns true if granted, false if denied or not supported
*/
export async function requestPersistentStorage(): Promise<boolean> {
if (!isPersistentStorageSupported()) {
console.warn('Persistent storage is not supported in this browser');
return false;
}
try {
// Check if already granted
const alreadyGranted = await isPersistentStorageGranted();
if (alreadyGranted) {
return true;
}
// Request persistent storage
const granted = await navigator.storage.persist();
return granted;
} catch (error) {
console.warn('Failed to request persistent storage:', error);
return false;
}
}
/**
* Automatically requests persistent storage if not already granted
* This is intended to be called silently after project creation/cloning
* Returns true if persistent storage is available (either already granted or newly granted)
*/
export async function ensurePersistentStorage(): Promise<boolean> {
try {
// Check if already granted
const alreadyGranted = await isPersistentStorageGranted();
if (alreadyGranted) {
return true;
}
// Only request if supported
if (!isPersistentStorageSupported()) {
return false;
}
// Silently request persistent storage
const granted = await requestPersistentStorage();
if (granted) {
console.log('Persistent storage granted automatically');
} else {
console.log('Persistent storage request denied or failed');
}
return granted;
} catch (error) {
console.warn('Failed to ensure persistent storage:', error);
return false;
}
}
/**
* Gets current storage usage information
*/
export async function getStorageInfo(): Promise<{
quota: number;
usage: number;
usageDetails?: Record<string, number>;
} | null> {
if (!('storage' in navigator)) {
return null;
}
try {
const estimate = await navigator.storage.estimate();
return {
quota: estimate.quota || 0,
usage: estimate.usage || 0,
usageDetails: (estimate as StorageEstimate & { usageDetails?: Record<string, number> }).usageDetails,
};
} catch (error) {
console.warn('Failed to get storage information:', error);
return null;
}
}
+20 -18
View File
@@ -10,6 +10,12 @@ 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 {
@@ -52,28 +58,24 @@ export function DataSettings() {
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();
// Extract usage details safely
const usageDetails: StorageUsageDetails | undefined = (estimate as StorageEstimate & { usageDetails?: StorageUsageDetails }).usageDetails;
setStorageInfo({
quota: estimate.quota || 0,
usage: estimate.usage || 0,
usageDetails,
});
const info = await getStorageInfo();
if (info) {
setStorageInfo({
quota: info.quota,
usage: info.usage,
usageDetails: info.usageDetails as StorageUsageDetails,
});
}
// Check if storage is persistent
if ('persist' in navigator.storage) {
const persistent = await navigator.storage.persisted();
setIsPersistent(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');
@@ -167,7 +169,7 @@ export function DataSettings() {
setIsRequestingPersistent(true);
try {
// Check if persist method is available
if (!('storage' in navigator) || !('persist' in navigator.storage)) {
if (!isPersistentStorageSupported()) {
toast({
title: t('persistentStorageNotSupported'),
description: t('persistentStorageNotSupportedDescription'),
@@ -177,7 +179,7 @@ export function DataSettings() {
}
// Check if already persistent
const alreadyPersistent = await navigator.storage.persisted();
const alreadyPersistent = await isPersistentStorageGranted();
if (alreadyPersistent) {
setIsPersistent(true);
toast({
@@ -188,7 +190,7 @@ export function DataSettings() {
}
// Request persistent storage
const granted = await navigator.storage.persist();
const granted = await requestPersistentStorage();
if (granted) {
setIsPersistent(true);