import { Alert } from '@mui/material' import { useState } from 'react' import { useTranslation } from 'react-i18next' import { BaseDialog } from '@/components/base' import { runStateQueryKey } from '@/hooks/use-system-state' import { useVisibility } from '@/hooks/use-visibility' import { continueWithSidecar, getRuntimeState, installService, reinstallService, repairService, restartCore, type RunState, type ServiceInstallOutcome, } from '@/services/cmds' import { showNotice } from '@/services/notice-service' import { setCacheData, useQuery } from '@/services/query-client' export const ServiceMigrationDialog = () => { const { t } = useTranslation() const pageVisible = useVisibility() const [loading, setLoading] = useState(false) const [stateRefreshFailed, setStateRefreshFailed] = useState(false) const [workflowIncomplete, setWorkflowIncomplete] = useState(false) const { data: runState } = useQuery({ queryKey: runStateQueryKey, queryFn: getRuntimeState, enabled: true, retry: 1, refetchInterval: pageVisible ? 30000 : false, }) // Whether the service needs a decision is derived once, in Rust, and travels with the // snapshot; a failed refresh is treated as needing one, since we cannot tell otherwise. const needsDecision = stateRefreshFailed || Boolean(runState?.serviceNeedsAttention) // Treat refresh failures as unreachable; an absent Service still needs install after a failed Sidecar attempt. const remedy: 'install' | 'repair' | 'reinstall' = runState?.pendingAction === 'install' ? 'install' : stateRefreshFailed || runState?.service === 'unavailable' ? 'repair' : runState?.service === 'notInstalled' ? 'install' : 'reinstall' const open = loading || workflowIncomplete || needsDecision const checking = loading || !runState || runState.opInFlight || runState.service === 'unknown' const showCheckingMessage = checking || !needsDecision const canContinue = Boolean( runState && !stateRefreshFailed && (runState.pendingAction === 'install' ? runState.mode === 'NotRunning' || runState.mode === 'Sidecar' : !runState.pendingAction && !runState.sidecarAllowed && runState.mode === 'NotRunning' && (runState.service === 'notInstalled' || runState.service === 'versionMismatch' || runState.service === 'unavailable')), ) // One cache entry to refresh, so there is nothing left to keep coherent by hand. const refreshRunState = async () => { try { const data = await getRuntimeState() await setCacheData(runStateQueryKey, data) setStateRefreshFailed(false) return data } catch (error) { setStateRefreshFailed(true) throw error } } const handleServiceAction = async () => { setLoading(true) setWorkflowIncomplete(true) let outcome: ServiceInstallOutcome | undefined try { if (remedy === 'install') { outcome = await installService() } else if (remedy === 'repair') { outcome = await repairService() } else { outcome = await reinstallService() } } catch (error) { showNotice.error( 'layout.components.serviceMigration.errors.actionFailed', error, ) } let initialRefreshSucceeded = false try { await refreshRunState() initialRefreshSucceeded = true } catch (error) { showNotice.error( 'layout.components.serviceMigration.errors.stateRefreshFailed', error, ) } if (!outcome || !initialRefreshSucceeded) { setLoading(false) return } if (outcome.status === 'sidecar') { setWorkflowIncomplete(false) setLoading(false) showNotice.warning( 'settings.feedback.notifications.clashService.permissionFallback', { reason: outcome.reason }, 0, ) return } let restartSucceeded = false try { await restartCore() restartSucceeded = true } catch (error) { showNotice.error( 'layout.components.serviceMigration.errors.restartFailed', error, ) } let finalRefreshSucceeded = false try { await refreshRunState() finalRefreshSucceeded = true } catch (error) { showNotice.error( 'layout.components.serviceMigration.errors.stateRefreshFailed', error, ) } if (restartSucceeded && finalRefreshSucceeded) { setWorkflowIncomplete(false) showNotice.success('layout.components.serviceMigration.success') } setLoading(false) } const handleContinue = async () => { setLoading(true) setWorkflowIncomplete(true) let startupError: unknown try { await continueWithSidecar() } catch (error) { startupError = error } let installRefreshSucceeded = false try { await refreshRunState() installRefreshSucceeded = true } catch (error) { showNotice.error( 'layout.components.serviceMigration.errors.stateRefreshFailed', error, ) } if (startupError) { showNotice.error( 'layout.components.serviceMigration.errors.sidecarFailed', startupError, ) } else if (installRefreshSucceeded) { setWorkflowIncomplete(false) } setLoading(false) } return ( void handleServiceAction()} onCancel={() => void handleContinue()} > {t( showCheckingMessage ? 'layout.components.serviceMigration.checkingMessage' : remedy === 'reinstall' ? 'layout.components.serviceMigration.message' : 'layout.components.serviceMigration.unavailableMessage', )} ) }