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

@@ -5,13 +5,9 @@ import { DEFAULT_FILE } from '../utils/constants';
export const useFileContent = () => {
const [content, setContent] = useState(DEFAULT_FILE.content);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
const loadFileContent = useCallback(async (filePath) => {
setIsLoading(true);
setError(null);
try {
if (filePath === DEFAULT_FILE.path) {
setContent(DEFAULT_FILE.content);
@@ -23,10 +19,7 @@ export const useFileContent = () => {
}
setHasUnsavedChanges(false);
} catch (err) {
setError('Failed to load file content');
console.error(err);
} finally {
setIsLoading(false);
}
}, []);
@@ -38,9 +31,8 @@ export const useFileContent = () => {
return {
content,
setContent,
isLoading,
error,
hasUnsavedChanges,
setHasUnsavedChanges,
loadFileContent,
handleContentChange,
};

View File

@@ -3,7 +3,6 @@ import { fetchFileList } from '../services/api';
export const useFileList = (gitEnabled) => {
const [files, setFiles] = useState([]);
const [error, setError] = useState(null);
const loadFileList = useCallback(async () => {
try {
@@ -15,13 +14,12 @@ export const useFileList = (gitEnabled) => {
}
} catch (error) {
console.error('Failed to load file list:', error);
setError('Failed to load file list. Please try again later.');
}
}, []);
useEffect(() => {
loadFileList();
}, [loadFileList, gitEnabled]);
}, [gitEnabled]);
return { files, error, loadFileList };
return { files, loadFileList };
};

View File

@@ -1,40 +0,0 @@
import { useEffect, useCallback } from 'react';
import { useFileSelection } from './useFileSelection';
import { useFileContent } from './useFileContent';
export const useFileManagement = () => {
const { selectedFile, isNewFile, handleFileSelect } = useFileSelection();
const {
content,
isLoading,
error,
hasUnsavedChanges,
loadFileContent,
handleContentChange,
} = useFileContent();
useEffect(() => {
if (selectedFile) {
loadFileContent(selectedFile);
}
}, [selectedFile, loadFileContent]);
const handleFileSelectAndLoad = useCallback(
async (filePath) => {
await handleFileSelect(filePath);
await loadFileContent(filePath);
},
[handleFileSelect, loadFileContent]
);
return {
selectedFile,
isNewFile,
content,
isLoading,
error,
hasUnsavedChanges,
handleFileSelect: handleFileSelectAndLoad,
handleContentChange,
};
};

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 };
};

View File

@@ -5,20 +5,17 @@ import { useToasts } from '@geist-ui/core';
export const useFileOperations = (setHasUnsavedChanges) => {
const { setToast } = useToasts();
const handleSave = useCallback(
async (filePath, content) => {
try {
await saveFileContent(filePath, content);
setHasUnsavedChanges(false);
setToast({ text: 'File saved successfully', type: 'success' });
return true;
} catch (error) {
console.error('Error saving file:', error);
return false;
}
},
[setHasUnsavedChanges]
);
const handleSave = useCallback(async (filePath, content) => {
try {
await saveFileContent(filePath, content);
setHasUnsavedChanges(false);
setToast({ text: 'File saved successfully', type: 'success' });
return true;
} catch (error) {
console.error('Error saving file:', error);
return false;
}
}, []);
const handleDelete = useCallback(async (filePath) => {
try {

View File

@@ -1,14 +0,0 @@
import { useState, useCallback } from 'react';
import { DEFAULT_FILE } from '../utils/constants'; // Assuming you have this constant defined
export const useFileSelection = () => {
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
const [isNewFile, setIsNewFile] = useState(true);
const handleFileSelect = useCallback(async (filePath) => {
setSelectedFile(filePath);
setIsNewFile(filePath === DEFAULT_FILE.path);
}, []);
return { selectedFile, isNewFile, handleFileSelect };
};

View File

@@ -2,13 +2,15 @@ import { useCallback } from 'react';
import { pullChanges, commitAndPush } from '../services/api';
export const useGitOperations = (gitEnabled) => {
const pullLatestChanges = useCallback(async () => {
const handlePull = useCallback(async () => {
if (!gitEnabled) return false;
try {
await pullChanges();
setToast({ text: 'Successfully pulled latest changes', type: 'success' });
return true;
} catch (error) {
console.error('Failed to pull latest changes:', error);
setToast({ text: 'Failed to pull latest changes', type: 'error' });
return false;
}
}, [gitEnabled]);
@@ -18,14 +20,19 @@ export const useGitOperations = (gitEnabled) => {
if (!gitEnabled) return false;
try {
await commitAndPush(message);
setToast({
text: 'Successfully committed and pushed changes',
type: 'success',
});
return true;
} catch (error) {
console.error('Failed to commit and push changes:', error);
setToast({ text: 'Failed to commit and push changes', type: 'error' });
return false;
}
},
[gitEnabled]
);
return { pullLatestChanges, handleCommitAndPush };
return { handlePull, handleCommitAndPush };
};