Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(approval): user-controlled approval island collapse and in-card Enter handling (#19655)

Arvin Xuf34ebfd52405
diff --git a/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/ApprovalActions.tsx b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/ApprovalActions.tsx
index 2263e79a973..5ded04a44c0 100644
--- a/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/ApprovalActions.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/ApprovalActions.tsx
@@ -10,6 +10,7 @@ import { useTranslation } from 'react-i18next';
import { useConversationResourceAccess } from '../../../../../hooks/useConversationResourceAccess';
import { useConversationStore } from '../../../../../store';
import { type ApprovalMode } from './index';
+import { isSubmitShortcutBlockedByTarget } from './submitShortcutGuard';
interface ApprovalActionsProps {
apiName: string;
@@ -225,7 +226,9 @@ const ApprovalActions = memo<ApprovalActionsProps>(
}, [choice]);
// Page-level keyboard: 1/2/↑/↓ to switch, Enter to submit. Skip while
- // typing anywhere on the page so we never hijack the main chat composer.
+ // typing anywhere on the page so we never hijack the main chat composer,
+ // and skip when another interactive control has focus β€” Enter must
+ // activate that control, never silently submit an approval.
// The reject input has its own onKeyDown for Enter / ↑.
//
// Kept fresh in a ref so the shared-arbiter registration below stays
@@ -235,11 +238,7 @@ const ApprovalActions = memo<ApprovalActionsProps>(
useEffect(() => {
onKeyDownRef.current = (e: KeyboardEvent) => {
if (e.defaultPrevented) return;
- const target = e.target as HTMLElement | null;
- if (target) {
- const tag = target.tagName;
- if (tag === 'INPUT' || tag === 'TEXTAREA' || target.isContentEditable) return;
- }
+ if (isSubmitShortcutBlockedByTarget(e.target as HTMLElement | null)) return;
if (e.metaKey || e.ctrlKey || e.altKey) return;
// Digit keys select the matching numbered row directly.
if (/^[1-9]$/.test(e.key)) {
diff --git a/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/submitShortcutGuard.test.ts b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/submitShortcutGuard.test.ts
new file mode 100644
index 00000000000..725bce7db58
--- /dev/null
+++ b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/submitShortcutGuard.test.ts
@@ -0,0 +1,47 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { isSubmitShortcutBlockedByTarget } from './submitShortcutGuard';
+
+const el = (tag: string, attrs: Record<string, string> = {}): HTMLElement => {
+ const node = document.createElement(tag);
+ for (const [k, v] of Object.entries(attrs)) node.setAttribute(k, v);
+ document.body.append(node);
+ return node;
+};
+
+describe('isSubmitShortcutBlockedByTarget', () => {
+ afterEach(() => {
+ document.body.innerHTML = '';
+ });
+
+ it('does NOT block on a null target or a plain non-interactive element', () => {
+ expect(isSubmitShortcutBlockedByTarget(null)).toBe(false);
+ expect(isSubmitShortcutBlockedByTarget(el('div'))).toBe(false);
+ expect(isSubmitShortcutBlockedByTarget(el('span'))).toBe(false);
+ });
+
+ it('blocks on typing surfaces so Enter never hijacks text entry', () => {
+ expect(isSubmitShortcutBlockedByTarget(el('input'))).toBe(true);
+ expect(isSubmitShortcutBlockedByTarget(el('textarea'))).toBe(true);
+ expect(isSubmitShortcutBlockedByTarget(el('div', { contenteditable: 'true' }))).toBe(true);
+ });
+
+ it('blocks on native interactive controls so Enter activates them, not the approval', () => {
+ expect(isSubmitShortcutBlockedByTarget(el('button'))).toBe(true);
+ expect(isSubmitShortcutBlockedByTarget(el('a'))).toBe(true);
+ expect(isSubmitShortcutBlockedByTarget(el('select'))).toBe(true);
+ });
+
+ it('blocks when the target is nested inside an ARIA button/menuitem/option', () => {
+ const menuItem = el('div', { role: 'menuitem' });
+ const inner = document.createElement('span');
+ menuItem.append(inner);
+ expect(isSubmitShortcutBlockedByTarget(inner)).toBe(true);
+
+ const option = el('div', { role: 'option' });
+ expect(isSubmitShortcutBlockedByTarget(option)).toBe(true);
+ });
+});
diff --git a/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/submitShortcutGuard.ts b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/submitShortcutGuard.ts
new file mode 100644
index 00000000000..daaa4de91f1
--- /dev/null
+++ b/src/features/Conversation/Messages/AssistantGroup/Tool/Detail/Intervention/submitShortcutGuard.ts
@@ -0,0 +1,20 @@
+/**
+ * Whether a keydown landing on `target` must NOT trigger the window-level
+ * approval submit shortcut (Enter / 1 / 2 / arrows).
+ *
+ * Two families of elements own their keys and must be left alone:
+ * - typing surfaces (`input` / `textarea` / contenteditable) β€” hijacking Enter
+ * there would swallow the user's text or fire the main chat composer;
+ * - interactive controls (`button` / `a` / `select`, or an ARIA
+ * `button` / `menuitem` / `option`) β€” Enter must activate that control, never
+ * silently submit the pending approval behind it.
+ */
+export const isSubmitShortcutBlockedByTarget = (target: HTMLElement | null): boolean => {
+ if (!target) return false;
+
+ const tag = target.tagName;
+ if (tag === 'INPUT' || tag === 'TEXTAREA' || target.isContentEditable) return true;
+ if (tag === 'BUTTON' || tag === 'A' || tag === 'SELECT') return true;
+
+ return Boolean(target.closest?.('[role="button"], [role="menuitem"], [role="option"]'));
+};
diff --git a/src/features/GlobalApprovalNotification/ApprovalCard.tsx b/src/features/GlobalApprovalNotification/ApprovalCard.tsx
index 62ab24ba6e3..a9a3bb7d560 100644
--- a/src/features/GlobalApprovalNotification/ApprovalCard.tsx
+++ b/src/features/GlobalApprovalNotification/ApprovalCard.tsx
@@ -3,7 +3,7 @@
import { AGENT_CHAT_TOPIC_URL, GROUP_CHAT_TOPIC_URL, GROUP_CHAT_URL } from '@lobechat/const';
import { agentDisplayName, type UIChatMessage } from '@lobechat/types';
import { ActionIcon, Avatar } from '@lobehub/ui/base-ui';
-import { ArrowUpRight } from 'lucide-react';
+import { ArrowUpRight, ChevronUp } from 'lucide-react';
import { memo, useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
@@ -27,6 +27,11 @@ import { type GlobalApprovalGroup } from './useGlobalPendingApprovals';
interface ApprovalCardProps {
group: GlobalApprovalGroup;
+ /**
+ * Collapse the island to its pill. Lives in the card header so the "put this
+ * away and keep working" escape hatch is visible on the surface itself.
+ */
+ onCollapse?: () => void;
}
/**
@@ -36,7 +41,7 @@ interface ApprovalCardProps {
* intervention UIs) operates on the right conversation without the user having
* to switch into it.
*/
-const ApprovalCard = memo<ApprovalCardProps>(({ group }) => {
+const ApprovalCard = memo<ApprovalCardProps>(({ group, onCollapse }) => {
const { context, interventions } = group;
const { t } = useTranslation('chat');
const navigate = useWorkspaceAwareNavigate();
@@ -165,6 +170,14 @@ const ApprovalCard = memo<ApprovalCardProps>(({ group }) => {
onClick={handleGoToConversation}
/>
)}
+ {onCollapse && (
+ <ActionIcon
+ icon={ChevronUp}
+ size="small"
+ title={t('globalApproval.collapse')}
+ onClick={onCollapse}
+ />
+ )}
</div>
<div className={styles.requestContext}>
diff --git a/src/features/GlobalApprovalNotification/index.tsx b/src/features/GlobalApprovalNotification/index.tsx
index 908a66226e6..e8762a13725 100644
--- a/src/features/GlobalApprovalNotification/index.tsx
+++ b/src/features/GlobalApprovalNotification/index.tsx
@@ -1,17 +1,16 @@
'use client';
import { TITLE_BAR_HEIGHT } from '@lobechat/desktop-bridge';
-import { ActionIcon } from '@lobehub/ui/base-ui';
-import { ChevronUp } from 'lucide-react';
import { AnimatePresence } from 'motion/react';
import * as m from 'motion/react-m';
-import { memo, useEffect, useRef, useState } from 'react';
+import { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { isDesktop } from '@/const/version';
import ApprovalCard from './ApprovalCard';
import { styles } from './styles';
+import { useApprovalIslandCollapse } from './useApprovalIslandCollapse';
import { useGlobalPendingApprovals } from './useGlobalPendingApprovals';
const SPRING = { damping: 30, stiffness: 320, type: 'spring' } as const;
@@ -30,14 +29,7 @@ const TOP_OFFSET = isDesktop ? TITLE_BAR_HEIGHT + 8 : 16;
const GlobalApprovalNotification = memo(() => {
const { t } = useTranslation('chat');
const groups = useGlobalPendingApprovals();
- const [collapsed, setCollapsed] = useState(false);
-
- // Auto-expand whenever a fresh batch of approvals arrives after being idle.
- const prevCount = useRef(0);
- useEffect(() => {
- if (groups.length > prevCount.current) setCollapsed(false);
- prevCount.current = groups.length;
- }, [groups.length]);
+ const [collapsed, setCollapsed] = useApprovalIslandCollapse(groups.length);
const hasApprovals = groups.length > 0;
// Only ONE card is actionable at a time: the reused `ApprovalActions`
@@ -75,14 +67,6 @@ const GlobalApprovalNotification = memo(() => {
key="stack"
transition={SPRING}
>
- <m.div layout style={{ alignSelf: 'flex-end', pointerEvents: 'auto' }}>
- <ActionIcon
- icon={ChevronUp}
- size="small"
- title={t('globalApproval.collapse')}
- onClick={() => setCollapsed(true)}
- />
- </m.div>
<AnimatePresence mode="popLayout">
{top && (
<m.div
@@ -94,7 +78,7 @@ const GlobalApprovalNotification = memo(() => {
style={{ pointerEvents: 'auto', width: '100%' }}
transition={SPRING}
>
- <ApprovalCard group={top} />
+ <ApprovalCard group={top} onCollapse={() => setCollapsed(true)} />
</m.div>
)}
</AnimatePresence>
diff --git a/src/features/GlobalApprovalNotification/useApprovalIslandCollapse.test.ts b/src/features/GlobalApprovalNotification/useApprovalIslandCollapse.test.ts
new file mode 100644
index 00000000000..1cf5d6db940
--- /dev/null
+++ b/src/features/GlobalApprovalNotification/useApprovalIslandCollapse.test.ts
@@ -0,0 +1,56 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { act, renderHook } from '@testing-library/react';
+import { describe, expect, it } from 'vitest';
+
+import { useApprovalIslandCollapse } from './useApprovalIslandCollapse';
+
+describe('useApprovalIslandCollapse', () => {
+ it('starts expanded (not collapsed)', () => {
+ const { result } = renderHook(({ count }) => useApprovalIslandCollapse(count), {
+ initialProps: { count: 0 },
+ });
+ expect(result.current[0]).toBe(false);
+ });
+
+ it('keeps the island expanded on the idle β†’ active transition', () => {
+ const { result, rerender } = renderHook(({ count }) => useApprovalIslandCollapse(count), {
+ initialProps: { count: 0 },
+ });
+ // First approval arrives β€” auto-expand keeps it open.
+ rerender({ count: 1 });
+ expect(result.current[0]).toBe(false);
+ });
+
+ it('does NOT re-expand when more approvals arrive after the user collapsed', () => {
+ const { result, rerender } = renderHook(({ count }) => useApprovalIslandCollapse(count), {
+ initialProps: { count: 1 },
+ });
+
+ // User collapses the island.
+ act(() => result.current[1](true));
+ expect(result.current[0]).toBe(true);
+
+ // A busy run produces a second pending approval (1 β†’ 2). The naive
+ // `count > prevCount` logic would flip collapsed back to false here; the
+ // idle-transition guard must leave the user's collapse intact.
+ rerender({ count: 2 });
+ expect(result.current[0]).toBe(true);
+ });
+
+ it('auto-expands again only after the island goes fully idle and reactivates', () => {
+ const { result, rerender } = renderHook(({ count }) => useApprovalIslandCollapse(count), {
+ initialProps: { count: 1 },
+ });
+
+ act(() => result.current[1](true));
+ expect(result.current[0]).toBe(true);
+
+ // Drain to idle, then a fresh batch arrives β†’ a legitimate idle β†’ active
+ // transition re-opens the island.
+ rerender({ count: 0 });
+ rerender({ count: 1 });
+ expect(result.current[0]).toBe(false);
+ });
+});
diff --git a/src/features/GlobalApprovalNotification/useApprovalIslandCollapse.ts b/src/features/GlobalApprovalNotification/useApprovalIslandCollapse.ts
new file mode 100644
index 00000000000..897e9ea1e12
--- /dev/null
+++ b/src/features/GlobalApprovalNotification/useApprovalIslandCollapse.ts
@@ -0,0 +1,25 @@
+import { type Dispatch, type SetStateAction, useEffect, useRef, useState } from 'react';
+
+/**
+ * Owns the approval island's collapsed/expanded state and the ONE piece of
+ * auto-expand logic that is easy to get wrong.
+ *
+ * The island auto-expands only on the idle β†’ active transition (`prevCount`
+ * was 0). Once the user collapses it, further approvals from a still-busy run
+ * must NOT pop it back open β€” re-expanding fights the user's explicit choice;
+ * the pill's live count is the only signal they need. A naive
+ * `count > prevCount` check regresses exactly this case.
+ */
+export const useApprovalIslandCollapse = (
+ count: number,
+): [boolean, Dispatch<SetStateAction<boolean>>] => {
+ const [collapsed, setCollapsed] = useState(false);
+ const prevCount = useRef(0);
+
+ useEffect(() => {
+ if (prevCount.current === 0 && count > 0) setCollapsed(false);
+ prevCount.current = count;
+ }, [count]);
+
+ return [collapsed, setCollapsed];
+};