import { groupBy } from 'lodash' import { ArrowLeft, ArrowRight } from 'lucide-react' import { useState } from 'react' import { Button, cn, Dialog, DialogContent, DialogDescription, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, DialogTrigger, StatusIcon, } from 'ui' import { GenericSkeletonLoader, TimestampInfo } from 'ui-patterns' import { ActionStatusBadge, ActionStatusBadgeCondensed, STATUS_TO_LABEL } from './ActionStatusBadge' import BranchStatusBadge from './BranchStatusBadge' import AlertError from '@/components/ui/AlertError' import { ActionRunData } from '@/data/actions/action-detail-query' import { useActionRunLogsQuery } from '@/data/actions/action-logs-query' import { useActionsQuery, type ActionRunStep, type ActionStatus, } from '@/data/actions/action-runs-query' import type { Branch } from '@/data/branches/branches-query' interface WorkflowLogsProps { branch: Branch } type StatusType = Branch['status'] const HEALTHY_STATUSES: StatusType[] = ['FUNCTIONS_DEPLOYED', 'MIGRATIONS_PASSED'] const UNHEALTHY_STATUSES: StatusType[] = ['MIGRATIONS_FAILED', 'FUNCTIONS_FAILED'] export const WorkflowLogs = ({ branch }: WorkflowLogsProps) => { const { project_ref: projectRef, status, name } = branch const [isOpen, setIsOpen] = useState(false) const { data: workflowRuns, isSuccess: isWorkflowRunsSuccess, isPending: isWorkflowRunsLoading, isError: isWorkflowRunsError, error: workflowRunsError, } = useActionsQuery({ ref: projectRef }, { enabled: isOpen }) const [selectedWorkflowRun, setSelectedWorkflowRun] = useState() const { data: workflowRunLogs, isSuccess: isWorkflowRunLogsSuccess, isPending: isWorkflowRunLogsLoading, isError: isWorkflowRunLogsError, error: workflowRunLogsError, } = useActionRunLogsQuery( { projectRef, runId: selectedWorkflowRun?.id }, { enabled: isOpen && Boolean(selectedWorkflowRun) } ) const showStatusIcon = !HEALTHY_STATUSES.includes(status) const isUnhealthy = UNHEALTHY_STATUSES.includes(status) return ( Workflow logs for {name} {!selectedWorkflowRun ? ( 'Select a workflow run to view logs' ) : ( <> Run created at{' '} )} {!selectedWorkflowRun ? ( <> {isWorkflowRunsLoading && } {isWorkflowRunsError && (
)} {isWorkflowRunsSuccess && (workflowRuns.length > 0 ? (
    {workflowRuns.map((workflowRun) => (
  • ))}
) : (

No workflow runs found.

))} ) : (
{isWorkflowRunLogsLoading && } {isWorkflowRunLogsError && (
)} {isWorkflowRunLogsSuccess && (
                  {workflowRunLogs}
                
)}
)}
) } function RunSteps({ steps }: { steps: Array }) { const stepsByStatus = groupBy(steps, 'status') as Record> const firstFailedStep = stepsByStatus.DEAD?.[0] const numberFailedSteps = stepsByStatus.DEAD?.length ?? 0 return ( <> {firstFailedStep && ( )} {numberFailedSteps > 1 && ( {numberFailedSteps - 1} more )}
{(Object.keys(stepsByStatus) as Array) .filter((status) => status !== 'DEAD') .map((status) => ( {stepsByStatus[status].length} {STATUS_TO_LABEL[status]} ))}
) }