({
initialPath,
}: UseDirectoryBrowserOptions = {})
| 36 | * and navigation utilities. |
| 37 | */ |
| 38 | export function useDirectoryBrowser({ |
| 39 | initialPath, |
| 40 | }: UseDirectoryBrowserOptions = {}): UseDirectoryBrowserReturn { |
| 41 | const [currentPath, setCurrentPath] = useState(initialPath ?? os.homedir()) |
| 42 | |
| 43 | // Get directories for current path |
| 44 | const directories = useMemo(() => getDirectories(currentPath), [currentPath]) |
| 45 | |
| 46 | // Check if current directory has .git |
| 47 | const isGitRepo = useMemo(() => hasGitDirectory(currentPath), [currentPath]) |
| 48 | |
| 49 | // Expand ~ to home directory |
| 50 | const expandPath = useCallback((inputPath: string): string => { |
| 51 | if (inputPath.startsWith('~')) { |
| 52 | return path.join(os.homedir(), inputPath.slice(1)) |
| 53 | } |
| 54 | return inputPath |
| 55 | }, []) |
| 56 | |
| 57 | // Try to navigate to a typed path |
| 58 | const tryNavigateToPath = useCallback( |
| 59 | (inputPath: string): boolean => { |
| 60 | const expandedPath = expandPath(inputPath.trim()) |
| 61 | try { |
| 62 | if (existsSync(expandedPath) && statSync(expandedPath).isDirectory()) { |
| 63 | setCurrentPath(expandedPath) |
| 64 | return true |
| 65 | } |
| 66 | } catch { |
| 67 | // Path doesn't exist or can't be accessed |
| 68 | } |
| 69 | return false |
| 70 | }, |
| 71 | [expandPath], |
| 72 | ) |
| 73 | |
| 74 | // Navigate to a directory entry |
| 75 | const navigateToDirectory = useCallback((entry: DirectoryEntry) => { |
| 76 | setCurrentPath(entry.path) |
| 77 | }, []) |
| 78 | |
| 79 | return { |
| 80 | currentPath, |
| 81 | setCurrentPath, |
| 82 | directories, |
| 83 | isGitRepo, |
| 84 | expandPath, |
| 85 | tryNavigateToPath, |
| 86 | navigateToDirectory, |
| 87 | } |
| 88 | } |
no test coverage detected