Elffuss BraidHub

kikocis/continue public

b06f5503dce8 · 3.7 KiBRawPermalink
1import { Box, Text } from "ink";
2import React, { useEffect, useState } from "react";
3
4import { useServices } from "../hooks/useService.js";
5import { FileIndexServiceState } from "../services/FileIndexService.js";
6
7interface FileSearchUIProps {
8 filter: string;
9 selectedIndex: number;
10 onSelect: (filePath: string) => void;
11 onFilesUpdated?: (files: FileItem[]) => void;
12}
13
14interface FileItem {
15 path: string;
16 displayName: string;
17}
18
19const FileSearchUI: React.FC<FileSearchUIProps> = ({
20 filter,
21 selectedIndex,
22 onFilesUpdated,
23}) => {
24 const [files, setFiles] = useState<FileItem[]>([]);
25
26 // Get file index service
27 const { services } = useServices<{
28 fileIndex: FileIndexServiceState;
29 }>(["fileIndex"]);
30
31 const fileIndexService = services.fileIndex;
32
33 useEffect(() => {
34 if (!fileIndexService) {
35 return;
36 }
37
38 // Get filtered files from the service
39 const filteredFiles = fileIndexService.files
40 .filter((file) => {
41 if (filter.length === 0) {
42 return true;
43 }
44 const lowerFilter = filter.toLowerCase();
45 return (
46 file.displayName.toLowerCase().includes(lowerFilter) ||
47 file.path.toLowerCase().includes(lowerFilter)
48 );
49 })
50 .sort((a, b) => {
51 if (filter.length === 0) {
52 // Sort alphabetically when no filter
53 const aFileName = a.path.split("/").pop() || a.path;
54 const bFileName = b.path.split("/").pop() || b.path;
55 return aFileName.localeCompare(bFileName);
56 }
57
58 const lowerFilter = filter.toLowerCase();
59 const aFileName = a.path.split("/").pop() || a.path;
60 const bFileName = b.path.split("/").pop() || b.path;
61
62 // Prioritize exact matches in file name
63 const aNameMatch = aFileName.toLowerCase().includes(lowerFilter);
64 const bNameMatch = bFileName.toLowerCase().includes(lowerFilter);
65
66 if (aNameMatch && !bNameMatch) return -1;
67 if (!aNameMatch && bNameMatch) return 1;
68
69 // Then prioritize files that start with the filter
70 const aStartsWith = aFileName.toLowerCase().startsWith(lowerFilter);
71 const bStartsWith = bFileName.toLowerCase().startsWith(lowerFilter);
72
73 if (aStartsWith && !bStartsWith) return -1;
74 if (!aStartsWith && bStartsWith) return 1;
75
76 // Finally, sort by file name
77 return aFileName.localeCompare(bFileName);
78 })
79 .slice(0, 10);
80
81 setFiles(filteredFiles);
82
83 if (onFilesUpdated) {
84 onFilesUpdated(filteredFiles);
85 }
86 }, [fileIndexService?.files, filter, onFilesUpdated]);
87
88 if (fileIndexService?.isIndexing) {
89 return (
90 <Box paddingX={1} marginX={1} marginBottom={1}>
91 <Text color="gray">Indexing files...</Text>
92 </Box>
93 );
94 }
95
96 if (fileIndexService?.error) {
97 return (
98 <Box paddingX={1} marginX={1} marginBottom={1}>
99 <Text color="red">Error indexing files: {fileIndexService.error}</Text>
100 </Box>
101 );
102 }
103
104 if (files.length === 0) {
105 return (
106 <Box paddingX={1} marginX={1} marginBottom={1}>
107 <Text color="gray">No matching files found</Text>
108 </Box>
109 );
110 }
111
112 return (
113 <Box paddingX={1} marginX={1} marginBottom={1} flexDirection="column">
114 {files.map((file, index) => {
115 const isSelected = index === selectedIndex;
116
117 return (
118 <Box key={file.path}>
119 <Text color="white" bold={isSelected}>
120 {" "}
121 <Text color="green">@{file.displayName}</Text>
122 </Text>
123 </Box>
124 );
125 })}
126
127 <Box marginTop={1}>
128 <Text color="gray" dimColor>
129 ↑/↓ to navigate, Enter to select, Tab to complete
130 </Text>
131 </Box>
132 </Box>
133 );
134};
135
136export { FileSearchUI };