mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Migrate useFileOperations
This commit is contained in:
@@ -1,15 +1,22 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { notifications } from '@mantine/notifications';
|
import { notifications } from '@mantine/notifications';
|
||||||
import { saveFileContent, deleteFile } from '../api/git';
|
import { saveFile, deleteFile } from '../api/file';
|
||||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||||
import { useGitOperations } from './useGitOperations';
|
import { useGitOperations } from './useGitOperations';
|
||||||
|
import { FileAction } from '../types/file';
|
||||||
|
|
||||||
export const useFileOperations = () => {
|
interface UseFileOperationsResult {
|
||||||
|
handleSave: (filePath: string, content: string) => Promise<boolean>;
|
||||||
|
handleDelete: (filePath: string) => Promise<boolean>;
|
||||||
|
handleCreate: (fileName: string, initialContent?: string) => Promise<boolean>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useFileOperations = (): UseFileOperationsResult => {
|
||||||
const { currentWorkspace, settings } = useWorkspace();
|
const { currentWorkspace, settings } = useWorkspace();
|
||||||
const { handleCommitAndPush } = useGitOperations();
|
const { handleCommitAndPush } = useGitOperations();
|
||||||
|
|
||||||
const autoCommit = useCallback(
|
const autoCommit = useCallback(
|
||||||
async (filePath, action) => {
|
async (filePath: string, action: FileAction): Promise<void> => {
|
||||||
if (settings.gitAutoCommit && settings.gitEnabled) {
|
if (settings.gitAutoCommit && settings.gitEnabled) {
|
||||||
let commitMessage = settings.gitCommitMsgTemplate
|
let commitMessage = settings.gitCommitMsgTemplate
|
||||||
.replace('${filename}', filePath)
|
.replace('${filename}', filePath)
|
||||||
@@ -25,17 +32,17 @@ export const useFileOperations = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const handleSave = useCallback(
|
const handleSave = useCallback(
|
||||||
async (filePath, content) => {
|
async (filePath: string, content: string): Promise<boolean> => {
|
||||||
if (!currentWorkspace) return false;
|
if (!currentWorkspace) return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await saveFileContent(currentWorkspace.name, filePath, content);
|
await saveFile(currentWorkspace.name, filePath, content);
|
||||||
notifications.show({
|
notifications.show({
|
||||||
title: 'Success',
|
title: 'Success',
|
||||||
message: 'File saved successfully',
|
message: 'File saved successfully',
|
||||||
color: 'green',
|
color: 'green',
|
||||||
});
|
});
|
||||||
autoCommit(filePath, 'update');
|
await autoCommit(filePath, FileAction.Update);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving file:', error);
|
console.error('Error saving file:', error);
|
||||||
@@ -51,7 +58,7 @@ export const useFileOperations = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const handleDelete = useCallback(
|
const handleDelete = useCallback(
|
||||||
async (filePath) => {
|
async (filePath: string): Promise<boolean> => {
|
||||||
if (!currentWorkspace) return false;
|
if (!currentWorkspace) return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -61,7 +68,7 @@ export const useFileOperations = () => {
|
|||||||
message: 'File deleted successfully',
|
message: 'File deleted successfully',
|
||||||
color: 'green',
|
color: 'green',
|
||||||
});
|
});
|
||||||
autoCommit(filePath, 'delete');
|
await autoCommit(filePath, FileAction.Delete);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error deleting file:', error);
|
console.error('Error deleting file:', error);
|
||||||
@@ -77,17 +84,17 @@ export const useFileOperations = () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const handleCreate = useCallback(
|
const handleCreate = useCallback(
|
||||||
async (fileName, initialContent = '') => {
|
async (fileName: string, initialContent: string = ''): Promise<boolean> => {
|
||||||
if (!currentWorkspace) return false;
|
if (!currentWorkspace) return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await saveFileContent(currentWorkspace.name, fileName, initialContent);
|
await saveFile(currentWorkspace.name, fileName, initialContent);
|
||||||
notifications.show({
|
notifications.show({
|
||||||
title: 'Success',
|
title: 'Success',
|
||||||
message: 'File created successfully',
|
message: 'File created successfully',
|
||||||
color: 'green',
|
color: 'green',
|
||||||
});
|
});
|
||||||
autoCommit(fileName, 'create');
|
await autoCommit(fileName, FileAction.Create);
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating new file:', error);
|
console.error('Error creating new file:', error);
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export enum FileAction {
|
export enum FileAction {
|
||||||
Create = 'create',
|
Create = 'create',
|
||||||
|
Update = 'update',
|
||||||
Delete = 'delete',
|
Delete = 'delete',
|
||||||
Rename = 'rename',
|
Rename = 'rename',
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user