From a73296451eb73199ae3d675adc84135f258bd66d Mon Sep 17 00:00:00 2001 From: LordMathis Date: Wed, 30 Oct 2024 21:43:37 +0100 Subject: [PATCH] Load last opened file on frontend --- frontend/src/hooks/useFileNavigation.js | 35 +++++++++++++++++------ frontend/src/hooks/useLastOpenedFile.js | 37 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 frontend/src/hooks/useLastOpenedFile.js diff --git a/frontend/src/hooks/useFileNavigation.js b/frontend/src/hooks/useFileNavigation.js index 15928b7..0803e64 100644 --- a/frontend/src/hooks/useFileNavigation.js +++ b/frontend/src/hooks/useFileNavigation.js @@ -1,18 +1,28 @@ -import { useState, useCallback, useEffect } from 'react'; // Added useEffect +import { useState, useCallback, useEffect } from 'react'; import { notifications } from '@mantine/notifications'; import { lookupFileByName } from '../services/api'; import { DEFAULT_FILE } from '../utils/constants'; import { useWorkspace } from '../contexts/WorkspaceContext'; +import { useLastOpenedFile } from './useLastOpenedFile'; export const useFileNavigation = () => { const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path); const [isNewFile, setIsNewFile] = useState(true); const { currentWorkspace } = useWorkspace(); + const { loadLastOpenedFile, saveLastOpenedFile } = useLastOpenedFile(); - const handleFileSelect = useCallback((filePath) => { - setSelectedFile(filePath || DEFAULT_FILE.path); - setIsNewFile(filePath ? false : true); - }, []); + const handleFileSelect = useCallback( + async (filePath) => { + const newPath = filePath || DEFAULT_FILE.path; + setSelectedFile(newPath); + setIsNewFile(!filePath); + + if (filePath) { + await saveLastOpenedFile(filePath); + } + }, + [saveLastOpenedFile] + ); const handleLinkClick = useCallback( async (filename) => { @@ -41,10 +51,19 @@ export const useFileNavigation = () => { [currentWorkspace, handleFileSelect] ); - // Reset to default file when workspace changes + // Load last opened file when workspace changes useEffect(() => { - handleFileSelect(null); - }, [currentWorkspace, handleFileSelect]); + const initializeFile = async () => { + const lastFile = await loadLastOpenedFile(); + if (lastFile) { + handleFileSelect(lastFile); + } else { + handleFileSelect(null); + } + }; + + initializeFile(); + }, [currentWorkspace, loadLastOpenedFile, handleFileSelect]); return { handleLinkClick, selectedFile, isNewFile, handleFileSelect }; }; diff --git a/frontend/src/hooks/useLastOpenedFile.js b/frontend/src/hooks/useLastOpenedFile.js new file mode 100644 index 0000000..1844fd6 --- /dev/null +++ b/frontend/src/hooks/useLastOpenedFile.js @@ -0,0 +1,37 @@ +import { useCallback } from 'react'; +import { getLastOpenedFile, updateLastOpenedFile } from '../services/api'; +import { useWorkspace } from '../contexts/WorkspaceContext'; + +export const useLastOpenedFile = () => { + const { currentWorkspace } = useWorkspace(); + + const loadLastOpenedFile = useCallback(async () => { + if (!currentWorkspace) return null; + + try { + const response = await getLastOpenedFile(currentWorkspace.id); + return response.lastOpenedFilePath || null; + } catch (error) { + console.error('Failed to load last opened file:', error); + return null; + } + }, [currentWorkspace]); + + const saveLastOpenedFile = useCallback( + async (filePath) => { + if (!currentWorkspace) return; + + try { + await updateLastOpenedFile(currentWorkspace.id, filePath); + } catch (error) { + console.error('Failed to save last opened file:', error); + } + }, + [currentWorkspace] + ); + + return { + loadLastOpenedFile, + saveLastOpenedFile, + }; +};