Fix image thumbnail persistance

This commit is contained in:
Reece Browne 2025-08-21 17:09:51 +01:00
parent cbaa945b7e
commit c0a08598dd
4 changed files with 32 additions and 12 deletions

View File

@ -25,6 +25,7 @@ interface IndexedDBContextValue {
// Utilities
getStorageStats: () => Promise<{ used: number; available: number; fileCount: number }>;
updateThumbnail: (fileId: FileId, thumbnail: string) => Promise<boolean>;
}
const IndexedDBContext = createContext<IndexedDBContextValue | null>(null);
@ -174,6 +175,10 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) {
return await fileStorage.getStorageStats();
}, []);
const updateThumbnail = useCallback(async (fileId: FileId, thumbnail: string): Promise<boolean> => {
return await fileStorage.updateThumbnail(fileId, thumbnail);
}, []);
const value: IndexedDBContextValue = {
saveFile,
loadFile,
@ -182,7 +187,8 @@ export function IndexedDBProvider({ children }: IndexedDBProviderProps) {
loadAllMetadata,
deleteMultiple,
clearAll,
getStorageStats
getStorageStats,
updateThumbnail
};
return (

View File

@ -238,15 +238,6 @@ export async function addFiles(
const record = toFileRecord(file, fileId);
// Restore metadata from storage
if (metadata.thumbnail) {
record.thumbnailUrl = metadata.thumbnail;
// Track blob URLs for cleanup (images return blob URLs that need revocation)
if (metadata.thumbnail.startsWith('blob:')) {
lifecycleManager.trackBlobUrl(metadata.thumbnail);
}
}
// Generate processedFile metadata for stored files
let pageCount: number = 1;
@ -271,6 +262,15 @@ export async function addFiles(
if (DEBUG) console.log(`📄 addFiles(stored): Non-PDF file ${file.name}, no page count`);
}
// Restore metadata from storage
if (metadata.thumbnail) {
record.thumbnailUrl = metadata.thumbnail;
// Track blob URLs for cleanup (images return blob URLs that need revocation)
if (metadata.thumbnail.startsWith('blob:')) {
lifecycleManager.trackBlobUrl(metadata.thumbnail);
}
}
// Create processedFile metadata with correct page count
if (pageCount > 0) {
record.processedFile = createProcessedFile(pageCount, metadata.thumbnail);

View File

@ -66,6 +66,15 @@ export function useIndexedDBThumbnail(file: FileMetadata | undefined | null): {
const thumbnail = await generateThumbnailForFile(fileObject);
if (!cancelled) {
setThumb(thumbnail);
// Save thumbnail to IndexedDB for persistence
if (file.id && indexedDB && thumbnail) {
try {
await indexedDB.updateThumbnail(file.id, thumbnail);
} catch (error) {
console.warn('Failed to save thumbnail to IndexedDB:', error);
}
}
}
} catch (error) {
console.warn('Failed to generate thumbnail for file', file.name, error);

View File

@ -333,9 +333,14 @@ export async function generateThumbnailForFile(file: File): Promise<string> {
return generatePlaceholderThumbnail(file);
}
// Handle image files - creates blob URL that needs cleanup by caller
// Handle image files - convert to data URL for persistence
if (file.type.startsWith('image/')) {
return URL.createObjectURL(file);
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(file);
});
}
// Handle PDF files