import { useEffect, useRef, useState } from 'react'; import { Badge } from '@renderer/components/ui/badge'; import { Button } from '@renderer/components/ui/button'; import { cn } from '@renderer/lib/utils'; import { ChevronDown, ChevronRight, Loader2 } from 'lucide-react'; import { MarkdownViewer } from '../chat/viewers/MarkdownViewer'; import { CliLogsRichView } from './CliLogsRichView'; import { STEP_LABELS, STEP_ORDER } from './provisioningSteps'; import type { ProvisioningStep } from './provisioningSteps'; export interface ProvisioningProgressBlockProps { /** Title above the steps, e.g. "Launching team" */ title: string; /** Optional status message */ message?: string | null; /** Visual tone (e.g. highlight errors) */ tone?: 'default' | 'error'; /** Whether Live output is expanded by default */ defaultLiveOutputOpen?: boolean; /** Index of the current step in STEP_ORDER (0-based), or -1 if unknown */ currentStepIndex: number; /** Show spinner next to title */ loading?: boolean; /** Cancel button label and handler */ onCancel?: (() => void) | null; /** ISO timestamp when provisioning started */ startedAt?: string; /** PID of the CLI process */ pid?: number; /** Tail of CLI logs */ cliLogsTail?: string; /** Accumulated assistant text output for live preview */ assistantOutput?: string; className?: string; } function formatElapsed(seconds: number): string { const m = Math.floor(seconds / 60); const s = seconds % 60; return `${m}:${String(s).padStart(2, '0')}`; } function useElapsedTimer(startedAt?: string): string | null { const [elapsed, setElapsed] = useState(null); useEffect(() => { if (!startedAt) return () => setElapsed(null); const startMs = Date.parse(startedAt); if (isNaN(startMs)) return () => setElapsed(null); const tick = (): void => { const seconds = Math.max(0, Math.floor((Date.now() - startMs) / 1000)); setElapsed(formatElapsed(seconds)); }; tick(); const id = window.setInterval(tick, 1000); return () => { window.clearInterval(id); }; }, [startedAt]); if (!startedAt) return null; return elapsed; } export const ProvisioningProgressBlock = ({ title, message, tone = 'default', defaultLiveOutputOpen = true, currentStepIndex, loading = false, onCancel, startedAt, pid, cliLogsTail, assistantOutput, className, }: ProvisioningProgressBlockProps): React.JSX.Element => { const elapsed = useElapsedTimer(startedAt); const [logsOpen, setLogsOpen] = useState(false); const [liveOutputOpen, setLiveOutputOpen] = useState(defaultLiveOutputOpen); const outputScrollRef = useRef(null); const isError = tone === 'error'; // Auto-scroll assistant output useEffect(() => { if (liveOutputOpen && outputScrollRef.current) { outputScrollRef.current.scrollTop = outputScrollRef.current.scrollHeight; } }, [assistantOutput, liveOutputOpen]); // If parent changes the default (e.g. transitioning to "ready"), respect it. useEffect(() => { setLiveOutputOpen(defaultLiveOutputOpen); }, [defaultLiveOutputOpen]); return (
{loading ? ( ) : null}

{title}

{elapsed !== null ? ( {elapsed} ) : null} {pid !== undefined ? ( PID {pid} ) : null}
{onCancel ? ( ) : null}
{message ? (

{message}

) : null}
{STEP_ORDER.filter((s): s is ProvisioningStep => s !== 'ready').map((step, index) => { const isDone = currentStepIndex >= 0 && index < currentStepIndex; const isCurrent = currentStepIndex >= 0 && index === currentStepIndex; return (
{/* eslint-disable tailwindcss/no-custom-classname -- theme CSS vars */} {index + 1} {STEP_LABELS[step]} {/* eslint-enable tailwindcss/no-custom-classname -- end theme CSS vars block */} {index < STEP_ORDER.filter((s) => s !== 'ready').length - 1 ? ( ) : null}
); })}
{liveOutputOpen ? (
{assistantOutput ? ( ) : (

No output captured yet.

)}
) : null}
{cliLogsTail ? (
{logsOpen ? : null}
) : null}
); };