Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

7.2 KB
Raw Blame
import { IconCheckCircle, IconClock, IconWarning, IconXCircle } from '@posthog/icons'
import { Spinner } from '@posthog/quill-primitives'

import { TZLabel } from 'lib/components/TZLabel'

import type { WizardRunApi, WizardRunTaskApi } from '../../generated/api.schemas'
import {
    WizardRunProgressStep,
    wizardRunFailureStage,
    wizardRunProgressStates,
    wizardRunProgressStep,
    wizardWorkspaceLabel,
} from '../../lib/wizardRunDisplay'
import type { WizardRunProgressState } from '../../lib/wizardRunDisplay'
import { wizardRunErrorDetails } from '../../lib/wizardRunErrorCatalog'

function ProgressIcon({ state }: { state: WizardRunProgressState }): JSX.Element {
    if (state === 'complete') {
        return <IconCheckCircle className="text-success" />
    }
    if (state === 'active') {
        return <Spinner />
    }
    if (state === 'failed') {
        return <IconXCircle className="text-danger" />
    }
    return <IconClock className="text-muted" />
}

function RunLevelOutcome({ run }: { run: WizardRunApi }): JSX.Element | null {
    if (run.status === 'cancelled') {
        return (
            <div className="flex items-center gap-1 text-xs text-muted">
                <IconXCircle className="shrink-0" /> Run canceled
                {run.finished_at && (
                    <>
                        {' '}
                        <TZLabel time={run.finished_at} />
                    </>
                )}
                .
            </div>
        )
    }

    if (run.status !== 'failed') {
        return null
    }

    const runError = wizardRunErrorDetails(run.error_code, run.error_message)

    return (
        <div className="space-y-1 text-xs">
            <div className="flex items-center gap-1 text-danger">
                <IconXCircle className="shrink-0" /> {runError.title}
            </div>
            {runError.description !== runError.title && <div className="text-muted">{runError.description}</div>}
            {runError.resolution && (
                <div className="flex items-start gap-1 text-muted">
                    <IconWarning className="mt-0.5 shrink-0" /> {runError.resolution}
                </div>
            )}
        </div>
    )
}

export function WizardRunProgress({
    run,
    tasks = [],
}: {
    run: WizardRunApi
    tasks?: readonly WizardRunTaskApi[]
}): JSX.Element {
    const runError = wizardRunErrorDetails(run.error_code, run.error_message)
    const failedAtStep = run.status === 'failed' && wizardRunFailureStage(run)
    const stepStates = wizardRunProgressStates(run)
    const steps = [
        {
            step: WizardRunProgressStep.Starting,
            title: 'Starting run',
            detail: run.created_at ? <TZLabel time={run.created_at} /> : 'Waiting to start',
        },
        {
            step: WizardRunProgressStep.PreparingWorkspace,
            title: 'Preparing workspace',
            detail:
                run.workspace.type === 'git_repository'
                    ? `Repository: ${run.workspace.repository}`
                    : `Folder: ${wizardWorkspaceLabel(run)}`,
        },
        {
            step: WizardRunProgressStep.ExecutingWizard,
            title: 'Running Wizard',
            detail: `Program: ${run.program.name}`,
        },
        {
            step: WizardRunProgressStep.CreatingArtifacts,
            title: 'Finishing up',
            detail:
                run.status === 'completed'
                    ? 'Changes and artifacts are ready'
                    : wizardRunProgressStep(run) === WizardRunProgressStep.CreatingArtifacts
                      ? 'Saving changes and artifacts'
                      : 'Starts after the program finishes',
        },
    ]

    return (
        <div className="space-y-0">
            {steps.map((step, index) => {
                const state = stepStates[step.step]
                return (
                    <div key={step.step} className="relative flex gap-3 pb-5 last:pb-0">
                        {index < steps.length - 1 && (
                            <div className="absolute left-[7px] top-5 h-[calc(100%-12px)] border-l border-primary" />
                        )}
                        <div className="relative z-10 mt-0.5 flex size-4 shrink-0 items-center justify-center bg-surface-primary">
                            <ProgressIcon state={state} />
                        </div>
                        <div className="min-w-0">
                            <div className="font-semibold">{step.title}</div>
                            <div className="text-xs text-muted">{step.detail}</div>
                            {step.step === WizardRunProgressStep.ExecutingWizard &&
                                run.status === 'running' &&
                                tasks.length > 0 && (
                                    <ul className="mt-2 space-y-1 text-xs">
                                        {tasks.map((task) => (
                                            <li key={task.name} className="flex items-center gap-2">
                                                <ProgressIcon
                                                    state={
                                                        task.status === 'completed'
                                                            ? 'complete'
                                                            : task.status === 'running'
                                                              ? 'active'
                                                              : task.status === 'failed'
                                                                ? 'failed'
                                                                : 'pending'
                                                    }
                                                />
                                                <span className="break-words">{task.name}</span>
                                                <span className="sr-only">({task.status})</span>
                                            </li>
                                        ))}
                                    </ul>
                                )}
                            {state === 'failed' && (
                                <div className="mt-1 space-y-1 text-xs">
                                    <div className="flex items-center gap-1 text-danger">
                                        <IconWarning /> {runError.title}
                                    </div>
                                    {runError.description !== runError.title && (
                                        <div className="text-muted">{runError.description}</div>
                                    )}
                                    {runError.resolution && (
                                        <div className="text-muted">Next step: {runError.resolution}</div>
                                    )}
                                </div>
                            )}
                        </div>
                    </div>
                )
            })}
            {(run.status === 'cancelled' || (run.status === 'failed' && !failedAtStep)) && (
                <div className="border-t border-primary pt-3">
                    <RunLevelOutcome run={run} />
                </div>
            )}
        </div>
    )
}