|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import { useNavigate } from 'react-router-dom' |
| 3 | +import { GitBranch, ArrowLeft } from 'lucide-react' |
| 4 | +import { api } from '@/services/api' |
| 5 | +import { cn } from '@/lib/utils' |
| 6 | + |
| 7 | +interface RunInfo { |
| 8 | + id: string |
| 9 | + workflowId: string |
| 10 | + workflowName: string |
| 11 | + parentRunId?: string | null |
| 12 | + parentNodeRef?: string | null |
| 13 | +} |
| 14 | + |
| 15 | +interface RunBreadcrumbsProps { |
| 16 | + currentRun: RunInfo | null |
| 17 | + className?: string |
| 18 | + /** 'floating' for canvas overlay, 'inline' for panel integration */ |
| 19 | + variant?: 'floating' | 'inline' |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Displays breadcrumb navigation for parent/child workflow runs. |
| 24 | + * Shows a link to navigate back to the parent run. |
| 25 | + */ |
| 26 | +export function RunBreadcrumbs({ currentRun, className, variant = 'inline' }: RunBreadcrumbsProps) { |
| 27 | + const navigate = useNavigate() |
| 28 | + const [parentRun, setParentRun] = useState<RunInfo | null>(null) |
| 29 | + const [loading, setLoading] = useState(false) |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + if (!currentRun?.parentRunId) { |
| 33 | + setParentRun(null) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + const fetchParentRun = async () => { |
| 38 | + setLoading(true) |
| 39 | + try { |
| 40 | + const run = await api.executions.getRun(currentRun.parentRunId!) |
| 41 | + if (run) { |
| 42 | + setParentRun({ |
| 43 | + id: run.id as string, |
| 44 | + workflowId: run.workflowId as string, |
| 45 | + workflowName: (run as any).workflowName || 'Parent Workflow', |
| 46 | + parentRunId: (run as any).parentRunId, |
| 47 | + parentNodeRef: (run as any).parentNodeRef, |
| 48 | + }) |
| 49 | + } |
| 50 | + } catch (err) { |
| 51 | + console.error('Failed to fetch parent run:', err) |
| 52 | + } finally { |
| 53 | + setLoading(false) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fetchParentRun() |
| 58 | + }, [currentRun?.parentRunId]) |
| 59 | + |
| 60 | + // Only show breadcrumbs if this is a child run |
| 61 | + if (!currentRun?.parentRunId) { |
| 62 | + return null |
| 63 | + } |
| 64 | + |
| 65 | + const handleNavigateToParent = () => { |
| 66 | + if (parentRun) { |
| 67 | + navigate(`/workflows/${parentRun.workflowId}/runs/${parentRun.id}`) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (variant === 'floating') { |
| 72 | + return ( |
| 73 | + <div |
| 74 | + className={cn( |
| 75 | + 'flex items-center gap-2 px-3 py-2 rounded-md border bg-background shadow-sm', |
| 76 | + 'text-xs font-medium transition-all duration-200', |
| 77 | + className |
| 78 | + )} |
| 79 | + > |
| 80 | + <GitBranch className="h-4 w-4 text-muted-foreground flex-shrink-0" /> |
| 81 | + <span className="text-muted-foreground">Child of</span> |
| 82 | + |
| 83 | + {loading ? ( |
| 84 | + <span className="text-muted-foreground animate-pulse">loading...</span> |
| 85 | + ) : parentRun ? ( |
| 86 | + <button |
| 87 | + onClick={handleNavigateToParent} |
| 88 | + className="inline-flex items-center gap-1.5 font-medium text-primary hover:text-primary/80 hover:underline" |
| 89 | + > |
| 90 | + <ArrowLeft className="h-3.5 w-3.5" /> |
| 91 | + <span className="truncate max-w-[180px]" title={parentRun.workflowName}> |
| 92 | + {parentRun.workflowName} |
| 93 | + </span> |
| 94 | + </button> |
| 95 | + ) : ( |
| 96 | + <span className="font-mono text-muted-foreground"> |
| 97 | + {currentRun.parentRunId.split('-').slice(0, 3).join('-')} |
| 98 | + </span> |
| 99 | + )} |
| 100 | + |
| 101 | + {currentRun.parentNodeRef && ( |
| 102 | + <code className="font-mono text-[10px] bg-muted px-1.5 py-0.5 rounded text-muted-foreground"> |
| 103 | + {currentRun.parentNodeRef} |
| 104 | + </code> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + // Inline variant (for panel integration) |
| 111 | + return ( |
| 112 | + <div className={cn('flex items-center gap-1.5 text-xs', className)}> |
| 113 | + <GitBranch className="h-3.5 w-3.5 text-muted-foreground flex-shrink-0" /> |
| 114 | + <span className="text-muted-foreground">Sub-workflow of</span> |
| 115 | + |
| 116 | + {loading ? ( |
| 117 | + <span className="text-muted-foreground animate-pulse">loading...</span> |
| 118 | + ) : parentRun ? ( |
| 119 | + <button |
| 120 | + onClick={handleNavigateToParent} |
| 121 | + className="inline-flex items-center gap-1 font-medium text-primary hover:text-primary/80 hover:underline" |
| 122 | + > |
| 123 | + <ArrowLeft className="h-3 w-3" /> |
| 124 | + <span className="truncate max-w-[200px]" title={parentRun.workflowName}> |
| 125 | + {parentRun.workflowName} |
| 126 | + </span> |
| 127 | + <span className="font-mono text-[10px] text-muted-foreground"> |
| 128 | + ({parentRun.id.split('-').slice(0, 3).join('-')}) |
| 129 | + </span> |
| 130 | + </button> |
| 131 | + ) : ( |
| 132 | + <span className="font-mono text-muted-foreground"> |
| 133 | + {currentRun.parentRunId.split('-').slice(0, 3).join('-')} |
| 134 | + </span> |
| 135 | + )} |
| 136 | + |
| 137 | + {currentRun.parentNodeRef && ( |
| 138 | + <> |
| 139 | + <span className="text-muted-foreground mx-1">•</span> |
| 140 | + <span className="text-muted-foreground"> |
| 141 | + node <code className="font-mono text-[10px] bg-muted px-1 py-0.5 rounded">{currentRun.parentNodeRef}</code> |
| 142 | + </span> |
| 143 | + </> |
| 144 | + )} |
| 145 | + </div> |
| 146 | + ) |
| 147 | +} |
0 commit comments