Spaces:
Sleeping

TBM99's picture
Deploy coverage-check 3559250
23d418f verified
Raw History Blame Contribute Delete
6.68 kB
import { ArrowDown, ArrowRight, CornerLeftUp, RotateCcw } from 'lucide-react'
import type { ReactNode } from 'react'
import { boxState } from '@/lib/architecture'
import { SERIES } from '@/lib/chart'
import type { ArchBox, ArchDiagram, SystemId } from '@/lib/types'
import { cn } from '@/lib/utils'
/** The chapter's diagram, drawn by hand (owner, 9d redo: a few boxes, no crossing lines,
* one level of detail per diagram). `lit`: the boxes the step being read talks about,
* or null when no step is active (nothing dimmed). */
export function Diagram({ diagram, lit }: { diagram: ArchDiagram; lit: string[] | null }) {
if (diagram.kind === 'grid') return <EvidenceGrid diagram={diagram} lit={lit} />
const b = (id: string, className?: string) => <Box key={id} box={diagram.boxes[id]} state={boxState(id, lit)} className={className} />
switch (diagram.kind) {
case 'documents':
return (
<Stack>
<div className="grid grid-cols-3 gap-2">{['oap', 'opcf', 'laws'].map((id) => b(id))}</div>
<Down />
{b('corpus', 'mx-auto w-2/3')}
</Stack>
)
case 'prepare':
return (
<Stack>
{b('docs', 'mx-auto w-1/2')}
<Down />
{b('parse', 'mx-auto w-1/2')}
<div className="grid grid-cols-2 gap-3">
<Down />
<Down />
<div className="space-y-1">
<Tag>System A</Tag>
{b('windows')}
</div>
<div className="space-y-1">
<Tag>Systems B and C</Tag>
{b('sections')}
<Down />
{b('refs')}
</div>
<Down />
<Down />
</div>
{b('index', 'mx-auto w-1/2')}
</Stack>
)
case 'answer':
return (
<Stack>
{b('passages', 'mx-auto w-2/3')}
<Down />
{b('answer', 'mx-auto w-2/3')}
<Down />
<div className="grid grid-cols-[2fr_auto_1fr] items-center gap-2">
{b('check')}
<span className="flex flex-col items-center text-muted-foreground" aria-hidden>
<ArrowRight className="size-4" />
<CornerLeftUp className="size-4" />
</span>
{b('retry')}
</div>
<Down />
{b('reply', 'mx-auto w-2/3')}
</Stack>
)
case 'measure':
return (
<Stack>
<div className="grid grid-cols-2 gap-3">
{b('tuning')}
{b('heldout')}
<p className="text-center text-[11px] text-muted-foreground">tunes the systems</p>
<Down />
</div>
<div className="ml-auto w-1/2 space-y-2">
{b('grading')}
<Down />
{b('stats')}
<Down />
{b('results')}
</div>
</Stack>
)
}
}
/** System A | B | C as aligned columns, the same steps on the same rows (small
* multiples): a step a system does not have is an empty dashed slot, so the
* differences show at a glance. A row lights up when the step being read names it. */
function EvidenceGrid({ diagram, lit }: { diagram: Extract<ArchDiagram, { kind: 'grid' }>; lit: string[] | null }) {
const systems: SystemId[] = ['a', 'b', 'c']
return (
<div className="grid grid-cols-[5.5rem_repeat(3,minmax(0,1fr))] gap-x-2 gap-y-1.5 text-xs">
<span />
{systems.map((s) => (
<div key={s} className="pb-1 text-center">
<span className="inline-flex items-center gap-1.5 font-semibold">
<span className="size-2.5 rounded-sm" style={{ background: SERIES[s] }} aria-hidden />
{diagram.columns[s].title}
</span>
<span className="block text-[11px] text-muted-foreground">{diagram.columns[s].name}</span>
</div>
))}
{diagram.rows.map((row) => {
const state = boxState(row.id, lit)
return (
<div key={row.id} className="contents">
<span className={cn('flex items-center text-[11px] font-medium text-muted-foreground transition-opacity', state === 'dim' && 'opacity-40', state === 'lit' && 'text-primary')}>
{row.label}
</span>
{systems.map((s) => {
const cell = row.cells[s]
return cell ? (
<Box key={s} box={cell} state={state} compact again={row.id === 'again'} />
) : (
<div
key={s}
className={cn('flex items-center justify-center rounded-lg border border-dashed text-muted-foreground/60 transition-opacity', state === 'dim' && 'opacity-40')}
aria-label={`System ${s.toUpperCase()} has no such step`}
>
—
</div>
)
})}
</div>
)
})}
<span className="flex items-center pt-2 text-[11px] font-medium text-muted-foreground">Final test</span>
{systems.map((s) => (
<div key={s} className="pt-2 text-center">
<span className="block text-lg font-semibold">{diagram.results[s].accuracy}</span>
<span className="block text-[11px] text-muted-foreground">correct · {diagram.results[s].time}</span>
</div>
))}
</div>
)
}
function Box({ box, state, className, compact, again }: { box: ArchBox; state: 'lit' | 'dim' | 'plain'; className?: string; compact?: boolean; again?: boolean }) {
return (
<div
className={cn(
'rounded-xl border bg-background text-center shadow-sm transition-all duration-300',
compact ? 'px-2 py-1.5' : 'px-3 py-2.5',
state === 'lit' && 'border-primary bg-accent shadow-md ring-2 ring-primary/25',
state === 'dim' && 'opacity-40',
className,
)}
>
<span className={cn('flex items-center justify-center gap-1 font-medium', compact ? 'text-xs' : 'text-sm')}>
{again && <RotateCcw className="size-3 text-muted-foreground" aria-label="a loop back to the judge" />}
{box.title}
</span>
{box.sub && <span className="block text-[11px] leading-snug text-muted-foreground">{box.sub}</span>}
</div>
)
}
function Stack({ children }: { children: ReactNode }) {
return <div className="space-y-2">{children}</div>
}
function Down() {
return (
<div className="flex justify-center text-muted-foreground/70" aria-hidden>
<ArrowDown className="size-4" />
</div>
)
}
function Tag({ children }: { children: ReactNode }) {
return <p className="text-center text-[10px] font-medium tracking-wide text-muted-foreground uppercase">{children}</p>
}