diff --git a/frontend/src/components/navigation/DesktopSidebar.tsx b/frontend/src/components/navigation/DesktopSidebar.tsx index 72996d29..e000ef23 100644 --- a/frontend/src/components/navigation/DesktopSidebar.tsx +++ b/frontend/src/components/navigation/DesktopSidebar.tsx @@ -2,6 +2,7 @@ import { useState } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import { useDesktop } from '@/hooks/useDesktop' import { useSidebarCollapsed } from '@/hooks/useSidebarCollapsed' +import { useSidebarWidth } from '@/hooks/useSidebarWidth' import { useAuth } from '@/hooks/useAuth' import { useUrlParams } from '@/hooks/useUrlParams' import { useOpenNavItem } from '@/hooks/useOpenNavItem' @@ -23,6 +24,7 @@ export function DesktopSidebar() { const { updateParams } = useUrlParams() const openNavItem = useOpenNavItem() const [collapsed, toggle] = useSidebarCollapsed() + const [sidebarWidth, setSidebarWidth] = useSidebarWidth() const [repoSwitcherOpen, setRepoSwitcherOpen] = useState(false) const { isAuthenticated, isLoading, logout } = useAuth() @@ -62,7 +64,13 @@ export function DesktopSidebar() { return ( <> - + {primary.length > 0 && ( {primary.map((item: NavPrimaryCta) => ( diff --git a/frontend/src/components/ui/sidebar.test.tsx b/frontend/src/components/ui/sidebar.test.tsx index 59dada0b..e0d20ece 100644 --- a/frontend/src/components/ui/sidebar.test.tsx +++ b/frontend/src/components/ui/sidebar.test.tsx @@ -2,8 +2,13 @@ import { describe, it, expect, vi } from 'vitest' import { render, screen, fireEvent } from '@testing-library/react' import { Sidebar, SidebarSection, SidebarItem, SidebarCollapseToggle } from './sidebar' import { TooltipProvider } from './tooltip' +import { SIDEBAR_MAX_WIDTH, SIDEBAR_MIN_WIDTH } from '@/hooks/useSidebarWidth' import { Folder } from 'lucide-react' +Element.prototype.setPointerCapture ??= () => {} +Element.prototype.hasPointerCapture ??= () => false +Element.prototype.releasePointerCapture ??= () => {} + describe('Sidebar', () => { it('renders children', () => { render( @@ -34,6 +39,153 @@ describe('Sidebar', () => { expect(container.firstChild).toHaveClass('w-60') }) + + it('renders an inline width instead of the width class when width is provided', () => { + const { container } = render( + +
Content
+
+ ) + + const aside = container.firstChild as HTMLElement + expect(aside.style.width).toBe('320px') + expect(aside).not.toHaveClass('w-60') + }) + + it('keeps the collapsed width class when collapsed even if width is provided', () => { + const { container } = render( + +
Content
+
+ ) + + const aside = container.firstChild as HTMLElement + expect(aside).toHaveClass('w-14') + expect(aside.style.width).toBe('') + }) +}) + +describe('Sidebar resize handle', () => { + it('is not rendered when collapsed', () => { + render( + +
Content
+
+ ) + + expect(screen.queryByRole('separator')).not.toBeInTheDocument() + }) + + it('is not rendered without onWidthChange', () => { + render( + +
Content
+
+ ) + + expect(screen.queryByRole('separator')).not.toBeInTheDocument() + }) + + it('exposes the current width via aria attributes', () => { + render( + +
Content
+
+ ) + + const handle = screen.getByRole('separator', { name: 'Resize sidebar' }) + expect(handle).toHaveAttribute('aria-valuenow', '320') + expect(handle).toHaveAttribute('aria-valuemin', String(SIDEBAR_MIN_WIDTH)) + expect(handle).toHaveAttribute('aria-valuemax', String(SIDEBAR_MAX_WIDTH)) + expect(handle).toHaveAttribute('aria-orientation', 'vertical') + }) + + it('reports the dragged width from the first pointer move', () => { + const handleWidthChange = vi.fn() + const { container } = render( + +
Content
+
+ ) + + const handle = screen.getByRole('separator', { name: 'Resize sidebar' }) + fireEvent.pointerDown(handle, { button: 0, pointerId: 1, clientX: 300 }) + fireEvent.pointerMove(handle, { pointerId: 1, clientX: 340 }) + + expect(handleWidthChange).toHaveBeenCalledWith(340) + expect(container.firstChild).toHaveClass('transition-none') + }) + + it('ignores pointer moves from another pointer id', () => { + const handleWidthChange = vi.fn() + render( + +
Content
+
+ ) + + const handle = screen.getByRole('separator', { name: 'Resize sidebar' }) + fireEvent.pointerDown(handle, { button: 0, pointerId: 1, clientX: 300 }) + fireEvent.pointerMove(handle, { pointerId: 2, clientX: 400 }) + + expect(handleWidthChange).not.toHaveBeenCalled() + }) + + it('stops reporting after pointer up and restores the transition classes', () => { + const handleWidthChange = vi.fn() + const { container } = render( + +
Content
+
+ ) + + const handle = screen.getByRole('separator', { name: 'Resize sidebar' }) + fireEvent.pointerDown(handle, { button: 0, pointerId: 1, clientX: 300 }) + fireEvent.pointerUp(handle, { pointerId: 1, clientX: 320 }) + fireEvent.pointerMove(handle, { pointerId: 1, clientX: 360 }) + + expect(handleWidthChange).not.toHaveBeenCalled() + expect(container.firstChild).toHaveClass('transition-[width]') + }) + + it('adjusts the width from the keyboard', () => { + const handleWidthChange = vi.fn() + render( + +
Content
+
+ ) + + const handle = screen.getByRole('separator', { name: 'Resize sidebar' }) + + fireEvent.keyDown(handle, { key: 'ArrowRight' }) + expect(handleWidthChange).toHaveBeenLastCalledWith(316) + + fireEvent.keyDown(handle, { key: 'ArrowLeft' }) + expect(handleWidthChange).toHaveBeenLastCalledWith(284) + + fireEvent.keyDown(handle, { key: 'ArrowRight', shiftKey: true }) + expect(handleWidthChange).toHaveBeenLastCalledWith(364) + + fireEvent.keyDown(handle, { key: 'Home' }) + expect(handleWidthChange).toHaveBeenLastCalledWith(SIDEBAR_MIN_WIDTH) + + fireEvent.keyDown(handle, { key: 'End' }) + expect(handleWidthChange).toHaveBeenLastCalledWith(SIDEBAR_MAX_WIDTH) + }) + + it('ignores unhandled keys', () => { + const handleWidthChange = vi.fn() + render( + +
Content
+
+ ) + + fireEvent.keyDown(screen.getByRole('separator', { name: 'Resize sidebar' }), { key: 'a' }) + + expect(handleWidthChange).not.toHaveBeenCalled() + }) }) describe('SidebarSection', () => { diff --git a/frontend/src/components/ui/sidebar.tsx b/frontend/src/components/ui/sidebar.tsx index 2a5f5479..22f0b318 100644 --- a/frontend/src/components/ui/sidebar.tsx +++ b/frontend/src/components/ui/sidebar.tsx @@ -1,6 +1,11 @@ +import { useCallback, useRef, useState } from 'react' import type { LucideIcon } from 'lucide-react' import { ChevronLeft, ChevronRight } from 'lucide-react' import { cn } from '@/lib/utils' +import { + SIDEBAR_MAX_WIDTH, + SIDEBAR_MIN_WIDTH, +} from '@/hooks/useSidebarWidth' import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' export interface SidebarProps { @@ -11,6 +16,14 @@ export interface SidebarProps { className?: string children: React.ReactNode ariaLabel?: string + width?: number + onWidthChange?: (width: number) => void +} + +interface SidebarDragState { + pointerId: number + startX: number + startWidth: number } export function Sidebar({ @@ -21,17 +34,112 @@ export function Sidebar({ className, children, ariaLabel = 'Sidebar', + width, + onWidthChange, }: SidebarProps) { + const dragRef = useRef(null) + const [isDragging, setIsDragging] = useState(false) + + const handlePointerDown = useCallback( + (event: React.PointerEvent) => { + if (event.button !== 0 || width === undefined || !onWidthChange) { + return + } + event.preventDefault() + event.currentTarget.setPointerCapture?.(event.pointerId) + dragRef.current = { + pointerId: event.pointerId, + startX: event.clientX, + startWidth: width, + } + setIsDragging(true) + }, + [onWidthChange, width] + ) + + const handlePointerMove = useCallback( + (event: React.PointerEvent) => { + const drag = dragRef.current + if (!drag || drag.pointerId !== event.pointerId || !onWidthChange) { + return + } + onWidthChange(drag.startWidth + event.clientX - drag.startX) + }, + [onWidthChange] + ) + + const handlePointerEnd = useCallback((event: React.PointerEvent) => { + const drag = dragRef.current + if (!drag || drag.pointerId !== event.pointerId) { + return + } + if (event.currentTarget.hasPointerCapture?.(event.pointerId)) { + event.currentTarget.releasePointerCapture?.(event.pointerId) + } + dragRef.current = null + setIsDragging(false) + }, []) + + const handleKeyDown = useCallback( + (event: React.KeyboardEvent) => { + if (!onWidthChange || width === undefined) { + return + } + const step = event.shiftKey ? 64 : 16 + let nextWidth: number + switch (event.key) { + case 'ArrowLeft': + nextWidth = width - step + break + case 'ArrowRight': + nextWidth = width + step + break + case 'Home': + nextWidth = SIDEBAR_MIN_WIDTH + break + case 'End': + nextWidth = SIDEBAR_MAX_WIDTH + break + default: + return + } + event.preventDefault() + onWidthChange(nextWidth) + }, + [onWidthChange, width] + ) + + const showResizeHandle = !collapsed && width !== undefined && onWidthChange !== undefined + return ( ) diff --git a/frontend/src/hooks/usePersistentBoolean.ts b/frontend/src/hooks/usePersistentBoolean.ts index 82d3f3d4..33748af8 100644 --- a/frontend/src/hooks/usePersistentBoolean.ts +++ b/frontend/src/hooks/usePersistentBoolean.ts @@ -1,23 +1,8 @@ import { useState, useCallback } from 'react' - -function readStorage(key: string): string | null { - try { - return localStorage.getItem(key) - } catch { - return null - } -} - -function writeStorage(key: string, value: unknown): void { - try { - localStorage.setItem(key, JSON.stringify(value)) - } catch { - return - } -} +import { readStoredValue, writeStoredValue } from '@/lib/persistentStorage' function readStoredBoolean(key: string, fallback: boolean): boolean { - const stored = readStorage(key) + const stored = readStoredValue(key) if (stored === null) { return fallback } @@ -39,7 +24,7 @@ export function usePersistentBoolean(key: string, fallback: boolean): [boolean, const toggle = useCallback(() => { setValue((prev: boolean) => { const newValue = !prev - writeStorage(key, newValue) + writeStoredValue(key, newValue) return newValue }) }, [key]) diff --git a/frontend/src/hooks/useSidebarWidth.test.tsx b/frontend/src/hooks/useSidebarWidth.test.tsx new file mode 100644 index 00000000..c59a26fa --- /dev/null +++ b/frontend/src/hooks/useSidebarWidth.test.tsx @@ -0,0 +1,94 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' +import { renderHook, act } from '@testing-library/react' +import { + clampSidebarWidth, + useSidebarWidth, + SIDEBAR_DEFAULT_WIDTH, + SIDEBAR_MAX_WIDTH, + SIDEBAR_MIN_WIDTH, +} from './useSidebarWidth' + +const localStorageMock = { + getItem: vi.fn(), + setItem: vi.fn(), + removeItem: vi.fn(), + clear: vi.fn(), +} + +describe('useSidebarWidth', () => { + beforeEach(() => { + vi.clearAllMocks() + Object.defineProperty(global, 'localStorage', { + value: localStorageMock, + writable: true, + }) + }) + + afterEach(() => { + Object.defineProperty(global, 'localStorage', { + value: window.localStorage, + writable: true, + }) + vi.restoreAllMocks() + }) + + it('falls back to the default width when nothing is stored', () => { + localStorageMock.getItem.mockReturnValue(null) + + const { result } = renderHook(() => useSidebarWidth()) + + expect(localStorageMock.getItem).toHaveBeenCalledWith('oc:sidebar:width') + expect(result.current[0]).toBe(SIDEBAR_DEFAULT_WIDTH) + }) + + it('reads a stored width', () => { + localStorageMock.getItem.mockReturnValue('320') + + const { result } = renderHook(() => useSidebarWidth()) + + expect(result.current[0]).toBe(320) + }) + + it.each(['not json', '"320"', 'null', 'true', '{}'])( + 'ignores the malformed stored value %s', + (stored) => { + localStorageMock.getItem.mockReturnValue(stored) + + const { result } = renderHook(() => useSidebarWidth()) + + expect(result.current[0]).toBe(SIDEBAR_DEFAULT_WIDTH) + } + ) + + it('clamps and persists the setter value below the minimum', () => { + localStorageMock.getItem.mockReturnValue(null) + + const { result } = renderHook(() => useSidebarWidth()) + + act(() => { + result.current[1](100) + }) + + expect(result.current[0]).toBe(SIDEBAR_MIN_WIDTH) + expect(localStorageMock.setItem).toHaveBeenCalledWith('oc:sidebar:width', '200') + }) + + it('clamps and persists the setter value above the maximum', () => { + localStorageMock.getItem.mockReturnValue(null) + + const { result } = renderHook(() => useSidebarWidth()) + + act(() => { + result.current[1](900) + }) + + expect(result.current[0]).toBe(SIDEBAR_MAX_WIDTH) + expect(localStorageMock.setItem).toHaveBeenCalledWith('oc:sidebar:width', '480') + }) + + it('rounds and clamps in clampSidebarWidth', () => { + expect(clampSidebarWidth(250.6)).toBe(251) + expect(clampSidebarWidth(100.4)).toBe(SIDEBAR_MIN_WIDTH) + expect(clampSidebarWidth(900)).toBe(SIDEBAR_MAX_WIDTH) + }) +}) diff --git a/frontend/src/hooks/useSidebarWidth.ts b/frontend/src/hooks/useSidebarWidth.ts new file mode 100644 index 00000000..1d1bdbb2 --- /dev/null +++ b/frontend/src/hooks/useSidebarWidth.ts @@ -0,0 +1,43 @@ +import { useCallback, useState } from 'react' +import { readStoredValue, writeStoredValue } from '@/lib/persistentStorage' + +export const SIDEBAR_DEFAULT_WIDTH = 240 +export const SIDEBAR_MIN_WIDTH = 200 +export const SIDEBAR_MAX_WIDTH = 480 + +const STORAGE_KEY = 'oc:sidebar:width' + +export function clampSidebarWidth(width: number): number { + return Math.min(SIDEBAR_MAX_WIDTH, Math.max(SIDEBAR_MIN_WIDTH, Math.round(width))) +} + +function readStoredWidth(): number { + const stored = readStoredValue(STORAGE_KEY) + if (stored === null) { + return SIDEBAR_DEFAULT_WIDTH + } + try { + const parsed: unknown = JSON.parse(stored) + return typeof parsed === 'number' && Number.isFinite(parsed) + ? clampSidebarWidth(parsed) + : SIDEBAR_DEFAULT_WIDTH + } catch { + return SIDEBAR_DEFAULT_WIDTH + } +} + +/** + * Persists the desktop sidebar width under `oc:sidebar:width` and returns it with a clamped setter. + * Falls back to `SIDEBAR_DEFAULT_WIDTH` when the stored value is missing, malformed, or not finite. + */ +export function useSidebarWidth(): [number, (width: number) => void] { + const [width, setWidth] = useState(readStoredWidth) + + const updateWidth = useCallback((next: number) => { + const clamped = clampSidebarWidth(next) + writeStoredValue(STORAGE_KEY, clamped) + setWidth(clamped) + }, []) + + return [width, updateWidth] +} diff --git a/frontend/src/lib/persistentStorage.ts b/frontend/src/lib/persistentStorage.ts new file mode 100644 index 00000000..65c610f6 --- /dev/null +++ b/frontend/src/lib/persistentStorage.ts @@ -0,0 +1,15 @@ +export function readStoredValue(key: string): string | null { + try { + return localStorage.getItem(key) + } catch { + return null + } +} + +export function writeStoredValue(key: string, value: unknown): void { + try { + localStorage.setItem(key, JSON.stringify(value)) + } catch { + return + } +}