mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
180 lines
5.9 KiB
TypeScript
180 lines
5.9 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { gitFetch, gitPull, gitPush, gitCommit, gitStageFiles, gitUnstageFiles, gitDiscardFiles, fetchGitLog, fetchGitDiff, gitReset, getApiErrorMessage, fetchGitStatus } from '@/api/git'
|
|
import { createBranch, switchBranch } from '@/api/repos'
|
|
import { showToast } from '@/lib/toast'
|
|
import { invalidateRepoGitCaches, setRepoGitStatusCaches } from '@/lib/queryInvalidation'
|
|
|
|
export function useGit(repoId: number | undefined, onError?: (error: unknown) => void) {
|
|
const queryClient = useQueryClient()
|
|
|
|
const handleError = (error: unknown) => {
|
|
if (onError) {
|
|
onError(error)
|
|
} else {
|
|
showToast.error(getApiErrorMessage(error))
|
|
}
|
|
}
|
|
|
|
const fetch = useMutation({
|
|
mutationFn: () => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitFetch(repoId)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false })
|
|
showToast.success('Fetch completed')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const pull = useMutation({
|
|
mutationFn: () => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitPull(repoId)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false })
|
|
showToast.success('Pull completed')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const push = useMutation({
|
|
mutationFn: (options?: { setUpstream?: boolean }) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitPush(repoId, options?.setUpstream ?? false)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
|
showToast.success('Push completed')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const commit = useMutation({
|
|
mutationFn: ({ message, stagedPaths }: { message: string; stagedPaths?: string[] }) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitCommit(repoId, message, stagedPaths)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
|
showToast.success('Commit created')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const stageFilesMutation = useMutation({
|
|
mutationFn: (paths: string[]) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitStageFiles(repoId, paths)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
|
showToast.success('Files staged')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const unstageFilesMutation = useMutation({
|
|
mutationFn: (paths: string[]) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitUnstageFiles(repoId, paths)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
|
showToast.success('Files unstaged')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const discardFilesMutation = useMutation({
|
|
mutationFn: ({ paths, staged }: { paths: string[]; staged: boolean }) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitDiscardFiles(repoId, paths, staged)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const log = useMutation({
|
|
mutationFn: ({ limit }: { limit?: number }) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return fetchGitLog(repoId, limit)
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const diff = useMutation({
|
|
mutationFn: (path: string) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return fetchGitDiff(repoId, path)
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const createBranchMutation = useMutation({
|
|
mutationFn: async (branchName: string) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
await createBranch(repoId, branchName)
|
|
return fetchGitStatus(repoId)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false })
|
|
showToast.success('Branch created')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const switchBranchMutation = useMutation({
|
|
mutationFn: async (branchName: string) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
await switchBranch(repoId, branchName)
|
|
return fetchGitStatus(repoId)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false })
|
|
showToast.success('Switched to branch')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
const resetMutation = useMutation({
|
|
mutationFn: (commitHash: string) => {
|
|
if (!repoId) throw new Error('No repo ID')
|
|
return gitReset(repoId, commitHash)
|
|
},
|
|
onSuccess: (data) => {
|
|
if (repoId) setRepoGitStatusCaches(queryClient, repoId, data)
|
|
invalidateRepoGitCaches(queryClient, repoId, { invalidateStatus: false, invalidateRepoMeta: false })
|
|
showToast.success('Reset to commit')
|
|
},
|
|
onError: handleError,
|
|
})
|
|
|
|
return {
|
|
fetch,
|
|
pull,
|
|
push,
|
|
commit,
|
|
stageFiles: stageFilesMutation,
|
|
unstageFiles: unstageFilesMutation,
|
|
discardFiles: discardFilesMutation,
|
|
log,
|
|
diff,
|
|
createBranch: createBranchMutation,
|
|
switchBranch: switchBranchMutation,
|
|
reset: resetMutation
|
|
}
|
|
}
|