Files
opencode-manager/frontend/src/hooks/useOpenCodeFailureToast.ts
T
Chris Scott 42dd733378 fix(servers): open settings tabs with history and keep server selection on load errors
External settings openers push a history entry and close the mobile sheet. The sidebar server selection only falls back to Local after the server list loads successfully.
2026-10-19 11:37:42 -04:00

43 lines
1.7 KiB
TypeScript

import { useEffect, useRef } from 'react'
import { showToast } from '@/lib/toast'
import { getOpenCodeServerIssue, useServerHealth } from '@/hooks/useServerHealth'
import { useSettingsDialog } from '@/hooks/useSettingsDialog'
export const OPENCODE_FAILURE_TOAST_ID = 'opencode-server-failure'
/**
* Raises one persistent error toast per distinct terminal OpenCode server
* failure (startup failure or exhausted recovery), offering to open the Logs
* settings tab or restart the server, and announces when the server recovers.
*/
export function useOpenCodeFailureToast(enabled: boolean, onRestart: () => void) {
const { data: health } = useServerHealth(enabled)
const { openTab } = useSettingsDialog()
const notifiedFailureRef = useRef<string | null>(null)
const issue = getOpenCodeServerIssue(health)
const failureMessage = issue?.state === 'failed' ? issue.message : null
const isHealthy = health?.opencodeLifecycle ? health.opencodeLifecycle.healthy : health?.opencode === 'healthy'
useEffect(() => {
if (failureMessage !== null) {
if (notifiedFailureRef.current === failureMessage) return
notifiedFailureRef.current = failureMessage
showToast.error('OpenCode server failed', {
id: OPENCODE_FAILURE_TOAST_ID,
duration: Infinity,
description: failureMessage,
action: { label: 'View logs', onClick: () => openTab('logs') },
cancel: { label: 'Restart', onClick: onRestart },
})
return
}
if (isHealthy && notifiedFailureRef.current !== null) {
notifiedFailureRef.current = null
showToast.dismiss(OPENCODE_FAILURE_TOAST_ID)
showToast.success('OpenCode server is back online')
}
}, [failureMessage, isHealthy, openTab, onRestart])
}