From 1341881968e26098024f2b7cc784882f1e66ff92 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 13 Oct 2024 15:02:31 +0200 Subject: [PATCH] Enable autocommit for delete and create --- frontend/src/components/MainContent.js | 10 +- frontend/src/hooks/useFileOperations.js | 148 +++++++++++++++--------- frontend/src/utils/constants.js | 2 +- 3 files changed, 93 insertions(+), 67 deletions(-) diff --git a/frontend/src/components/MainContent.js b/frontend/src/components/MainContent.js index 2717ede..08d71ef 100644 --- a/frontend/src/components/MainContent.js +++ b/frontend/src/components/MainContent.js @@ -33,18 +33,10 @@ const MainContent = ({ selectedFile, handleFileSelect, handleLinkClick }) => { let success = await handleSave(filePath, content); if (success) { setHasUnsavedChanges(false); - - if (settings.gitAutoCommit && settings.gitEnabled) { - const commitMessage = settings.gitCommitMsgTemplate.replace( - '${filename}', - filePath - ); - success = await handleCommitAndPush(commitMessage); - } } return success; }, - [handleSave, setHasUnsavedChanges, settings, handleCommitAndPush] + [handleSave, setHasUnsavedChanges] ); const handleCreateFile = useCallback( diff --git a/frontend/src/hooks/useFileOperations.js b/frontend/src/hooks/useFileOperations.js index 1e8fd3d..f4dda7e 100644 --- a/frontend/src/hooks/useFileOperations.js +++ b/frontend/src/hooks/useFileOperations.js @@ -1,67 +1,101 @@ import { useCallback } from 'react'; import { notifications } from '@mantine/notifications'; import { saveFileContent, deleteFile } from '../services/api'; +import { useSettings } from '../contexts/SettingsContext'; +import { useGitOperations } from './useGitOperations'; export const useFileOperations = () => { - const handleSave = useCallback(async (filePath, content) => { - try { - await saveFileContent(filePath, content); - notifications.show({ - title: 'Success', - message: 'File saved successfully', - color: 'green', - }); - return true; - } catch (error) { - console.error('Error saving file:', error); - notifications.show({ - title: 'Error', - message: 'Failed to save file', - color: 'red', - }); - return false; - } - }, []); + const { settings } = useSettings(); + const { handleCommitAndPush } = useGitOperations(settings.gitEnabled); - const handleDelete = useCallback(async (filePath) => { - try { - await deleteFile(filePath); - notifications.show({ - title: 'Success', - message: 'File deleted successfully', - color: 'green', - }); - return true; - } catch (error) { - console.error('Error deleting file:', error); - notifications.show({ - title: 'Error', - message: 'Failed to delete file', - color: 'red', - }); - return false; - } - }, []); + const autoCommit = useCallback( + async (filePath, action) => { + if (settings.gitAutoCommit && settings.gitEnabled) { + let commitMessage = settings.gitCommitMsgTemplate + .replace('${filename}', filePath) + .replace('${action}', action); - const handleCreate = useCallback(async (fileName, initialContent = '') => { - try { - await saveFileContent(fileName, initialContent); - notifications.show({ - title: 'Success', - message: 'File created successfully', - color: 'green', - }); - return true; - } catch (error) { - console.error('Error creating new file:', error); - notifications.show({ - title: 'Error', - message: 'Failed to create new file', - color: 'red', - }); - return false; - } - }, []); + // Capitalize the first letter of the commit message + commitMessage = + commitMessage.charAt(0).toUpperCase() + commitMessage.slice(1); + + await handleCommitAndPush(commitMessage); + } + }, + [settings, handleCommitAndPush] + ); + + const handleSave = useCallback( + async (filePath, content) => { + try { + await saveFileContent(filePath, content); + notifications.show({ + title: 'Success', + message: 'File saved successfully', + color: 'green', + }); + await autoCommit(filePath, 'update'); + return true; + } catch (error) { + console.error('Error saving file:', error); + notifications.show({ + title: 'Error', + message: 'Failed to save file', + color: 'red', + }); + return false; + } + }, + [autoCommit] + ); + + const handleDelete = useCallback( + async (filePath) => { + try { + await deleteFile(filePath); + notifications.show({ + title: 'Success', + message: 'File deleted successfully', + color: 'green', + }); + await autoCommit(filePath, 'delete'); + return true; + } catch (error) { + console.error('Error deleting file:', error); + notifications.show({ + title: 'Error', + message: 'Failed to delete file', + color: 'red', + }); + return false; + } + }, + [autoCommit] + ); + + const handleCreate = useCallback( + async (fileName, initialContent = '') => { + try { + await saveFileContent(fileName, initialContent); + notifications.show({ + title: 'Success', + message: 'File created successfully', + color: 'green', + }); + await autoCommit(fileName, 'create'); + return true; + } catch (error) { + console.error('Error creating new file:', error); + notifications.show({ + title: 'Error', + message: 'Failed to create new file', + color: 'red', + }); + return false; + } + }, + [autoCommit] + ); return { handleSave, handleDelete, handleCreate }; }; diff --git a/frontend/src/utils/constants.js b/frontend/src/utils/constants.js index 6b7f0a4..c4971b0 100644 --- a/frontend/src/utils/constants.js +++ b/frontend/src/utils/constants.js @@ -34,7 +34,7 @@ export const DEFAULT_SETTINGS = { gitUser: '', gitToken: '', gitAutoCommit: false, - gitCommitMsgTemplate: 'Update ${filename}', + gitCommitMsgTemplate: '${action} ${filename}', }; export const DEFAULT_FILE = {