|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import type { PRContext, PRCheck } from '@plannotator/shared/pr-provider'; |
| 3 | + |
| 4 | +interface PRChecksTabProps { |
| 5 | + context: PRContext; |
| 6 | +} |
| 7 | + |
| 8 | +const DECISION_STYLES: Record<string, { bg: string; text: string; label: string }> = { |
| 9 | + APPROVED: { bg: 'bg-success/15', text: 'text-success', label: 'Approved' }, |
| 10 | + CHANGES_REQUESTED: { bg: 'bg-destructive/15', text: 'text-destructive', label: 'Changes Requested' }, |
| 11 | + REVIEW_REQUIRED: { bg: 'bg-yellow-500/15', text: 'text-yellow-500', label: 'Review Required' }, |
| 12 | +}; |
| 13 | + |
| 14 | +const MERGE_STATUS_STYLES: Record<string, { bg: string; text: string; label: string }> = { |
| 15 | + CLEAN: { bg: 'bg-success/15', text: 'text-success', label: 'Ready to merge' }, |
| 16 | + BLOCKED: { bg: 'bg-yellow-500/15', text: 'text-yellow-500', label: 'Blocked' }, |
| 17 | + BEHIND: { bg: 'bg-yellow-500/15', text: 'text-yellow-500', label: 'Behind base branch' }, |
| 18 | + DIRTY: { bg: 'bg-destructive/15', text: 'text-destructive', label: 'Has conflicts' }, |
| 19 | + UNKNOWN: { bg: 'bg-muted', text: 'text-muted-foreground', label: 'Unknown' }, |
| 20 | +}; |
| 21 | + |
| 22 | +function CheckIcon({ check }: { check: PRCheck }) { |
| 23 | + if (check.status !== 'COMPLETED') { |
| 24 | + // In progress or queued |
| 25 | + return ( |
| 26 | + <svg className="w-3.5 h-3.5 text-yellow-500 animate-spin" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 27 | + <path strokeLinecap="round" strokeLinejoin="round" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /> |
| 28 | + </svg> |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + if (check.conclusion === 'SUCCESS') { |
| 33 | + return ( |
| 34 | + <svg className="w-3.5 h-3.5 text-success" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 35 | + <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" /> |
| 36 | + </svg> |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + if (check.conclusion === 'FAILURE' || check.conclusion === 'TIMED_OUT') { |
| 41 | + return ( |
| 42 | + <svg className="w-3.5 h-3.5 text-destructive" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 43 | + <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" /> |
| 44 | + </svg> |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if (check.conclusion === 'SKIPPED' || check.conclusion === 'NEUTRAL') { |
| 49 | + return ( |
| 50 | + <svg className="w-3.5 h-3.5 text-muted-foreground" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 51 | + <path strokeLinecap="round" strokeLinejoin="round" d="M20 12H4" /> |
| 52 | + </svg> |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + // Fallback |
| 57 | + return ( |
| 58 | + <svg className="w-3.5 h-3.5 text-muted-foreground" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 59 | + <circle cx="12" cy="12" r="10" /> |
| 60 | + </svg> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +export const PRChecksTab: React.FC<PRChecksTabProps> = ({ context }) => { |
| 65 | + // Group checks by workflow |
| 66 | + const groupedChecks = useMemo(() => { |
| 67 | + const groups = new Map<string, PRCheck[]>(); |
| 68 | + for (const check of context.checks) { |
| 69 | + const workflow = check.workflowName || 'Other'; |
| 70 | + const existing = groups.get(workflow) || []; |
| 71 | + existing.push(check); |
| 72 | + groups.set(workflow, existing); |
| 73 | + } |
| 74 | + return groups; |
| 75 | + }, [context.checks]); |
| 76 | + |
| 77 | + const checkSummary = useMemo(() => { |
| 78 | + const total = context.checks.length; |
| 79 | + const passed = context.checks.filter(c => c.conclusion === 'SUCCESS').length; |
| 80 | + const failed = context.checks.filter(c => c.conclusion === 'FAILURE' || c.conclusion === 'TIMED_OUT').length; |
| 81 | + const pending = context.checks.filter(c => c.status !== 'COMPLETED').length; |
| 82 | + const skipped = context.checks.filter(c => c.conclusion === 'SKIPPED' || c.conclusion === 'NEUTRAL').length; |
| 83 | + return { total, passed, failed, pending, skipped }; |
| 84 | + }, [context.checks]); |
| 85 | + |
| 86 | + const isMerged = context.state === 'MERGED'; |
| 87 | + const isClosed = context.state === 'CLOSED'; |
| 88 | + const decisionStyle = DECISION_STYLES[context.reviewDecision]; |
| 89 | + const mergeStyle = isMerged |
| 90 | + ? { bg: 'bg-violet-500/15', text: 'text-violet-400', label: 'Merged' } |
| 91 | + : isClosed |
| 92 | + ? { bg: 'bg-destructive/15', text: 'text-destructive', label: 'Closed' } |
| 93 | + : MERGE_STATUS_STYLES[context.mergeStateStatus] ?? MERGE_STATUS_STYLES.UNKNOWN; |
| 94 | + const mergeableConflict = !isMerged && !isClosed && context.mergeable === 'CONFLICTING'; |
| 95 | + |
| 96 | + return ( |
| 97 | + <div className="p-3 space-y-4"> |
| 98 | + {/* Merge readiness */} |
| 99 | + <div className="space-y-2"> |
| 100 | + <h3 className="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground"> |
| 101 | + Merge Status |
| 102 | + </h3> |
| 103 | + |
| 104 | + <div className="space-y-1.5"> |
| 105 | + {/* Review decision */} |
| 106 | + {decisionStyle && ( |
| 107 | + <div className={`flex items-center gap-2 px-2 py-1.5 rounded-md ${decisionStyle.bg}`}> |
| 108 | + <span className={`text-xs font-medium ${decisionStyle.text}`}> |
| 109 | + {decisionStyle.label} |
| 110 | + </span> |
| 111 | + </div> |
| 112 | + )} |
| 113 | + |
| 114 | + {/* Merge state */} |
| 115 | + <div className={`flex items-center gap-2 px-2 py-1.5 rounded-md ${mergeStyle.bg}`}> |
| 116 | + <span className={`text-xs font-medium ${mergeStyle.text}`}> |
| 117 | + {mergeableConflict ? 'Has merge conflicts' : mergeStyle.label} |
| 118 | + </span> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + |
| 123 | + {/* Check runs */} |
| 124 | + {context.checks.length > 0 && ( |
| 125 | + <div className="space-y-2"> |
| 126 | + <div className="flex items-center justify-between"> |
| 127 | + <h3 className="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground"> |
| 128 | + Checks |
| 129 | + </h3> |
| 130 | + <span className="text-[10px] font-mono text-muted-foreground"> |
| 131 | + {checkSummary.passed}/{checkSummary.total} passed |
| 132 | + {checkSummary.failed > 0 && <span className="text-destructive ml-1">{checkSummary.failed} failed</span>} |
| 133 | + {checkSummary.pending > 0 && <span className="text-yellow-500 ml-1">{checkSummary.pending} pending</span>} |
| 134 | + </span> |
| 135 | + </div> |
| 136 | + |
| 137 | + <div className="space-y-3"> |
| 138 | + {Array.from(groupedChecks.entries()).map(([workflow, checks]) => ( |
| 139 | + <div key={workflow}> |
| 140 | + <div className="text-[10px] font-medium text-muted-foreground mb-1">{workflow}</div> |
| 141 | + <div className="space-y-0.5"> |
| 142 | + {checks.map((check, i) => ( |
| 143 | + <a |
| 144 | + key={`${check.name}-${i}`} |
| 145 | + href={check.detailsUrl || undefined} |
| 146 | + target="_blank" |
| 147 | + rel="noopener noreferrer" |
| 148 | + className="flex items-center gap-2 px-2 py-1 rounded hover:bg-muted/50 transition-colors group" |
| 149 | + > |
| 150 | + <CheckIcon check={check} /> |
| 151 | + <span className="text-xs text-foreground/80 truncate group-hover:text-foreground"> |
| 152 | + {check.name} |
| 153 | + </span> |
| 154 | + </a> |
| 155 | + ))} |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + ))} |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + )} |
| 162 | + |
| 163 | + {context.checks.length === 0 && !decisionStyle && ( |
| 164 | + <div className="flex flex-col items-center justify-center h-24 text-center"> |
| 165 | + <p className="text-xs text-muted-foreground">No checks configured.</p> |
| 166 | + </div> |
| 167 | + )} |
| 168 | + </div> |
| 169 | + ); |
| 170 | +}; |
0 commit comments