import { useCallback } from 'react'; import { notifications } from '@mantine/notifications'; import { saveFile, deleteFile } from '../api/file'; import { useWorkspaceData } from '../contexts/WorkspaceDataContext'; import { useGitOperations } from './useGitOperations'; import { FileAction } from '@/types/models'; interface UseFileOperationsResult { handleSave: (filePath: string, content: string) => Promise; handleDelete: (filePath: string) => Promise; handleCreate: (fileName: string, initialContent?: string) => Promise; } export const useFileOperations = (): UseFileOperationsResult => { const { currentWorkspace, settings } = useWorkspaceData(); const { handleCommitAndPush } = useGitOperations(); const autoCommit = useCallback( async (filePath: string, action: FileAction): Promise => { if (settings.gitAutoCommit && settings.gitEnabled) { let commitMessage = settings.gitCommitMsgTemplate .replace('${filename}', filePath) .replace('${action}', action); commitMessage = commitMessage.charAt(0).toUpperCase() + commitMessage.slice(1); await handleCommitAndPush(commitMessage); } }, [settings, handleCommitAndPush] ); const handleSave = useCallback( async (filePath: string, content: string): Promise => { if (!currentWorkspace) return false; try { await saveFile(currentWorkspace.name, filePath, content); notifications.show({ title: 'Success', message: 'File saved successfully', color: 'green', }); await autoCommit(filePath, FileAction.Update); return true; } catch (error) { console.error('Error saving file:', error); notifications.show({ title: 'Error', message: 'Failed to save file', color: 'red', }); return false; } }, [currentWorkspace, autoCommit] ); const handleDelete = useCallback( async (filePath: string): Promise => { if (!currentWorkspace) return false; try { await deleteFile(currentWorkspace.name, filePath); notifications.show({ title: 'Success', message: 'File deleted successfully', color: 'green', }); await autoCommit(filePath, FileAction.Delete); return true; } catch (error) { console.error('Error deleting file:', error); notifications.show({ title: 'Error', message: 'Failed to delete file', color: 'red', }); return false; } }, [currentWorkspace, autoCommit] ); const handleCreate = useCallback( async (fileName: string, initialContent: string = ''): Promise => { if (!currentWorkspace) return false; try { await saveFile(currentWorkspace.name, fileName, initialContent); notifications.show({ title: 'Success', message: 'File created successfully', color: 'green', }); await autoCommit(fileName, FileAction.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; } }, [currentWorkspace, autoCommit] ); return { handleSave, handleDelete, handleCreate }; };