Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): end any in-progress chat-drawer drag before starting a new one

Diego Zacariasb1f90d783343
diff --git a/desktop-app/ui/src/hooks/__tests__/useChatDrawerResize.reentry.test.tsx b/desktop-app/ui/src/hooks/__tests__/useChatDrawerResize.reentry.test.tsx
new file mode 100644
index 000000000..c698ac48e
--- /dev/null
+++ b/desktop-app/ui/src/hooks/__tests__/useChatDrawerResize.reentry.test.tsx
@@ -0,0 +1,65 @@
+// @vitest-environment jsdom
+import { createRef } from 'react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { act, renderHook } from '@testing-library/react'
+import { CHAT_DRAWER_DEFAULT_WIDTH, useChatDrawerResize } from '../useChatDrawerResize'
+
+// Re-entrant drag leak: a second mousedown during an active drag used to
+// overwrite `endDragRef.current` without ending the first drag. The first
+// drag's teardown then hit its own `endDragRef.current !== endDrag` guard and
+// could never remove its `onMove` — leaving it pinned to `window` forever. From
+// then on a bare `mousemove` (no button, isResizing false) resized the drawer.
+// A right-click on the 8px strip while the left button is held triggers the
+// double entry. This pins the fix: a stray mousemove after the interaction must
+// NOT resize the drawer.
+
+function mousedown(result: ReturnType<typeof renderHook>['result'], clientX: number) {
+ const event = {
+ button: 0,
+ clientX,
+ preventDefault: vi.fn(),
+ currentTarget: { closest: () => null },
+ } as unknown as React.MouseEvent<HTMLElement>
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ act(() => (result.current as any).onResizeHandleMouseDown(event))
+}
+
+describe('useChatDrawerResize re-entrant drag leak', () => {
+ beforeEach(() => {
+ // Run rAF synchronously so a leaked onMove's apply() (which schedules via
+ // requestAnimationFrame) is observable within the same act().
+ vi.stubGlobal('requestAnimationFrame', (cb: FrameRequestCallback) => {
+ cb(0)
+ return 1
+ })
+ vi.stubGlobal('cancelAnimationFrame', () => {})
+ })
+ afterEach(() => {
+ vi.unstubAllGlobals()
+ })
+
+ it('does not resize on a bare mousemove after a re-entrant mousedown + mouseup', () => {
+ const ref = createRef<HTMLElement>()
+ const { result } = renderHook(() => useChatDrawerResize(ref, true))
+ expect(result.current.width).toBe(CHAT_DRAWER_DEFAULT_WIDTH) // 420
+
+ // Drag #1 starts.
+ mousedown(result, 500)
+ expect(result.current.isResizing).toBe(true)
+
+ // Second mousedown while drag #1 is still live (the right-click-strip
+ // re-entry). Must end drag #1, never orphan its onMove.
+ mousedown(result, 500)
+
+ // Release the button.
+ act(() => window.dispatchEvent(new MouseEvent('mouseup')))
+ expect(result.current.isResizing).toBe(false)
+
+ const settledWidth = result.current.width
+
+ // A stray mousemove with no drag in progress must NOT resize the drawer.
+ act(() => window.dispatchEvent(new MouseEvent('mousemove', { clientX: 50 })))
+ expect(result.current.width).toBe(settledWidth)
+ expect(result.current.isResizing).toBe(false)
+ })
+})
diff --git a/desktop-app/ui/src/hooks/useChatDrawerResize.ts b/desktop-app/ui/src/hooks/useChatDrawerResize.ts
index 22a21c1c1..fcedac8ef 100644
--- a/desktop-app/ui/src/hooks/useChatDrawerResize.ts
+++ b/desktop-app/ui/src/hooks/useChatDrawerResize.ts
@@ -122,7 +122,15 @@ export function useChatDrawerResize(
const onResizeHandleMouseDown = React.useCallback(
(event: React.MouseEvent<HTMLElement>) => {
+ // Only the left button starts a drag; ignore middle/right so a right-click
+ // over the 8px strip while the left button is held can't open a second,
+ // re-entrant drag.
+ if (event.button !== 0) return
event.preventDefault()
+ // End any drag still in progress before installing a new one, so the
+ // previous drag's window listeners/rAF are always torn down first — a
+ // re-entrant mousedown must never orphan an earlier drag's `onMove`.
+ endDragRef.current?.()
// The drawer's right edge is fixed while docked; measure it once from the
// live element (robust to any transformed ancestor) so the width tracks
// `rightEdge - cursorX` for the whole drag.
@@ -143,14 +151,20 @@ export function useChatDrawerResize(
pointerX = moveEvent.clientX
if (!frame) frame = window.requestAnimationFrame(apply)
}
- // Idempotent teardown. Also runs on `blur` (mouse released outside the
- // window never fires `mouseup`) and from the cleanup effect below.
+ // Teardown. Each closure removes its OWN listeners/rAF unconditionally,
+ // BEFORE the guard, so a superseded drag can always detach itself and can
+ // never leave this closure's `onMove` pinned to `window` (the leak this
+ // ordering fixes). The guard then protects ONLY the shared state
+ // (body style, isResizing, endDragRef) so a superseded drag's late
+ // teardown doesn't clobber the drag that replaced it. Also runs on `blur`
+ // (mouse released outside the window never fires `mouseup`) and from the
+ // cleanup effect below.
const endDrag = () => {
- if (endDragRef.current !== endDrag) return
if (frame) window.cancelAnimationFrame(frame)
window.removeEventListener('mousemove', onMove)
window.removeEventListener('mouseup', endDrag)
window.removeEventListener('blur', endDrag)
+ if (endDragRef.current !== endDrag) return
document.body.style.userSelect = ''
document.body.style.cursor = ''
endDragRef.current = null