import { ChevronDown, ChevronRight, Command, Folder, FolderOpen, FolderTree, GitPullRequest, House, MonitorCog, ScrollText, SlidersHorizontal, SwatchBook, } from "lucide-react"; import { memo, useMemo } from "react"; import { GitHostIcon } from "@/components/git-host-icon"; import { RepositoryFileIcon } from "@/components/repository-file-icon"; import { type ChangeKind, type FileNode, useFileTree } from "@/lib/file-tree-context"; import type { GitHost } from "@/lib/git-host/types"; import { cn } from "@/lib/utils"; interface FileTreeProps { path: string; level?: number; kinds?: ReadonlyMap; activeFile?: string; filterQuery?: string; allowedFiles?: ReadonlySet; viewedFiles?: ReadonlySet; onToggleViewed?: (path: string) => void; onFileClick?: (node: FileNode) => void; onDirectoryClick?: (node: FileNode) => boolean | undefined; showUnviewedIndicator?: boolean; } const SETTINGS_PATH_PREFIX = "__settings__/"; const HOME_PATH = "__home__"; const PR_PATH_PREFIX = "pr:"; const REPO_PATH_PREFIX = "repo:"; const HOST_PATH_PREFIX = "host:"; function kindColor(kind: ChangeKind) { switch (kind) { case "add": return "text-status-added"; case "del": return "text-status-removed"; case "mix": return "text-status-modified"; } } function kindMarker(kind?: ChangeKind) { if (!kind) return null; return ; } function isSettingsPath(path: string) { return path.startsWith(SETTINGS_PATH_PREFIX); } function isHomePath(path: string) { return path === HOME_PATH; } function isPullRequestPath(path: string) { return path.startsWith(PR_PATH_PREFIX); } function isRepositoryPath(path: string) { return path.startsWith(REPO_PATH_PREFIX); } function isHostPath(path: string) { return path.startsWith(HOST_PATH_PREFIX); } function hostFromTreePath(path: string): GitHost | null { const [, host] = path.split(":"); if (host === "github" || host === "bitbucket") return host; return null; } function settingsIcon(path: string) { if (!isSettingsPath(path)) return null; const tab = path.slice(SETTINGS_PATH_PREFIX.length); if (tab === "appearance") return ; if (tab === "diff") return ; if (tab === "tree") return ; if (tab === "shortcuts") return ; if (tab === "workspace") return ; return null; } export function FileTree({ path, level = 0, kinds, activeFile, filterQuery, allowedFiles, viewedFiles, onToggleViewed, onFileClick, onDirectoryClick, showUnviewedIndicator = true, }: FileTreeProps) { const tree = useFileTree(); const resolvedKinds = kinds ?? tree.kinds; const rawNodes = tree.getChildrenForPath(path); const active = activeFile ?? tree.activeFile; const treeIndentSize = tree.treeIndentSize; const normalizedQuery = filterQuery?.trim().toLowerCase() ?? ""; const nodes = useMemo(() => { const matchesNode = (node: FileNode): boolean => { if (node.type === "summary") { if (!normalizedQuery) return true; const summarySearch = `${node.name} ${node.path}`.toLowerCase(); return summarySearch.includes(normalizedQuery); } if (node.type === "file") { const queryMatch = !normalizedQuery || node.path.toLowerCase().includes(normalizedQuery); const allowedMatch = !allowedFiles || allowedFiles.has(node.path); return queryMatch && allowedMatch; } const children = tree.getChildrenForPath(node.path); const host = hostFromTreePath(node.path); if (host && children.length === 0) { if (!normalizedQuery) return true; const hostSearch = `${node.name} ${node.path}`.toLowerCase(); return hostSearch.includes(normalizedQuery); } return children.some(matchesNode); }; return rawNodes.filter(matchesNode); }, [allowedFiles, normalizedQuery, rawNodes, tree]); return (
{nodes.map((node) => { if (node.type === "directory") { return ( ); } return ( ); })}
); } function DirectoryNode({ node, level, treeIndentSize, kinds, activeFile, filterQuery, allowedFiles, viewedFiles, onToggleViewed, onFileClick, onDirectoryClick, showUnviewedIndicator, }: { node: FileNode; level: number; treeIndentSize: number; kinds: ReadonlyMap; activeFile?: string; filterQuery?: string; allowedFiles?: ReadonlySet; viewedFiles?: ReadonlySet; onToggleViewed?: (path: string) => void; onFileClick?: (node: FileNode) => void; onDirectoryClick?: (node: FileNode) => boolean | undefined; showUnviewedIndicator: boolean; }) { const tree = useFileTree(); const compactEnabled = tree.compactSingleChildDirectories; let displayNode = node; const nameParts = [node.name]; // Keep host roots stable and visible in landing trees. if (compactEnabled && !isHostPath(node.path)) { while (true) { const children = tree.getChildrenForPath(displayNode.path); if (children.length !== 1) break; const nextNode = children[0]; if (nextNode.type !== "directory") break; nameParts.push(nextNode.name); displayNode = nextNode; } } const expanded = tree.isExpanded(displayNode.path); const kind = kinds.get(displayNode.path); const displayName = nameParts.join("/"); const host = hostFromTreePath(displayNode.path); const isActive = activeFile === displayNode.path; const isRepositoryNode = isRepositoryPath(displayNode.path); const pullRequestCount = isRepositoryNode ? tree.getChildrenForPath(displayNode.path).filter((child) => child.type === "file" && isPullRequestPath(child.path)).length : 0; return (
{expanded && (
)}
); } const FileNodeRow = memo(function FileNodeRow({ node, level, treeIndentSize, kinds, active, viewed, onFileClick, showUnviewedIndicator, }: { node: FileNode; level: number; treeIndentSize: number; kinds: ReadonlyMap; active?: string; viewed?: boolean; onFileClick?: (node: FileNode) => void; showUnviewedIndicator: boolean; }) { const tree = useFileTree(); const kind = node.type === "summary" ? undefined : kinds.get(node.path); const nodeSettingsIcon = node.type === "file" ? settingsIcon(node.path) : null; const isHomeNode = isHomePath(node.path); const isPullRequestNode = isPullRequestPath(node.path); const isSettingsNode = node.type === "file" && isSettingsPath(node.path); const isActive = node.path === active; const isUnviewedFile = showUnviewedIndicator && node.type !== "summary" && !isSettingsNode && !isHomeNode && !viewed; return ( ); }, areFileNodeRowPropsEqual); function areFileNodeRowPropsEqual( previous: Readonly<{ node: FileNode; level: number; treeIndentSize: number; kinds: ReadonlyMap; active?: string; viewed?: boolean; onFileClick?: (node: FileNode) => void; showUnviewedIndicator: boolean; }>, next: Readonly<{ node: FileNode; level: number; treeIndentSize: number; kinds: ReadonlyMap; active?: string; viewed?: boolean; onFileClick?: (node: FileNode) => void; showUnviewedIndicator: boolean; }>, ) { if (previous.node !== next.node) return false; if (previous.level !== next.level) return false; if (previous.treeIndentSize !== next.treeIndentSize) return false; if (previous.viewed !== next.viewed) return false; if (previous.onFileClick !== next.onFileClick) return false; if (previous.showUnviewedIndicator !== next.showUnviewedIndicator) return false; const previousWasActive = previous.active === previous.node.path; const nextIsActive = next.active === next.node.path; if (previousWasActive !== nextIsActive) return false; const previousKind = previous.node.type === "summary" ? undefined : previous.kinds.get(previous.node.path); const nextKind = next.node.type === "summary" ? undefined : next.kinds.get(next.node.path); return previousKind === nextKind; }