mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
# Description of Changes Locking to just having one instance of the app running unifies the experience across all OSs. Opening new files in Stirling will cause the files to be opened in the existing window rather than spawning a new instance of the app with just that file in the new instance. There's much more to explore here to allow multiple windows open at once, but that can be done all from one instance of the app, and will likely make it easier to allow movement of files etc. across different windows. Also fixes extra newlines in the logs and directly builds to `.app` on Mac because it's frustrating during development to have to repeatedly mount & unmount the `.dmg`.
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { useState, useEffect } from 'react';
|
||
import { fileOpenService } from '@app/services/fileOpenService';
|
||
|
||
export function useOpenedFile() {
|
||
const [openedFilePaths, setOpenedFilePaths] = useState<string[]>([]);
|
||
const [loading, setLoading] = useState(true);
|
||
|
||
useEffect(() => {
|
||
const checkForOpenedFile = async () => {
|
||
console.log('🔍 Checking for opened file(s)...');
|
||
try {
|
||
const filePaths = await fileOpenService.getOpenedFiles();
|
||
console.log('🔍 fileOpenService.getOpenedFiles() returned:', filePaths);
|
||
|
||
if (filePaths.length > 0) {
|
||
console.log(`✅ App opened with ${filePaths.length} file(s):`, filePaths);
|
||
setOpenedFilePaths(filePaths);
|
||
|
||
// Clear the files from service state after consuming them
|
||
await fileOpenService.clearOpenedFiles();
|
||
} else {
|
||
console.log('ℹ️ No files were opened with the app');
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('❌ Failed to check for opened files:', error);
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
checkForOpenedFile();
|
||
|
||
// Listen for runtime file open events (abstracted through service)
|
||
const unlistenRuntimeEvents = fileOpenService.onFileOpened((filePath: string) => {
|
||
console.log('📂 Runtime file open event:', filePath);
|
||
setOpenedFilePaths(prev => [...prev, filePath]);
|
||
});
|
||
|
||
// Cleanup function
|
||
return () => {
|
||
unlistenRuntimeEvents();
|
||
};
|
||
}, []);
|
||
|
||
return { openedFilePaths, loading };
|
||
}
|