({
basePath,
onSelect,
onClose,
initialQuery = "",
className,
})
| 95 | * /> |
| 96 | */ |
| 97 | export 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}`; |
nothing calls this directly
no test coverage detected