Skip to content
Myr

ref-switcher.tsx

This file

Path

apps/web/src/features/code/ref-switcher.tsx

153 lines · 4.5 KiB · TypeScript

Last changed by

Merge build/1211-code-browseroscabrielbefore adoptiona6808d3

History of this file

Take it

Merge build/1211-code-browseroscabrielbefore adoptiona6808d3

lines 1–153why, one step back
1/**
2 * The code pages' ref line: the revision being read (main, a tag, or a3 * commit by its short id), and a switcher that opens the same page at4 * main or at any tag. Each choice is a link, so it can be opened in a new5 * tab, and the tags filter as the reader types. The list of tags arrives6 * after the page; until it has, only main is offered.7 */8import type { RefsView } from "@repo/protocol/domain/code";9import { GitBranch, GitCommitHorizontal, Tag } from "lucide-react";10import { useState } from "react";1112import {13 Popover,14 PopoverContent,15 PopoverTrigger,16} from "@/components/ui/popover";1718import { AppLink } from "../shared/link.tsx";19import { isCommitId } from "./headers.ts";20import { revLabel } from "./rev.ts";2122/** At most this many tags listed at once; the filter finds the rest. */23const SHOWN = 100;2425/** A ref's mark: a branch for main, a tag for a tag, a commit for a SHA. */26const RefMark = ({ rev }: { readonly rev: string }) => {27 if (rev === "main") {28 return (29 <GitBranch30 aria-hidden="true"31 className="size-4 shrink-0"32 strokeWidth={1.75}33 />34 );35 }3637 return isCommitId(rev) ? (38 <GitCommitHorizontal39 aria-hidden="true"40 className="size-4 shrink-0"41 strokeWidth={1.75}42 />43 ) : (44 <Tag aria-hidden="true" className="size-4 shrink-0" strokeWidth={1.75} />45 );46};4748/** Under the list: that the tags are still being read, or how many the filter has left out. */49const ListFoot = ({50 count,51 read,52}: {53 readonly count: number;54 readonly read: boolean;55}) => {56 if (!read) {57 return (58 <p className="text-label text-muted-foreground px-2 pb-1">59 Reading the tags…60 </p>61 );62 }6364 return count > SHOWN ? (65 <p className="text-label text-muted-foreground px-2 pb-1">66 {count - SHOWN} more: type to find them.67 </p>68 ) : null;69};7071const ROW =72 "text-ui hover:bg-surface flex h-8 min-w-0 items-center gap-2 px-2 no-underline aria-[current]:bg-surface aria-[current]:font-semibold";7374export const RefSwitcher = ({75 hrefAt,76 refs,77 rev,78}: {79 /** The revision the page reads. */80 readonly rev: string;81 /** Main and the tags, once read. */82 readonly refs?: RefsView | undefined;83 /** The same page at another revision. */84 readonly hrefAt: (rev: string) => string;85}) => {86 const [filter, setFilter] = useState("");87 const [open, setOpen] = useState(false);8889 const tags = (refs?.tags ?? [])90 .map(({ name }) => name)91 .filter((name) => name.toLowerCase().includes(filter.trim().toLowerCase()));9293 return (94 <Popover onOpenChange={setOpen} open={open}>95 <PopoverTrigger96 aria-label={`Revision: ${revLabel(rev)}. Switch revision`}97 className="text-ui hover:bg-surface border-input flex h-8 min-w-0 flex-1 cursor-pointer items-center gap-2 border px-2"98 data-ref={rev}99 >100 <RefMark rev={rev} />101 <span102 className={`min-w-0 truncate ${rev === "main" ? "" : "text-mono font-mono"}`}103 >104 {revLabel(rev)}105 </span>106 </PopoverTrigger>107 <PopoverContent align="start" className="w-64 gap-2 p-1.5">108 <input109 aria-label="Find a tag"110 className="border-input bg-background sm:text-ui h-9 w-full border px-2 text-base sm:h-8"111 onChange={(event) => {112 setFilter(event.currentTarget.value);113 }}114 placeholder="Find a tag"115 value={filter}116 />117 <ul className="flex max-h-80 flex-col overflow-y-auto overscroll-contain">118 <li>119 <AppLink120 aria-current={rev === "main" ? "true" : undefined}121 className={ROW}122 onClick={() => {123 setOpen(false);124 }}125 to={hrefAt("main")}126 >127 <RefMark rev="main" />128 main129 </AppLink>130 </li>131 {tags.slice(0, SHOWN).map((name) => (132 <li data-tag={name} key={name}>133 <AppLink134 aria-current={rev === name ? "true" : undefined}135 className={ROW}136 onClick={() => {137 setOpen(false);138 }}139 to={hrefAt(name)}140 >141 <RefMark rev={name} />142 <span className="text-mono min-w-0 truncate font-mono">143 {name}144 </span>145 </AppLink>146 </li>147 ))}148 </ul>149 <ListFoot count={tags.length} read={refs !== undefined} />150 </PopoverContent>151 </Popover>152 );153};