Stirling-PDF/frontend/src/hooks/useIndexedDBThumbnail.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.6 KiB
TypeScript
Raw Normal View History

import { useState, useEffect } from "react";
import { FileWithUrl } from "../types/file";
2025-08-05 14:39:27 +01:00
import { fileStorage } from "../services/fileStorage";
2025-08-06 10:28:15 +01:00
import { generateThumbnailForFile } from "../utils/thumbnailUtils";
2025-08-05 14:39:27 +01:00
/**
* Calculate optimal scale for thumbnail generation
* Ensures high quality while preventing oversized renders
*/
function calculateThumbnailScale(pageViewport: { width: number; height: number }): number {
const maxWidth = 400; // Max thumbnail width
const maxHeight = 600; // Max thumbnail height
const scaleX = maxWidth / pageViewport.width;
const scaleY = maxHeight / pageViewport.height;
// Don't upscale, only downscale if needed
return Math.min(scaleX, scaleY, 1.0);
}
/**
* Hook for IndexedDB-aware thumbnail loading
* Handles thumbnail generation for files not in IndexedDB
*/
export function useIndexedDBThumbnail(file: FileWithUrl | undefined | null): {
thumbnail: string | null;
isGenerating: boolean
} {
const [thumb, setThumb] = useState<string | null>(null);
const [generating, setGenerating] = useState(false);
useEffect(() => {
let cancelled = false;
async function loadThumbnail() {
if (!file) {
setThumb(null);
return;
}
// First priority: use stored thumbnail
if (file.thumbnail) {
setThumb(file.thumbnail);
return;
}
2025-08-06 10:28:15 +01:00
// Second priority: generate thumbnail for any file type
if (file.size < 100 * 1024 * 1024 && !generating) {
setGenerating(true);
try {
2025-08-06 10:28:15 +01:00
let fileObject: File;
2025-08-05 14:39:27 +01:00
// Handle IndexedDB files vs regular File objects
if (file.storedInIndexedDB && file.id) {
2025-08-06 10:28:15 +01:00
// For IndexedDB files, recreate File object from stored data
2025-08-05 14:39:27 +01:00
const storedFile = await fileStorage.getFile(file.id);
if (!storedFile) {
throw new Error('File not found in IndexedDB');
}
2025-08-06 10:28:15 +01:00
fileObject = new File([storedFile.data], storedFile.name, {
type: storedFile.type,
lastModified: storedFile.lastModified
});
} else if (file.file) {
// For FileWithUrl objects that have a File object
fileObject = file.file;
2025-08-05 14:39:27 +01:00
} else if (file.id) {
// Fallback: try to get from IndexedDB even if storedInIndexedDB flag is missing
const storedFile = await fileStorage.getFile(file.id);
if (!storedFile) {
2025-08-06 10:28:15 +01:00
throw new Error('File not found in IndexedDB and no File object available');
2025-08-05 14:39:27 +01:00
}
2025-08-06 10:28:15 +01:00
fileObject = new File([storedFile.data], storedFile.name, {
type: storedFile.type,
lastModified: storedFile.lastModified
});
2025-08-05 14:39:27 +01:00
} else {
2025-08-06 10:28:15 +01:00
throw new Error('File object not available and no ID for IndexedDB lookup');
2025-08-05 14:39:27 +01:00
}
2025-08-06 10:28:15 +01:00
// Use the universal thumbnail generator
const thumbnail = await generateThumbnailForFile(fileObject);
if (!cancelled && thumbnail) {
setThumb(thumbnail);
} else if (!cancelled) {
setThumb(null);
}
} catch (error) {
2025-08-05 14:39:27 +01:00
console.warn('Failed to generate thumbnail for file', file.name, error);
if (!cancelled) setThumb(null);
} finally {
if (!cancelled) setGenerating(false);
}
} else {
2025-08-06 10:28:15 +01:00
// Large files - generate placeholder
setThumb(null);
}
}
loadThumbnail();
return () => { cancelled = true; };
}, [file, file?.thumbnail, file?.id]);
return { thumbnail: thumb, isGenerating: generating };
}