mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-07 16:34:26 +00:00
Merge pull request #5 from LordMathis/fix/autocommit-on-delete
Fix/autocommit on delete
This commit is contained in:
@@ -190,11 +190,6 @@ func (fs *FileSystem) SaveFile(filePath string, content []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fs.Settings.Settings.GitAutoCommit && fs.GitRepo != nil {
|
|
||||||
message := strings.Replace(fs.Settings.Settings.GitCommitMsgTemplate, "${filename}", filePath, -1)
|
|
||||||
return fs.StageCommitAndPush(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
return os.WriteFile(fullPath, content, 0644)
|
return os.WriteFile(fullPath, content, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ import Header from './Header';
|
|||||||
import Sidebar from './Sidebar';
|
import Sidebar from './Sidebar';
|
||||||
import MainContent from './MainContent';
|
import MainContent from './MainContent';
|
||||||
import { useFileNavigation } from '../hooks/useFileNavigation';
|
import { useFileNavigation } from '../hooks/useFileNavigation';
|
||||||
|
import { useFileList } from '../hooks/useFileList';
|
||||||
|
|
||||||
const Layout = () => {
|
const Layout = () => {
|
||||||
const { selectedFile, handleFileSelect, handleLinkClick } =
|
const { selectedFile, handleFileSelect, handleLinkClick } =
|
||||||
useFileNavigation();
|
useFileNavigation();
|
||||||
|
const { files, loadFileList } = useFileList();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppShell header={{ height: 60 }} padding="md">
|
<AppShell header={{ height: 60 }} padding="md">
|
||||||
@@ -27,11 +29,14 @@ const Layout = () => {
|
|||||||
<Sidebar
|
<Sidebar
|
||||||
selectedFile={selectedFile}
|
selectedFile={selectedFile}
|
||||||
handleFileSelect={handleFileSelect}
|
handleFileSelect={handleFileSelect}
|
||||||
|
files={files}
|
||||||
|
loadFileList={loadFileList}
|
||||||
/>
|
/>
|
||||||
<MainContent
|
<MainContent
|
||||||
selectedFile={selectedFile}
|
selectedFile={selectedFile}
|
||||||
handleFileSelect={handleFileSelect}
|
handleFileSelect={handleFileSelect}
|
||||||
handleLinkClick={handleLinkClick}
|
handleLinkClick={handleLinkClick}
|
||||||
|
loadFileList={loadFileList}
|
||||||
/>
|
/>
|
||||||
</Container>
|
</Container>
|
||||||
</AppShell.Main>
|
</AppShell.Main>
|
||||||
|
|||||||
@@ -12,7 +12,12 @@ import { useFileOperations } from '../hooks/useFileOperations';
|
|||||||
import { useGitOperations } from '../hooks/useGitOperations';
|
import { useGitOperations } from '../hooks/useGitOperations';
|
||||||
import { useSettings } from '../contexts/SettingsContext';
|
import { useSettings } from '../contexts/SettingsContext';
|
||||||
|
|
||||||
const MainContent = ({ selectedFile, handleFileSelect, handleLinkClick }) => {
|
const MainContent = ({
|
||||||
|
selectedFile,
|
||||||
|
handleFileSelect,
|
||||||
|
handleLinkClick,
|
||||||
|
loadFileList,
|
||||||
|
}) => {
|
||||||
const [activeTab, setActiveTab] = useState('source');
|
const [activeTab, setActiveTab] = useState('source');
|
||||||
const { settings } = useSettings();
|
const { settings } = useSettings();
|
||||||
const {
|
const {
|
||||||
@@ -33,38 +38,32 @@ 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(
|
||||||
async (fileName) => {
|
async (fileName) => {
|
||||||
const success = await handleCreate(fileName);
|
const success = await handleCreate(fileName);
|
||||||
if (success) {
|
if (success) {
|
||||||
|
loadFileList();
|
||||||
handleFileSelect(fileName);
|
handleFileSelect(fileName);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[handleCreate, handleFileSelect]
|
[handleCreate, handleFileSelect, loadFileList]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleDeleteFile = useCallback(
|
const handleDeleteFile = useCallback(
|
||||||
async (filePath) => {
|
async (filePath) => {
|
||||||
const success = await handleDelete(filePath);
|
const success = await handleDelete(filePath);
|
||||||
if (success) {
|
if (success) {
|
||||||
|
loadFileList();
|
||||||
handleFileSelect(null);
|
handleFileSelect(null);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[handleDelete, handleFileSelect]
|
[handleDelete, handleFileSelect, loadFileList]
|
||||||
);
|
);
|
||||||
|
|
||||||
const renderBreadcrumbs = useMemo(() => {
|
const renderBreadcrumbs = useMemo(() => {
|
||||||
|
|||||||
@@ -2,18 +2,16 @@ import React, { useEffect } from 'react';
|
|||||||
import { Box } from '@mantine/core';
|
import { Box } from '@mantine/core';
|
||||||
import FileActions from './FileActions';
|
import FileActions from './FileActions';
|
||||||
import FileTree from './FileTree';
|
import FileTree from './FileTree';
|
||||||
import { useFileList } from '../hooks/useFileList';
|
|
||||||
import { useGitOperations } from '../hooks/useGitOperations';
|
import { useGitOperations } from '../hooks/useGitOperations';
|
||||||
import { useSettings } from '../contexts/SettingsContext';
|
import { useSettings } from '../contexts/SettingsContext';
|
||||||
|
|
||||||
const Sidebar = ({ selectedFile, handleFileSelect }) => {
|
const Sidebar = ({ selectedFile, handleFileSelect, files, loadFileList }) => {
|
||||||
const { settings } = useSettings();
|
const { settings } = useSettings();
|
||||||
const { files, loadFileList } = useFileList();
|
|
||||||
const { handlePull } = useGitOperations(settings.gitEnabled);
|
const { handlePull } = useGitOperations(settings.gitEnabled);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadFileList();
|
loadFileList();
|
||||||
}, [settings.gitEnabled, loadFileList]);
|
}, [loadFileList]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
|
|||||||
@@ -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',
|
||||||
}, []);
|
});
|
||||||
|
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',
|
||||||
|
});
|
||||||
|
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',
|
||||||
|
});
|
||||||
|
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 };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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