Open History to see the last commit
1import type { ProjectEntry } from "@t3tools/contracts";
2import { SymbolView } from "expo-symbols";
3import { memo, useCallback, useEffect, useMemo, useState } from "react";
4import { ActivityIndicator, FlatList, Pressable, RefreshControl, View } from "react-native";
5
6import { AppText as Text } from "../../components/AppText";
7import { PierreEntryIcon } from "../../components/PierreEntryIcon";
8import { cn } from "../../lib/cn";
9import { useThemeColor } from "../../lib/useThemeColor";
10import {
11 buildFileTree,
12 defaultExpandedTreePaths,
13 flattenFileTree,
14 type VisibleFileTreeNode,
15} from "./fileTree";
16
17function ancestorPaths(path: string): ReadonlyArray<string> {
18 const parts = path.split("/").filter(Boolean);
19 const ancestors: string[] = [];
20 for (let index = 1; index < parts.length; index += 1) {
21 ancestors.push(parts.slice(0, index).join("/"));
22 }
23 return ancestors;
24}
25
26const FileTreeRow = memo(function FileTreeRow(props: {
27 readonly item: VisibleFileTreeNode;
28 readonly selectedPath: string | null;
29 readonly expanded: boolean;
30 readonly iconColor: string;
31 readonly onPressDirectory: (path: string) => void;
32 readonly onPressFile: (path: string) => void;
33}) {
34 const { node, depth } = props.item;
35 const selected = node.kind === "file" && node.path === props.selectedPath;
36
37 return (
38 <Pressable
39 accessibilityRole="button"
40 accessibilityLabel={node.path}
41 onPress={() => {
42 if (node.kind === "directory") {
43 props.onPressDirectory(node.path);
44 return;
45 }
46 props.onPressFile(node.path);
47 }}
48 className={cn(
49 "mx-2 min-h-[42px] flex-row items-center gap-2 rounded-[12px] px-2 active:bg-subtle",
50 selected && "bg-subtle-strong",
51 )}
52 style={{ paddingLeft: 8 + depth * 18 }}
53 >
54 {node.kind === "directory" ? (
55 <SymbolView
56 name={props.expanded ? "chevron.down" : "chevron.right"}
57 size={12}
58 tintColor={props.iconColor}
59 type="monochrome"
60 />
61 ) : (
62 <View className="w-3" />
63 )}
64 <PierreEntryIcon path={node.path} kind={node.kind} size={17} />
65 <Text
66 className={cn(
67 "min-w-0 flex-1 text-sm leading-[19px]",
68 selected ? "font-t3-bold text-foreground" : "font-t3-medium text-foreground-secondary",
69 )}
70 numberOfLines={1}
71 >
72 {node.name}
73 </Text>
74 {node.kind === "directory" ? (
75 <Text className="text-2xs font-t3-medium text-foreground-tertiary">
76 {node.children.length}
77 </Text>
78 ) : null}
79 </Pressable>
80 );
81});
82
83export function FileTreeBrowser(props: {
84 readonly entries: ReadonlyArray<ProjectEntry>;
85 readonly error: string | null;
86 readonly isPending: boolean;
87 readonly searchQuery: string;
88 readonly selectedPath: string | null;
89 readonly onRefresh: () => void;
90 readonly onSelectFile: (path: string) => void;
91}) {
92 const [expandedPaths, setExpandedPaths] = useState<ReadonlySet<string>>(() => new Set());
93 const iconColor = String(useThemeColor("--color-icon-muted"));
94
95 const tree = useMemo(() => buildFileTree(props.entries), [props.entries]);
96 const defaultExpanded = useMemo(() => defaultExpandedTreePaths(tree), [tree]);
97 const visibleNodes = useMemo(
98 () =>
99 flattenFileTree({
100 nodes: tree,
101 expanded: expandedPaths,
102 searchQuery: props.searchQuery,
103 }),
104 [expandedPaths, props.searchQuery, tree],
105 );
106
107 useEffect(() => {
108 setExpandedPaths((current) => {
109 if (current.size > 0 || defaultExpanded.size === 0) {
110 return current;
111 }
112 return new Set(defaultExpanded);
113 });
114 }, [defaultExpanded]);
115
116 useEffect(() => {
117 if (!props.selectedPath) {
118 return;
119 }
120 setExpandedPaths((current) => {
121 const next = new Set(current);
122 for (const ancestor of ancestorPaths(props.selectedPath ?? "")) {
123 next.add(ancestor);
124 }
125 return next;
126 });
127 }, [props.selectedPath]);
128
129 const toggleDirectory = useCallback((path: string) => {
130 setExpandedPaths((current) => {
131 const next = new Set(current);
132 if (next.has(path)) {
133 next.delete(path);
134 } else {
135 next.add(path);
136 }
137 return next;
138 });
139 }, []);
140
141 return (
142 <View className="flex-1 bg-sheet">
143 {props.error && props.entries.length === 0 ? (
144 <View className="px-4 py-5">
145 <Text className="text-sm font-t3-bold text-foreground">Files unavailable</Text>
146 <Text className="mt-1 text-xs leading-[18px] text-foreground-muted">{props.error}</Text>
147 </View>
148 ) : (
149 <FlatList
150 data={visibleNodes}
151 keyExtractor={(item) => item.node.path}
152 contentInsetAdjustmentBehavior="automatic"
153 keyboardDismissMode="on-drag"
154 keyboardShouldPersistTaps="handled"
155 contentContainerStyle={{ paddingVertical: 8 }}
156 refreshControl={
157 <RefreshControl refreshing={props.isPending} onRefresh={props.onRefresh} />
158 }
159 renderItem={({ item }) => (
160 <FileTreeRow
161 item={item}
162 selectedPath={props.selectedPath}
163 expanded={expandedPaths.has(item.node.path)}
164 iconColor={iconColor}
165 onPressDirectory={toggleDirectory}
166 onPressFile={props.onSelectFile}
167 />
168 )}
169 ListEmptyComponent={
170 <View className="px-4 py-5">
171 {props.isPending ? (
172 <ActivityIndicator size="small" />
173 ) : (
174 <>
175 <Text className="text-sm font-t3-bold text-foreground">No files found</Text>
176 <Text className="mt-1 text-xs leading-[18px] text-foreground-muted">
177 {props.searchQuery.trim().length > 0
178 ? "Try a different search."
179 : "The workspace file index is empty."}
180 </Text>
181 </>
182 )}
183 </View>
184 }
185 />
186 )}
187 </View>
188 );
189}