ref-switcher.tsx
This file
Path
apps/web/src/features/code/ref-switcher.tsx
280 lines · 8.3 KiB · TypeScript
Last changed by
Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281/** * The code bar's revision picker (ADR 0043): `main`, the project's tags, and * the answers in claim forks, each with its claimant, ask and claim state. A * fork's entry opens the same page in that fork, at its address * (`@<handle>/<ask>`), never its repo's name. Its trigger reads "At main", * "At v1.2.0" or "At <handle>/<ask>": a tag and a fork are revisions, not * branches, so no branch is named and main carries no branch mark. */import { forkRev } from "@repo/protocol/domain/code";import type { ForkRef, RefsView } from "@repo/protocol/domain/code";import type { AskId, ProjectName } from "@repo/protocol/ids";import { ChevronDown, GitCommitHorizontal, GitFork, Tag } from "lucide-react";import { useState } from "react";import type { ComponentType } from "react";
import { Popover, PopoverContent, PopoverTrigger,} from "@/components/ui/popover";
import { AppLink } from "../shared/link.tsx";import { StateMarkIcon } from "../shared/marks.tsx";import { CLAIM_STATES } from "../shared/row.tsx";import { isCommitId } from "./headers.ts";import { isForkRev, readsFork, revLabel } from "./rev.ts";
const SHOWN = 100;
const ICON = "size-4 shrink-0";
/** A tag's, a commit's or a fork's mark; main has none, since it is the project itself, not a ref among refs. */const RefMark = ({ rev }: { readonly rev: string }) => { if (rev === "main") { return null; }
if (isForkRev(rev)) { return <GitFork aria-hidden="true" className={ICON} strokeWidth={1.75} />; }
return isCommitId(rev) ? ( <GitCommitHorizontal aria-hidden="true" className={ICON} strokeWidth={1.75} /> ) : ( <Tag aria-hidden="true" className={ICON} strokeWidth={1.75} /> );};
const ROW = "text-ui hover:bg-surface flex min-h-8 min-w-0 items-center gap-2 px-2 py-1 no-underline aria-[current]:bg-surface aria-[current]:font-semibold";
const GROUP = "text-label text-muted-foreground px-2 pt-2 pb-1 font-medium";
/** A claim fork that holds an answer: the only forks the switcher lists. */type AnswerFork = ForkRef & { readonly ask: AskId };
const answerForks = (refs: RefsView | undefined): readonly AnswerFork[] => (refs?.forks ?? []).flatMap((fork) => fork.kind === "claim" && fork.ask !== undefined ? [{ ...fork, ask: fork.ask }] : [] );
const matches = (filter: string) => (text: string) => text.toLowerCase().includes(filter.trim().toLowerCase());
const ForkEntry = ({ fork, hrefAt, onPick, rev,}: { readonly fork: AnswerFork; readonly hrefAt: (rev: string) => string; readonly onPick: () => void; readonly rev: string;}) => { const state = fork.state === undefined ? undefined : CLAIM_STATES[fork.state];
return ( <li data-fork={`${fork.handle}/${fork.ask}`}> <AppLink aria-current={readsFork(rev, fork) ? "true" : undefined} className={ROW} onClick={onPick} to={hrefAt(forkRev(fork))} > {state === undefined ? ( <GitFork aria-hidden="true" className={ICON} strokeWidth={1.75} /> ) : ( <StateMarkIcon className={ICON} mark={state.mark} /> )} <span className="flex min-w-0 flex-1 flex-col"> <span className="text-mono min-w-0 truncate font-mono"> {fork.ask} </span> <span className="text-label text-muted-foreground min-w-0 truncate"> {fork.handle} {state === undefined ? null : ` · ${state.word}`} </span> </span> </AppLink> </li> );};
/** * What the switcher lists, once the refs are read: `main`, the tags a filter * leaves, and the answers in forks. */export const RefChoices = ({ filter = "", hrefAt, onPick, refs, rev,}: { readonly filter?: string; readonly hrefAt: (rev: string) => string; readonly onPick: () => void; readonly refs: RefsView | undefined; readonly rev: string;}) => { const found = matches(filter); const tags = (refs?.tags ?? []).map(({ name }) => name).filter(found);
const forks = answerForks(refs).filter( (fork) => found(fork.ask) || found(fork.handle) );
return ( <div className="flex max-h-80 flex-col overflow-y-auto overscroll-contain"> <ul> <li data-ref-choice="main"> <AppLink aria-current={rev === "main" ? "true" : undefined} className={ROW} onClick={onPick} to={hrefAt("main")} > main <span className="text-label text-muted-foreground ml-auto"> moves only by a landing </span> </AppLink> </li> </ul> {refs === undefined ? ( <p className="text-label text-muted-foreground px-2 py-1"> Reading the tags and forks… </p> ) : null} {tags.length === 0 ? null : ( <> <p className={GROUP}>Tags</p> <ul> {tags.slice(0, SHOWN).map((name) => ( <li data-tag={name} key={name}> <AppLink aria-current={rev === name ? "true" : undefined} className={ROW} onClick={onPick} to={hrefAt(name)} > <RefMark rev={name} /> <span className="text-mono min-w-0 truncate font-mono"> {name} </span> </AppLink> </li> ))} </ul> {tags.length > SHOWN ? ( <p className="text-label text-muted-foreground px-2 pb-1"> {tags.length - SHOWN} more: type to find them. </p> ) : null} </> )} {forks.length === 0 ? null : ( <> <p className={GROUP}>Answers in claim forks</p> <ul> {forks.map((fork) => ( <ForkEntry fork={fork} hrefAt={hrefAt} key={`${fork.handle}/${fork.ask}`} onPick={onPick} rev={rev} /> ))} </ul> </> )} </div> );};
/** What the switcher lists once open: the page's route reads the refs and hands them to `RefChoices`. */export type RefChoicesReader = ComponentType<{ readonly filter: string; readonly hrefAt: (rev: string) => string; readonly onPick: () => void; readonly project: ProjectName; readonly rev: string;}>;
/** The refs are read when the list first opens, never for the page's first paint. */export const RefSwitcher = ({ Choices, hrefAt, project, rev,}: { readonly Choices: RefChoicesReader; readonly hrefAt: (rev: string) => string; readonly project: ProjectName; readonly rev: string;}) => { const [filter, setFilter] = useState(""); const [open, setOpen] = useState(false);
const close = () => { setOpen(false); };
return ( <Popover onOpenChange={setOpen} open={open}> <PopoverTrigger aria-label={`At ${revLabel(rev)}. Switch revision`} className="text-ui hover:bg-surface border-input flex h-8 max-w-[16rem] min-w-0 shrink-0 cursor-pointer items-center gap-1.5 border px-2" data-ref={rev} > <span className="shrink-0">At</span> <span className={`min-w-0 truncate ${rev === "main" ? "" : "text-mono font-mono"}`} > {revLabel(rev)} </span> <ChevronDown aria-hidden="true" className="text-muted-foreground size-3.5 shrink-0" strokeWidth={1.75} /> </PopoverTrigger> <PopoverContent align="start" className="w-80 gap-1 p-1.5"> <input aria-label="Find a tag or answer" className="border-input bg-background sm:text-ui h-9 w-full border px-2 text-base sm:h-8" onChange={(event) => { setFilter(event.currentTarget.value); }} placeholder="Find a tag or answer" value={filter} /> {open ? ( <Choices filter={filter} hrefAt={hrefAt} onPick={close} project={project} rev={rev} /> ) : null} <AppLink className="text-ui text-muted-foreground hover:text-foreground border-rule border-t px-2 pt-2 pb-1 no-underline" onClick={close} to={`/${project}/refs`} > Every ref, with its commit </AppLink> </PopoverContent> </Popover> );};