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
Merge redesign into mainoscabrielbefore adoptioncaedd21
line 1why, one step back1/**Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
lines 2–7why, one step back2 * The code bar's revision picker (ADR 0043): `main`, the project's tags, and3 * the answers in claim forks, each with its claimant, ask and claim state. A4 * fork's entry opens the same page in that fork, at its address5 * (`@<handle>/<ask>`), never its repo's name. Its trigger reads "At main",6 * "At v1.2.0" or "At <handle>/<ask>": a tag and a fork are revisions, not7 * branches, so no branch is named and main carries no branch mark.Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 8–11why, one step back8 */9import { forkRev } from "@repo/protocol/domain/code";10import type { ForkRef, RefsView } from "@repo/protocol/domain/code";11import type { AskId, ProjectName } from "@repo/protocol/ids";Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 12why, one step back12import { ChevronDown, GitCommitHorizontal, GitFork, Tag } from "lucide-react";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 13why, one step back13import { useState } from "react";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 14why, one step back14import type { ComponentType } from "react";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 15–22why, one step back1516import {17 Popover,18 PopoverContent,19 PopoverTrigger,20} from "@/components/ui/popover";2122import { AppLink } from "../shared/link.tsx";Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 23–24why, one step back23import { StateMarkIcon } from "../shared/marks.tsx";24import { CLAIM_STATES } from "../shared/row.tsx";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 25why, one step back25import { isCommitId } from "./headers.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 26why, one step back26import { isForkRev, readsFork, revLabel } from "./rev.ts";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 27–29why, one step back2728const SHOWN = 100;29Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 30–31why, one step back30const ICON = "size-4 shrink-0";31Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 32why, one step back32/** 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. */Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 33–34why, one step back33const RefMark = ({ rev }: { readonly rev: string }) => {34 if (rev === "main") {Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 35why, one step back35 return null;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 36–37why, one step back36 }37Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 38–41why, one step back38 if (isForkRev(rev)) {39 return <GitFork aria-hidden="true" className={ICON} strokeWidth={1.75} />;40 }41Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 42–44why, one step back42 return isCommitId(rev) ? (43 <GitCommitHorizontal44 aria-hidden="true"Merge redesign into mainoscabrielbefore adoptioncaedd21
line 45why, one step back45 className={ICON}Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 46–48why, one step back46 strokeWidth={1.75}47 />48 ) : (Merge redesign into mainoscabrielbefore adoptioncaedd21
line 49why, one step back49 <Tag aria-hidden="true" className={ICON} strokeWidth={1.75} />Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 50–52why, one step back50 );51};52Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 53–75why, one step back53const ROW =54 "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";5556const GROUP = "text-label text-muted-foreground px-2 pt-2 pb-1 font-medium";5758/** A claim fork that holds an answer: the only forks the switcher lists. */59type AnswerFork = ForkRef & { readonly ask: AskId };6061const answerForks = (refs: RefsView | undefined): readonly AnswerFork[] =>62 (refs?.forks ?? []).flatMap((fork) =>63 fork.kind === "claim" && fork.ask !== undefined64 ? [{ ...fork, ask: fork.ask }]65 : []66 );6768const matches = (filter: string) => (text: string) =>69 text.toLowerCase().includes(filter.trim().toLowerCase());7071const ForkEntry = ({72 fork,73 hrefAt,74 onPick,75 rev,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 76why, one step back76}: {Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 77–80why, one step back77 readonly fork: AnswerFork;78 readonly hrefAt: (rev: string) => string;79 readonly onPick: () => void;80 readonly rev: string;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 81why, one step back81}) => {Merge redesign into mainoscabrielbefore adoptioncaedd21
line 82why, one step back82 const state = fork.state === undefined ? undefined : CLAIM_STATES[fork.state];Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 83why, one step back83Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 84–108why, one step back84 return (85 <li data-fork={`${fork.handle}/${fork.ask}`}>86 <AppLink87 aria-current={readsFork(rev, fork) ? "true" : undefined}88 className={ROW}89 onClick={onPick}90 to={hrefAt(forkRev(fork))}91 >92 {state === undefined ? (93 <GitFork aria-hidden="true" className={ICON} strokeWidth={1.75} />94 ) : (95 <StateMarkIcon className={ICON} mark={state.mark} />96 )}97 <span className="flex min-w-0 flex-1 flex-col">98 <span className="text-mono min-w-0 truncate font-mono">99 {fork.ask}100 </span>101 <span className="text-label text-muted-foreground min-w-0 truncate">102 {fork.handle}103 {state === undefined ? null : ` · ${state.word}`}104 </span>105 </span>106 </AppLink>107 </li>108 );Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 109–110why, one step back109};110Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 111–116why, one step back111/**112 * What the switcher lists, once the refs are read: `main`, the tags a filter113 * leaves, and the answers in forks.114 */115export const RefChoices = ({116 filter = "",Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 117why, one step back117 hrefAt,Merge redesign into mainoscabrielbefore adoptioncaedd21
line 118why, one step back118 onPick,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 119–121why, one step back119 refs,120 rev,121}: {Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 122–125why, one step back122 readonly filter?: string;123 readonly hrefAt: (rev: string) => string;124 readonly onPick: () => void;125 readonly refs: RefsView | undefined;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 126why, one step back126 readonly rev: string;Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 127–185why, one step back127}) => {128 const found = matches(filter);129 const tags = (refs?.tags ?? []).map(({ name }) => name).filter(found);130131 const forks = answerForks(refs).filter(132 (fork) => found(fork.ask) || found(fork.handle)133 );134135 return (136 <div className="flex max-h-80 flex-col overflow-y-auto overscroll-contain">137 <ul>138 <li data-ref-choice="main">139 <AppLink140 aria-current={rev === "main" ? "true" : undefined}141 className={ROW}142 onClick={onPick}143 to={hrefAt("main")}144 >145 main146 <span className="text-label text-muted-foreground ml-auto">147 moves only by a landing148 </span>149 </AppLink>150 </li>151 </ul>152 {refs === undefined ? (153 <p className="text-label text-muted-foreground px-2 py-1">154 Reading the tags and forks…155 </p>156 ) : null}157 {tags.length === 0 ? null : (158 <>159 <p className={GROUP}>Tags</p>160 <ul>161 {tags.slice(0, SHOWN).map((name) => (162 <li data-tag={name} key={name}>163 <AppLink164 aria-current={rev === name ? "true" : undefined}165 className={ROW}166 onClick={onPick}167 to={hrefAt(name)}168 >169 <RefMark rev={name} />170 <span className="text-mono min-w-0 truncate font-mono">171 {name}172 </span>173 </AppLink>174 </li>175 ))}176 </ul>177 {tags.length > SHOWN ? (178 <p className="text-label text-muted-foreground px-2 pb-1">179 {tags.length - SHOWN} more: type to find them.180 </p>181 ) : null}182 </>183 )}184 {forks.length === 0 ? null : (185 <>Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 186why, one step back186 <p className={GROUP}>Answers in claim forks</p>Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 187–206why, one step back187 <ul>188 {forks.map((fork) => (189 <ForkEntry190 fork={fork}191 hrefAt={hrefAt}192 key={`${fork.handle}/${fork.ask}`}193 onPick={onPick}194 rev={rev}195 />196 ))}197 </ul>198 </>199 )}200 </div>201 );202};203204/** What the switcher lists once open: the page's route reads the refs and hands them to `RefChoices`. */205export type RefChoicesReader = ComponentType<{206 readonly filter: string;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 207why, one step back207 readonly hrefAt: (rev: string) => string;Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 208–223why, one step back208 readonly onPick: () => void;209 readonly project: ProjectName;210 readonly rev: string;211}>;212213/** The refs are read when the list first opens, never for the page's first paint. */214export const RefSwitcher = ({215 Choices,216 hrefAt,217 project,218 rev,219}: {220 readonly Choices: RefChoicesReader;221 readonly hrefAt: (rev: string) => string;222 readonly project: ProjectName;223 readonly rev: string;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 224–227why, one step back224}) => {225 const [filter, setFilter] = useState("");226 const [open, setOpen] = useState(false);227Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 228–230why, one step back228 const close = () => {229 setOpen(false);230 };Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 231–234why, one step back231232 return (233 <Popover onOpenChange={setOpen} open={open}>234 <PopoverTriggerMerge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
lines 235–236why, one step back235 aria-label={`At ${revLabel(rev)}. Switch revision`}236 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"Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 237–238why, one step back237 data-ref={rev}238 >Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 239why, one step back239 <span className="shrink-0">At</span>Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 240–244why, one step back240 <span241 className={`min-w-0 truncate ${rev === "main" ? "" : "text-mono font-mono"}`}242 >243 {revLabel(rev)}244 </span>Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 245–249why, one step back245 <ChevronDown246 aria-hidden="true"247 className="text-muted-foreground size-3.5 shrink-0"248 strokeWidth={1.75}249 />Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 250why, one step back250 </PopoverTrigger>Merge redesign into mainoscabrielbefore adoptioncaedd21
line 251why, one step back251 <PopoverContent align="start" className="w-80 gap-1 p-1.5">Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 252why, one step back252 <inputMerge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 253why, one step back253 aria-label="Find a tag or answer"Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 254–257why, one step back254 className="border-input bg-background sm:text-ui h-9 w-full border px-2 text-base sm:h-8"255 onChange={(event) => {256 setFilter(event.currentTarget.value);257 }}Merge web/code-tab-and-fixes into main, keeping both the hero's concurrency line and its second actionoscabrielbefore adoption2eaa076
line 258why, one step back258 placeholder="Find a tag or answer"Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 259–260why, one step back259 value={filter}260 />Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 261–276why, one step back261 {open ? (262 <Choices263 filter={filter}264 hrefAt={hrefAt}265 onPick={close}266 project={project}267 rev={rev}268 />269 ) : null}270 <AppLink271 className="text-ui text-muted-foreground hover:text-foreground border-rule border-t px-2 pt-2 pb-1 no-underline"272 onClick={close}273 to={`/${project}/refs`}274 >275 Every ref, with its commit276 </AppLink>Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 277–280why, one step back277 </PopoverContent>278 </Popover>279 );280};