improve remote folder picker UI (#1240)
- polish remote folder picker layout and button borders - wip in improving remote folder picker Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
# Remote SSH Folder Picker — Redesign Proposal
|
||||
|
||||
## Context
|
||||
|
||||
The "Browse remote filesystem" dialog lets users pick a directory on a connected SSH target to open as a remote project. It's reached from: sidebar → Add project → Open remote project → folder-picker icon next to the Remote path input.
|
||||
|
||||
**Source:** `src/renderer/src/components/sidebar/RemoteFileBrowser.tsx`
|
||||
**Invoked from:** `src/renderer/src/components/sidebar/AddRepoSteps.tsx` (`RemoteStep`, around line 167)
|
||||
|
||||
## User complaints
|
||||
|
||||
1. **Hard to find the desired folder** — list is flat and unfiltered; on a home directory with 30+ dotfiles/dirs, you scroll.
|
||||
2. **Nested folders not discoverable as navigable** — current model is single-click = highlight, double-click = navigate. Users don't discover the double-click.
|
||||
3. **Unclear consequence of pressing "Select"** — button is generic; the dialog doesn't say what happens to the chosen folder next.
|
||||
|
||||
## Current behavior (for reference)
|
||||
|
||||
- Single-click a row → sets `selectedName`, highlights the row.
|
||||
- Double-click a folder row → navigates into it.
|
||||
- "Select" button → if a row is highlighted, returns `<resolvedPath>/<selectedName>`; otherwise returns `resolvedPath` (the current directory).
|
||||
- Breadcrumb bar at top with ↑, 🏠, and clickable path segments.
|
||||
- Footer shows the path that will be returned: either current dir or `current/highlighted`.
|
||||
|
||||
## Reference: how others do it
|
||||
|
||||
- **VS Code** (`simpleFileDialog.ts`): one unified input at the top doubles as (a) current path, (b) filter as you type, (c) editable path entry. Enter key navigates into folders; OK button label is caller-supplied ("Open Folder", etc.). Auto-complete suggests folders inline.
|
||||
- **Superset, Warp**: no comparable picker.
|
||||
|
||||
## Options considered
|
||||
|
||||
### Option A — VS Code-style: selection = current directory
|
||||
Drop the "highlight a row" model entirely. Navigate into the folder you want (single-click enters it), then "Select" always returns the current directory.
|
||||
|
||||
**Rejected because:** breaks Finder/Explorer muscle memory (double-click to open is a universal convention), and adds a click for the common case of picking a visible child folder.
|
||||
|
||||
### Option B — chevron-on-folders affordance
|
||||
Add a `›` navigation button on folder rows to make "enter this folder" discoverable to mouse users. A permanent chevron is visual noise, so we show it only on row hover (and on keyboard focus for a11y parity).
|
||||
|
||||
### Option C — add a filter input
|
||||
Add a text input at the top of the list that live-filters visible entries by substring. No change to the selection model.
|
||||
|
||||
### Chosen: Option C + Option B
|
||||
The filter input (C) addresses complaint #1 and surfaces `Enter`-to-navigate for keyboard users. The hover/focus chevron (B) addresses complaint #2 for mouse users without the visual noise of a permanent affordance. The two are independent and compose cleanly.
|
||||
|
||||
## Proposed change
|
||||
|
||||
### 1. Filter input (addresses complaint #1)
|
||||
|
||||
- Add a text input above the file list (below the breadcrumb bar) that auto-focuses on mount. The picker only mounts when the outer dialog opens (verified: `AddRepoSteps.tsx` conditionally renders `RemoteFileBrowser` based on dialog-open state), so the auto-focus cannot steal focus from the outer dialog's inputs.
|
||||
- Live-filter `entries` by case-insensitive substring match on `entry.name`. **Filters both files and folders** — hiding files would confuse users trying to confirm they're in the right directory (e.g. looking for a README). Files remain non-actionable.
|
||||
- Keyboard (handled on the input's `onKeyDown`):
|
||||
- `↓` / `↑` — move the highlight (`selectedName`) through the *filtered* list. `preventDefault` so the caret doesn't jump. Clamps at the ends: ArrowUp at the first filtered entry (or with nothing highlighted) stays put; it never triggers parent-directory navigation. Parent-nav is exclusively the breadcrumb `↑` button.
|
||||
- `Enter` — precedence: (a) if a folder is highlighted, navigate into it; (b) else if a **file** is highlighted, surface the transient footer hint (below) and do not navigate; (c) else if the filtered set contains exactly one folder (regardless of how many files are also in the set), navigate into that folder; (d) else highlight the first filtered entry (file or folder) — this is a highlight-only step and the visible highlight is the feedback; a subsequent `Enter` then re-enters this ladder and hits (a) or (b). Rule (b) is the only path that triggers the hint, so a filter that matches only files and is already highlighted on the first entry keeps yielding the hint on repeated `Enter`, never a silent no-op. The hint text is `Files can't be opened as a project`, shown in the footer for 2s before reverting. Chose the footer hint over an input shake/flash because it needs no animation infrastructure and the footer is already the dialog's status region.
|
||||
- `Esc` — if filter is non-empty, clear it and `stopPropagation` so the outer dialog doesn't close; otherwise let the event bubble and call `onCancel`.
|
||||
- **Focus management:** the input retains focus across row clicks. Clicking a row calls `setSelectedName` but does not steal focus (`onMouseDown` with `preventDefault` on the row buttons, or explicit `inputRef.current?.focus()` after selection). This keeps arrow keys and typing working after the user mouses.
|
||||
- **Navigation helper.** Introduce a `navigate(path)` wrapper that calls `loadDir(path)` *and* clears the filter. All user-initiated navigation (breadcrumb segment click, the breadcrumb `↑` parent-directory button, double-click, chevron click, `Enter`-to-enter-folder) goes through `navigate`. The `↑`/`↓` ArrowUp/ArrowDown *keys* move the filter-list highlight per the keyboard spec above and do not call `navigate`. The mount effect calls `loadDir(path)` directly — it never clears the filter, so a user who types before the first load completes does not lose their input. This removes the `didInitialLoad` ref.
|
||||
- When the filter changes, if the current `selectedName` is no longer in the filtered list, clear it so the button label doesn't go stale.
|
||||
- **Empty-state copy.** When `entries.length > 0` but `filteredEntries.length === 0`, render `No matches for '<filter>'` instead of the generic `Empty directory` copy — the latter is misleading when the directory has contents that are simply filtered out.
|
||||
- Placeholder: `Type to filter…` with a leading `Search` icon (lucide) for scannability, matching the existing icon-prefixed inputs in the sidebar.
|
||||
|
||||
### 2. Dynamic button label (addresses complaint #3)
|
||||
|
||||
Replace the static "Select" label with the path it will return:
|
||||
|
||||
- When a row is highlighted: `Select /home/neil/myproject`
|
||||
- When no row is highlighted: `Select /home/neil` (the current directory)
|
||||
- **Left-ellipsis truncation.** Plain Tailwind `truncate` right-ellipsizes, which hides the meaningful tail. Use `direction: rtl; text-align: left;` on an inner span wrapping the path (keep the word "Select" in a separate LTR span), or equivalent CSS. Add the full path as a `title` attribute for hover-tooltip verification. Note: RTL-directionality on the path span can reorder punctuation in filenames that themselves contain RTL characters — accepted as a tiny edge-case risk for SSH target paths.
|
||||
|
||||
This makes the two selection modes (child vs. current dir) visible without any model change, and tells the user exactly what will be opened.
|
||||
|
||||
### 3. Footer
|
||||
|
||||
Keep the existing muted full-path line — it's the unambiguous source of truth when the button label is ellipsized. Prepend a short hint so first-time users know what Select does:
|
||||
|
||||
> Opens as a remote project · `/home/neil/myproject`
|
||||
|
||||
Single line, muted, truncates with right-ellipsis (the prefix is the fixed part, the path tail can be cut since it's already in the button's `title`).
|
||||
|
||||
When `Enter` is pressed on a highlighted file, swap this line for `Files can't be opened as a project` (same muted style) for 2s, then revert. Clear the hint's timer eagerly on any filter change or navigation so the message never outlives the state it describes.
|
||||
|
||||
### 4. Folder-row chevron (addresses complaint #2)
|
||||
|
||||
Folder rows render a `›` icon (lucide `ChevronRight`) on the right edge, shown only on row hover or keyboard focus. Files never render the chevron.
|
||||
|
||||
- **Trigger:** CSS `:hover` on the row plus a `:focus-visible` rule so keyboard-focused rows also show it. Never always-visible — a permanent chevron is visual noise across a long list.
|
||||
- **Click handler:** the chevron is a nested `<button>` with its own `onClick` that calls `navigate(entry.path)`. It must `stopPropagation` so the parent row's single-click-select handler doesn't also fire. Clicking the chevron is equivalent to double-clicking the row.
|
||||
- **a11y:** `aria-label="Open <name>"`. Reachable via Tab when the row is focused; `Enter`/`Space` on the focused chevron navigates into the folder. Screen readers announce the label distinctly from the row's select action.
|
||||
- **Hit target:** ≥ 24px square, padded inside the row so touchpad taps land reliably.
|
||||
|
||||
## What is NOT changing
|
||||
|
||||
- **Selection model** — single-click highlights, double-click navigates. Finder/Explorer conventions preserved.
|
||||
- **Breadcrumb bar** — already good; nicer than VS Code's path-in-input approach.
|
||||
- **Non-git-folder handling** — `AddRepoSteps.tsx` lines 108–117 still opens the `confirm-non-git-folder` modal on `Not a valid git repository`. Out of scope.
|
||||
|
||||
## Tests
|
||||
|
||||
Add a co-located test file (following the pattern of `smart-sort.test.ts` etc. in the same directory). Minimum coverage:
|
||||
|
||||
- Filter substring-matches case-insensitively across files and folders.
|
||||
- `Enter` with a single folder match navigates into it.
|
||||
- `Enter` with multiple matches and no highlight highlights the first filtered entry.
|
||||
- `Enter` on a highlighted file surfaces the transient footer hint (`Files can't be opened as a project`) and does not navigate.
|
||||
- `Esc` with a non-empty filter clears it and does not call `onCancel`; `Esc` with empty filter calls `onCancel`.
|
||||
- `selectedName` is cleared when the filter change removes it from the visible list.
|
||||
- Filter is *not* cleared by the initial mount load; *is* cleared by a subsequent `navigate(path)` call.
|
||||
- **Path equivalence:** navigating into `foo` (via chevron/double-click/`Enter`) then pressing `Select` returns the same `onSelect` path as highlighting `foo` from its parent and pressing `Select`.
|
||||
- Clicking a folder row's chevron navigates into the folder and does not leave the row merely highlighted (i.e. chevron click is not swallowed by the row's select handler).
|
||||
- Empty-state copy: with a non-empty directory and a filter that matches nothing, the list shows `No matches for '<filter>'`, not `Empty directory`.
|
||||
|
||||
## Files to change
|
||||
|
||||
- `src/renderer/src/components/sidebar/RemoteFileBrowser.tsx` — all UI changes live here (filter input, `navigate` wrapper, chevron on folder rows, footer hint state, empty-state copy).
|
||||
- `src/renderer/src/components/sidebar/RemoteFileBrowser.test.tsx` — new.
|
||||
- No IPC, main-process, or `AddRepoSteps.tsx` changes required.
|
||||
|
||||
## Follow-ups (not this PR)
|
||||
|
||||
- **State bloat.** The component will accumulate several pieces of local state (`entries`, `selectedName`, `filter`, `resolvedPath`, transient footer-hint flag, loading/error). Consider consolidating into a `useReducer` in a later pass; leaving as discrete `useState` hooks for this PR to keep the diff reviewable.
|
||||
@@ -261,7 +261,7 @@ const AddRepoDialog = React.memo(function AddRepoDialog() {
|
||||
onClick={handleBrowse}
|
||||
disabled={isAdding}
|
||||
variant="outline"
|
||||
className="h-auto py-5 px-2 flex flex-col items-center gap-2 text-center"
|
||||
className="h-auto py-5 px-2 flex flex-col items-center gap-2 text-center border-border/80"
|
||||
>
|
||||
<FolderOpen className="size-6 text-muted-foreground" />
|
||||
<div>
|
||||
@@ -275,7 +275,7 @@ const AddRepoDialog = React.memo(function AddRepoDialog() {
|
||||
<Button
|
||||
onClick={() => setStep('clone')}
|
||||
variant="outline"
|
||||
className="h-auto py-5 px-2 flex flex-col items-center gap-2 text-center"
|
||||
className="h-auto py-5 px-2 flex flex-col items-center gap-2 text-center border-border/80"
|
||||
>
|
||||
<Globe className="size-6 text-muted-foreground" />
|
||||
<div>
|
||||
@@ -289,7 +289,7 @@ const AddRepoDialog = React.memo(function AddRepoDialog() {
|
||||
<Button
|
||||
onClick={handleOpenRemoteStep}
|
||||
variant="outline"
|
||||
className="h-auto py-5 px-2 flex flex-col items-center gap-2 text-center"
|
||||
className="h-auto py-5 px-2 flex flex-col items-center gap-2 text-center border-border/80"
|
||||
>
|
||||
<Monitor className="size-6 text-muted-foreground" />
|
||||
<div>
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { ChevronRight, Folder, File, ArrowUp, LoaderCircle, Home } from 'lucide-react'
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { ChevronRight, Folder, File, ArrowUp, LoaderCircle, Home, Search } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
type DirEntry = {
|
||||
name: string
|
||||
isDirectory: boolean
|
||||
}
|
||||
import {
|
||||
decideEnterAction,
|
||||
decideEscAction,
|
||||
filterEntries,
|
||||
joinPath,
|
||||
type DirEntry
|
||||
} from './remote-file-browser-helpers'
|
||||
|
||||
type RemoteFileBrowserProps = {
|
||||
targetId: string
|
||||
@@ -15,6 +17,9 @@ type RemoteFileBrowserProps = {
|
||||
onCancel: () => void
|
||||
}
|
||||
|
||||
const FILE_HINT_MS = 2000
|
||||
const FILE_HINT_TEXT = "Files can't be opened as a project"
|
||||
|
||||
export function RemoteFileBrowser({
|
||||
targetId,
|
||||
initialPath = '~',
|
||||
@@ -25,15 +30,33 @@ export function RemoteFileBrowser({
|
||||
const [entries, setEntries] = useState<DirEntry[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [selectedName, setSelectedName] = useState<string | null>(null)
|
||||
const [filter, setFilter] = useState('')
|
||||
const [fileHint, setFileHint] = useState(false)
|
||||
const genRef = useRef(0)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
const fileHintTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
|
||||
const clearFileHint = useCallback(() => {
|
||||
if (fileHintTimerRef.current) {
|
||||
clearTimeout(fileHintTimerRef.current)
|
||||
fileHintTimerRef.current = null
|
||||
}
|
||||
setFileHint(false)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (fileHintTimerRef.current) {
|
||||
clearTimeout(fileHintTimerRef.current)
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
|
||||
const loadDir = useCallback(
|
||||
async (dirPath: string) => {
|
||||
const gen = ++genRef.current
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
setSelectedName(null)
|
||||
try {
|
||||
const result = await window.api.ssh.browseDir({ targetId, dirPath })
|
||||
if (gen !== genRef.current) {
|
||||
@@ -56,16 +79,27 @@ export function RemoteFileBrowser({
|
||||
[targetId]
|
||||
)
|
||||
|
||||
// All user-initiated navigation goes through this wrapper so filter + hint
|
||||
// state is always cleared. The initial mount calls loadDir directly so a
|
||||
// user who types during the first load keeps their input.
|
||||
const navigate = useCallback(
|
||||
(dirPath: string) => {
|
||||
setFilter('')
|
||||
clearFileHint()
|
||||
loadDir(dirPath)
|
||||
},
|
||||
[loadDir, clearFileHint]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
loadDir(initialPath)
|
||||
}, [loadDir, initialPath])
|
||||
|
||||
const navigateTo = useCallback(
|
||||
const navigateInto = useCallback(
|
||||
(name: string) => {
|
||||
const next = resolvedPath === '/' ? `/${name}` : `${resolvedPath}/${name}`
|
||||
loadDir(next)
|
||||
navigate(joinPath(resolvedPath, name))
|
||||
},
|
||||
[resolvedPath, loadDir]
|
||||
[resolvedPath, navigate]
|
||||
)
|
||||
|
||||
const navigateUp = useCallback(() => {
|
||||
@@ -73,31 +107,108 @@ export function RemoteFileBrowser({
|
||||
return
|
||||
}
|
||||
const parent = resolvedPath.replace(/\/[^/]+\/?$/, '') || '/'
|
||||
loadDir(parent)
|
||||
}, [resolvedPath, loadDir])
|
||||
navigate(parent)
|
||||
}, [resolvedPath, navigate])
|
||||
|
||||
const handleDoubleClick = useCallback(
|
||||
(entry: DirEntry) => {
|
||||
if (entry.isDirectory) {
|
||||
navigateTo(entry.name)
|
||||
const filteredEntries = useMemo(() => filterEntries(entries, filter), [entries, filter])
|
||||
|
||||
const triggerFileHint = useCallback(() => {
|
||||
if (fileHintTimerRef.current) {
|
||||
clearTimeout(fileHintTimerRef.current)
|
||||
}
|
||||
setFileHint(true)
|
||||
fileHintTimerRef.current = setTimeout(() => {
|
||||
setFileHint(false)
|
||||
fileHintTimerRef.current = null
|
||||
}, FILE_HINT_MS)
|
||||
}, [])
|
||||
|
||||
// Select always returns the current directory. Selection model = "navigate
|
||||
// to the folder you want, then Select"; this is the VS Code approach and
|
||||
// was chosen after feedback that the highlight-a-row model was confusing.
|
||||
const handleSelect = useCallback(() => {
|
||||
onSelect(resolvedPath)
|
||||
}, [resolvedPath, onSelect])
|
||||
|
||||
// Single-click navigates; double-click on a folder selects it. Because
|
||||
// onClick fires on both mousedowns of a dblclick, we have to delay the
|
||||
// single-click so a subsequent dblclick can cancel it. 220ms matches the
|
||||
// platform dblclick threshold on macOS closely enough that the single-click
|
||||
// latency is imperceptible.
|
||||
const clickTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (clickTimerRef.current) {
|
||||
clearTimeout(clickTimerRef.current)
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
|
||||
const handleRowClick = useCallback(
|
||||
(entry: DirEntry) => {
|
||||
if (clickTimerRef.current) {
|
||||
clearTimeout(clickTimerRef.current)
|
||||
}
|
||||
clickTimerRef.current = setTimeout(() => {
|
||||
clickTimerRef.current = null
|
||||
if (entry.isDirectory) {
|
||||
navigateInto(entry.name)
|
||||
} else {
|
||||
// Files aren't navigable and can't be opened as a project — the
|
||||
// footer hint keeps the click from being a silent no-op.
|
||||
triggerFileHint()
|
||||
}
|
||||
}, 220)
|
||||
},
|
||||
[navigateTo]
|
||||
[navigateInto, triggerFileHint]
|
||||
)
|
||||
|
||||
const handleSelect = useCallback(() => {
|
||||
if (selectedName) {
|
||||
const full = resolvedPath === '/' ? `/${selectedName}` : `${resolvedPath}/${selectedName}`
|
||||
onSelect(full)
|
||||
} else {
|
||||
onSelect(resolvedPath)
|
||||
}
|
||||
}, [resolvedPath, selectedName, onSelect])
|
||||
const handleRowDoubleClick = useCallback(
|
||||
(entry: DirEntry) => {
|
||||
if (!entry.isDirectory) {
|
||||
return
|
||||
}
|
||||
if (clickTimerRef.current) {
|
||||
clearTimeout(clickTimerRef.current)
|
||||
clickTimerRef.current = null
|
||||
}
|
||||
onSelect(joinPath(resolvedPath, entry.name))
|
||||
},
|
||||
[resolvedPath, onSelect]
|
||||
)
|
||||
|
||||
const handleFilterKeyDown = useCallback(
|
||||
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
if (e.key === 'Enter') {
|
||||
const action = decideEnterAction(filteredEntries)
|
||||
if (action.type === 'navigate') {
|
||||
e.preventDefault()
|
||||
navigateInto(action.name)
|
||||
} else if (action.type === 'fileHint') {
|
||||
e.preventDefault()
|
||||
triggerFileHint()
|
||||
}
|
||||
return
|
||||
}
|
||||
if (e.key === 'Escape') {
|
||||
const action = decideEscAction(filter)
|
||||
if (action.type === 'clearFilter') {
|
||||
e.stopPropagation()
|
||||
e.preventDefault()
|
||||
setFilter('')
|
||||
clearFileHint()
|
||||
} else {
|
||||
onCancel()
|
||||
}
|
||||
}
|
||||
},
|
||||
[filter, filteredEntries, navigateInto, triggerFileHint, clearFileHint, onCancel]
|
||||
)
|
||||
|
||||
const pathSegments = resolvedPath.split('/').filter(Boolean)
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<div className="flex flex-col gap-2 min-w-0 w-full">
|
||||
{/* Breadcrumb bar */}
|
||||
<div className="flex items-center gap-0.5 min-h-[28px] overflow-x-auto scrollbar-none">
|
||||
<button
|
||||
@@ -110,7 +221,7 @@ export function RemoteFileBrowser({
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => loadDir('~')}
|
||||
onClick={() => navigate('~')}
|
||||
disabled={loading}
|
||||
className="shrink-0 p-1 rounded hover:bg-accent transition-colors cursor-pointer"
|
||||
>
|
||||
@@ -119,7 +230,7 @@ export function RemoteFileBrowser({
|
||||
<div className="flex items-center gap-0 text-[11px] text-muted-foreground ml-1 min-w-0">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => loadDir('/')}
|
||||
onClick={() => navigate('/')}
|
||||
className="shrink-0 hover:text-foreground transition-colors cursor-pointer px-0.5"
|
||||
>
|
||||
/
|
||||
@@ -129,7 +240,7 @@ export function RemoteFileBrowser({
|
||||
<ChevronRight className="size-2.5 shrink-0 text-muted-foreground/50" />
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => loadDir(`/${pathSegments.slice(0, i + 1).join('/')}`)}
|
||||
onClick={() => navigate(`/${pathSegments.slice(0, i + 1).join('/')}`)}
|
||||
className={cn(
|
||||
'truncate max-w-[120px] hover:text-foreground transition-colors cursor-pointer px-0.5',
|
||||
i === pathSegments.length - 1 && 'text-foreground font-medium'
|
||||
@@ -142,6 +253,27 @@ export function RemoteFileBrowser({
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Filter input */}
|
||||
<div className="relative">
|
||||
<Search className="size-3.5 text-muted-foreground absolute left-2 top-1/2 -translate-y-1/2 pointer-events-none" />
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
autoFocus
|
||||
value={filter}
|
||||
onChange={(e) => {
|
||||
clearFileHint()
|
||||
setFilter(e.target.value)
|
||||
}}
|
||||
onKeyDown={handleFilterKeyDown}
|
||||
placeholder="Type to filter…"
|
||||
className={cn(
|
||||
'w-full h-7 pl-7 pr-2 text-xs rounded-md bg-background',
|
||||
'border border-border focus:outline-none focus:ring-1 focus:ring-ring'
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* File listing */}
|
||||
<div className="border border-border rounded-md overflow-hidden bg-background">
|
||||
<div className="h-[240px] overflow-y-auto scrollbar-sleek">
|
||||
@@ -157,44 +289,74 @@ export function RemoteFileBrowser({
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<p className="text-xs text-muted-foreground">Empty directory</p>
|
||||
</div>
|
||||
) : filteredEntries.length === 0 ? (
|
||||
// The directory has contents; the filter just hid them all. Generic
|
||||
// "Empty directory" copy would mislead here.
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<p className="text-xs text-muted-foreground">{`No matches for '${filter}'`}</p>
|
||||
</div>
|
||||
) : (
|
||||
entries.map((entry) => (
|
||||
filteredEntries.map((entry) => (
|
||||
<button
|
||||
key={entry.name}
|
||||
type="button"
|
||||
// Single-click on a folder enters it (feedback: the old
|
||||
// click-to-highlight model was undiscoverable). Double-click
|
||||
// on a folder selects it directly — the shortcut for users
|
||||
// who don't want to step inside first.
|
||||
onClick={() => handleRowClick(entry)}
|
||||
onDoubleClick={() => handleRowDoubleClick(entry)}
|
||||
// Keep focus on the filter input so typing / Enter / Esc keep
|
||||
// working after a mouse click on a row.
|
||||
onMouseDown={(e) => {
|
||||
e.preventDefault()
|
||||
inputRef.current?.focus()
|
||||
}}
|
||||
className={cn(
|
||||
'w-full flex items-center gap-2 px-3 py-1.5 text-xs text-left transition-colors cursor-pointer',
|
||||
'hover:bg-accent/60',
|
||||
selectedName === entry.name && 'bg-accent'
|
||||
'hover:bg-accent/60'
|
||||
)}
|
||||
onClick={() => setSelectedName(entry.name)}
|
||||
onDoubleClick={() => handleDoubleClick(entry)}
|
||||
>
|
||||
{entry.isDirectory ? (
|
||||
<Folder className="size-3.5 text-blue-400 shrink-0" />
|
||||
) : (
|
||||
<File className="size-3.5 text-muted-foreground/60 shrink-0" />
|
||||
)}
|
||||
<span className="truncate">{entry.name}</span>
|
||||
<span className="truncate flex-1 min-w-0">{entry.name}</span>
|
||||
{entry.isDirectory && (
|
||||
// Chevron is always visible (user feedback): makes the
|
||||
// "click to enter" affordance unambiguous without relying on
|
||||
// hover discovery.
|
||||
<ChevronRight className="size-3.5 text-muted-foreground/60 shrink-0" />
|
||||
)}
|
||||
</button>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<p className="text-[10px] text-muted-foreground truncate">
|
||||
{selectedName ? `${resolvedPath}/${selectedName}` : resolvedPath}
|
||||
</p>
|
||||
<div className="flex items-center gap-2 shrink-0">
|
||||
<Button variant="outline" size="sm" className="h-7 text-xs" onClick={onCancel}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button size="sm" className="h-7 text-xs" onClick={handleSelect} disabled={loading}>
|
||||
Select
|
||||
</Button>
|
||||
</div>
|
||||
{/* Footer. Path line is on its own row with `block + truncate` so long
|
||||
paths can't push the container wider; buttons sit on a separate row
|
||||
and are free to align right without competing for space. */}
|
||||
<p
|
||||
className="block text-[10px] text-muted-foreground truncate w-full"
|
||||
title={fileHint ? undefined : resolvedPath}
|
||||
>
|
||||
{fileHint ? FILE_HINT_TEXT : `Opens as a remote project · ${resolvedPath}`}
|
||||
</p>
|
||||
<div className="flex items-center justify-end gap-2">
|
||||
<Button variant="outline" size="sm" className="h-7 text-xs" onClick={onCancel}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
className="h-7 text-xs"
|
||||
onClick={handleSelect}
|
||||
disabled={loading}
|
||||
title={resolvedPath}
|
||||
>
|
||||
Select folder
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
decideEnterAction,
|
||||
decideEscAction,
|
||||
filterEntries,
|
||||
type DirEntry
|
||||
} from './remote-file-browser-helpers'
|
||||
|
||||
const entries: DirEntry[] = [
|
||||
{ name: 'src', isDirectory: true },
|
||||
{ name: 'docs', isDirectory: true },
|
||||
{ name: 'README.md', isDirectory: false },
|
||||
{ name: '.env', isDirectory: false },
|
||||
{ name: 'node_modules', isDirectory: true }
|
||||
]
|
||||
|
||||
describe('filterEntries', () => {
|
||||
it('substring-matches case-insensitively across files and folders', () => {
|
||||
expect(filterEntries(entries, 'RE').map((e) => e.name)).toEqual(['README.md'])
|
||||
expect(filterEntries(entries, 'o').map((e) => e.name)).toEqual(['docs', 'node_modules'])
|
||||
})
|
||||
|
||||
it('returns the full list when filter is empty or whitespace', () => {
|
||||
expect(filterEntries(entries, '')).toHaveLength(entries.length)
|
||||
expect(filterEntries(entries, ' ')).toHaveLength(entries.length)
|
||||
})
|
||||
})
|
||||
|
||||
describe('decideEnterAction', () => {
|
||||
it('navigates when filter matches exactly one folder (files alongside do not block)', () => {
|
||||
const filtered = filterEntries(entries, 'e') // README.md, .env, node_modules
|
||||
expect(decideEnterAction(filtered)).toEqual({ type: 'navigate', name: 'node_modules' })
|
||||
})
|
||||
|
||||
it('is a no-op when multiple folders match', () => {
|
||||
const filtered = filterEntries(entries, 's') // src, docs (+ no files with s)
|
||||
// two folders → cannot disambiguate → noop
|
||||
expect(decideEnterAction(filtered)).toEqual({ type: 'noop' })
|
||||
})
|
||||
|
||||
it('shows the file hint when only files match', () => {
|
||||
const filtered = filterEntries(entries, 'readme')
|
||||
expect(decideEnterAction(filtered)).toEqual({ type: 'fileHint' })
|
||||
})
|
||||
|
||||
it('is a no-op on empty filtered list', () => {
|
||||
expect(decideEnterAction([])).toEqual({ type: 'noop' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('decideEscAction', () => {
|
||||
it('clears a non-empty filter', () => {
|
||||
expect(decideEscAction('foo')).toEqual({ type: 'clearFilter' })
|
||||
})
|
||||
|
||||
it('cancels when filter is empty', () => {
|
||||
expect(decideEscAction('')).toEqual({ type: 'cancel' })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,43 @@
|
||||
export type DirEntry = {
|
||||
name: string
|
||||
isDirectory: boolean
|
||||
}
|
||||
|
||||
export function filterEntries(entries: DirEntry[], filter: string): DirEntry[] {
|
||||
const q = filter.trim().toLowerCase()
|
||||
if (!q) {
|
||||
return entries
|
||||
}
|
||||
return entries.filter((e) => e.name.toLowerCase().includes(q))
|
||||
}
|
||||
|
||||
// Enter-key behavior for the filter input:
|
||||
// (a) filtered set has exactly one folder → navigate into it
|
||||
// (files alongside it don't block — a folder match wins)
|
||||
// (b) filtered set has exactly one file and no folders → fileHint
|
||||
// (c) otherwise → noop
|
||||
export type EnterAction =
|
||||
| { type: 'navigate'; name: string }
|
||||
| { type: 'fileHint' }
|
||||
| { type: 'noop' }
|
||||
|
||||
export function decideEnterAction(filteredEntries: DirEntry[]): EnterAction {
|
||||
const folders = filteredEntries.filter((e) => e.isDirectory)
|
||||
if (folders.length === 1) {
|
||||
return { type: 'navigate', name: folders[0].name }
|
||||
}
|
||||
if (folders.length === 0 && filteredEntries.length > 0) {
|
||||
return { type: 'fileHint' }
|
||||
}
|
||||
return { type: 'noop' }
|
||||
}
|
||||
|
||||
export type EscAction = { type: 'clearFilter' } | { type: 'cancel' }
|
||||
|
||||
export function decideEscAction(filter: string): EscAction {
|
||||
return filter.length > 0 ? { type: 'clearFilter' } : { type: 'cancel' }
|
||||
}
|
||||
|
||||
export function joinPath(resolvedPath: string, name: string): string {
|
||||
return resolvedPath === '/' ? `/${name}` : `${resolvedPath}/${name}`
|
||||
}
|
||||
Reference in New Issue
Block a user