Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(web): close user context panel on outside click

whqaffc03c028c6
diff --git a/jiuwenswarm/channels/web/frontend/package.json b/jiuwenswarm/channels/web/frontend/package.json
index 68d09a0c71..705d92bed6 100644
--- a/jiuwenswarm/channels/web/frontend/package.json
+++ b/jiuwenswarm/channels/web/frontend/package.json
@@ -25,6 +25,7 @@
"test:runtime-scope": "tsc src/services/runtimeScope.ts --target ES2020 --module ES2020 --moduleResolution Bundler --rootDir src --outDir node_modules/.cache/runtime-scope --skipLibCheck --noEmitOnError && node --test tests/runtimeScope.test.mjs",
"test:user-web-entry": "esbuild src/EnterpriseEntry.tsx --bundle --packages=external --platform=node --format=esm --outfile=node_modules/.cache/user-web-entry/EnterpriseEntry.mjs --alias:virtual:login-auth-simulate-provider=./src/auth/simulate/available.ts --define:import.meta.env.VITE_JIUWENSWARM_EDITION='\"personal\"' --define:import.meta.env.VITE_LOGIN_AUTH_SIMULATE='\"false\"' && esbuild src/auth/config.ts src/auth/simulate/SimulatedAuthProvider.ts --bundle --packages=external --platform=node --format=esm --outdir=node_modules/.cache/user-web-entry/auth && node --test tests/userWebEntry.test.mjs",
"test:editable-combobox": "tsc src/components/SessionSidebar/editableComboboxModel.ts --target ES2020 --module ES2020 --moduleResolution Bundler --rootDir src --outDir node_modules/.cache/editable-combobox --skipLibCheck --noEmitOnError && node --test tests/editableCombobox.test.mjs",
+ "test:click-outside": "tsc src/components/SessionSidebar/clickOutside.ts --target ES2020 --module ES2020 --moduleResolution Bundler --rootDir src --outDir node_modules/.cache/click-outside --skipLibCheck --noEmitOnError && node --test tests/clickOutside.test.mjs",
"test:a2a-ingress-panel": "tsc src/components/A2AIngressPanel/a2aIngressPanelState.ts --target ES2020 --module ES2020 --moduleResolution Bundler --rootDir src --outDir node_modules/.cache/a2a-ingress-panel-state --skipLibCheck --noEmitOnError && node --test tests/a2aIngressPanelState.test.mjs",
"test:a2a-outbound-panel": "tsc src/components/A2AIngressPanel/a2aOutboundPanelState.ts --target ES2020 --module ES2020 --moduleResolution Bundler --rootDir src --outDir node_modules/.cache/a2a-outbound-panel-state --skipLibCheck --noEmitOnError && node --test tests/a2aOutboundPanelState.test.mjs",
"test:chat-store-streaming": "esbuild src/stores/chatStore.ts --bundle --platform=node --format=esm --outfile=node_modules/.cache/chat-store-streaming/chatStore.mjs --define:import.meta.env.DEV=false && node --test tests/chatStoreStreaming.test.mjs",
diff --git a/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/clickOutside.ts b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/clickOutside.ts
new file mode 100644
index 0000000000..1d9ed2e0fd
--- /dev/null
+++ b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/clickOutside.ts
@@ -0,0 +1,13 @@
+interface ContainsNode {
+ contains(node: Node | null): boolean;
+}
+
+/**
+ * Return true only when every click boundary is mounted and the target is
+ * outside all of them. Waiting for every boundary avoids closing a popover
+ * while React is still attaching its trigger or panel ref.
+ */
+export function isClickOutside(target: Node | null, boundaries: readonly (ContainsNode | null)[]): boolean {
+ if (!target || boundaries.some(boundary => boundary === null)) return false;
+ return boundaries.every(boundary => !boundary?.contains(target));
+}
diff --git a/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx
index d0ccb04bc8..352fb7895c 100644
--- a/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx
+++ b/jiuwenswarm/channels/web/frontend/src/components/SessionSidebar/index.tsx
@@ -22,6 +22,7 @@ import AgentDesignIcon from '../../assets/智能体.svg?react';
import MoreDesignIcon from '../../assets/更多.svg?react';
import { webRequest } from '../../services/webClient';
import { useEnterpriseContext } from '../../services/enterpriseContext';
+import { isClickOutside } from './clickOutside';
import { EditableCombobox } from './EditableCombobox';
type MainNavKey =
@@ -168,6 +169,8 @@ export function SessionSidebar({
const settingsRef = useRef<HTMLButtonElement>(null);
const enterprise = useEnterpriseContext();
const [contextOpen, setContextOpen] = useState(false);
+ const contextButtonRef = useRef<HTMLButtonElement>(null);
+ const contextPanelRef = useRef<HTMLDivElement>(null);
const handleNewSession = useCallback(() => {
onNavigate('chat');
@@ -210,6 +213,19 @@ export function SessionSidebar({
return () => onMorePanelOpenChange?.(false);
}, [isMoreActive, onMorePanelOpenChange]);
+ useEffect(() => {
+ if (!contextOpen) return;
+
+ const handleClickOutside = (event: MouseEvent) => {
+ if (isClickOutside(event.target as Node | null, [contextPanelRef.current, contextButtonRef.current])) {
+ setContextOpen(false);
+ }
+ };
+
+ document.addEventListener('mousedown', handleClickOutside);
+ return () => document.removeEventListener('mousedown', handleClickOutside);
+ }, [contextOpen]);
+
return (
<aside className="sidebar sidebar--icon-rail">
<div className="icon-rail-logo">
@@ -273,6 +289,7 @@ export function SessionSidebar({
{enterprise && (
<button
+ ref={contextButtonRef}
type="button"
className={`icon-rail-nav-item${contextOpen ? ' icon-rail-nav-item--active' : ''}`}
onClick={() => setContextOpen(open => !open)}
@@ -284,7 +301,7 @@ export function SessionSidebar({
</button>
)}
{enterprise && contextOpen && (
- <div className="enterprise-context-popover">
+ <div ref={contextPanelRef} className="enterprise-context-popover">
<div className="enterprise-context-popover__user">
{enterprise.user.display_name || enterprise.user.user_id}
<small>{enterprise.user.user_id}</small>
diff --git a/jiuwenswarm/channels/web/frontend/tests/clickOutside.test.mjs b/jiuwenswarm/channels/web/frontend/tests/clickOutside.test.mjs
new file mode 100644
index 0000000000..39bdcfe4cb
--- /dev/null
+++ b/jiuwenswarm/channels/web/frontend/tests/clickOutside.test.mjs
@@ -0,0 +1,25 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+import { isClickOutside } from '../node_modules/.cache/click-outside/components/SessionSidebar/clickOutside.js';
+
+const target = {};
+const containingBoundary = { contains: candidate => candidate === target };
+const outsideBoundary = { contains: () => false };
+
+test('an interaction inside the popover does not close it', () => {
+ assert.equal(isClickOutside(target, [containingBoundary, outsideBoundary]), false);
+});
+
+test('an interaction on the trigger does not close the popover before its toggle handler runs', () => {
+ assert.equal(isClickOutside(target, [outsideBoundary, containingBoundary]), false);
+});
+
+test('an interaction outside both the popover and trigger closes it', () => {
+ assert.equal(isClickOutside(target, [outsideBoundary, outsideBoundary]), true);
+});
+
+test('missing targets or boundaries do not close a mounting popover', () => {
+ assert.equal(isClickOutside(null, [outsideBoundary, outsideBoundary]), false);
+ assert.equal(isClickOutside(target, [null, outsideBoundary]), false);
+});