Split up hooks

This commit is contained in:
2024-10-05 00:30:18 +02:00
parent 50b0fbb03c
commit 3691a59666
10 changed files with 237 additions and 145 deletions

View File

@@ -1,34 +1,47 @@
import { useFileList } from './useFileList';
import { useEffect, useCallback } from 'react';
import { useFileSelection } from './useFileSelection';
import { useFileContent } from './useFileContent';
import { useGitOperations } from './useGitOperations';
import { useFileOperations } from './useFileOperations';
export const useFileManagement = (gitEnabled) => {
const { files, error: fileListError, loadFileList } = useFileList(gitEnabled);
export const useFileManagement = () => {
const { selectedFile, isNewFile, handleFileSelect } = useFileSelection();
const {
content,
selectedFile,
isNewFile,
isLoading,
error,
hasUnsavedChanges,
error: fileContentError,
handleFileSelect,
loadFileContent,
handleContentChange,
handleSave,
setHasUnsavedChanges,
} = useFileContent();
const { pullLatestChanges, handleCommitAndPush } =
useGitOperations(gitEnabled);
const { handleSave, handleDelete, handleCreateNewFile } =
useFileOperations(setHasUnsavedChanges);
useEffect(() => {
if (selectedFile) {
loadFileContent(selectedFile);
}
}, [selectedFile, loadFileContent]);
const handleFileSelectAndLoad = useCallback(
async (filePath) => {
await handleFileSelect(filePath);
await loadFileContent(filePath);
},
[handleFileSelect, loadFileContent]
);
return {
files,
content,
selectedFile,
isNewFile,
content,
isLoading,
error,
hasUnsavedChanges,
error: fileListError || fileContentError,
handleFileSelect,
handleFileSelect: handleFileSelectAndLoad,
handleContentChange,
handleSave,
pullLatestChanges,
handleCommitAndPush,
loadFileList,
handleSave: (filePath) => handleSave(filePath, content),
handleDelete,
handleCreateNewFile,
};
};