feat(frontend): consolidate session picker navigation and remote project loading

This commit is contained in:
Chris Scott committed 2026-10-23 22:35:10 -04:00
1 parent 81fb2d4033
commit 52aeb7780f
42 files changed
+1486 -769

No files matched your search

+132 -16
View File
@@ -1,8 +1,15 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useState } from 'react'
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { stubMatchMedia } from '@/test/test-utils'
import { BottomSheet, BottomSheetHeader, BottomSheetContent } from './bottom-sheet'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { useSwipeDismiss } from '@/hooks/useMobile'
vi.mock('@/hooks/useMobile', () => ({
@@ -36,6 +43,34 @@ function AutoFocusSheetHost() {
)
}
function OpenSheetHost({ onClose }: { onClose: () => void }) {
const [open, setOpen] = useState(false)
return (
<>
<button type="button" onClick={() => setOpen(true)}>open sheet</button>
<BottomSheet isOpen={open} onClose={() => { onClose(); setOpen(false) }} ariaLabel="Test sheet">
<button type="button">first</button>
<button type="button">last</button>
</BottomSheet>
</>
)
}
function SheetWithMenu({ onClose }: { onClose: () => void }) {
return (
<BottomSheet isOpen onClose={onClose} ariaLabel="Test sheet">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button type="button">row actions</button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>Delete session</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</BottomSheet>
)
}
afterEach(() => {
Reflect.deleteProperty(window, 'matchMedia')
})
@@ -43,6 +78,11 @@ afterEach(() => {
describe('BottomSheet', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(useSwipeDismiss).mockReturnValue({
bind: vi.fn(),
swipeProgress: 0,
swipeStyles: {},
})
})
it('renders when isOpen is true', () => {
@@ -55,17 +95,56 @@ describe('BottomSheet', () => {
})
it('does not render when isOpen is false', async () => {
const { container } = render(
render(
<BottomSheet isOpen={false} onClose={() => {}} ariaLabel="Test sheet">
<div>Test content</div>
</BottomSheet>,
)
await waitFor(() => {
expect(container.querySelector('[role="dialog"]')).not.toBeInTheDocument()
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
})
})
it('calls onClose when backdrop is clicked', () => {
it('exposes the sheet as a labelled modal dialog', () => {
render(
<BottomSheet isOpen onClose={() => {}} ariaLabel="Test sheet">
<div>Test content</div>
</BottomSheet>,
)
const dialog = screen.getByRole('dialog', { name: 'Test sheet' })
expect(dialog).toHaveAttribute('aria-modal', 'true')
expect(dialog).toHaveAttribute('data-state', 'open')
})
it('moves focus into the sheet panel on open', () => {
render(
<BottomSheet isOpen onClose={() => {}} ariaLabel="Test sheet">
<button type="button">inside</button>
</BottomSheet>,
)
expect(screen.getByRole('dialog', { name: 'Test sheet' })).toHaveFocus()
})
it('keeps tab focus inside the sheet', () => {
render(
<BottomSheet isOpen onClose={() => {}} ariaLabel="Test sheet">
<button type="button">first</button>
<button type="button">last</button>
</BottomSheet>,
)
const first = screen.getByRole('button', { name: 'first' })
const last = screen.getByRole('button', { name: 'last' })
last.focus()
fireEvent.keyDown(last, { key: 'Tab' })
expect(first).toHaveFocus()
})
it('calls onClose when the backdrop is clicked', async () => {
const handleClose = vi.fn()
render(
<BottomSheet isOpen onClose={handleClose} ariaLabel="Test sheet">
@@ -73,10 +152,12 @@ describe('BottomSheet', () => {
</BottomSheet>,
)
const backdrop = document.querySelector('.bg-black\\/40')
if (backdrop) {
fireEvent.click(backdrop)
}
expect(handleClose).toHaveBeenCalled()
if (!backdrop) throw new Error('Missing sheet backdrop')
await new Promise((resolve) => setTimeout(resolve, 0))
fireEvent.pointerDown(backdrop)
await waitFor(() => expect(handleClose).toHaveBeenCalledTimes(1))
})
it('calls onClose when Escape key is pressed', () => {
@@ -87,7 +168,26 @@ describe('BottomSheet', () => {
</BottomSheet>,
)
fireEvent.keyDown(document, { key: 'Escape' })
expect(handleClose).toHaveBeenCalled()
expect(handleClose).toHaveBeenCalledTimes(1)
})
it('closes only the nested menu when Escape is pressed inside it', async () => {
const user = userEvent.setup()
const handleClose = vi.fn()
render(<SheetWithMenu onClose={handleClose} />)
await user.click(screen.getByRole('button', { name: 'row actions' }))
expect(await screen.findByRole('menu')).toBeInTheDocument()
await user.keyboard('{Escape}')
await waitFor(() => expect(screen.queryByRole('menu')).not.toBeInTheDocument())
expect(handleClose).not.toHaveBeenCalled()
expect(screen.getByRole('dialog', { name: 'Test sheet' })).toBeInTheDocument()
await user.keyboard('{Escape}')
expect(handleClose).toHaveBeenCalledTimes(1)
})
it('applies pb-safe class', () => {
@@ -96,7 +196,7 @@ describe('BottomSheet', () => {
<div>Test content</div>
</BottomSheet>,
)
const dialog = document.querySelector('[role="dialog"]')
const dialog = screen.getByRole('dialog', { name: 'Test sheet' })
expect(dialog).toBeInTheDocument()
})
@@ -118,7 +218,7 @@ describe('BottomSheet', () => {
<div>Test content</div>
</BottomSheet>,
)
const dialog = document.querySelector('[role="dialog"]')
const dialog = screen.getByRole('dialog', { name: 'Test sheet' })
expect(dialog).toHaveClass('h-[50dvh]')
})
@@ -172,10 +272,10 @@ describe('BottomSheet', () => {
const panelElement = document.querySelector('[aria-modal="true"]')
expect(panelElement).toBeInTheDocument()
expect(mockBind).toHaveBeenCalled()
expect(mockBind).toHaveBeenCalledWith(panelElement)
})
it('moves focus to the chat prompt on desktop when it closes', () => {
it('moves focus to the chat prompt on desktop when it closes', async () => {
stubMatchMedia(true)
const { rerender } = renderSheet(false)
@@ -198,10 +298,10 @@ describe('BottomSheet', () => {
</>,
)
expect(screen.getByLabelText('prompt')).toHaveFocus()
await waitFor(() => expect(screen.getByLabelText('prompt')).toHaveFocus())
})
it('does not refocus the chat prompt on touch when it closes', () => {
it('does not refocus the chat prompt on touch when it closes', async () => {
stubMatchMedia(false)
const { rerender } = renderSheet(false)
@@ -224,6 +324,7 @@ describe('BottomSheet', () => {
</>,
)
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
expect(screen.getByLabelText('prompt')).not.toHaveFocus()
})
@@ -241,7 +342,7 @@ describe('BottomSheet', () => {
await waitFor(() => expect(opener).toHaveFocus())
})
it('captures the opener when mounted already open and restores it on close', () => {
it('captures the opener when mounted already open and restores it on close', async () => {
stubMatchMedia(true)
const { rerender } = render(
<button type="button">opener</button>,
@@ -267,7 +368,22 @@ describe('BottomSheet', () => {
</>,
)
expect(opener).toHaveFocus()
await waitFor(() => expect(opener).toHaveFocus())
})
it('returns focus to the opener after the sheet closes through the keyboard', async () => {
const user = userEvent.setup()
const handleClose = vi.fn()
render(<OpenSheetHost onClose={handleClose} />)
const opener = screen.getByRole('button', { name: 'open sheet' })
await user.click(opener)
await waitFor(() => expect(screen.getByRole('dialog', { name: 'Test sheet' })).toHaveFocus())
await user.keyboard('{Escape}')
expect(handleClose).toHaveBeenCalledTimes(1)
await waitFor(() => expect(opener).toHaveFocus())
})
})
+75 -54
View File
@@ -1,9 +1,15 @@
import { useEffect, useState, useRef } from 'react'
import { createPortal } from 'react-dom'
import { useCallback, useEffect, useRef, useState } from 'react'
import {
Content as DialogContent,
Overlay as DialogOverlay,
Portal as DialogPortal,
Root as DialogRoot,
Title as DialogTitle,
} from '@radix-ui/react-dialog'
import { cn } from '@/lib/utils'
import { MODAL_TRANSITION_MS } from '@/lib/utils'
import { useSwipeDismiss } from '@/hooks/useMobile'
import { useRestoreFocusOnClose } from '@/hooks/useRestoreFocusOnClose'
import { getFocusedElement, restoreOverlayFocus } from '@/lib/overlayFocus'
export interface BottomSheetProps {
isOpen: boolean
@@ -25,9 +31,23 @@ export function BottomSheet({
dismissible = true,
}: BottomSheetProps) {
const [shouldRender, setShouldRender] = useState(false)
const [openState, setOpenState] = useState(() => ({
open: isOpen,
returnFocus: isOpen ? getFocusedElement() : null,
}))
const panelRef = useRef<HTMLDivElement>(null)
const [panelElement, setPanelElement] = useState<HTMLDivElement | null>(null)
const setPanelRef = useCallback((node: HTMLDivElement | null) => {
panelRef.current = node
setPanelElement(node)
}, [])
useRestoreFocusOnClose(isOpen, panelRef)
if (openState.open !== isOpen) {
setOpenState({
open: isOpen,
returnFocus: isOpen ? getFocusedElement() : openState.returnFocus,
})
}
const { bind, swipeStyles } = useSwipeDismiss(onClose, {
enabled: isOpen && dismissible,
@@ -35,8 +55,8 @@ export function BottomSheet({
})
useEffect(() => {
return bind(panelRef.current)
}, [bind])
return bind(panelElement)
}, [bind, panelElement])
useEffect(() => {
if (isOpen) {
@@ -47,64 +67,65 @@ export function BottomSheet({
}
}, [isOpen])
useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape' && isOpen) {
onClose()
}
}
const handleOpenChange = (nextOpen: boolean) => {
if (!nextOpen) onClose()
}
if (isOpen) {
document.addEventListener('keydown', handleEscape)
document.body.style.overflow = 'hidden'
}
const handleOpenAutoFocus = (event: Event) => {
event.preventDefault()
panelRef.current?.focus({ preventScroll: true })
}
return () => {
document.removeEventListener('keydown', handleEscape)
document.body.style.overflow = 'unset'
const handleCloseAutoFocus = (event: Event) => {
if (restoreOverlayFocus(openState.returnFocus, event.currentTarget as Element | null)) {
event.preventDefault()
}
}, [isOpen, onClose])
}
if (!isOpen && !shouldRender) return null
const sheet = (
<div
className="fixed inset-0 z-[250]"
style={{
opacity: isOpen ? 1 : 0,
pointerEvents: isOpen ? 'auto' : 'none',
transition: `opacity ${MODAL_TRANSITION_MS}ms ease-out`,
}}
>
<div
className="absolute inset-0 bg-black/40 backdrop-blur-sm"
onClick={onClose}
aria-hidden="true"
/>
<div
ref={panelRef}
className={cn(
'fixed inset-x-0 bottom-0 flex flex-col bg-background rounded-t-2xl border-t border-border',
heightClass,
className,
)}
role="dialog"
aria-modal="true"
aria-label={ariaLabel}
data-state={isOpen ? 'open' : 'closed'}
style={swipeStyles}
>
<div className="w-full flex justify-center pt-3 pb-2">
<div className="w-9 h-1 rounded-full bg-muted-foreground/30" />
<DialogRoot open={isOpen} onOpenChange={handleOpenChange}>
<DialogPortal forceMount>
<div
className="fixed inset-0 z-[250]"
style={{
opacity: isOpen ? 1 : 0,
pointerEvents: isOpen ? 'auto' : 'none',
transition: `opacity ${MODAL_TRANSITION_MS}ms ease-out`,
}}
>
<DialogOverlay
forceMount
className="absolute inset-0 bg-black/40 backdrop-blur-sm"
/>
<DialogContent
ref={setPanelRef}
forceMount
aria-modal="true"
aria-describedby={undefined}
tabIndex={-1}
onOpenAutoFocus={handleOpenAutoFocus}
onCloseAutoFocus={handleCloseAutoFocus}
className={cn(
'fixed inset-x-0 bottom-0 flex flex-col bg-background rounded-t-2xl border-t border-border focus:outline-none',
heightClass,
className,
)}
style={swipeStyles}
>
<DialogTitle className="sr-only">{ariaLabel}</DialogTitle>
<div className="w-full flex justify-center pt-3 pb-2">
<div className="w-9 h-1 rounded-full bg-muted-foreground/30" />
</div>
{children}
</DialogContent>
</div>
{children}
</div>
</div>
</DialogPortal>
</DialogRoot>
)
if (typeof document === 'undefined') return sheet
return createPortal(sheet, document.body)
return sheet
}
export interface BottomSheetHeaderProps {
+10 -2
View File
@@ -4,10 +4,14 @@ import { cn } from "@/lib/utils"
interface ProgressProps extends React.HTMLAttributes<HTMLDivElement> {
value?: number
max?: number
/** Extra classes merged onto the filled indicator. */
indicatorClassName?: string
/** Optional test id applied to the filled indicator. */
indicatorTestId?: string
}
const Progress = React.forwardRef<HTMLDivElement, ProgressProps>(
({ className, value = 0, max = 100, ...props }, ref) => {
({ className, value = 0, max = 100, indicatorClassName, indicatorTestId, ...props }, ref) => {
const percentage = Math.min(100, Math.max(0, (value / max) * 100))
return (
@@ -24,7 +28,11 @@ const Progress = React.forwardRef<HTMLDivElement, ProgressProps>(
{...props}
>
<div
className="h-full bg-primary transition-all duration-300 ease-in-out"
data-testid={indicatorTestId}
className={cn(
"h-full bg-primary transition-all duration-300 ease-in-out",
indicatorClassName
)}
style={{ width: `${percentage}%` }}
/>
</div>