Spaces:
Sleeping
Sleeping
File size: 5,035 Bytes
23d418f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import { forwardRef } from 'react'
import type { ArchChapter, ArchStep } from '@/lib/types'
import { cn } from '@/lib/utils'
import { Diagram } from './Diagrams'
/** One chapter (one level of detail): its steps on the left, read top to bottom, and its
* diagram pinned on the right (on a phone, above the steps). The active step (the one
* being read, or the one clicked) lights up the boxes it talks about. */
export function Chapter({
chapter,
number,
active,
onStep,
stepRef,
}: {
chapter: ArchChapter
number: number
active: number // the step being read in this chapter, -1 for none
onStep: (i: number) => void
stepRef: (i: number, el: HTMLElement | null) => void
}) {
const lit = active >= 0 ? chapter.steps[active].highlight : null
return (
<section id={chapter.id} className="scroll-mt-4 border-t pt-10">
<div className="mb-6 max-w-2xl">
<p className="text-xs font-semibold tracking-wide text-primary uppercase">Step {number}</p>
<h2 className="text-2xl font-semibold tracking-tight">{chapter.title}</h2>
<p className="mt-1 text-muted-foreground">{chapter.lead}</p>
</div>
<div className="grid gap-8 lg:grid-cols-[minmax(0,5fr)_minmax(0,6fr)]">
<div className="order-2 lg:order-1">
{chapter.steps.map((s, i) => (
<Step key={s.id} ref={(el) => stepRef(i, el)} step={s} active={i === active} onClick={() => onStep(i)} />
))}
</div>
<div className="order-1 lg:order-2">
<figure className="rounded-2xl border bg-muted/40 p-4 lg:sticky lg:top-4" aria-label={`Diagram: ${chapter.title}`}>
<figcaption className="mb-3 flex items-baseline justify-between gap-2">
<span className="text-sm font-semibold">{chapter.title}</span>
<span className="text-[11px] text-muted-foreground">
{chapter.diagram.kind === 'grid' ? 'Lit row: the step you are reading · — : no such step' : 'Lit boxes: the step you are reading'}
</span>
</figcaption>
<Diagram diagram={chapter.diagram} lit={lit} />
</figure>
</div>
</div>
</section>
)
}
const Step = forwardRef<HTMLDivElement, { step: ArchStep; active: boolean; onClick: () => void }>(function Step(
{ step, active, onClick },
ref,
) {
const d = step.details
const hasDetails = d.numbers.length > 0 || d.example || d.tried.length > 0 || d.files.length > 0
return (
<div ref={ref} className="pb-10 lg:min-h-[38vh]">
<div
role="button"
tabIndex={0}
onClick={onClick}
onKeyDown={(e) => (e.key === 'Enter' || e.key === ' ') && (e.preventDefault(), onClick())}
aria-pressed={active}
className={cn(
'cursor-pointer rounded-xl border-l-4 py-1 pl-4 transition-colors outline-none focus-visible:ring-2 focus-visible:ring-primary',
active ? 'border-primary' : 'border-transparent hover:border-border',
)}
>
<p className={cn('text-lg leading-snug font-semibold', !active && 'text-foreground/80')}>{step.point}</p>
<p className="mt-2 leading-relaxed text-muted-foreground">{step.body}</p>
</div>
{hasDetails && (
<details className="mt-3 ml-5 text-sm">
<summary className="cursor-pointer text-xs font-medium text-primary">Details: numbers, a real example, what we tried</summary>
<div className="mt-3 space-y-4">
{d.numbers.length > 0 && (
<dl className="grid gap-x-6 gap-y-1.5 sm:grid-cols-2">
{d.numbers.map((x) => (
<div key={x.label} className="border-b pb-1.5">
<dt className="text-xs text-muted-foreground">{x.label}</dt>
<dd className="font-medium break-words">{x.value}</dd>
</div>
))}
</dl>
)}
{d.example && (
<div>
<p className="mb-1 text-xs font-medium text-muted-foreground">Real example · {d.example.title}</p>
<pre className="rounded-lg bg-muted p-3 font-mono text-xs leading-relaxed whitespace-pre-wrap break-words">{d.example.body}</pre>
</div>
)}
{d.tried.length > 0 && (
<div>
<p className="mb-1 text-xs font-medium text-muted-foreground">Tried or considered, and rejected</p>
<ul className="list-disc space-y-1 pl-5">
{d.tried.map((t) => (
<li key={t}>{t}</li>
))}
</ul>
</div>
)}
{d.files.length > 0 && (
<div className="flex flex-wrap gap-1.5">
{d.files.map((f) => (
<code key={f} className="rounded-md bg-muted px-1.5 py-0.5 text-[11px]">
{f}
</code>
))}
</div>
)}
</div>
</details>
)}
</div>
)
})
|