import * as React from 'react'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Icon } from '@/components/icon/Icon'; import { toast } from '@/components/ui'; import { useI18n } from '@/lib/i18n'; import { applyPendingOpenCodeRestart } from '@/lib/opencode/deferredRestart'; import { cn } from '@/lib/utils'; import { reloadOpenCodeConfiguration } from '@/stores/useAgentsStore'; import { selectPendingOpenCodeRestartCount, usePendingOpenCodeRestartStore, } from '@/stores/usePendingOpenCodeRestartStore'; import { useUIStore } from '@/stores/useUIStore'; type OpenCodeReloadFooterActionProps = { className?: string; }; export const OpenCodeReloadFooterAction: React.FC = ({ className, }) => { const { t } = useI18n(); const pendingCount = usePendingOpenCodeRestartStore(selectPendingOpenCodeRestartCount); const isApplying = usePendingOpenCodeRestartStore((state) => state.isApplying); const showRestartConfirm = useUIStore((state) => state.showOpenCodeRestartConfirm); const setShowOpenCodeRestartConfirm = useUIStore((state) => state.setShowOpenCodeRestartConfirm); const [confirmOpen, setConfirmOpen] = React.useState(false); const [dontShowAgain, setDontShowAgain] = React.useState(false); const hasPending = pendingCount > 0; const runApply = React.useCallback(async () => { try { const result = await applyPendingOpenCodeRestart({ message: t('settings.view.pendingRestart.applying'), }); if (result.requiresManualRestart) { toast.warning(t('settings.view.pendingRestart.manualRestartRequired')); return; } if (result.ok) { toast.success(t('settings.view.pendingRestart.applied')); } } catch (error) { const message = error instanceof Error && error.message ? error.message : t('settings.view.pendingRestart.applyFailed'); toast.error(message); } }, [t]); const runManualReload = React.useCallback(async () => { try { await reloadOpenCodeConfiguration({ message: t('settings.view.pendingRestart.applying'), mode: 'projects', scopes: ['all'], }); usePendingOpenCodeRestartStore.getState().clear(); } catch { // ignore } }, [t]); const handleConfirmApply = React.useCallback(() => { if (dontShowAgain) { setShowOpenCodeRestartConfirm(false); } setConfirmOpen(false); setDontShowAgain(false); void runApply(); }, [dontShowAgain, runApply, setShowOpenCodeRestartConfirm]); const handleClick = React.useCallback(() => { if (!hasPending) { void runManualReload(); return; } if (showRestartConfirm) { setDontShowAgain(false); setConfirmOpen(true); return; } void runApply(); }, [hasPending, runApply, runManualReload, showRestartConfirm]); const label = !hasPending ? t('settings.view.actions.reloadOpenCode') : isApplying ? t('settings.view.pendingRestart.applying') : t('settings.view.actions.applyAndRestartOpenCode'); const tooltip = !hasPending ? t('settings.view.actions.reloadOpenCodeTooltip') : pendingCount === 1 ? t('settings.view.actions.applyAndRestartOpenCodeTooltipSingle') : t('settings.view.actions.applyAndRestartOpenCodeTooltipPlural', { count: pendingCount }); return ( <> { setConfirmOpen(open); if (!open) { setDontShowAgain(false); } }} > {t('settings.view.pendingRestart.confirm.title')} {t('settings.view.pendingRestart.confirm.description')} {/* Plain stack — avoid DialogFooter (sm:flex-row) fighting this layout */}
); };