mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Load last opened file on frontend
This commit is contained in:
@@ -1,18 +1,28 @@
|
|||||||
import { useState, useCallback, useEffect } from 'react'; // Added useEffect
|
import { useState, useCallback, useEffect } from 'react';
|
||||||
import { notifications } from '@mantine/notifications';
|
import { notifications } from '@mantine/notifications';
|
||||||
import { lookupFileByName } from '../services/api';
|
import { lookupFileByName } from '../services/api';
|
||||||
import { DEFAULT_FILE } from '../utils/constants';
|
import { DEFAULT_FILE } from '../utils/constants';
|
||||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||||
|
import { useLastOpenedFile } from './useLastOpenedFile';
|
||||||
|
|
||||||
export const useFileNavigation = () => {
|
export const useFileNavigation = () => {
|
||||||
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
|
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
|
||||||
const [isNewFile, setIsNewFile] = useState(true);
|
const [isNewFile, setIsNewFile] = useState(true);
|
||||||
const { currentWorkspace } = useWorkspace();
|
const { currentWorkspace } = useWorkspace();
|
||||||
|
const { loadLastOpenedFile, saveLastOpenedFile } = useLastOpenedFile();
|
||||||
|
|
||||||
const handleFileSelect = useCallback((filePath) => {
|
const handleFileSelect = useCallback(
|
||||||
setSelectedFile(filePath || DEFAULT_FILE.path);
|
async (filePath) => {
|
||||||
setIsNewFile(filePath ? false : true);
|
const newPath = filePath || DEFAULT_FILE.path;
|
||||||
}, []);
|
setSelectedFile(newPath);
|
||||||
|
setIsNewFile(!filePath);
|
||||||
|
|
||||||
|
if (filePath) {
|
||||||
|
await saveLastOpenedFile(filePath);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[saveLastOpenedFile]
|
||||||
|
);
|
||||||
|
|
||||||
const handleLinkClick = useCallback(
|
const handleLinkClick = useCallback(
|
||||||
async (filename) => {
|
async (filename) => {
|
||||||
@@ -41,10 +51,19 @@ export const useFileNavigation = () => {
|
|||||||
[currentWorkspace, handleFileSelect]
|
[currentWorkspace, handleFileSelect]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Reset to default file when workspace changes
|
// Load last opened file when workspace changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
handleFileSelect(null);
|
const initializeFile = async () => {
|
||||||
}, [currentWorkspace, handleFileSelect]);
|
const lastFile = await loadLastOpenedFile();
|
||||||
|
if (lastFile) {
|
||||||
|
handleFileSelect(lastFile);
|
||||||
|
} else {
|
||||||
|
handleFileSelect(null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
initializeFile();
|
||||||
|
}, [currentWorkspace, loadLastOpenedFile, handleFileSelect]);
|
||||||
|
|
||||||
return { handleLinkClick, selectedFile, isNewFile, handleFileSelect };
|
return { handleLinkClick, selectedFile, isNewFile, handleFileSelect };
|
||||||
};
|
};
|
||||||
|
|||||||
37
frontend/src/hooks/useLastOpenedFile.js
Normal file
37
frontend/src/hooks/useLastOpenedFile.js
Normal file
@@ -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,
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user