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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174import { prepareFileTreeInput } from "@pierre/trees";import type { FileTree, FileTreeDirectoryHandle, FileTreeItemHandle, FileTreeOptions, FileTreeRowDecoration, GitStatusEntry,} from "@pierre/trees";import { FileTree as FileTreeView, useFileTree } from "@pierre/trees/react";import { useEffect, useMemo } from "react";
import { Input } from "@/components/ui/input";
import type { Counts } from "./diff-model.ts";import { ancestorsOf } from "./paths.ts";import { treesOptions } from "./pierre.ts";
export interface RepoTreeProps { readonly paths: readonly string[]; readonly current?: string | undefined; readonly onOpen: (path: string) => void; readonly status?: readonly GitStatusEntry[]; readonly counts?: ReadonlyMap<string, Counts>; readonly open?: boolean;}
const NO_STATUS: readonly GitStatusEntry[] = [];
const TREE_CSS = [ "[data-type='item'][aria-selected='true'] { font-weight: 600; color: var(--foreground); }", "[data-item-flattened-subitems] { color: var(--muted); }", "[data-item-flattened-subitem]:last-child { color: var(--foreground); }",].join("\n");
const folderId = (path: string) => `${path}/`;
const openFor = (current: string | undefined) => current === undefined || current === "" ? [] : [...ancestorsOf(current), current].map(folderId);
const decorationOf = (counts: Counts): FileTreeRowDecoration => ({ parts: [ { color: "var(--diff-add-sign)", text: `+${counts.additions}` }, // A no-break space: the row lays its parts out as boxes, where a plain // space collapses to nothing. { text: "\u00A0" }, { color: "var(--diff-del-sign)", text: `−${counts.deletions}` }, ], text: `+${counts.additions} −${counts.deletions}`, title: `${counts.additions} added, ${counts.deletions} removed`,});
export const treeOptionsFor = ( props: Omit<RepoTreeProps, "onOpen">, preparedInput = prepareFileTreeInput(props.paths)): FileTreeOptions => { const { counts, current, open = false, status = NO_STATUS } = props;
const options: FileTreeOptions = { ...treesOptions, fileTreeSearchMode: "hide-non-matches", flattenEmptyDirectories: true, gitStatus: status, id: "repo-tree", initialExpandedPaths: openFor(current), initialExpansion: open ? "open" : "closed", initialSelectedPaths: current === undefined ? [] : [current], preparedInput, // A field inside the tree's role would not be one of its rows to a screen // reader. search: false, stickyFolders: true, unsafeCSS: TREE_CSS, };
if (counts !== undefined) { options.renderRowDecoration = ({ item }) => { const changed = counts.get(item.path);
return changed === undefined ? null : decorationOf(changed); }; }
return options;};
export const followChoices = ( tree: Pick<FileTree, "getSelectedPaths" | "subscribe">, current: string | undefined, onOpen: (path: string) => void) => tree.subscribe(() => { const path = tree.getSelectedPaths().at(-1);
if (path !== undefined && !path.endsWith("/") && path !== current) { onOpen(path); } });
export const filterTree = (tree: Pick<FileTree, "setSearch">, text: string) => { tree.setSearch(text.trim() === "" ? null : text);};
const isFolder = (item: FileTreeItemHandle): item is FileTreeDirectoryHandle => item.isDirectory();
export const RepoTree = (props: RepoTreeProps) => { const { current, onOpen } = props;
// Made from its first options: the page keys the tree by project and // revision. const preparedInput = useMemo( () => prepareFileTreeInput(props.paths), [props.paths] );
const { model } = useFileTree(treeOptionsFor(props, preparedInput));
useEffect( () => followChoices(model, current, onOpen), [model, current, onOpen] );
useEffect(() => { if (current === undefined || current === "") { return; }
for (const folder of openFor(current)) { const item = model.getItem(folder);
if (item !== null && isFolder(item)) { item.expand(); } }
for (const selected of model.getSelectedPaths()) { if (selected !== current) { model.getItem(selected)?.deselect(); } }
const here = model.getItem(current) ?? model.getItem(folderId(current));
if (here !== null && !here.isSelected()) { here.select(); }
model.scrollToPath(current, { focus: false, offset: "nearest" }); }, [model, current]);
return ( <div className="flex h-full min-h-0 flex-col"> <div className="shrink-0 px-3 pb-1.5"> <Input aria-label="Filter files" data-keys-off="" onChange={(event) => { filterTree(model, event.currentTarget.value); }} placeholder="Filter files" type="search" /> </div> <FileTreeView className="block min-h-0 flex-1" model={model} /> </div> );};
// `React.lazy` takes a module's default export.export default RepoTree;