MCPcopy Create free account
hub / github.com/winfunc/opcode / FilePicker

Function FilePicker

src/components/FilePicker.tsx:97–492  ·  view source on GitHub ↗
({
  basePath,
  onSelect,
  onClose,
  initialQuery = "",
  className,
})

Source from the content-addressed store, hash-verified

95 * />
96 */
97export const FilePicker: React.FC<FilePickerProps> = ({
98 basePath,
99 onSelect,
100 onClose,
101 initialQuery = "",
102 className,
103}) => {
104 const searchQuery = initialQuery;
105
106 const [currentPath, setCurrentPath] = useState(basePath);
107 const [entries, setEntries] = useState<FileEntry[]>(() =>
108 searchQuery.trim() ? [] : globalDirectoryCache.get(basePath) || []
109 );
110 const [searchResults, setSearchResults] = useState<FileEntry[]>(() => {
111 if (searchQuery.trim()) {
112 const cacheKey = `${basePath}:${searchQuery}`;
113 return globalSearchCache.get(cacheKey) || [];
114 }
115 return [];
116 });
117 const [isLoading, setIsLoading] = useState(false);
118 const [error, setError] = useState<string | null>(null);
119 const [pathHistory, setPathHistory] = useState<string[]>([basePath]);
120 const [selectedIndex, setSelectedIndex] = useState(0);
121 const [isShowingCached, setIsShowingCached] = useState(() => {
122 // Check if we're showing cached data on mount
123 if (searchQuery.trim()) {
124 const cacheKey = `${basePath}:${searchQuery}`;
125 return globalSearchCache.has(cacheKey);
126 }
127 return globalDirectoryCache.has(basePath);
128 });
129
130 const searchDebounceRef = useRef<NodeJS.Timeout | null>(null);
131 const fileListRef = useRef<HTMLDivElement>(null);
132
133 // Computed values
134 const displayEntries = searchQuery.trim() ? searchResults : entries;
135 const canGoBack = pathHistory.length > 1;
136
137 // Get relative path for display
138 const relativePath = currentPath.startsWith(basePath)
139 ? currentPath.slice(basePath.length) || '/'
140 : currentPath;
141
142 // Load directory contents
143 useEffect(() => {
144 loadDirectory(currentPath);
145 }, [currentPath]);
146
147 // Debounced search
148 useEffect(() => {
149 if (searchDebounceRef.current) {
150 clearTimeout(searchDebounceRef.current);
151 }
152
153 if (searchQuery.trim()) {
154 const cacheKey = `${basePath}:${searchQuery}`;

Callers

nothing calls this directly

Calls 7

cnFunction · 0.90
loadDirectoryFunction · 0.85
performSearchFunction · 0.85
handleEntryClickFunction · 0.85
handleEntryDoubleClickFunction · 0.85
getFileIconFunction · 0.70
formatFileSizeFunction · 0.70

Tested by

no test coverage detected