feat(servers): open a remote directory session from a dialog

The open-directory form no longer sits at the bottom of the project list, where it overlapped the mobile tab bar. A button beside the search input opens it in a dialog.
This commit is contained in:
Chris Scott committed 2026-10-19 15:41:23 -04:00
1 parent c18c3584ef
commit 8605dbf494
3 files changed
+189 -47

No files matched your search

@@ -0,0 +1,103 @@
import { useEffect, useState, type FormEvent } from 'react'
import { useNavigate } from 'react-router-dom'
import { AlertCircle, Loader2, Plus } from 'lucide-react'
import { useCreateSession } from '@/hooks/useOpenCode'
import { getServerSessionPath } from '@/lib/navigation'
import { parseNetworkError } from '@/lib/opencode-errors'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
interface OpenDirectoryDialogProps {
serverId: string
open: boolean
onOpenChange: (open: boolean) => void
}
/**
* Dialog that starts a new session in an absolute directory on the linked server.
*/
export function OpenDirectoryDialog({ serverId, open, onOpenChange }: OpenDirectoryDialogProps) {
const navigate = useNavigate()
const [directory, setDirectory] = useState('')
const [error, setError] = useState<string | null>(null)
const createSession = useCreateSession(directory)
useEffect(() => {
if (!open) {
setDirectory('')
setError(null)
}
}, [open])
const canSubmit = directory.trim().length > 0 && !createSession.isPending
const handleSubmit = async (event: FormEvent) => {
event.preventDefault()
if (!canSubmit) return
setError(null)
try {
const session = await createSession.mutateAsync({ agent: undefined })
onOpenChange(false)
navigate(getServerSessionPath(serverId, session.id))
} catch (cause) {
setError(parseNetworkError(cause).message)
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Open directory</DialogTitle>
<DialogDescription>
Start a session in an absolute path on the linked server.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-1.5">
<label htmlFor="open-directory-path" className="text-sm font-medium">
Directory
</label>
<Input
id="open-directory-path"
value={directory}
onChange={(event) => setDirectory(event.target.value)}
placeholder="/absolute/path/on/the/server"
autoComplete="off"
name="remote-directory"
className="font-mono text-sm"
autoFocus
disabled={createSession.isPending}
/>
</div>
{error ? (
<div className="flex items-start gap-2 rounded border border-destructive/30 bg-destructive/10 p-3">
<AlertCircle className="mt-0.5 h-4 w-4 shrink-0 text-destructive" />
<p className="text-sm text-destructive">{error}</p>
</div>
) : null}
<div className="flex justify-end gap-2">
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={createSession.isPending}
>
Cancel
</Button>
<Button type="submit" disabled={!canSubmit}>
{createSession.isPending ? (
<Loader2 className="mr-1 h-4 w-4 animate-spin" />
) : (
<Plus className="mr-1 h-4 w-4" />
)}
Start session
</Button>
</div>
</form>
</DialogContent>
</Dialog>
)
}
@@ -1,21 +1,20 @@
import { useMemo, useState, type FormEvent } from 'react'
import { useMemo, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { useQuery } from '@tanstack/react-query'
import { Folder, FolderGit2, GitBranch, Loader2, Plus, Search, Server } from 'lucide-react'
import { Folder, FolderGit2, FolderPlus, GitBranch, Search, Server } from 'lucide-react'
import { listProjects, type OpenCodeProject } from '@/api/opencode'
import { useCreateSession } from '@/hooks/useOpenCode'
import { useOpenCodeServers } from '@/hooks/useOpenCodeServers'
import { useSettingsDialog } from '@/hooks/useSettingsDialog'
import { useProjectActivityState } from '@/hooks/useActiveWork'
import { serverScopedKey } from '@/lib/serverScope'
import { getServerProjectPath, getServerSessionPath } from '@/lib/navigation'
import { getServerProjectPath } from '@/lib/navigation'
import { formatShortRelativeTime, getDirectoryName } from '@/lib/utils'
import { OpenCodeServerProvider } from '@/contexts/OpenCodeServerContext'
import { ActivityStateIndicator } from '@/components/activity/ActivityStateIndicator'
import { Button } from '@/components/ui/button'
import { Card } from '@/components/ui/card'
import { Input } from '@/components/ui/input'
import { LinkedServerStateCard } from '@/components/servers/LinkedServerStateCard'
import { OpenDirectoryDialog } from '@/components/servers/OpenDirectoryDialog'
import { RemoteProjectRowActions } from '@/components/servers/RemoteProjectRowActions'
import { RepoCardShell } from '@/components/repo/RepoCardShell'
import { RepoCardSkeleton } from '@/components/repo/RepoCardSkeleton'
@@ -53,6 +52,21 @@ function RemoteProjectCard({ project, serverId }: { project: OpenCodeProject; se
)
}
function OpenDirectoryTrigger({ onClick }: { onClick: () => void }) {
return (
<Button
variant="ghost"
size="icon"
aria-label="Open directory"
title="Open directory"
className="h-9 w-9 shrink-0"
onClick={onClick}
>
<FolderPlus className="h-4 w-4" />
</Button>
)
}
function RemoteProjectListLoading() {
return (
<div className="flex flex-1 flex-col">
@@ -73,11 +87,9 @@ function RemoteProjectListLoading() {
}
function RemoteProjectListContent({ serverId }: { serverId: string }) {
const navigate = useNavigate()
const { servers, isLoading: serversLoading } = useOpenCodeServers()
const { openTab } = useSettingsDialog()
const createSession = useCreateSession()
const [openDirectory, setOpenDirectory] = useState('')
const [openDirectoryOpen, setOpenDirectoryOpen] = useState(false)
const [searchQuery, setSearchQuery] = useState('')
const server = servers.find((candidate) => candidate.id === serverId)
@@ -104,18 +116,6 @@ function RemoteProjectListContent({ serverId }: { serverId: string }) {
const openSettings = () => openTab('servers')
const handleOpenDirectory = async (event: FormEvent) => {
event.preventDefault()
const directory = openDirectory.trim()
if (!directory) return
try {
const session = await createSession.mutateAsync({ directory })
navigate(getServerSessionPath(serverId, session.id))
} catch {
return
}
}
return (
<div className="flex h-full flex-col px-0 md:p-4">
{!serversLoading && !server ? (
@@ -163,18 +163,22 @@ function RemoteProjectListContent({ serverId }: { serverId: string }) {
<FolderGit2 className="w-12 h-12 mx-auto mb-4 text-muted-foreground" />
<p className="font-medium">No projects on this server yet</p>
<p className="text-sm text-muted-foreground">
Start a session in a directory below to create one.
Start a session in a directory to create one.
</p>
<div className="mt-4">
<OpenDirectoryTrigger onClick={() => setOpenDirectoryOpen(true)} />
</div>
</div>
) : (
<>
<div className="px-2 md:px-0">
<div className="flex items-center gap-2 px-2 md:px-0">
<RepoSearchInput
value={searchQuery}
onValueChange={setSearchQuery}
placeholder="Search projects..."
name="remote-project-search"
/>
<OpenDirectoryTrigger onClick={() => setOpenDirectoryOpen(true)} />
</div>
<div className="mx-2 flex min-h-0 flex-1 md:mx-0">
<div className="h-full w-full overflow-y-auto pt-4 md:pb-0 [mask-image:linear-gradient(to_bottom,transparent,black_16px,black)]">
@@ -203,28 +207,11 @@ function RemoteProjectListContent({ serverId }: { serverId: string }) {
) : null}
{server && !isDisabled ? (
<section aria-label="Open directory" className="mt-4 flex shrink-0 flex-col gap-2 px-2 md:px-0">
<h2 className="text-xs font-medium uppercase tracking-wider text-muted-foreground">Open directory</h2>
<form onSubmit={handleOpenDirectory} className="flex items-center gap-2">
<Input
value={openDirectory}
onChange={(event) => setOpenDirectory(event.target.value)}
placeholder="/absolute/path/on/the/server"
aria-label="Directory path"
autoComplete="off"
name="remote-directory"
className="font-mono text-sm"
/>
<Button type="submit" disabled={!openDirectory.trim() || createSession.isPending} className="shrink-0">
{createSession.isPending ? (
<Loader2 className="mr-1 h-4 w-4 animate-spin" />
) : (
<Plus className="mr-1 h-4 w-4" />
)}
Start session
</Button>
</form>
</section>
<OpenDirectoryDialog
serverId={serverId}
open={openDirectoryOpen}
onOpenChange={setOpenDirectoryOpen}
/>
) : null}
</div>
)
@@ -33,6 +33,11 @@ vi.mock('@/hooks/useSettingsDialog', () => ({
vi.mock('@/hooks/useMobile', () => ({
useMobile: () => mocks.isMobile,
useSwipeBack: () => ({
bind: vi.fn(),
swipeProgress: 0,
swipeStyles: {},
}),
}))
vi.mock('@/hooks/useActiveWork', () => ({
@@ -215,22 +220,69 @@ describe('RemoteProjectList', () => {
expect(screen.queryByTestId('project-route')).not.toBeInTheDocument()
})
it('opens a session in an arbitrary directory', async () => {
it('opens a session in an arbitrary directory from the dialog', async () => {
const user = userEvent.setup()
mocks.createSession.mockResolvedValue({ id: 'ses_other' })
renderProjectList()
await user.type(screen.getByLabelText('Directory path'), '/srv/other')
await user.click(await screen.findByRole('button', { name: 'Open directory' }))
await user.type(screen.getByLabelText('Directory'), '/srv/other')
await user.click(screen.getByRole('button', { name: /Start session/ }))
await waitFor(() => {
expect(mocks.createSession).toHaveBeenCalledWith({ directory: '/srv/other' })
expect(mocks.useCreateSession).toHaveBeenCalledWith('/srv/other')
})
expect(mocks.createSession).toHaveBeenCalledWith({ agent: undefined })
await waitFor(() => {
expect(screen.getByTestId('session-route')).toBeInTheDocument()
})
})
it('does not render the open-directory form in the list until the dialog opens', async () => {
const user = userEvent.setup()
renderProjectList()
await screen.findByText('App')
expect(screen.queryByLabelText('Directory')).not.toBeInTheDocument()
expect(screen.queryByPlaceholderText('/absolute/path/on/the/server')).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Open directory' }))
expect(await screen.findByLabelText('Directory')).toBeInTheDocument()
expect(screen.queryByPlaceholderText('/absolute/path/on/the/server')).toBeInTheDocument()
})
it('disables Start session until a directory is entered', async () => {
const user = userEvent.setup()
renderProjectList()
await user.click(await screen.findByRole('button', { name: 'Open directory' }))
expect(screen.getByRole('button', { name: /Start session/ })).toBeDisabled()
await user.type(screen.getByLabelText('Directory'), ' ')
expect(screen.getByRole('button', { name: /Start session/ })).toBeDisabled()
await user.type(screen.getByLabelText('Directory'), '/srv/other')
expect(screen.getByRole('button', { name: /Start session/ })).toBeEnabled()
})
it('clears the directory when the dialog is cancelled', async () => {
const user = userEvent.setup()
renderProjectList()
await user.click(await screen.findByRole('button', { name: 'Open directory' }))
await user.type(screen.getByLabelText('Directory'), '/srv/other')
await user.click(screen.getByRole('button', { name: 'Cancel' }))
await waitFor(() => {
expect(screen.queryByLabelText('Directory')).not.toBeInTheDocument()
})
await user.click(screen.getByRole('button', { name: 'Open directory' }))
expect(await screen.findByLabelText('Directory')).toHaveValue('')
})
it('filters projects by name and path', async () => {
const user = userEvent.setup()
mocks.listProjects.mockResolvedValue([project, otherProject])