mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Migrate useGitOperations
This commit is contained in:
65
app/src/hooks/useGitOperations.ts
Normal file
65
app/src/hooks/useGitOperations.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { useCallback } from 'react';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import { pullChanges, commitAndPush } from '../api/git';
|
||||
import { useWorkspace } from '../contexts/WorkspaceContext';
|
||||
|
||||
interface UseGitOperationsResult {
|
||||
handlePull: () => Promise<boolean>;
|
||||
handleCommitAndPush: (message: string) => Promise<boolean>;
|
||||
}
|
||||
|
||||
export const useGitOperations = (): UseGitOperationsResult => {
|
||||
const { currentWorkspace, settings } = useWorkspace();
|
||||
|
||||
const handlePull = useCallback(async (): Promise<boolean> => {
|
||||
if (!currentWorkspace || !settings.gitEnabled) return false;
|
||||
|
||||
try {
|
||||
await pullChanges(currentWorkspace.name);
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
message: 'Successfully pulled latest changes',
|
||||
color: 'green',
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Failed to pull latest changes:', error);
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: 'Failed to pull latest changes',
|
||||
color: 'red',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}, [currentWorkspace, settings.gitEnabled]);
|
||||
|
||||
const handleCommitAndPush = useCallback(
|
||||
async (message: string): Promise<boolean> => {
|
||||
if (!currentWorkspace || !settings.gitEnabled) return false;
|
||||
|
||||
try {
|
||||
const commitHash: CommitHash = await commitAndPush(
|
||||
currentWorkspace.name,
|
||||
message
|
||||
);
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
message: 'Successfully committed and pushed changes',
|
||||
color: 'green',
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Failed to commit and push changes:', error);
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: 'Failed to commit and push changes',
|
||||
color: 'red',
|
||||
});
|
||||
return false;
|
||||
}
|
||||
},
|
||||
[currentWorkspace, settings.gitEnabled]
|
||||
);
|
||||
|
||||
return { handlePull, handleCommitAndPush };
|
||||
};
|
||||
Reference in New Issue
Block a user