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.

🐛 fix(command-menu): promote inline type filters from setSearch (#14986)

YuTengjingd2d3888f43b9
diff --git a/src/features/CommandMenu/CommandMenuContext.test.tsx b/src/features/CommandMenu/CommandMenuContext.test.tsx
new file mode 100644
index 00000000000..050950bb420
--- /dev/null
+++ b/src/features/CommandMenu/CommandMenuContext.test.tsx
@@ -0,0 +1,41 @@
+import { act, renderHook } from '@testing-library/react';
+import type { ReactNode } from 'react';
+import { describe, expect, it, vi } from 'vitest';
+
+import { CommandMenuProvider, useCommandMenuContext } from './CommandMenuContext';
+
+const createWrapper = () => {
+ const onClose = vi.fn();
+
+ const Wrapper = ({ children }: { children: ReactNode }) => (
+ <CommandMenuProvider pathname="/" onClose={onClose}>
+ {children}
+ </CommandMenuProvider>
+ );
+
+ return Wrapper;
+};
+
+describe('CommandMenuProvider', () => {
+ it('should promote inline type filters into command menu state', () => {
+ const { result } = renderHook(() => useCommandMenuContext(), { wrapper: createWrapper() });
+
+ act(() => {
+ result.current.setSearch('type:message search content');
+ });
+
+ expect(result.current.search).toBe('search content');
+ expect(result.current.typeFilter).toBe('message');
+ });
+
+ it('should keep invalid type filters as literal search text', () => {
+ const { result } = renderHook(() => useCommandMenuContext(), { wrapper: createWrapper() });
+
+ act(() => {
+ result.current.setSearch('type:unknown search content');
+ });
+
+ expect(result.current.search).toBe('type:unknown search content');
+ expect(result.current.typeFilter).toBeUndefined();
+ });
+});
diff --git a/src/features/CommandMenu/CommandMenuContext.tsx b/src/features/CommandMenu/CommandMenuContext.tsx
index 053aa919f2a..3044fac2440 100644
--- a/src/features/CommandMenu/CommandMenuContext.tsx
+++ b/src/features/CommandMenu/CommandMenuContext.tsx
@@ -1,11 +1,12 @@
'use client';
-import { type Dispatch, type ReactNode, type SetStateAction } from 'react';
+import type { Dispatch, ReactNode, SetStateAction } from 'react';
import { createContext, use, useCallback, useMemo, useState } from 'react';
-import { type MenuContext, type PageType, type SelectedAgent } from './types';
+import type { MenuContext, PageType, SelectedAgent } from './types';
import { detectContext } from './utils/context';
-import { type ValidSearchType } from './utils/queryParser';
+import type { ValidSearchType } from './utils/queryParser';
+import { parseSearchQuery } from './utils/queryParser';
interface CommandMenuContextValue {
activeAgentId: string | undefined;
@@ -53,7 +54,17 @@ export const CommandMenuProvider = ({ children, onClose, pathname }: CommandMenu
const viewMode: MenuViewMode = search.trim().length > 0 ? 'search' : 'default';
// Memoize setters to maintain stable references
- const setSearch = useCallback((value: string) => setSearchState(value), []);
+ const setSearch = useCallback((value: string) => {
+ const parsedQuery = parseSearchQuery(value);
+
+ if (parsedQuery.typeFilter) {
+ setTypeFilterState(parsedQuery.typeFilter);
+ setSearchState(parsedQuery.cleanQuery);
+ return;
+ }
+
+ setSearchState(value);
+ }, []);
const setTypeFilter = useCallback(
(value: ValidSearchType | undefined) => setTypeFilterState(value),
[],