repo-tree.tsx
This file
Path
apps/web/src/features/code/repo-tree.tsx
173 lines · 4.9 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 1why, one step back1import { prepareFileTreeInput } from "@pierre/trees";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 2–10why, one step back2import type {3 FileTree,4 FileTreeDirectoryHandle,5 FileTreeItemHandle,6 FileTreeOptions,7 FileTreeRowDecoration,8 GitStatusEntry,9} from "@pierre/trees";10import { FileTree as FileTreeView, useFileTree } from "@pierre/trees/react";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 11–12why, one step back11import { useEffect, useMemo } from "react";12Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
lines 13–14why, one step back13import { Input } from "@/components/ui/input";14Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 15why, one step back15import type { Counts } from "./diff-model.ts";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 16why, one step back16import { ancestorsOf } from "./paths.ts";Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 17why, one step back17import { treesOptions } from "./pierre.ts";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 18–20why, one step back1819export interface RepoTreeProps {20 readonly paths: readonly string[];Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 21–22why, one step back21 readonly current?: string | undefined;22 readonly onOpen: (path: string) => void;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 23why, one step back23 readonly status?: readonly GitStatusEntry[];Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 24–25why, one step back24 readonly counts?: ReadonlyMap<string, Counts>;25 readonly open?: boolean;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 26–29why, one step back26}2728const NO_STATUS: readonly GitStatusEntry[] = [];29Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
lines 30–34why, one step back30const TREE_CSS = [31 "[data-type='item'][aria-selected='true'] { font-weight: 600; color: var(--foreground); }",32 "[data-item-flattened-subitems] { color: var(--muted); }",33 "[data-item-flattened-subitem]:last-child { color: var(--foreground); }",34].join("\n");Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 35why, one step back35Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 36–45why, one step back36const folderId = (path: string) => `${path}/`;3738const openFor = (current: string | undefined) =>39 current === undefined || current === ""40 ? []41 : [...ancestorsOf(current), current].map(folderId);4243const decorationOf = (counts: Counts): FileTreeRowDecoration => ({44 parts: [45 { color: "var(--diff-add-sign)", text: `+${counts.additions}` },Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 46–47why, one step back46 // A no-break space: the row lays its parts out as boxes, where a plain47 // space collapses to nothing.Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 48–61why, one step back48 { text: "\u00A0" },49 { color: "var(--diff-del-sign)", text: `−${counts.deletions}` },50 ],51 text: `+${counts.additions} −${counts.deletions}`,52 title: `${counts.additions} added, ${counts.deletions} removed`,53});5455export const treeOptionsFor = (56 props: Omit<RepoTreeProps, "onOpen">,57 preparedInput = prepareFileTreeInput(props.paths)58): FileTreeOptions => {59 const { counts, current, open = false, status = NO_STATUS } = props;6061 const options: FileTreeOptions = {Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 62why, one step back62 ...treesOptions,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 63why, one step back63 fileTreeSearchMode: "hide-non-matches",Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 64–65why, one step back64 flattenEmptyDirectories: true,65 gitStatus: status,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 66why, one step back66 id: "repo-tree",Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 67–68why, one step back67 initialExpandedPaths: openFor(current),68 initialExpansion: open ? "open" : "closed",Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 69–70why, one step back69 initialSelectedPaths: current === undefined ? [] : [current],70 preparedInput,Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 71–72why, one step back71 // A field inside the tree's role would not be one of its rows to a screen72 // reader.Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
line 73why, one step back73 search: false,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 74why, one step back74 stickyFolders: true,Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
line 75why, one step back75 unsafeCSS: TREE_CSS,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 76–99why, one step back76 };7778 if (counts !== undefined) {79 options.renderRowDecoration = ({ item }) => {80 const changed = counts.get(item.path);8182 return changed === undefined ? null : decorationOf(changed);83 };84 }8586 return options;87};8889export const followChoices = (90 tree: Pick<FileTree, "getSelectedPaths" | "subscribe">,91 current: string | undefined,92 onOpen: (path: string) => void93) =>94 tree.subscribe(() => {95 const path = tree.getSelectedPaths().at(-1);9697 if (path !== undefined && !path.endsWith("/") && path !== current) {98 onOpen(path);99 }Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 100–101why, one step back100 });101Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
lines 102–105why, one step back102export const filterTree = (tree: Pick<FileTree, "setSearch">, text: string) => {103 tree.setSearch(text.trim() === "" ? null : text);104};105Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 106–111why, one step back106const isFolder = (item: FileTreeItemHandle): item is FileTreeDirectoryHandle =>107 item.isDirectory();108109export const RepoTree = (props: RepoTreeProps) => {110 const { current, onOpen } = props;111Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 112–113why, one step back112 // Made from its first options: the page keys the tree by project and113 // revision.Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 114–125why, one step back114 const preparedInput = useMemo(115 () => prepareFileTreeInput(props.paths),116 [props.paths]117 );118119 const { model } = useFileTree(treeOptionsFor(props, preparedInput));120121 useEffect(122 () => followChoices(model, current, onOpen),123 [model, current, onOpen]124 );125Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 126why, one step back126 useEffect(() => {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 127–128why, one step back127 if (current === undefined || current === "") {128 return;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 129why, one step back129 }Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 130–151why, one step back130131 for (const folder of openFor(current)) {132 const item = model.getItem(folder);133134 if (item !== null && isFolder(item)) {135 item.expand();136 }137 }138139 for (const selected of model.getSelectedPaths()) {140 if (selected !== current) {141 model.getItem(selected)?.deselect();142 }143 }144145 const here = model.getItem(current) ?? model.getItem(folderId(current));146147 if (here !== null && !here.isSelected()) {148 here.select();149 }150151 model.scrollToPath(current, { focus: false, offset: "nearest" });Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 152–153why, one step back152 }, [model, current]);153Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
lines 154–169why, one step back154 return (155 <div className="flex h-full min-h-0 flex-col">156 <div className="shrink-0 px-3 pb-1.5">157 <Input158 aria-label="Filter files"159 data-keys-off=""160 onChange={(event) => {161 filterTree(model, event.currentTarget.value);162 }}163 placeholder="Filter files"164 type="search"165 />166 </div>167 <FileTreeView className="block min-h-0 flex-1" model={model} />168 </div>169 );Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 170–173why, one step back170};171172// `React.lazy` takes a module's default export.173export default RepoTree;