Elffuss BraidHub

kikocis/continue public

main · 5.1 KiBRawPermalink
1import { Box, Text, useInput } from "ink";
2import React, { useEffect, useState } from "react";
3
4import { useServices } from "../hooks/useService.js";
5import {
6 FileIndexServiceState,
7 FileItem,
8} from "../services/FileIndexService.js";
9import { services } from "../services/index.js";
10
11interface FileSearchUIProps {
12 filter: string;
13 selectedIndex: number;
14 onSelect: (filePath: string) => void;
15 onFilesUpdated?: (files: FileItem[]) => void;
16}
17
18// Highlighting component for matched characters
19const HighlightedText: React.FC<{ text: string; positions?: Set<number> }> = ({
20 text,
21 positions,
22}) => {
23 if (!positions || positions.size === 0) {
24 return <>{text}</>;
25 }
26
27 const chars = text.split("");
28 const elements: React.ReactNode[] = [];
29
30 chars.forEach((char, index) => {
31 if (positions.has(index)) {
32 elements.push(
33 <Text key={index} color="white">
34 {char}
35 </Text>,
36 );
37 } else {
38 elements.push(char);
39 }
40 });
41
42 return <>{elements}</>;
43};
44
45// Reusable container for consistent layout
46const FileSearchContainer: React.FC<{ children: React.ReactNode }> = ({
47 children,
48}) => (
49 <Box paddingX={1} marginX={1} marginBottom={1} flexDirection="column">
50 <Box marginTop={1}>{children}</Box>
51 </Box>
52);
53
54// Keyboard navigation shortcuts help text
55const KEYBOARD_SHORTCUTS =
56 "↑/↓ to navigate, Enter to select, Tab to complete, Ctrl+r to refresh list";
57
58const FileSearchUI: React.FC<FileSearchUIProps> = ({
59 filter,
60 selectedIndex,
61 onFilesUpdated,
62}) => {
63 const [files, setFiles] = useState<FileItem[]>([]);
64
65 // Get file index service state for reactive updates
66 const { services: serviceStates } = useServices<{
67 fileIndex: FileIndexServiceState;
68 }>(["fileIndex"]);
69
70 const fileIndexServiceState = serviceStates.fileIndex;
71
72 useEffect(() => {
73 if (!fileIndexServiceState) {
74 return;
75 }
76
77 // Use the FileIndexService's filterFiles method which includes fzf fuzzy matching
78 const fetchFilteredFiles = async () => {
79 try {
80 // console.log(`FileSearchUI: Filtering files with "${filter}", total files in state:`, fileIndexServiceState?.files?.length);
81 const filteredFiles = await services.fileIndex.filterFiles(filter, 10);
82 // console.log(`FileSearchUI: Filtered to ${filteredFiles.length} files`);
83 setFiles(filteredFiles);
84
85 if (onFilesUpdated) {
86 onFilesUpdated(filteredFiles);
87 }
88 } catch (error) {
89 // Suppress expected "search cancelled" errors from AsyncFzf when typing fast
90 const errorMessage =
91 error instanceof Error ? error.message : String(error);
92 if (!errorMessage.includes("search cancelled")) {
93 console.error("Error filtering files:", error);
94 }
95 setFiles([]);
96 }
97 };
98
99 fetchFilteredFiles();
100 }, [fileIndexServiceState?.files, filter, onFilesUpdated]);
101
102 // Handle Ctrl+R to refresh file index
103 useInput((input, key) => {
104 if (key.ctrl && input === "r") {
105 services.fileIndex.refreshIndex(true); // Bypass timeout for manual refresh
106 }
107 });
108
109 if (fileIndexServiceState?.error) {
110 // Special handling for directory too large error
111 if (fileIndexServiceState.error === "directory-too-large") {
112 return (
113 <FileSearchContainer>
114 <Text color="gray" dimColor>
115 Ctrl+r to refresh list (this may take several seconds)
116 </Text>
117 </FileSearchContainer>
118 );
119 }
120
121 // Other errors
122 return (
123 <FileSearchContainer>
124 <Text color="red">
125 Error indexing files: {fileIndexServiceState.error}
126 </Text>
127 </FileSearchContainer>
128 );
129 }
130
131 if (files.length === 0 && !fileIndexServiceState?.isIndexing) {
132 // Only show "No matching files found" if:
133 // 1. There are files in the index (so it's not a system issue)
134 // 2. There's actually a search filter (not just "@")
135 if (
136 fileIndexServiceState?.files &&
137 fileIndexServiceState.files.length > 0 &&
138 filter.length > 0
139 ) {
140 return (
141 <FileSearchContainer>
142 <Text color="gray" dimColor>
143 No matching files found
144 </Text>
145 </FileSearchContainer>
146 );
147 }
148
149 // Show keyboard navigation hints
150 return (
151 <FileSearchContainer>
152 <Text color="gray" dimColor>
153 {KEYBOARD_SHORTCUTS}
154 </Text>
155 </FileSearchContainer>
156 );
157 }
158
159 return (
160 <Box paddingX={1} marginX={1} marginBottom={1} flexDirection="column">
161 {files.map((file, index) => {
162 const isSelected = index === selectedIndex;
163
164 return (
165 <Box key={file.path}>
166 <Text color="white" bold={isSelected}>
167 {" "}
168 <Text color="green" dimColor={!isSelected}>
169 @
170 <HighlightedText
171 text={file.displayName}
172 positions={file.positions}
173 />
174 </Text>
175 </Text>
176 </Box>
177 );
178 })}
179
180 <Box marginTop={1}>
181 <Text color="gray" dimColor>
182 {KEYBOARD_SHORTCUTS}
183 </Text>
184 </Box>
185 </Box>
186 );
187};
188
189export { FileSearchUI };