tree-view.tsx
This file
Path
apps/web/src/features/code/tree-view.tsx
218 lines · 7.1 KiB · TypeScript
Last changed by
Merge fix/w12-reviewoscabrielbefore adoption7b8b393
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 1why, one step back1/**Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 2–9why, one step back2 * One folder of a project at a revision. The tree in the rail is the3 * navigation; main lists the folder once, as rows: each entry's name (a4 * folder first, as `readTree` orders them), what last changed it (the ask5 * its landing answered, or the commit's subject) and how long ago, then6 * the folder's README below on the page's prose.7 *8 * What last changed each entry arrives after the page (`last`): its9 * column holds its place until then, so nothing moves.Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 10–12why, one step back10 */11import type {12 CodeEntry,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 13why, one step back13 CommitView,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 14–18why, one step back14 FileView,15 TreeListing,16} from "@repo/protocol/domain/code";17import type { ProjectName } from "@repo/protocol/ids";18import { Match, Option } from "effect";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 19why, one step back19import type { DateTime } from "effect";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 20–23why, one step back20import { File, FileSymlink, Folder, FolderGit2 } from "lucide-react";21import type { LucideIcon } from "lucide-react";22import type { ReactNode } from "react";23Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
line 24why, one step back24import { ReadmeSection } from "../project/readme-section.tsx";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 25–30why, one step back25import { AppLink } from "../shared/link.tsx";26import { GUTTER, MEASURE, Workspace } from "../shared/shell/workspace.tsx";27import { When } from "../shared/time.tsx";28import { askOf } from "../why/rows.ts";29import { FileHeader } from "./file-header.tsx";30import { blobHref, commitsHref, treeHref } from "./paths.ts";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 31–51why, one step back3132/** Each kind of entry: its mark and the word a screen reader says for it. */33const kinds = Match.type<CodeEntry["type"]>().pipe(34 Match.when("tree", () => ({ icon: Folder, word: "Folder" })),35 Match.when("blob", () => ({ icon: File, word: "File" })),36 Match.when("symlink", () => ({ icon: FileSymlink, word: "Link" })),37 Match.when("submodule", () => ({ icon: FolderGit2, word: "Submodule" })),38 Match.exhaustive39) satisfies (type: CodeEntry["type"]) => {40 readonly icon: LucideIcon;41 readonly word: string;42};4344/** Where an entry's row links: a folder's page or a file's; a submodule's commit lives elsewhere. */45const entryHref = (project: ProjectName, rev: string, entry: CodeEntry) =>46 Match.value(entry.type).pipe(47 Match.when("tree", () => Option.some(treeHref(project, rev, entry.path))),48 Match.when("submodule", () => Option.none<string>()),49 Match.orElse(() => Option.some(blobHref(project, rev, entry.path)))50 );51Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 52–57why, one step back52/** What last changed an entry, in a few words: the ask its landing answered or opened, or the commit's subject. */53const changeWords = (commit: CommitView) => {54 const linked =55 commit.landing === undefined56 ? undefined57 : Option.getOrUndefined(askOf(commit.landing));Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 58why, one step back58Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 59–61why, one step back59 if (linked === undefined) {60 return commit.subject === "" ? "(no subject)" : commit.subject;61 }Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 62why, one step back62Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 63–73why, one step back63 return linked.kind === "opened" ? `opened ${linked.ask}` : linked.ask;64};6566const LINK =67 "text-foreground no-underline outline-none after:absolute after:inset-0 after:content-[''] focus-visible:after:outline-2 focus-visible:after:-outline-offset-2 focus-visible:after:outline-ring focus-visible:after:outline-solid";6869/** One entry, one 40 px row: its mark, its name, what last changed it, and when. */70const EntryRow = ({71 entry,72 last,73 now,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 74–76why, one step back74 project,75 rev,76}: {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 77–79why, one step back77 readonly entry: CodeEntry;78 readonly last?: CommitView | undefined;79 readonly now?: DateTime.Utc | undefined;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 80–82why, one step back80 readonly project: ProjectName;81 readonly rev: string;82}) => {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 83–84why, one step back83 const { icon: Icon, word } = kinds(entry.type);84 const href = Option.getOrUndefined(entryHref(project, rev, entry));Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 85–86why, one step back8586 return (Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 87–103why, one step back87 <li88 className="group/row border-rule hover:bg-surface/50 relative isolate -mx-2 flex h-10 min-w-0 items-center gap-3 border-b px-2 transition-colors duration-[120ms]"89 data-entry={entry.path}90 >91 <Icon92 aria-hidden="true"93 className="text-muted-foreground size-4 shrink-0"94 strokeWidth={1.75}95 />96 <span className="sr-only">{word}:</span>97 <span className="text-ui min-w-0 shrink-0 basis-[14rem] truncate font-medium">98 {href === undefined ? (99 entry.name100 ) : (101 <AppLink className={LINK} title={entry.name} to={href}>102 {entry.name}103 </AppLink>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 104why, one step back104 )}Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 105–117why, one step back105 </span>106 <span107 className="text-ui text-muted-foreground tablet:block hidden min-w-0 flex-1 truncate"108 data-last=""109 >110 {last === undefined ? null : changeWords(last)}111 </span>112 <span className="text-label text-muted-foreground ml-auto shrink-0 tabular-nums">113 {last === undefined || now === undefined ? null : (114 <When at={last.committer.at} now={now} />115 )}116 </span>117 </li>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 118–120why, one step back118 );119};120Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 121–123why, one step back121/** A folder's entries as rows, each linking its page at `rev` (a project's home lists its root so). */122export const EntriesTable = ({123 entries,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 124why, one step back124 project,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 125why, one step back125 rev,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 126why, one step back126}: {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 127why, one step back127 readonly entries: readonly CodeEntry[];Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 128why, one step back128 readonly project: ProjectName;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 129why, one step back129 readonly rev: string;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 130why, one step back130}) => (Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 131–135why, one step back131 <ul className="border-rule flex flex-col border-t" data-entries="">132 {entries.map((entry) => (133 <EntryRow entry={entry} key={entry.path} project={project} rev={rev} />134 ))}135 </ul>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 136–137why, one step back136);137Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 138–152why, one step back138export interface TreeViewProps {139 readonly listing: TreeListing;140 readonly project: ProjectName;141 readonly rev: string;142 /** The folder's README, read; absent when it has none. */143 readonly readme?: FileView | undefined;144 /** What last changed each entry, by path, once read. */145 readonly last?: ReadonlyMap<string, CommitView>;146 readonly now: DateTime.Utc;147 /** The Files button, below the shell's width. */148 readonly files?: ReactNode;149 /** A notice above the folder (the refusal a missing path landed here with). */150 readonly notice?: ReactNode;151}152Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 153why, one step back153export const TreeView = ({Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 154–155why, one step back154 files,155 last,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 156why, one step back156 listing,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 157–158why, one step back157 notice,158 now,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 159–161why, one step back159 project,160 readme,161 rev,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 162–184why, one step back162}: TreeViewProps) => {163 const name = listing.path?.split("/").at(-1) ?? project;164165 return (166 <Workspace167 crumbs={[168 { label: project, to: `/${project}` },169 listing.path === undefined170 ? { label: "Code" }171 : { label: "Code", to: `/${project}/tree/main` },172 ...(listing.path === undefined ? [] : [{ label: name, mono: true }]),173 ]}174 title={name}175 toolbar={176 <>177 <FileHeader178 files={files}179 path={listing.path}180 project={project}181 rev={rev}182 />183 <AppLink184 className="text-ui text-muted-foreground hover:text-foreground tablet:inline hidden shrink-0 no-underline"Merge fix/w12-reviewoscabrielbefore adoption7b8b393
lines 185–186why, one step back185 search={listing.path === undefined ? {} : { path: listing.path }}186 to={commitsHref(project, rev)}Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 187–218why, one step back187 >188 History189 </AppLink>190 </>191 }192 variant="code"193 >194 <div className={`${GUTTER} ${MEASURE} flex flex-col gap-8`}>195 {notice}196 <ul197 aria-label={`${name}'s entries`}198 className="border-rule flex flex-col border-t"199 data-entries=""200 >201 {listing.entries.map((entry) => (202 <EntryRow203 entry={entry}204 key={entry.path}205 last={last?.get(entry.path)}206 now={now}207 project={project}208 rev={rev}209 />210 ))}211 </ul>212 {readme === undefined ? null : (213 <ReadmeSection file={readme} project={project} />214 )}215 </div>216 </Workspace>217 );218};