Enable autocommit for delete and create

This commit is contained in:
2024-10-13 15:02:31 +02:00
parent 593a01db2e
commit 1341881968
3 changed files with 93 additions and 67 deletions

View File

@@ -33,18 +33,10 @@ const MainContent = ({ selectedFile, handleFileSelect, handleLinkClick }) => {
let success = await handleSave(filePath, content); let success = await handleSave(filePath, content);
if (success) { if (success) {
setHasUnsavedChanges(false); setHasUnsavedChanges(false);
if (settings.gitAutoCommit && settings.gitEnabled) {
const commitMessage = settings.gitCommitMsgTemplate.replace(
'${filename}',
filePath
);
success = await handleCommitAndPush(commitMessage);
}
} }
return success; return success;
}, },
[handleSave, setHasUnsavedChanges, settings, handleCommitAndPush] [handleSave, setHasUnsavedChanges]
); );
const handleCreateFile = useCallback( const handleCreateFile = useCallback(

View File

@@ -1,67 +1,101 @@
import { useCallback } from 'react'; import { useCallback } from 'react';
import { notifications } from '@mantine/notifications'; import { notifications } from '@mantine/notifications';
import { saveFileContent, deleteFile } from '../services/api'; import { saveFileContent, deleteFile } from '../services/api';
import { useSettings } from '../contexts/SettingsContext';
import { useGitOperations } from './useGitOperations';
export const useFileOperations = () => { export const useFileOperations = () => {
const handleSave = useCallback(async (filePath, content) => { const { settings } = useSettings();
try { const { handleCommitAndPush } = useGitOperations(settings.gitEnabled);
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 handleDelete = useCallback(async (filePath) => { const autoCommit = useCallback(
try { async (filePath, action) => {
await deleteFile(filePath); if (settings.gitAutoCommit && settings.gitEnabled) {
notifications.show({ let commitMessage = settings.gitCommitMsgTemplate
title: 'Success', .replace('${filename}', filePath)
message: 'File deleted successfully', .replace('${action}', action);
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 handleCreate = useCallback(async (fileName, initialContent = '') => { // Capitalize the first letter of the commit message
try { commitMessage =
await saveFileContent(fileName, initialContent); commitMessage.charAt(0).toUpperCase() + commitMessage.slice(1);
notifications.show({
title: 'Success', await handleCommitAndPush(commitMessage);
message: 'File created successfully', }
color: 'green', },
}); [settings, handleCommitAndPush]
return true; );
} catch (error) {
console.error('Error creating new file:', error); const handleSave = useCallback(
notifications.show({ async (filePath, content) => {
title: 'Error', try {
message: 'Failed to create new file', await saveFileContent(filePath, content);
color: 'red', notifications.show({
}); title: 'Success',
return false; 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 }; return { handleSave, handleDelete, handleCreate };
}; };

View File

@@ -34,7 +34,7 @@ export const DEFAULT_SETTINGS = {
gitUser: '', gitUser: '',
gitToken: '', gitToken: '',
gitAutoCommit: false, gitAutoCommit: false,
gitCommitMsgTemplate: 'Update ${filename}', gitCommitMsgTemplate: '${action} ${filename}',
}; };
export const DEFAULT_FILE = { export const DEFAULT_FILE = {