Tidy Tauri code and enable "Open file in Stirling PDF" (#4836)

# Description of Changes
Tidy Tauri code and enable "Open file in Stirling PDF"
This commit is contained in:
James Brunton
2025-11-10 12:15:39 +00:00
committed by GitHub
parent f4543d26cd
commit ebf4bab80b
15 changed files with 241 additions and 1102 deletions
@@ -0,0 +1,47 @@
import { useEffect } from 'react';
import { useBackendInitializer } from '@app/hooks/useBackendInitializer';
import { useOpenedFile } from '@app/hooks/useOpenedFile';
import { fileOpenService } from '@app/services/fileOpenService';
import { useFileManagement } from '@app/contexts/file/fileHooks';
/**
* App initialization hook
* Desktop version: Handles Tauri-specific initialization
* - Starts the backend on app startup
* - Handles files opened with the app (adds directly to FileContext)
*/
export function useAppInitialization(): void {
// Initialize backend on app startup
useBackendInitializer();
// Get file management actions
const { addFiles } = useFileManagement();
// Handle file opened with app (Tauri mode)
const { openedFilePath, loading: openedFileLoading } = useOpenedFile();
// Load opened file and add directly to FileContext
useEffect(() => {
if (openedFilePath && !openedFileLoading) {
const loadOpenedFile = async () => {
try {
const fileData = await fileOpenService.readFileAsArrayBuffer(openedFilePath);
if (fileData) {
// Create a File object from the ArrayBuffer
const file = new File([fileData.arrayBuffer], fileData.fileName, {
type: 'application/pdf'
});
// Add directly to FileContext
await addFiles([file]);
console.log('[Desktop] Opened file added to FileContext:', fileData.fileName);
}
} catch (error) {
console.error('[Desktop] Failed to load opened file:', error);
}
};
loadOpenedFile();
}
}, [openedFilePath, openedFileLoading, addFiles]);
}