mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Migrate useFileNavigation hook
This commit is contained in:
@@ -1,16 +1,22 @@
|
|||||||
import { useState, useCallback, useEffect } from 'react';
|
import { useState, useCallback, useEffect } from 'react';
|
||||||
import { DEFAULT_FILE } from '../utils/constants';
|
import { DEFAULT_FILE } from '../types/file';
|
||||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||||
import { useLastOpenedFile } from './useLastOpenedFile';
|
import { useLastOpenedFile } from './useLastOpenedFile';
|
||||||
|
|
||||||
export const useFileNavigation = () => {
|
interface UseFileNavigationResult {
|
||||||
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
|
selectedFile: string;
|
||||||
const [isNewFile, setIsNewFile] = useState(true);
|
isNewFile: boolean;
|
||||||
|
handleFileSelect: (filePath: string | null) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useFileNavigation = (): UseFileNavigationResult => {
|
||||||
|
const [selectedFile, setSelectedFile] = useState<string>(DEFAULT_FILE.path);
|
||||||
|
const [isNewFile, setIsNewFile] = useState<boolean>(true);
|
||||||
const { currentWorkspace } = useWorkspace();
|
const { currentWorkspace } = useWorkspace();
|
||||||
const { loadLastOpenedFile, saveLastOpenedFile } = useLastOpenedFile();
|
const { loadLastOpenedFile, saveLastOpenedFile } = useLastOpenedFile();
|
||||||
|
|
||||||
const handleFileSelect = useCallback(
|
const handleFileSelect = useCallback(
|
||||||
async (filePath) => {
|
async (filePath: string | null): Promise<void> => {
|
||||||
const newPath = filePath || DEFAULT_FILE.path;
|
const newPath = filePath || DEFAULT_FILE.path;
|
||||||
setSelectedFile(newPath);
|
setSelectedFile(newPath);
|
||||||
setIsNewFile(!filePath);
|
setIsNewFile(!filePath);
|
||||||
@@ -24,7 +30,7 @@ export const useFileNavigation = () => {
|
|||||||
|
|
||||||
// Load last opened file when workspace changes
|
// Load last opened file when workspace changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initializeFile = async () => {
|
const initializeFile = async (): Promise<void> => {
|
||||||
setSelectedFile(DEFAULT_FILE.path);
|
setSelectedFile(DEFAULT_FILE.path);
|
||||||
setIsNewFile(true);
|
setIsNewFile(true);
|
||||||
|
|
||||||
Reference in New Issue
Block a user