119 lines · 5.3 KBRaw
| 1 | import { WarningCircleIcon } from "@refs/design-system/icons"; |
| 2 | import type { DecisionRecord } from "@refs/protocol"; |
| 3 | |
| 4 | export interface DecisionPicks { |
| 5 | readonly challenge: (assumption: string) => void; |
| 6 | readonly takePath: (name: string) => void; |
| 7 | } |
| 8 | |
| 9 | function Section({ title, children }: { title: string; children: React.ReactNode }) { |
| 10 | return ( |
| 11 | <section className="flex flex-col gap-2"> |
| 12 | <h4 className="caption m-0 text-faint">{title}</h4> |
| 13 | {children} |
| 14 | </section> |
| 15 | ); |
| 16 | } |
| 17 | |
| 18 | function Pick({ label, onPick }: { label: string; onPick: () => void }) { |
| 19 | return ( |
| 20 | <button type="button" onClick={onPick} className="shrink-0 cursor-pointer rounded-control text-compact text-accent-text opacity-0 transition-opacity duration-150 group-hover:opacity-100 focus-visible:opacity-100 focus-visible:outline-2 focus-visible:outline-accent-hover max-sm:opacity-100"> |
| 21 | {label} |
| 22 | </button> |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | function basename(path: string): string { |
| 27 | return path.split("/").filter(Boolean).pop() ?? path; |
| 28 | } |
| 29 | |
| 30 | export function DecisionView({ record, picks, muted = false }: { record: DecisionRecord; picks?: DecisionPicks; muted?: boolean }) { |
| 31 | const notTaken = [...new Set([...record.alternativesConsidered, ...Object.keys(record.rejected)])]; |
| 32 | |
| 33 | return ( |
| 34 | <div className="flex min-w-0 flex-col gap-5 wrap-anywhere"> |
| 35 | <p className={`m-0 max-w-[60ch] text-headline leading-snug font-semibold text-balance ${muted ? "text-muted line-through decoration-1" : "text-ink"}`}>{record.decision}</p> |
| 36 | |
| 37 | {record.reason.length > 0 && ( |
| 38 | <Section title="Why"> |
| 39 | <ul className="m-0 flex max-w-[70ch] list-none flex-col gap-1.5 p-0 text-compact leading-relaxed"> |
| 40 | {record.reason.map((reason) => ( |
| 41 | <li key={reason} className="flex gap-2.5"> |
| 42 | <span aria-hidden="true" className="mt-2 size-1 shrink-0 rounded-full bg-current opacity-50" /> |
| 43 | <span>{reason}</span> |
| 44 | </li> |
| 45 | ))} |
| 46 | </ul> |
| 47 | </Section> |
| 48 | )} |
| 49 | |
| 50 | {record.toReverse && ( |
| 51 | <Section title="If this changes later"> |
| 52 | <p className="m-0 max-w-[70ch] text-compact leading-relaxed">{record.toReverse}</p> |
| 53 | </Section> |
| 54 | )} |
| 55 | |
| 56 | {notTaken.length > 0 && ( |
| 57 | <Section title="Paths considered"> |
| 58 | <ol className="m-0 flex max-w-[70ch] list-none flex-col p-0 text-sm"> |
| 59 | <li className="relative flex items-center gap-3 pb-3"> |
| 60 | <span aria-hidden="true" className="relative z-1 size-2.5 shrink-0 rounded-full bg-accent-hover ring-4 ring-panel" /> |
| 61 | <span className="font-medium">Chosen</span> |
| 62 | <span aria-hidden="true" className="absolute top-3 bottom-0 left-[4.5px] w-px bg-line" /> |
| 63 | </li> |
| 64 | {notTaken.map((name, index) => ( |
| 65 | <li key={name} className="group relative flex items-start gap-3 pb-3 last:pb-0"> |
| 66 | <span aria-hidden="true" className="relative z-1 mt-1 size-2.5 shrink-0 rounded-full shadow-[inset_0_0_0_1px_var(--line)] bg-panel" /> |
| 67 | <span aria-hidden="true" className={`absolute top-0 left-[4.5px] w-px bg-line ${index < notTaken.length - 1 ? "bottom-0" : "h-2"}`} /> |
| 68 | <span className="flex min-w-0 flex-1 flex-col gap-0.5"> |
| 69 | <span className="text-ink">{name}</span> |
| 70 | {record.rejected[name] && <span className="text-muted">{record.rejected[name]}</span>} |
| 71 | </span> |
| 72 | {picks && <Pick label="Take this path" onPick={() => picks.takePath(name)} />} |
| 73 | </li> |
| 74 | ))} |
| 75 | </ol> |
| 76 | </Section> |
| 77 | )} |
| 78 | |
| 79 | {record.assumptions.length > 0 && ( |
| 80 | <Section title="Assumes"> |
| 81 | <ul className="m-0 flex max-w-[70ch] list-none flex-col gap-1.5 p-0 text-compact leading-relaxed"> |
| 82 | {record.assumptions.map((assumption) => ( |
| 83 | <li key={assumption} className="group flex items-start gap-2.5"> |
| 84 | <WarningCircleIcon size={16} className="mt-0.5 shrink-0 text-caution" aria-hidden="true" /> |
| 85 | <span className="min-w-0 flex-1">{assumption}</span> |
| 86 | {picks && <Pick label="Challenge" onPick={() => picks.challenge(assumption)} />} |
| 87 | </li> |
| 88 | ))} |
| 89 | </ul> |
| 90 | </Section> |
| 91 | )} |
| 92 | |
| 93 | {record.affectedComponents.length > 0 && ( |
| 94 | <Section title="Touches"> |
| 95 | <ul className="m-0 flex list-none flex-wrap gap-1.5 p-0"> |
| 96 | {record.affectedComponents.map((path) => ( |
| 97 | <li key={path}> |
| 98 | <code title={path} className="rounded-chip bg-sunken px-1.5 py-0.5 shadow-[inset_0_0_0_1px_var(--hairline)] font-mono text-xs text-muted"> |
| 99 | {basename(path)} |
| 100 | </code> |
| 101 | </li> |
| 102 | ))} |
| 103 | </ul> |
| 104 | </Section> |
| 105 | )} |
| 106 | |
| 107 | {record.evidence.length > 0 && ( |
| 108 | <details className="group/evidence text-compact"> |
| 109 | <summary className="w-fit cursor-pointer rounded text-xs font-medium text-muted select-none hover:text-ink focus-visible:outline-2 focus-visible:outline-accent-hover"> |
| 110 | Evidence · {record.evidence.length} |
| 111 | </summary> |
| 112 | <ul className="m-0 mt-2 flex max-w-[70ch] list-none flex-col gap-1 p-0 text-muted"> |
| 113 | {record.evidence.map((item) => <li key={item}>{item}</li>)} |
| 114 | </ul> |
| 115 | </details> |
| 116 | )} |
| 117 | </div> |
| 118 | ); |
| 119 | } |