Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(console): a click on Cancel or Create works while participant suggestions are open

raullopez-sandboxd79ce8f302b6
diff --git a/console/apps/switch-console-desktop/src/renderer/lib/components/participant-picker.tsx b/console/apps/switch-console-desktop/src/renderer/lib/components/participant-picker.tsx
index 33bab8c52..b6952e656 100644
--- a/console/apps/switch-console-desktop/src/renderer/lib/components/participant-picker.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/lib/components/participant-picker.tsx
@@ -1,5 +1,5 @@
import { Loader2, X } from 'lucide-react';
-import { useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
import { useAgentAccent } from '@renderer/lib/components/agent-accent';
import { AgentAvatar } from '@renderer/lib/components/agent-avatar';
import { cn } from '@renderer/utils/utils';
@@ -110,6 +110,32 @@ export function ParticipantPicker({
const [query, setQuery] = useState('');
const [open, setOpen] = useState(false);
const [active, setActive] = useState(0);
+ const field = useRef<HTMLInputElement>(null);
+ const pointerDown = useRef(false);
+ useEffect(() => {
+ const down = () => (pointerDown.current = true);
+ const up = () => (pointerDown.current = false);
+ window.addEventListener('pointerdown', down, true);
+ window.addEventListener('pointerup', up, true);
+ return () => {
+ window.removeEventListener('pointerdown', down, true);
+ window.removeEventListener('pointerup', up, true);
+ };
+ }, []);
+ // Closing the list moves what follows it up; closed on a press, it would move a button out
+ // from under the pointer before the release, and that click would be lost. So a press
+ // elsewhere closes it after the release, once the click has landed.
+ const close = () => {
+ if (!pointerDown.current) return setOpen(false);
+ window.addEventListener(
+ 'pointerup',
+ () =>
+ setTimeout(() => {
+ if (document.activeElement !== field.current) setOpen(false);
+ }),
+ { once: true }
+ );
+ };
const results = searchParticipants(candidates, picked, query);
const current = Math.min(active, Math.max(results.length - 1, 0));
@@ -140,6 +166,7 @@ export function ParticipantPicker({
/>
))}
<input
+ ref={field}
id={inputId}
autoFocus={autoFocus}
role="combobox"
@@ -158,7 +185,7 @@ export function ParticipantPicker({
setOpen(true);
}}
onFocus={() => setOpen(true)}
- onBlur={() => setOpen(false)}
+ onBlur={close}
onKeyDown={(event) => {
// keyCode 229: a key the IME is handling, which some IMEs send with isComposing false.
if (event.nativeEvent.isComposing || event.keyCode === 229) return;
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/participant-picker-click-through.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/participant-picker-click-through.test.tsx
new file mode 100644
index 000000000..4c311044d
--- /dev/null
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/participant-picker-click-through.test.tsx
@@ -0,0 +1,78 @@
+/**
+ * With the participant suggestions open, a real click on a button below them
+ * does what it says the first time: the list closing must not move the button
+ * out from under the pointer before the click lands.
+ */
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, expect, it, vi } from 'vitest';
+import { userEvent } from 'vitest/browser';
+import { ParticipantPicker } from '@renderer/lib/components/participant-picker';
+import type { Participant } from '@renderer/lib/components/participant-search';
+
+let container: HTMLDivElement | null = null;
+let root: Root | null = null;
+
+afterEach(async () => {
+ if (root) await act(async () => root!.unmount());
+ container?.remove();
+ container = null;
+ root = null;
+});
+
+const candidates: Participant[] = Array.from({ length: 6 }, (_, i) => ({
+ kind: 'agent',
+ id: `a${i}`,
+ name: `helper-${i}`,
+ iconUrl: null,
+ detail: 'Agent · Claude Code',
+}));
+
+it.each(['Cancel', 'Create'])(
+ 'runs %s on the first click while the suggestions are open, once',
+ async (label) => {
+ const onCancel = vi.fn();
+ const onCreate = vi.fn();
+ container = document.createElement('div');
+ container.style.width = '400px';
+ document.body.appendChild(container);
+ root = createRoot(container);
+ await act(async () =>
+ root!.render(
+ <QueryClientProvider client={new QueryClient()}>
+ <ParticipantPicker
+ candidates={candidates}
+ picked={[]}
+ onChange={() => {}}
+ serverId={null}
+ loading={false}
+ inputId="participants"
+ autoFocus={false}
+ busy={false}
+ // As in the New room dialog: the list pushes the buttons down.
+ overlay={false}
+ />
+ <button type="button" onClick={onCancel}>
+ Cancel
+ </button>
+ <button type="button" onClick={onCreate}>
+ Create
+ </button>
+ </QueryClientProvider>
+ )
+ );
+ const input = container.querySelector('input')!;
+ await userEvent.click(input);
+ await userEvent.type(input, 'helper');
+ expect(container.querySelector('[role="listbox"]')).not.toBeNull();
+
+ const button = [...container.querySelectorAll('button')].find((b) => b.textContent === label)!;
+ await userEvent.click(button);
+ await act(async () => await new Promise((resolve) => setTimeout(resolve, 20)));
+
+ expect((label === 'Cancel' ? onCancel : onCreate).mock.calls).toHaveLength(1);
+ expect((label === 'Cancel' ? onCreate : onCancel).mock.calls).toHaveLength(0);
+ expect(container.querySelector('[role="listbox"]')).toBeNull();
+ }
+);