Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(cowork): make the side panel resize actually flexible
Akarshan Biswas49e605353da4
diff --git a/web-app/src/containers/CoworkSidePanel.tsx b/web-app/src/containers/CoworkSidePanel.tsxindex 1b15613b0..d306efac2 100644--- a/web-app/src/containers/CoworkSidePanel.tsx+++ b/web-app/src/containers/CoworkSidePanel.tsx@@ -12,8 +12,25 @@ type CoworkSidePanelProps = {}const PANEL_MIN_W = 240-const PANEL_MAX_W = 640const PANEL_DEFAULT_W = 320+/** One width across the preview/diff/todo/tasks panels, kept across sessions. */+const PANEL_WIDTH_KEY = 'cowork:sidePanelWidth'++/** The panel may grow with the window but never crush the transcript. */+function panelMaxWidth(): number {+ return Math.max(PANEL_MIN_W, Math.round(window.innerWidth * 0.7))+}++function clampWidth(w: number): number {+ return Math.min(panelMaxWidth(), Math.max(PANEL_MIN_W, Math.round(w)))+}++function storedWidth(): number {+ const raw = Number(window.localStorage.getItem(PANEL_WIDTH_KEY))+ return Number.isFinite(raw) && raw >= PANEL_MIN_W+ ? clampWidth(raw)+ : PANEL_DEFAULT_W+}export function CoworkSidePanel({title,@@ -24,48 +41,72 @@ export function CoworkSidePanel({}: CoworkSidePanelProps): React.ReactElement {const { t } = useTranslation()const [expanded, setExpanded] = useState(false)- const [width, setWidth] = useState(PANEL_DEFAULT_W)- const dragging = useRef(false)- const startX = useRef(0)- const startW = useRef(0)+ const [width, setWidth] = useState(storedWidth)+ const [resizing, setResizing] = useState(false)+ const asideRef = useRef<HTMLElement>(null)+ const widthRef = useRef(width)- const onMouseDown = useCallback(- (e: React.MouseEvent) => {+ // Pointer capture keeps move events flowing to the handle even while the+ // pointer crosses the preview's iframe, which otherwise swallows them and+ // freezes the drag mid-resize.+ const onPointerDown = useCallback(+ (e: React.PointerEvent<HTMLDivElement>) => {e.preventDefault()- dragging.current = true- startX.current = e.clientX- startW.current = expanded ? PANEL_MAX_W : width- const onMove = (ev: MouseEvent) => {- if (!dragging.current) return- const delta = startX.current - ev.clientX // left edge -> drag left = wider- const next = Math.min(PANEL_MAX_W, Math.max(PANEL_MIN_W, startW.current + delta))+ const handle = e.currentTarget+ const pointerId = e.pointerId+ const startX = e.clientX+ // Measure the rendered width so a drag that starts while expanded picks+ // up from the size on screen instead of a stale state value.+ const startW =+ asideRef.current?.getBoundingClientRect().width ?? widthRef.current+ setExpanded(false)+ setResizing(true)+ handle.setPointerCapture(pointerId)+ const onMove = (ev: PointerEvent) => {+ const next = clampWidth(startW + (startX - ev.clientX)) // left edge -> drag left = wider+ widthRef.current = nextsetWidth(next)}const onUp = () => {- dragging.current = false- window.removeEventListener('mousemove', onMove)- window.removeEventListener('mouseup', onUp)+ handle.removeEventListener('pointermove', onMove)+ handle.removeEventListener('pointerup', onUp)+ handle.removeEventListener('pointercancel', onUp)+ if (handle.hasPointerCapture(pointerId)) {+ handle.releasePointerCapture(pointerId)+ }+ setResizing(false)+ window.localStorage.setItem(PANEL_WIDTH_KEY, String(widthRef.current))}- window.addEventListener('mousemove', onMove)- window.addEventListener('mouseup', onUp)+ handle.addEventListener('pointermove', onMove)+ handle.addEventListener('pointerup', onUp)+ handle.addEventListener('pointercancel', onUp)},- [expanded, width]+ [])+ const resetWidth = useCallback(() => {+ widthRef.current = PANEL_DEFAULT_W+ setWidth(PANEL_DEFAULT_W)+ setExpanded(false)+ window.localStorage.setItem(PANEL_WIDTH_KEY, String(PANEL_DEFAULT_W))+ }, [])+return (<aside- className={cn(- 'relative flex h-full shrink-0 flex-col border-l bg-main-view',- expanded ? 'w-[32rem] max-w-[60%]' : ''- )}- style={expanded ? undefined : { width: `${width}px` }}+ ref={asideRef}+ className="relative flex h-full max-w-[70%] shrink-0 flex-col border-l bg-main-view"+ // Expanded takes everything the max-width cap allows, so it can only+ // grow the panel; a dragged width is honored otherwise.+ style={{ width: expanded ? '70%' : `${width}px` }}>- {/* Resize handle – thin invisible strip on the left edge. */}+ {/* Resize handle: thin invisible strip on the left edge. Double-click+ resets to the default width. */}<divrole="separator"aria-orientation="vertical"- onMouseDown={onMouseDown}- className="absolute inset-y-0 left-0 z-10 w-1 cursor-col-resize bg-main-view-fg/0 transition-colors hover:bg-main-view-fg/20"+ onPointerDown={onPointerDown}+ onDoubleClick={resetWidth}+ className="absolute inset-y-0 left-0 z-10 w-1.5 cursor-col-resize touch-none bg-main-view-fg/0 transition-colors hover:bg-main-view-fg/20"/><div className="flex h-11 shrink-0 items-center gap-2 border-b px-3">{leading}@@ -89,7 +130,11 @@ export function CoworkSidePanel({<X size={18} /></button></div>- <div className="min-h-0 flex-1">{children}</div>+ {/* An iframe under the pointer would steal the drag on WebKit even with+ pointer capture, so the body goes inert while resizing. */}+ <div className={cn('min-h-0 flex-1', resizing && 'pointer-events-none select-none')}>+ {children}+ </div></aside>)}