ask-page.tsx
This file
Path
apps/web/src/features/ask/ask-page.tsx
619 lines · 16.6 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
1import { Brand } from "@repo/brand";2import type { Verdict } from "@repo/protocol/domain/answer";3import type { Ask } from "@repo/protocol/domain/ask";4import { CheckSpec } from "@repo/protocol/domain/check";5import { Outcome } from "@repo/protocol/domain/outcome";6import type { AskId, Handle, ProjectName } from "@repo/protocol/ids";7import type { Posture } from "@repo/protocol/posture";8import type { DateTime } from "effect";9import { ChevronDown, Hourglass } from "lucide-react";10import { useState } from "react";11import type { ReactNode } from "react";1213import {14 deleteChildOf,15 familyHead,16 roundsAsTheyStand,17} from "../answer/subject.ts";18import { ClientFile } from "../code/client-code.tsx";19import { linesOf } from "../code/file-header.tsx";20import { NumberedLines } from "../code/numbered-lines.tsx";21import { blobHref, shortSha } from "../code/paths.ts";22import { postureLabel } from "../project/posture-label.ts";23import { familyMarks } from "../shared/ask-progress.ts";24import type { AskProgress } from "../shared/ask-progress.ts";25import {26 ClaimFacts,27 PostureFacts,28 TakeItFacts,29 TerritoryFacts,30} from "../shared/facts.tsx";31import { FamilyStrip } from "../shared/family.tsx";32import { HandleLink } from "../shared/handle.tsx";33import { AppLink } from "../shared/link.tsx";34import { answerPath, askPath } from "../shared/paths.ts";35import { MetaCode, MetaLine, Row } from "../shared/row.tsx";36import type { Crumb } from "../shared/shell/sections.ts";37import { Workspace } from "../shared/shell/workspace.tsx";38import { StatusLine, readStatus } from "../shared/status-line.tsx";39import type { StatusSubject } from "../shared/status-line.tsx";40import { When } from "../shared/time.tsx";41import { Region } from "./fields.tsx";42import { markdownPath } from "./markdown.ts";4344export interface CheckSource {45 readonly path: string;46 readonly sha: string;47 readonly text: string;48}4950const LINK = "decoration-rule underline hover:decoration-current";5152const NONE: ReadonlyMap<AskId, AskProgress> = new Map();5354const CHECK_LINES = 30;5556export const askCrumbs = (ask: Ask): readonly Crumb[] => [57 { label: ask.project, to: `/${ask.project}` },58 { label: "Asks", to: `/${ask.project}` },59 { label: ask.id, mono: true },60];6162export const claimCommand = (ask: Ask) =>63 `${Brand.cli} claim ${ask.id} --project ${ask.project}`;6465const CheckSummary = ({ ask }: { readonly ask: Ask }) =>66 CheckSpec.match(ask.check, {67 LintToZero: ({ rule, territory }) => (68 <>69 Lint to zero: <MetaCode>{rule}</MetaCode>70 {territory === undefined ? null : (71 <>72 {" "}73 in <MetaCode>{territory}</MetaCode>74 </>75 )}76 </>77 ),78 Migrate: ({ from, to }) => (79 <>80 Migrate: no caller of <MetaCode>{from}</MetaCode> is left81 {to === undefined ? null : (82 <>83 ; each moves to <MetaCode>{to}</MetaCode>84 </>85 )}86 </>87 ),88 Ratchet: ({ ratchet }) => (89 <>90 The ratchet <MetaCode>{ratchet}</MetaCode> goes down91 </>92 ),93 RedThenGreen: ({ path }) => (94 <>95 Red, then green:{" "}96 <AppLink97 className={`text-mono font-mono [overflow-wrap:anywhere] ${LINK}`}98 to={blobHref(ask.project, "main", path)}99 >100 {path}101 </AppLink>{" "}102 fails on main, and passes once the ask is answered.103 </>104 ),105 SymbolAbsent: ({ symbol }) => (106 <>107 Nothing uses <MetaCode>{symbol}</MetaCode> any more108 </>109 ),110 });111112const CheckFile = ({ source }: { readonly source: CheckSource }) => {113 const [whole, setWhole] = useState(false);114 const lines = linesOf(source.text);115 const long = lines.length > CHECK_LINES;116117 const shown =118 whole || !long ? source.text : lines.slice(0, CHECK_LINES).join("\n");119120 return (121 <figure className="border-rule flex min-w-0 flex-col border">122 <figcaption className="border-rule text-label text-muted-foreground flex h-9 min-w-0 items-center gap-3 border-b px-3">123 <span className="text-mono min-w-0 flex-1 truncate font-mono">124 {source.path}125 </span>126 <span className="shrink-0 tabular-nums">127 {lines.length} {lines.length === 1 ? "line" : "lines"}128 </span>129 </figcaption>130 <ClientFile131 fallback={<NumberedLines text={shown} />}132 key={whole ? "whole" : "head"}133 path={source.path}134 sha={whole || !long ? source.sha : `${source.sha}:${CHECK_LINES}`}135 text={shown}136 />137 {long ? (138 <button139 aria-expanded={whole}140 className="border-rule text-ui hover:bg-surface flex min-h-10 items-center gap-2 border-t px-3 text-left transition-colors duration-[120ms]"141 onClick={() => {142 setWhole((now) => !now);143 }}144 type="button"145 >146 <ChevronDown147 aria-hidden="true"148 className={`text-muted-foreground size-4 shrink-0 transition-transform duration-150 ${whole ? "rotate-180" : ""}`}149 strokeWidth={1.75}150 />151 {whole152 ? `Show the first ${CHECK_LINES} lines`153 : `Show the whole check, ${lines.length} lines`}154 </button>155 ) : null}156 </figure>157 );158};159160const BriefPart = ({161 children,162 title,163}: {164 readonly children: ReactNode;165 readonly title: string;166}) => (167 <div className="flex flex-col gap-1.5">168 <h3 className="text-ui font-semibold">{title}</h3>169 {children}170 </div>171);172173const BriefList = ({ items }: { readonly items: readonly string[] }) => (174 <ul className="flex list-[square] flex-col gap-1 pl-5 marker:text-[color:var(--rule-strong)]">175 {items.map((item) => (176 <li key={item}>{item}</li>177 ))}178 </ul>179);180181const Brief = ({ ask }: { readonly ask: Ask }) => {182 const { context, current, outOfScope, terms } = ask.brief;183184 const parts = [185 current === undefined ? null : (186 <BriefPart key="current" title="Current behaviour">187 <p>{current}</p>188 </BriefPart>189 ),190 outOfScope === undefined ? null : (191 <BriefPart key="out" title="Out of scope">192 <BriefList items={outOfScope} />193 </BriefPart>194 ),195 terms === undefined || terms.length === 0 ? null : (196 <BriefPart key="terms" title="Terms">197 <BriefList items={terms} />198 </BriefPart>199 ),200 context === undefined || context.length === 0 ? null : (201 <BriefPart key="context" title="Context">202 <BriefList items={context} />203 </BriefPart>204 ),205 ].filter((part) => part !== null);206207 return parts.length === 0 ? null : (208 <Region id="ask-brief" title="Brief">209 <div210 className="text-body flex max-w-(--measure) flex-col gap-5"211 data-said=""212 >213 {parts}214 </div>215 </Region>216 );217};218219const Answers = ({220 ask,221 now,222 rounds,223}: {224 readonly ask: Ask;225 readonly now: DateTime.Utc;226 readonly rounds: AskProgress["rounds"];227}) => (228 <Region id="ask-answers" title="Answers">229 {rounds.length === 0 ? (230 <p className="text-ui text-muted-foreground">231 No answer has been judged yet.232 </p>233 ) : (234 <ul aria-label="Answers, oldest first" className="border-rule border-t">235 {rounds.map(({ at, head, outcome, seq }, index) => {236 const { lead, mark } = readStatus({ kind: "answer", outcome });237238 return (239 <Row240 href={answerPath(ask.project, ask.id)}241 id={`${head}:${seq}`}242 key={seq}243 meta={<MetaCode>{shortSha(head)}</MetaCode>}244 search={{ head }}245 size="one-line"246 state={{ mark, time: <When at={at} now={now} />, word: lead }}247 title={`Round ${index + 1}`}248 />249 );250 })}251 </ul>252 )}253 <AppLink254 className={`text-ui self-start ${LINK}`}255 to={answerPath(ask.project, ask.id)}256 >257 Where the latest answer stands258 </AppLink>259 </Region>260);261262const WaitsOn = ({263 asks,264 project,265}: {266 readonly asks: readonly AskId[];267 readonly project: ProjectName;268}) => (269 <p className="text-ui mt-2 flex items-start gap-2" data-state="waiting">270 <Hourglass271 aria-hidden="true"272 className="text-waiting mt-0.5 size-4 shrink-0"273 strokeWidth={1.75}274 />275 <span className="min-w-0">276 Waits on{" "}277 {asks.map((ask, index) => (278 <span key={ask}>279 {index === 0 ? null : ", "}280 <AppLink281 className={`text-mono font-mono ${LINK}`}282 to={askPath(project, ask)}283 >284 {ask}285 </AppLink>286 </span>287 ))}{" "}288 to land first; a claim waits until they do.289 </span>290 </p>291);292293const FamilyWaits = ({294 finale,295 parent,296 project,297 states,298}: {299 readonly finale: AskId;300 readonly parent: Ask;301 readonly project: ProjectName;302 readonly states: ReadonlyMap<AskId, AskProgress>;303}) => {304 const children = parent.blockedBy.filter((child) => child !== finale);305306 const left = children.filter(307 (child) => states.get(child)?.landed === undefined308 ).length;309310 const deleteStep = (311 <AppLink className={LINK} to={askPath(project, finale)}>312 the delete step313 </AppLink>314 );315316 return (317 <p className="text-ui mt-2 flex items-start gap-2" data-state="waiting">318 <Hourglass319 aria-hidden="true"320 className="text-waiting mt-0.5 size-4 shrink-0"321 strokeWidth={1.75}322 />323 <span className="min-w-0">324 Waits for{" "}325 {left === 0 ? null : (326 <>327 {left} of {children.length}{" "}328 {children.length === 1 ? "child" : "children"} and{" "}329 </>330 )}331 {deleteStep} to land first; a claim waits until they do.332 </span>333 </p>334 );335};336337export interface AskViewProps {338 readonly ask: Ask;339 readonly brief: string;340 readonly now: DateTime.Utc;341 readonly platformUrl: string;342 readonly states?: ReadonlyMap<AskId, AskProgress>;343 readonly parent?: Ask | undefined;344 readonly verdict?: Verdict | undefined;345 readonly check?: CheckSource | undefined;346 readonly posture?: Posture | undefined;347 readonly viewer?: Handle | undefined;348}349350const subjectOf = (351 ask: Ask,352 states: ReadonlyMap<AskId, AskProgress>,353 verdict: Verdict | undefined,354 takeable: boolean355): StatusSubject => {356 const progress = states.get(ask.id);357 const claimant = progress?.claimant;358 const latest = progress?.latest;359360 if (progress?.landed !== undefined) {361 return {362 kind: "answer",363 outcome: Outcome.cases.Landed.make({ sha: progress.landed.sha }),364 project: ask.project,365 };366 }367368 const lastRound = progress?.rounds.at(-1);369370 if (371 latest !== undefined &&372 Outcome.isAnyOf(["Refused"])(latest) &&373 verdict !== undefined &&374 verdict.head === lastRound?.head375 ) {376 return {377 claimant,378 kind: "answer",379 outcome: latest,380 project: ask.project,381 stage: verdict.stage,382 };383 }384385 return {386 blockers:387 ask.parent !== undefined && ask.blockedBy.length > 0388 ? familyMarks(ask, states)389 : undefined,390 claimant,391 kind: "ask",392 latest,393 ask,394 takeable,395 };396};397398const standing = (399 ask: Ask,400 states: ReadonlyMap<AskId, AskProgress>,401 parent: Ask | undefined402) => {403 const progress = states.get(ask.id);404405 const remaining = ask.blockedBy.filter(406 (blocker) => states.get(blocker)?.landed === undefined407 );408409 const family = familyHead(ask, parent);410 const latest = progress?.latest;411412 return {413 family,414 finale: family === undefined ? undefined : deleteChildOf(family),415 held: progress?.claimant !== undefined || ask.state === "claimed",416 progress,417 remaining,418 takeable: ask.state === "open" && remaining.length === 0,419 waitsOn:420 ask.parent === undefined &&421 remaining.length > 0 &&422 !(latest !== undefined && Outcome.isAnyOf(["WaitingOnParent"])(latest)),423 };424};425426const AskFacts = ({427 ask,428 now,429 parent,430 platformUrl,431 posture,432 states,433 viewer,434}: {435 readonly ask: Ask;436 readonly now: DateTime.Utc;437 readonly parent: Ask | undefined;438 readonly platformUrl: string;439 readonly posture: Posture | undefined;440 readonly states: ReadonlyMap<AskId, AskProgress>;441 readonly viewer: Handle | undefined;442}) => {443 const { held, progress, takeable } = standing(ask, states, parent);444445 return (446 <>447 {takeable ? <TakeItFacts ask={ask} platformUrl={platformUrl} /> : null}448 {!takeable && held && ask.state !== "closed" ? (449 <ClaimFacts450 now={now}451 {...(progress?.claimant === undefined452 ? {}453 : { claimant: progress.claimant })}454 {...(progress?.lastPush === undefined455 ? {}456 : { lastPush: progress.lastPush })}457 {...(viewer === undefined ? {} : { viewer })}458 />459 ) : null}460 <TerritoryFacts ask={ask} />461 {posture === undefined ? null : (462 <PostureFacts463 label={postureLabel(posture)}464 posture={posture}465 project={ask.project}466 />467 )}468 </>469 );470};471472const AskMeta = ({473 ask,474 now,475}: {476 readonly ask: Ask;477 readonly now: DateTime.Utc;478}) => (479 <MetaLine480 parts={[481 ask.kind,482 <span key="asked">483 asked by <HandleLink handle={ask.askedBy} />484 {ask.mentor === undefined ? null : (485 <>486 , mentored by <HandleLink handle={ask.mentor} />487 </>488 )}489 </span>,490 <span key="opened">491 opened <When at={ask.openedAt} now={now} />492 </span>,493 ]}494 />495);496497const ForAgents = ({498 ask,499 brief,500}: {501 readonly ask: Ask;502 readonly brief: string;503}) => (504 <section aria-label="For agents" className="flex min-w-0 flex-col gap-3">505 <details className="group border-rule border-y">506 <summary className="text-ui hover:bg-surface/50 flex min-h-10 cursor-pointer list-none items-center gap-3 transition-colors duration-[120ms] [&::-webkit-details-marker]:hidden">507 <span className="min-w-0 flex-1">The brief, as an agent reads it</span>508 <ChevronDown509 aria-hidden="true"510 className="text-muted-foreground size-4 shrink-0 transition-transform duration-150 group-open:rotate-180"511 strokeWidth={1.75}512 />513 </summary>514 <pre className="border-rule bg-surface text-mono leading-terminal mb-3 overflow-x-auto border p-3 font-mono whitespace-pre-wrap">515 {brief}516 </pre>517 </details>518 <p className="text-ui text-muted-foreground">519 {/* A server route, not one of the router's pages. */}520 <a521 className={`text-foreground ${LINK}`}522 href={markdownPath(ask.project, ask.id)}523 type="text/markdown"524 >525 This ask as Markdown526 </a>527 , for an agent to fetch.528 </p>529 </section>530);531532export const AskView = ({533 ask,534 brief,535 check,536 now,537 parent,538 platformUrl,539 posture,540 states = NONE,541 verdict,542 viewer,543}: AskViewProps) => {544 const { family, finale, progress, remaining, takeable, waitsOn } = standing(545 ask,546 states,547 parent548 );549550 const subject = subjectOf(ask, states, verdict, takeable);551552 return (553 <Workspace554 crumbs={askCrumbs(ask)}555 facts={556 <AskFacts557 ask={ask}558 now={now}559 parent={parent}560 platformUrl={platformUrl}561 posture={posture}562 states={states}563 viewer={viewer}564 />565 }566 factsHeading="This ask"567 meta={<AskMeta ask={ask} now={now} />}568 state={<StatusLine compact subject={subject} />}569 status={570 <>571 <StatusLine subject={subject} />572 {waitsOn && family === ask && finale !== undefined ? (573 <FamilyWaits574 finale={finale}575 parent={ask}576 project={ask.project}577 states={states}578 />579 ) : null}580 {waitsOn && family !== ask ? (581 <WaitsOn asks={remaining} project={ask.project} />582 ) : null}583 {family === undefined ? null : (584 <div className="mt-4">585 <FamilyStrip586 expanded={false}587 parent={family}588 project={ask.project}589 stamps={new Set()}590 states={states}591 {...(finale === undefined ? {} : { finale })}592 />593 </div>594 )}595 </>596 }597 said598 title={ask.brief.want}599 {...(takeable ? { primaryCommand: claimCommand(ask) } : {})}600 >601 <Brief ask={ask} />602603 <Region id="ask-check" title="The check">604 <p className="text-ui max-w-(--measure)">605 <CheckSummary ask={ask} />606 </p>607 {check === undefined ? null : <CheckFile source={check} />}608 </Region>609610 <Answers611 ask={ask}612 now={now}613 rounds={roundsAsTheyStand(progress, verdict)}614 />615616 <ForAgents ask={ask} brief={brief} />617 </Workspace>618 );619};