2026-02-23 20:34:12 +01:00
|
|
|
import { useCallback, useState } from 'react'
|
2026-03-19 09:51:06 +01:00
|
|
|
import type { GitPushResult } from '../types'
|
2026-02-23 20:34:12 +01:00
|
|
|
|
|
|
|
|
interface CommitFlowConfig {
|
2026-02-24 12:19:09 +01:00
|
|
|
savePending: () => Promise<void | boolean>
|
2026-02-23 20:34:12 +01:00
|
|
|
loadModifiedFiles: () => Promise<void>
|
2026-03-19 09:51:06 +01:00
|
|
|
commitAndPush: (message: string) => Promise<GitPushResult>
|
2026-02-23 20:34:12 +01:00
|
|
|
setToastMessage: (msg: string | null) => void
|
2026-03-19 09:51:06 +01:00
|
|
|
onPushRejected?: () => void
|
2026-02-23 20:34:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Manages the Commit & Push dialog state and the save→commit→push flow. */
|
2026-03-19 09:51:06 +01:00
|
|
|
export function useCommitFlow({ savePending, loadModifiedFiles, commitAndPush, setToastMessage, onPushRejected }: CommitFlowConfig) {
|
2026-02-23 20:34:12 +01:00
|
|
|
const [showCommitDialog, setShowCommitDialog] = useState(false)
|
|
|
|
|
|
|
|
|
|
const openCommitDialog = useCallback(async () => {
|
|
|
|
|
await savePending()
|
|
|
|
|
await loadModifiedFiles()
|
|
|
|
|
setShowCommitDialog(true)
|
|
|
|
|
}, [savePending, loadModifiedFiles])
|
|
|
|
|
|
|
|
|
|
const handleCommitPush = useCallback(async (message: string) => {
|
|
|
|
|
setShowCommitDialog(false)
|
|
|
|
|
try {
|
|
|
|
|
await savePending()
|
|
|
|
|
const result = await commitAndPush(message)
|
2026-03-19 09:51:06 +01:00
|
|
|
if (result.status === 'ok') {
|
|
|
|
|
setToastMessage('Committed and pushed')
|
|
|
|
|
} else if (result.status === 'rejected') {
|
|
|
|
|
setToastMessage('Committed, but push rejected — remote has new commits. Pull first.')
|
|
|
|
|
onPushRejected?.()
|
|
|
|
|
} else {
|
|
|
|
|
setToastMessage(result.message)
|
|
|
|
|
}
|
2026-02-23 20:34:12 +01:00
|
|
|
loadModifiedFiles()
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Commit failed:', err)
|
|
|
|
|
setToastMessage(`Commit failed: ${err}`)
|
|
|
|
|
}
|
2026-03-19 09:51:06 +01:00
|
|
|
}, [savePending, commitAndPush, loadModifiedFiles, setToastMessage, onPushRejected])
|
2026-02-23 20:34:12 +01:00
|
|
|
|
|
|
|
|
const closeCommitDialog = useCallback(() => setShowCommitDialog(false), [])
|
|
|
|
|
|
|
|
|
|
return { showCommitDialog, openCommitDialog, handleCommitPush, closeCommitDialog }
|
|
|
|
|
}
|