import { useEffect, useMemo, 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 hljs from 'highlight.js/lib/core'; import json from 'highlight.js/lib/languages/json'; import { ChevronDown, ChevronRight, Loader2 } from 'lucide-react'; hljs.registerLanguage('json', json); 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; /** 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; 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; } function highlightLogsHtml(text: string): string { return text .split('\n') .map((line) => { const trimmed = line.trimStart(); if (trimmed.startsWith('{') || trimmed.startsWith('[')) { try { return hljs.highlight(line, { language: 'json' }).value; } catch { return escapeHtml(line); } } if (line === '[stdout]' || line === '[stderr]') { return `${escapeHtml(line)}`; } return escapeHtml(line); }) .join('\n'); } function escapeHtml(text: string): string { return text.replace(/&/g, '&').replace(//g, '>'); } export const ProvisioningProgressBlock = ({ title, message, currentStepIndex, loading = false, onCancel, startedAt, pid, cliLogsTail, className, }: ProvisioningProgressBlockProps): React.JSX.Element => { const elapsed = useElapsedTimer(startedAt); const [logsOpen, setLogsOpen] = useState(false); const logsRef = useRef(null); const highlightedHtml = useMemo( () => (cliLogsTail ? highlightLogsHtml(cliLogsTail) : ''), [cliLogsTail] ); useEffect(() => { if (logsOpen && logsRef.current) { logsRef.current.scrollTop = logsRef.current.scrollHeight; } }, [logsOpen, cliLogsTail]); 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}
); })}
{cliLogsTail ? (
{logsOpen ? (
 tags, combined with escapeHtml() for non-JSON lines.
              // Input is CLI stdout/stderr from a local process, not user-supplied web content.

              dangerouslySetInnerHTML={{ __html: highlightedHtml }}
            />
          ) : null}
        
) : null}
); };