mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-26 14:19:24 +00:00
Fix loop
This commit is contained in:
parent
0f40f0187b
commit
50f4ccd4d5
@ -74,18 +74,7 @@ function FileContextInner({
|
|||||||
|
|
||||||
// File operations using unified addFiles helper with persistence
|
// File operations using unified addFiles helper with persistence
|
||||||
const addRawFiles = useCallback(async (files: File[]): Promise<File[]> => {
|
const addRawFiles = useCallback(async (files: File[]): Promise<File[]> => {
|
||||||
// Get IndexedDB metadata for comprehensive deduplication
|
const addedFilesWithIds = await addFiles('raw', { files }, stateRef, filesRef, dispatch);
|
||||||
let indexedDBMetadata: Array<{ name: string; size: number; lastModified: number }> | undefined;
|
|
||||||
if (indexedDB && enablePersistence) {
|
|
||||||
try {
|
|
||||||
const metadata = await indexedDB.loadAllMetadata();
|
|
||||||
indexedDBMetadata = metadata.map(m => ({ name: m.name, size: m.size, lastModified: m.lastModified }));
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Failed to load IndexedDB metadata for deduplication:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const addedFilesWithIds = await addFiles('raw', { files }, stateRef, filesRef, dispatch, indexedDBMetadata);
|
|
||||||
|
|
||||||
// Persist to IndexedDB if enabled - pass existing thumbnail to prevent double generation
|
// Persist to IndexedDB if enabled - pass existing thumbnail to prevent double generation
|
||||||
if (indexedDB && enablePersistence && addedFilesWithIds.length > 0) {
|
if (indexedDB && enablePersistence && addedFilesWithIds.length > 0) {
|
||||||
@ -193,12 +182,37 @@ function FileContextInner({
|
|||||||
trackPdfDocument: lifecycleManager.trackPdfDocument,
|
trackPdfDocument: lifecycleManager.trackPdfDocument,
|
||||||
cleanupFile: (fileId: string) => lifecycleManager.cleanupFile(fileId, stateRef),
|
cleanupFile: (fileId: string) => lifecycleManager.cleanupFile(fileId, stateRef),
|
||||||
scheduleCleanup: (fileId: string, delay?: number) =>
|
scheduleCleanup: (fileId: string, delay?: number) =>
|
||||||
lifecycleManager.scheduleCleanup(fileId, delay, stateRef),
|
lifecycleManager.scheduleCleanup(fileId, delay, stateRef)
|
||||||
|
}), [
|
||||||
|
baseActions,
|
||||||
|
addRawFiles,
|
||||||
|
addProcessedFiles,
|
||||||
|
addStoredFiles,
|
||||||
|
lifecycleManager,
|
||||||
|
setHasUnsavedChanges,
|
||||||
|
consumeFilesWrapper,
|
||||||
|
pinFileWrapper,
|
||||||
|
unpinFileWrapper,
|
||||||
|
indexedDB,
|
||||||
|
enablePersistence
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Split context values to minimize re-renders
|
||||||
|
const stateValue = useMemo<FileContextStateValue>(() => ({
|
||||||
|
state,
|
||||||
|
selectors
|
||||||
|
}), [state, selectors]);
|
||||||
|
|
||||||
|
const actionsValue = useMemo<FileContextActionsValue>(() => ({
|
||||||
|
actions,
|
||||||
|
dispatch
|
||||||
|
}), [actions]);
|
||||||
|
|
||||||
|
// Load files from persistence on mount
|
||||||
|
useEffect(() => {
|
||||||
|
if (!enablePersistence || !indexedDB) return;
|
||||||
|
|
||||||
// Persistence operations - load file list from IndexedDB
|
const loadFromPersistence = async () => {
|
||||||
loadFromPersistence: async () => {
|
|
||||||
if (!indexedDB || !enablePersistence) return;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Load metadata to populate file list (actual File objects loaded on-demand)
|
// Load metadata to populate file list (actual File objects loaded on-demand)
|
||||||
const metadata = await indexedDB.loadAllMetadata();
|
const metadata = await indexedDB.loadAllMetadata();
|
||||||
@ -232,38 +246,10 @@ function FileContextInner({
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to load files from persistence:', error);
|
console.error('Failed to load files from persistence:', error);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}), [
|
|
||||||
baseActions,
|
loadFromPersistence();
|
||||||
addRawFiles,
|
}, [enablePersistence, indexedDB]); // Only run when these change
|
||||||
addProcessedFiles,
|
|
||||||
addStoredFiles,
|
|
||||||
lifecycleManager,
|
|
||||||
setHasUnsavedChanges,
|
|
||||||
consumeFilesWrapper,
|
|
||||||
pinFileWrapper,
|
|
||||||
unpinFileWrapper,
|
|
||||||
indexedDB,
|
|
||||||
enablePersistence
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Split context values to minimize re-renders
|
|
||||||
const stateValue = useMemo<FileContextStateValue>(() => ({
|
|
||||||
state,
|
|
||||||
selectors
|
|
||||||
}), [state, selectors]);
|
|
||||||
|
|
||||||
const actionsValue = useMemo<FileContextActionsValue>(() => ({
|
|
||||||
actions,
|
|
||||||
dispatch
|
|
||||||
}), [actions]);
|
|
||||||
|
|
||||||
// Load files from persistence on mount
|
|
||||||
useEffect(() => {
|
|
||||||
if (enablePersistence && indexedDB) {
|
|
||||||
actions.loadFromPersistence();
|
|
||||||
}
|
|
||||||
}, [enablePersistence, indexedDB]); // Only run once on mount
|
|
||||||
|
|
||||||
// Cleanup on unmount
|
// Cleanup on unmount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -59,8 +59,7 @@ export async function addFiles(
|
|||||||
options: AddFileOptions,
|
options: AddFileOptions,
|
||||||
stateRef: React.MutableRefObject<FileContextState>,
|
stateRef: React.MutableRefObject<FileContextState>,
|
||||||
filesRef: React.MutableRefObject<Map<FileId, File>>,
|
filesRef: React.MutableRefObject<Map<FileId, File>>,
|
||||||
dispatch: React.Dispatch<FileContextAction>,
|
dispatch: React.Dispatch<FileContextAction>
|
||||||
indexedDBMetadata?: Array<{ name: string; size: number; lastModified: number }>
|
|
||||||
): Promise<Array<{ file: File; id: FileId; thumbnail?: string }>> {
|
): Promise<Array<{ file: File; id: FileId; thumbnail?: string }>> {
|
||||||
const fileRecords: FileRecord[] = [];
|
const fileRecords: FileRecord[] = [];
|
||||||
const addedFiles: Array<{ file: File; id: FileId; thumbnail?: string }> = [];
|
const addedFiles: Array<{ file: File; id: FileId; thumbnail?: string }> = [];
|
||||||
@ -68,12 +67,6 @@ export async function addFiles(
|
|||||||
// Build quickKey lookup from existing files for deduplication
|
// Build quickKey lookup from existing files for deduplication
|
||||||
const existingQuickKeys = buildQuickKeySet(stateRef.current.files.byId);
|
const existingQuickKeys = buildQuickKeySet(stateRef.current.files.byId);
|
||||||
|
|
||||||
// Add IndexedDB quickKeys if metadata provided for comprehensive deduplication
|
|
||||||
if (indexedDBMetadata) {
|
|
||||||
const indexedDBQuickKeys = buildQuickKeySetFromMetadata(indexedDBMetadata);
|
|
||||||
indexedDBQuickKeys.forEach(key => existingQuickKeys.add(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case 'raw': {
|
case 'raw': {
|
||||||
const { files = [] } = options;
|
const { files = [] } = options;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user