mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Enable autocommit for delete and create
This commit is contained in:
@@ -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(
|
||||||
|
|||||||
@@ -1,9 +1,32 @@
|
|||||||
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();
|
||||||
|
const { handleCommitAndPush } = useGitOperations(settings.gitEnabled);
|
||||||
|
|
||||||
|
const autoCommit = useCallback(
|
||||||
|
async (filePath, action) => {
|
||||||
|
if (settings.gitAutoCommit && settings.gitEnabled) {
|
||||||
|
let commitMessage = settings.gitCommitMsgTemplate
|
||||||
|
.replace('${filename}', filePath)
|
||||||
|
.replace('${action}', action);
|
||||||
|
|
||||||
|
// 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 {
|
try {
|
||||||
await saveFileContent(filePath, content);
|
await saveFileContent(filePath, content);
|
||||||
notifications.show({
|
notifications.show({
|
||||||
@@ -11,6 +34,7 @@ export const useFileOperations = () => {
|
|||||||
message: 'File saved successfully',
|
message: 'File saved successfully',
|
||||||
color: 'green',
|
color: 'green',
|
||||||
});
|
});
|
||||||
|
await autoCommit(filePath, 'update');
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving file:', error);
|
console.error('Error saving file:', error);
|
||||||
@@ -21,9 +45,12 @@ export const useFileOperations = () => {
|
|||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, []);
|
},
|
||||||
|
[autoCommit]
|
||||||
|
);
|
||||||
|
|
||||||
const handleDelete = useCallback(async (filePath) => {
|
const handleDelete = useCallback(
|
||||||
|
async (filePath) => {
|
||||||
try {
|
try {
|
||||||
await deleteFile(filePath);
|
await deleteFile(filePath);
|
||||||
notifications.show({
|
notifications.show({
|
||||||
@@ -31,6 +58,7 @@ export const useFileOperations = () => {
|
|||||||
message: 'File deleted successfully',
|
message: 'File deleted successfully',
|
||||||
color: 'green',
|
color: 'green',
|
||||||
});
|
});
|
||||||
|
await autoCommit(filePath, 'delete');
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting file:', error);
|
console.error('Error deleting file:', error);
|
||||||
@@ -41,9 +69,12 @@ export const useFileOperations = () => {
|
|||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, []);
|
},
|
||||||
|
[autoCommit]
|
||||||
|
);
|
||||||
|
|
||||||
const handleCreate = useCallback(async (fileName, initialContent = '') => {
|
const handleCreate = useCallback(
|
||||||
|
async (fileName, initialContent = '') => {
|
||||||
try {
|
try {
|
||||||
await saveFileContent(fileName, initialContent);
|
await saveFileContent(fileName, initialContent);
|
||||||
notifications.show({
|
notifications.show({
|
||||||
@@ -51,6 +82,7 @@ export const useFileOperations = () => {
|
|||||||
message: 'File created successfully',
|
message: 'File created successfully',
|
||||||
color: 'green',
|
color: 'green',
|
||||||
});
|
});
|
||||||
|
await autoCommit(fileName, 'create');
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating new file:', error);
|
console.error('Error creating new file:', error);
|
||||||
@@ -61,7 +93,9 @@ export const useFileOperations = () => {
|
|||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, []);
|
},
|
||||||
|
[autoCommit]
|
||||||
|
);
|
||||||
|
|
||||||
return { handleSave, handleDelete, handleCreate };
|
return { handleSave, handleDelete, handleCreate };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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 = {
|
||||||
|
|||||||
Reference in New Issue
Block a user