|
| 1 | +import { Folder } from "@phosphor-icons/react"; |
| 2 | +import { ChevronDownIcon } from "@radix-ui/react-icons"; |
| 3 | +import { Box, Button, Flex, Popover, Text, TextField } from "@radix-ui/themes"; |
| 4 | +import { useEffect, useRef, useState } from "react"; |
| 5 | +import { useHotkeys } from "react-hotkeys-hook"; |
| 6 | +import { useFolderPickerStore } from "../stores/folderPickerStore"; |
| 7 | +import { Command } from "./command"; |
| 8 | + |
| 9 | +interface FolderPickerProps { |
| 10 | + value: string; |
| 11 | + onChange: (path: string) => void; |
| 12 | + placeholder?: string; |
| 13 | + size?: "1" | "2" | "3"; |
| 14 | +} |
| 15 | + |
| 16 | +const HOTKEYS = { |
| 17 | + ARROW_UP: "arrowup", |
| 18 | + ARROW_DOWN: "arrowdown", |
| 19 | + ENTER: "enter", |
| 20 | + ESCAPE: "escape", |
| 21 | +} as const; |
| 22 | + |
| 23 | +const MAX_RECENT_ITEMS = 5; |
| 24 | +const SEARCH_DEBOUNCE_MS = 100; |
| 25 | +const MAX_LIST_HEIGHT = "300px"; |
| 26 | + |
| 27 | +const displayPath = (path: string): string => { |
| 28 | + const homePattern = /^\/Users\/[^/]+|^\/home\/[^/]+/; |
| 29 | + const match = path.match(homePattern); |
| 30 | + return match ? path.replace(match[0], "~") : path; |
| 31 | +}; |
| 32 | + |
| 33 | +export function FolderPicker({ |
| 34 | + value, |
| 35 | + onChange, |
| 36 | + placeholder = "Select folder...", |
| 37 | + size = "2", |
| 38 | +}: FolderPickerProps) { |
| 39 | + const [open, setOpen] = useState(false); |
| 40 | + const [searchValue, setSearchValue] = useState(""); |
| 41 | + const [directoryPreview, setDirectoryPreview] = useState<string[]>([]); |
| 42 | + const [recentPreview, setRecentPreview] = useState<string[]>([]); |
| 43 | + const [selectedIndex, setSelectedIndex] = useState(0); |
| 44 | + const [isSearching, setIsSearching] = useState(false); |
| 45 | + const searchInputRef = useRef<HTMLInputElement>(null); |
| 46 | + |
| 47 | + const { recentDirectories, addRecentDirectory } = useFolderPickerStore(); |
| 48 | + |
| 49 | + const displayValue = value ? displayPath(value) : placeholder; |
| 50 | + const totalItems = recentPreview.length + directoryPreview.length; |
| 51 | + |
| 52 | + useHotkeys( |
| 53 | + Object.values(HOTKEYS).join(","), |
| 54 | + (ev, handler) => { |
| 55 | + const key = handler.keys?.join(""); |
| 56 | + |
| 57 | + if (key === HOTKEYS.ARROW_UP || key === HOTKEYS.ARROW_DOWN) { |
| 58 | + ev.preventDefault(); |
| 59 | + if (totalItems > 0) { |
| 60 | + const direction = key === HOTKEYS.ARROW_UP ? -1 : 1; |
| 61 | + setSelectedIndex( |
| 62 | + (selectedIndex + direction + totalItems) % totalItems, |
| 63 | + ); |
| 64 | + } |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (key === HOTKEYS.ENTER) { |
| 69 | + ev.preventDefault(); |
| 70 | + if (totalItems > 0) { |
| 71 | + const selectedPath = |
| 72 | + selectedIndex < recentPreview.length |
| 73 | + ? recentPreview[selectedIndex] |
| 74 | + : directoryPreview[selectedIndex - recentPreview.length]; |
| 75 | + |
| 76 | + if (selectedPath) { |
| 77 | + handleSelect(selectedPath); |
| 78 | + } |
| 79 | + } |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (key === HOTKEYS.ESCAPE) { |
| 84 | + ev.stopPropagation(); |
| 85 | + setOpen(false); |
| 86 | + } |
| 87 | + }, |
| 88 | + { enabled: open, enableOnFormTags: true }, |
| 89 | + ); |
| 90 | + |
| 91 | + useEffect(() => { |
| 92 | + if (!open) { |
| 93 | + setSearchValue(""); |
| 94 | + setDirectoryPreview([]); |
| 95 | + setRecentPreview([]); |
| 96 | + setIsSearching(false); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + if (!searchValue.trim()) { |
| 101 | + setRecentPreview(recentDirectories.slice(0, MAX_RECENT_ITEMS)); |
| 102 | + setDirectoryPreview([]); |
| 103 | + setIsSearching(false); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + setIsSearching(true); |
| 108 | + const timer = setTimeout(async () => { |
| 109 | + try { |
| 110 | + const results = await window.electronAPI.searchDirectories(searchValue); |
| 111 | + setDirectoryPreview(results); |
| 112 | + |
| 113 | + const searchLower = searchValue.toLowerCase(); |
| 114 | + const filtered = recentDirectories |
| 115 | + .filter((dir) => dir.toLowerCase().includes(searchLower)) |
| 116 | + .slice(0, MAX_RECENT_ITEMS); |
| 117 | + setRecentPreview(filtered); |
| 118 | + } finally { |
| 119 | + setIsSearching(false); |
| 120 | + } |
| 121 | + }, SEARCH_DEBOUNCE_MS); |
| 122 | + |
| 123 | + return () => clearTimeout(timer); |
| 124 | + }, [searchValue, recentDirectories, open]); |
| 125 | + |
| 126 | + const handleSelect = (path: string) => { |
| 127 | + onChange(path); |
| 128 | + addRecentDirectory(path); |
| 129 | + setSearchValue(""); |
| 130 | + setOpen(false); |
| 131 | + }; |
| 132 | + |
| 133 | + const handleSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 134 | + setSearchValue(e.target.value); |
| 135 | + setSelectedIndex(0); |
| 136 | + }; |
| 137 | + |
| 138 | + const handleOpenChange = (newOpen: boolean) => { |
| 139 | + setOpen(newOpen); |
| 140 | + if (!newOpen) { |
| 141 | + setSearchValue(""); |
| 142 | + setSelectedIndex(0); |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + const renderItem = (path: string, itemIndex: number) => ( |
| 147 | + <Command.Item |
| 148 | + key={path} |
| 149 | + className={selectedIndex === itemIndex ? "!bg-accent-3" : ""} |
| 150 | + onSelect={() => handleSelect(path)} |
| 151 | + > |
| 152 | + <Text |
| 153 | + size="2" |
| 154 | + style={{ |
| 155 | + overflow: "hidden", |
| 156 | + textOverflow: "ellipsis", |
| 157 | + }} |
| 158 | + > |
| 159 | + {displayPath(path)} |
| 160 | + </Text> |
| 161 | + </Command.Item> |
| 162 | + ); |
| 163 | + |
| 164 | + return ( |
| 165 | + <Popover.Root open={open} onOpenChange={handleOpenChange}> |
| 166 | + <Popover.Trigger> |
| 167 | + <Button |
| 168 | + variant="surface" |
| 169 | + size={size} |
| 170 | + color="gray" |
| 171 | + style={{ width: "100%" }} |
| 172 | + > |
| 173 | + <Flex justify="between" align="center" gap="2" width="100%"> |
| 174 | + <Flex align="center" gap="2" style={{ minWidth: 0, flex: 1 }}> |
| 175 | + <Folder size={16} weight="regular" style={{ flexShrink: 0 }} /> |
| 176 | + <Text |
| 177 | + size={size} |
| 178 | + style={{ |
| 179 | + overflow: "hidden", |
| 180 | + textOverflow: "ellipsis", |
| 181 | + whiteSpace: "nowrap", |
| 182 | + }} |
| 183 | + > |
| 184 | + {displayValue} |
| 185 | + </Text> |
| 186 | + </Flex> |
| 187 | + <ChevronDownIcon style={{ flexShrink: 0 }} /> |
| 188 | + </Flex> |
| 189 | + </Button> |
| 190 | + </Popover.Trigger> |
| 191 | + |
| 192 | + <Popover.Content |
| 193 | + side="bottom" |
| 194 | + align="start" |
| 195 | + avoidCollisions={false} |
| 196 | + style={{ padding: 0, width: "var(--radix-popover-trigger-width)" }} |
| 197 | + onOpenAutoFocus={() => { |
| 198 | + setTimeout(() => searchInputRef.current?.focus(), 0); |
| 199 | + }} |
| 200 | + > |
| 201 | + <Command.Root shouldFilter={false}> |
| 202 | + <Box p="2" style={{ borderBottom: "1px solid var(--gray-a5)" }}> |
| 203 | + <TextField.Root |
| 204 | + ref={searchInputRef} |
| 205 | + placeholder="Search folders..." |
| 206 | + value={searchValue} |
| 207 | + onChange={handleSearchChange} |
| 208 | + size={size} |
| 209 | + > |
| 210 | + <TextField.Slot> |
| 211 | + <Folder size={16} weight="regular" /> |
| 212 | + </TextField.Slot> |
| 213 | + </TextField.Root> |
| 214 | + </Box> |
| 215 | + |
| 216 | + <Command.List |
| 217 | + style={{ maxHeight: MAX_LIST_HEIGHT, overflowY: "auto" }} |
| 218 | + > |
| 219 | + {totalItems === 0 && ( |
| 220 | + <Command.Empty> |
| 221 | + <Box p="4"> |
| 222 | + <Text size="2" color="gray"> |
| 223 | + {isSearching ? "Searching..." : "No folders found"} |
| 224 | + </Text> |
| 225 | + </Box> |
| 226 | + </Command.Empty> |
| 227 | + )} |
| 228 | + |
| 229 | + {recentPreview.length > 0 && ( |
| 230 | + <Command.Group heading="Recent Directories"> |
| 231 | + {recentPreview.map((path, idx) => renderItem(path, idx))} |
| 232 | + </Command.Group> |
| 233 | + )} |
| 234 | + |
| 235 | + {directoryPreview.length > 0 && ( |
| 236 | + <Command.Group heading="Paths"> |
| 237 | + {directoryPreview.map((path, idx) => |
| 238 | + renderItem(path, recentPreview.length + idx), |
| 239 | + )} |
| 240 | + </Command.Group> |
| 241 | + )} |
| 242 | + </Command.List> |
| 243 | + </Command.Root> |
| 244 | + </Popover.Content> |
| 245 | + </Popover.Root> |
| 246 | + ); |
| 247 | +} |
0 commit comments