Remove more contexts

This commit is contained in:
2024-10-05 20:50:55 +02:00
parent 5ea932c96e
commit 9de434ff2e
19 changed files with 105 additions and 291 deletions

View File

@@ -1,38 +1,39 @@
// hooks/useFileNavigation.js
import { useCallback } from 'react';
import { useState, useCallback } from 'react';
import { useToasts } from '@geist-ui/core';
import { lookupFileByName } from '../services/api';
import { useFileSelection } from '../contexts/FileSelectionContext';
import { DEFAULT_FILE } from '../utils/constants'; // Assuming you have this constant defined
export const useFileNavigation = () => {
const { setToast } = useToasts();
const { handleFileSelect } = useFileSelection();
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
const [isNewFile, setIsNewFile] = useState(true);
const handleLinkClick = useCallback(
async (filename) => {
try {
const filePaths = await lookupFileByName(filename);
if (filePaths.length === 1) {
if (filePaths.length >= 1) {
handleFileSelect(filePaths[0]);
} else if (filePaths.length > 1) {
// Handle multiple file options (you may want to show a modal or dropdown)
setToast({
text: 'Multiple files found with the same name. Please specify the full path.',
type: 'warning',
});
} else {
setToast({ text: `File "${filename}" not found`, type: 'error' });
}
} catch (error) {
console.error('Error looking up file:', error);
setToast({
text: 'Failed to lookup file. Please try again.',
text: 'Failed to lookup file.',
type: 'error',
});
}
},
[handleFileSelect, setToast]
[handleFileSelect]
);
return { handleLinkClick };
const handleFileSelect = useCallback(async (filePath) => {
setSelectedFile(filePath);
setIsNewFile(filePath === DEFAULT_FILE.path);
}, []);
return { handleLinkClick, selectedFile, isNewFile, handleFileSelect };
};