|
| 1 | +import { ExternalLink, Loader2 } from 'lucide-react' |
| 2 | + |
| 3 | +import type { MergeReadiness, PrReadinessSnapshot, ReviewSummary } from '../api/types' |
| 4 | + |
| 5 | +type Props = { |
| 6 | + readiness?: PrReadinessSnapshot |
| 7 | + isLoading?: boolean |
| 8 | + error?: unknown |
| 9 | + onOpenReview?: (reviewId: string) => void |
| 10 | +} |
| 11 | + |
| 12 | +const READINESS_STYLES: Record<MergeReadiness, string> = { |
| 13 | + Ready: 'bg-sev-suggestion/10 text-sev-suggestion border border-sev-suggestion/20', |
| 14 | + NeedsAttention: 'bg-sev-warning/10 text-sev-warning border border-sev-warning/20', |
| 15 | + NeedsReReview: 'bg-accent/10 text-accent border border-accent/20', |
| 16 | +} |
| 17 | + |
| 18 | +const READINESS_LABELS: Record<MergeReadiness, string> = { |
| 19 | + Ready: 'Merge Ready', |
| 20 | + NeedsAttention: 'Needs Attention', |
| 21 | + NeedsReReview: 'Needs Re-review', |
| 22 | +} |
| 23 | + |
| 24 | +export function PrReadinessSummary({ readiness, isLoading = false, error, onOpenReview }: Props) { |
| 25 | + const latestReview = readiness?.latest_review |
| 26 | + const summary = latestReview?.summary |
| 27 | + |
| 28 | + return ( |
| 29 | + <div className="mb-4 rounded-lg border border-border-subtle bg-surface p-3"> |
| 30 | + <div className="flex items-start justify-between gap-3 mb-3"> |
| 31 | + <div> |
| 32 | + <div className="text-[13px] text-text-primary">Latest DiffScope readiness</div> |
| 33 | + <div className="text-[11px] text-text-muted mt-0.5"> |
| 34 | + Merge guidance from the latest stored DiffScope review for this PR. |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + {latestReview?.id && onOpenReview && ( |
| 38 | + <button |
| 39 | + type="button" |
| 40 | + onClick={() => onOpenReview(latestReview.id)} |
| 41 | + className="inline-flex items-center gap-1 px-2 py-1 rounded text-[11px] font-medium bg-surface-2 border border-border text-text-secondary hover:text-text-primary hover:border-text-muted transition-colors" |
| 42 | + > |
| 43 | + Open latest review <ExternalLink size={12} /> |
| 44 | + </button> |
| 45 | + )} |
| 46 | + </div> |
| 47 | + |
| 48 | + {isLoading ? ( |
| 49 | + <div className="flex items-center gap-2 text-[12px] text-text-muted"> |
| 50 | + <Loader2 size={14} className="animate-spin" /> |
| 51 | + Loading readiness… |
| 52 | + </div> |
| 53 | + ) : error ? ( |
| 54 | + <div className="text-[12px] text-sev-error"> |
| 55 | + {error instanceof Error ? error.message : 'Failed to load readiness'} |
| 56 | + </div> |
| 57 | + ) : !latestReview ? ( |
| 58 | + <div className="text-[12px] text-text-secondary"> |
| 59 | + No DiffScope review has been recorded for this PR yet. Start a review below to populate merge guidance. |
| 60 | + </div> |
| 61 | + ) : !summary ? ( |
| 62 | + <div className="text-[12px] text-text-secondary"> |
| 63 | + The latest DiffScope review does not have a readiness summary yet. |
| 64 | + </div> |
| 65 | + ) : ( |
| 66 | + <> |
| 67 | + <div className="flex flex-wrap items-center gap-2 mb-3"> |
| 68 | + <span className={`text-[10px] px-2 py-0.5 rounded font-code ${READINESS_STYLES[summary.merge_readiness]}`}> |
| 69 | + {READINESS_LABELS[summary.merge_readiness]} |
| 70 | + </span> |
| 71 | + <span className="text-[10px] text-text-muted font-code"> |
| 72 | + Review {latestReview.id.slice(0, 8)} |
| 73 | + </span> |
| 74 | + <span className="text-[10px] text-text-muted font-code">{latestReview.status}</span> |
| 75 | + </div> |
| 76 | + |
| 77 | + <div className="grid grid-cols-2 gap-3 text-[11px] mb-3"> |
| 78 | + <Metric label="Open blockers" value={String(summary.open_blockers)} tone={summary.open_blockers > 0 ? 'warning' : 'success'} /> |
| 79 | + <Metric label="Verification" value={summary.verification.state} /> |
| 80 | + <Metric label="Lifecycle" value={`${summary.open_comments} open`} hint={`${summary.resolved_comments} resolved · ${summary.dismissed_comments} dismissed`} /> |
| 81 | + <Metric label="Findings" value={String(summary.total_comments)} hint={`${latestReview.files_reviewed} file${latestReview.files_reviewed === 1 ? '' : 's'} reviewed`} /> |
| 82 | + </div> |
| 83 | + |
| 84 | + {(readiness?.current_head_sha || latestReview.reviewed_head_sha) && ( |
| 85 | + <div className="grid grid-cols-2 gap-3 text-[10px] font-code text-text-muted mb-3"> |
| 86 | + {readiness?.current_head_sha && ( |
| 87 | + <Metric label="Current head" value={shortSha(readiness.current_head_sha)} /> |
| 88 | + )} |
| 89 | + {latestReview.reviewed_head_sha && ( |
| 90 | + <Metric label="Reviewed head" value={shortSha(latestReview.reviewed_head_sha)} /> |
| 91 | + )} |
| 92 | + </div> |
| 93 | + )} |
| 94 | + |
| 95 | + <div className="rounded border border-border-subtle bg-surface-1 p-3"> |
| 96 | + <div className="text-[11px] font-medium text-text-primary mb-2">What still blocks merge</div> |
| 97 | + <ul className="space-y-1 text-[11px] text-text-secondary list-disc pl-4"> |
| 98 | + {buildReadinessReasons(summary).map(reason => ( |
| 99 | + <li key={reason}>{reason}</li> |
| 100 | + ))} |
| 101 | + </ul> |
| 102 | + </div> |
| 103 | + </> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +function buildReadinessReasons(summary: ReviewSummary): string[] { |
| 110 | + const reasons: string[] = [] |
| 111 | + if (summary.open_blockers > 0) { |
| 112 | + reasons.push(`${summary.open_blockers} blocking finding${summary.open_blockers === 1 ? '' : 's'} ${summary.open_blockers === 1 ? 'remains' : 'remain'} open.`) |
| 113 | + } |
| 114 | + reasons.push(...summary.readiness_reasons) |
| 115 | + |
| 116 | + if (reasons.length === 0) { |
| 117 | + reasons.push('No open blockers remain in the latest DiffScope review.') |
| 118 | + } |
| 119 | + |
| 120 | + return reasons |
| 121 | +} |
| 122 | + |
| 123 | +function shortSha(sha: string): string { |
| 124 | + return sha.slice(0, 12) |
| 125 | +} |
| 126 | + |
| 127 | +function Metric({ |
| 128 | + label, |
| 129 | + value, |
| 130 | + hint, |
| 131 | + tone = 'default', |
| 132 | +}: { |
| 133 | + label: string |
| 134 | + value: string |
| 135 | + hint?: string |
| 136 | + tone?: 'default' | 'warning' | 'success' |
| 137 | +}) { |
| 138 | + const toneClass = tone === 'warning' |
| 139 | + ? 'text-sev-warning' |
| 140 | + : tone === 'success' |
| 141 | + ? 'text-sev-suggestion' |
| 142 | + : 'text-text-primary' |
| 143 | + |
| 144 | + return ( |
| 145 | + <div> |
| 146 | + <div className="text-text-muted">{label}</div> |
| 147 | + <div className={`mt-0.5 ${toneClass}`}>{value}</div> |
| 148 | + {hint && <div className="text-text-muted mt-0.5">{hint}</div>} |
| 149 | + </div> |
| 150 | + ) |
| 151 | +} |
0 commit comments