π€― 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.
CommandMenu Feature Documentation
Overview
The CommandMenu is a powerful command palette feature inspired by tools like Raycast and VS Code's Command Palette. It provides a unified, keyboard-driven interface for quick navigation, searching, and actions across the entire application.
Key Library: Built on top of cmdk (Command K) by Paco Coursey.
Architecture
Core Design Principles
- Context-Aware: Automatically detects the current page/context and shows relevant commands
- Modal Navigation: Uses a page stack system for hierarchical navigation (e.g., Main β Theme)
- Multi-Mode: Supports different modes (default, search, AI chat)
- Global State: Integrated with Zustand store for open/close state management
- Portal-Based: Renders as a portal to
document.bodyfor proper z-index layering
File Structure
CommandMenu/
βββ index.tsx # Main component & orchestration
βββ useCommandMenu.ts # Core hook with business logic
βββ types.ts # TypeScript type definitions
βββ styles.ts # createStaticStyles styles
β
βββ components/
β βββ CommandInput.tsx # Search input with context/back navigation
β βββ CommandFooter.tsx # Keyboard shortcuts help
β
βββ MainMenu.tsx # Default menu (navigation, settings, etc.)
βββ ContextCommands.tsx # Context-specific commands
βββ SearchResults.tsx # Search result display
βββ ChatList.tsx # AI chat mode message list
βββ ThemeMenu.tsx # Theme selection submenu
β
βββ utils/
βββ context.ts # Context detection logic
βββ contextCommands.ts # Context command definitions
Core Concepts
1. Context Detection
The CommandMenu automatically detects what page you're on and shows relevant commands.
File: utils/context.ts
// Supported contexts
type ContextType = 'agent' | 'painting' | 'settings' | 'resource' | 'page';
// Context detection based on pathname
const CONTEXT_CONFIGS: ContextConfig[] = [
{ matcher: /^\/agent\/[^/]+$/, name: 'Agent', type: 'agent' },
{ matcher: /^\/image$/, name: 'Painting', type: 'painting' },
{
matcher: /^\/settings(?:\/([^/]+))?/,
name: 'Settings',
type: 'settings',
captureSubPath: true, // Captures sub-route like "profile"
},
// ...
];
Example: When on /settings/profile, context is:
{
type: 'settings',
name: 'Settings',
subPath: 'profile'
}
2. Page Stack Navigation
Uses an array-based stack for hierarchical navigation:
// State
const [pages, setPages] = useState<string[]>([]);
const page = pages.at(-1); // Current page
// Navigate to submenu
navigateToPage('theme'); // pages = ['theme']
// Navigate deeper
navigateToPage('ai-chat'); // pages = ['theme', 'ai-chat']
// Go back
handleBack(); // pages = ['theme']
Keyboard Shortcuts:
Escape: Go back one level or close if at rootBackspace: Go back when search is empty
3. AI Mode
Special mode for asking AI questions about your work.
Activation: Press Tab when you have search text (and not already in AI mode)
Flow:
- User types query in search
- Presses
Tab - Query becomes a user message in chat
- Page stack pushes
'ai-chat' - Search input switches to AI mode placeholder
State Management:
const [chatMessages, setChatMessages] = useState<ChatMessage[]>([]);
const isAiMode = page === 'ai-chat';
const handleAskAI = () => {
if (search.trim()) {
const userMessage = {
content: search,
id: Date.now().toString(),
role: 'user',
};
setChatMessages((prev) => [...prev, userMessage]);
}
setPages([...pages, 'ai-chat']);
};
4. Search Functionality
Backend: Uses tRPC Lambda client to query the search API
File: useCommandMenu.ts:38-51
// Debounced search to reduce API calls
const debouncedSearch = useDebounce(search, { wait: 300 });
// SWR-based search
const { data: searchResults, isLoading: isSearching } = useSWR<SearchResult[]>(
hasSearch && !isAiMode ? ['search', searchQuery] : null,
async () => lambdaClient.search.query.query({ query: searchQuery }),
{ revalidateOnFocus: false, revalidateOnReconnect: false },
);
Search Types:
message: Chat messages (NEW - highest priority in agent context)agent: AI agents/assistantstopic: Conversation topics/threadsfile: Uploaded files/knowledge base
Display: Results are grouped by type in SearchResults.tsx with priority order: Messages β Topics β Agents β Files
Context-Aware Priority (NEW):
- Agent Context (
/agent/*): Messages (10), Topics (5), Agents (3), Files (3)- Messages from current agent get 0.5-0.7 relevance (highest priority)
- Other messages get 1-3 relevance (normal)
- General Context: Balanced 5/5/5/5 distribution
5. Context Commands
Commands that appear based on current context (e.g., Settings submenu navigation).
File: utils/contextCommands.ts
// Define commands for each context type
export const CONTEXT_COMMANDS: Record<ContextType, ContextCommand[]> = {
settings: [
{
label: 'Profile',
path: '/settings/profile',
subPath: 'profile',
icon: UserCircle,
keywords: ['profile', 'user', 'account'],
},
// ...more settings pages
],
agent: [], // No context commands for agent pages yet
// ...
};
Smart Filtering: Automatically hides the current page from the list
// If on /settings/profile, won't show "Profile" in context commands
return commands.filter((cmd) => cmd.subPath !== currentSubPath);
Data Flow
Opening the Command Menu
User presses Cmd+K
β
GlobalStore.updateSystemStatus({ showCommandMenu: true })
β
useCommandMenu hook detects open=true
β
useEffect resets state (pages=[], search='', chatMessages=[])
β
CommandMenu renders via portal to document.body
β
detectContext(pathname) determines current context
β
Renders appropriate menu based on state:
- No page + no search β MainMenu + ContextCommands
- No page + has search β MainMenu + SearchResults
- page='theme' β ThemeMenu
- page='ai-chat' β ChatList
Search Flow
User types in input
β
setSearch(value)
β
useDebounce delays by 300ms
β
SWR key changes to ['search', query]
β
lambdaClient.search.query.query({ query })
β
SearchResults receives results
β
Groups by type (agents/topics/files)
β
Renders with type-specific icons and navigation
Navigation Flow
User selects "Settings" command
β
handleNavigate('/settings') called
β
react-router navigate('/settings')
β
closeCommandMenu() β setOpen({ showCommandMenu: false })
β
CommandMenu unmounts
Key Features
Keyboard Shortcuts
| Key | Action |
|---|---|
Cmd/Ctrl + K |
Open/Close command menu (global) |
Escape |
Go back or close |
Backspace |
Go back (when search empty) |
Tab |
Enter AI mode (when search has text) |
β/β |
Navigate items |
Enter |
Select item |
Smart Filtering
The cmdk library provides built-in fuzzy filtering:
- Searches across command labels, keywords, and descriptions
- Only active when
shouldFilter={!isAiMode}(disabled in AI mode) - Custom value strings for better search relevance (see
SearchResults.tsx:71-78)
Body Scroll Lock
useEffect(() => {
if (open) {
const originalStyle = window.getComputedStyle(document.body).overflow;
document.body.style.overflow = 'hidden';
return () => {
document.body.style.overflow = originalStyle;
};
}
}, [open]);
Loading States
Search results show skeleton loaders while fetching:
if (isLoading) {
return (
<Command.Group heading={t('cmdk.search.searching')}>
{[1, 2, 3].map((i) => (
<div className={styles.skeletonItem} key={i}>
<div className={styles.skeleton} />
{/* ... */}
</div>
))}
</Command.Group>
);
}
Styling
Uses @lobehub/ui createStaticStyles + cssVar for theme-aware styles:
Key Patterns:
- Uses
token.*for colors/spacing to support dark mode - CSS animations for smooth transitions
- Responsive sizing with viewport units
- Flexbox for layouts
Example:
commandRoot: css`
width: min(640px, 90vw);
max-height: min(500px, 70vh);
background: ${token.colorBgElevated};
box-shadow: ${token.boxShadowSecondary};
animation: slide-down 0.12s ease-out;
@keyframes slide-down {
from {
transform: translateY(-20px) scale(0.96);
opacity: 0;
}
to {
transform: translateY(0) scale(1);
opacity: 1;
}
}
`,
Integration Points
Global State (Zustand)
// src/store/global
interface SystemStatus {
showCommandMenu: boolean;
// ...
}
// Usage in hook
const [open, setOpen] = useGlobalStore((s) => [s.status.showCommandMenu, s.updateSystemStatus]);
Router Integration
import { useLocation, useNavigate } from 'react-router-dom';
const navigate = useNavigate();
const location = useLocation();
const pathname = location.pathname;
const handleNavigate = (path: string) => {
navigate(path);
closeCommandMenu();
};
tRPC Search API
import { lambdaClient } from '@/libs/trpc/client';
// Call server-side search function
lambdaClient.search.query.query({ query: searchQuery });
i18n (Internationalization)
import { useTranslation } from 'react-i18next';
const { t } = useTranslation('common');
// Usage
<Command.Empty>{t('cmdk.noResults')}</Command.Empty>
Translation Keys (in src/locales/default/common.ts):
cmdk.searchPlaceholdercmdk.aiModePlaceholdercmdk.noResultscmdk.newAgentcmdk.settings- etc.
How to Extend
1. Add a New Context
Step 1: Add context type to types.ts:
export type ContextType =
'agent' | 'painting' | 'settings' | 'resource' | 'page' | 'your-new-context'; // Add this
Step 2: Add detection rule to utils/context.ts:
const CONTEXT_CONFIGS: ContextConfig[] = [
// ...existing configs
{
matcher: /^\/your-route/,
name: 'Your Context Name',
type: 'your-new-context',
captureSubPath: true, // optional
},
];
Step 3: Add commands to utils/contextCommands.ts:
export const CONTEXT_COMMANDS: Record<ContextType, ContextCommand[]> = {
// ...
'your-new-context': [
{
label: 'Sub Command',
path: '/your-route/sub',
subPath: 'sub',
icon: YourIcon,
keywords: ['keyword1', 'keyword2'],
},
],
};
2. Add a New Menu Page
Step 1: Create component (e.g., YourMenu.tsx):
import { Command } from 'cmdk';
import { memo } from 'react';
interface YourMenuProps {
onSomething: (value: string) => void;
styles: any;
}
const YourMenu = memo<YourMenuProps>(({ onSomething, styles }) => {
return (
<>
<Command.Item onSelect={() => onSomething('value1')} value="option-1">
<YourIcon className={styles.icon} />
<div className={styles.itemContent}>
<div className={styles.itemLabel}>Option 1</div>
</div>
</Command.Item>
{/* More items... */}
</>
);
});
export default YourMenu;
Step 2: Add handler to useCommandMenu.ts:
const handleYourAction = (value: string) => {
// Do something
closeCommandMenu();
};
return {
// ...
handleYourAction,
};
Step 3: Render in index.tsx:
{page === 'your-page' && (
<YourMenu
onSomething={handleYourAction}
styles={styles}
/>
)}
Step 4: Add navigation to it:
// In MainMenu or elsewhere
<Command.Item onSelect={() => navigateToPage('your-page')}>
Your Page
</Command.Item>
3. Add a New Main Menu Item
In MainMenu.tsx:
<Command.Item
onSelect={() => onNavigate('/your-route')}
value="your-command keywords here"
>
<YourIcon className={styles.icon} />
<div className={styles.itemContent}>
<div className={styles.itemLabel}>{t('cmdk.yourLabel')}</div>
</div>
</Command.Item>
Remember to:
- Add translation keys to
src/locales/default/common.ts - Use existing icons from
lucide-react - Follow the existing pattern for consistency
4. Modify Search Behavior
Search is handled server-side via tRPC. To modify:
Backend: Update apps/server/src/routers/lambda/search.ts (or similar)
Frontend: Search results display in SearchResults.tsx
- Modify
getIcon()for custom icons - Modify
handleNavigate()for custom routing - Modify
getItemValue()for search ranking
Common Patterns
Command Item Structure
<Command.Item
onSelect={() => handleAction()}
value="searchable keywords here"
>
<Icon className={styles.icon} />
<div className={styles.itemContent}>
<div className={styles.itemLabel}>Primary Label</div>
</div>
</Command.Item>
Grouped Commands
<Command.Group heading={t('cmdk.groupName')}>
<Command.Item>...</Command.Item>
<Command.Item>...</Command.Item>
</Command.Group>
Conditional Rendering
{!pathname?.startsWith('/settings') && (
<Command.Item onSelect={() => onNavigate('/settings')}>
Settings
</Command.Item>
)}
External Links
<Command.Item
onSelect={() => onExternalLink('https://example.com')}
>
External Link
</Command.Item>
Testing Considerations
When testing CommandMenu features:
Context Detection: Test pathname matching
expect(detectContext('/agent/123')).toEqual({ type: 'agent', name: 'Agent' });Navigation Stack: Test page state management
navigateToPage('theme'); expect(pages).toEqual(['theme']); handleBack(); expect(pages).toEqual([]);Search Debouncing: Mock timers or use
vi.advanceTimersByTime(300)Portal Rendering: Use
screen.getByRole('dialog')or similarKeyboard Events: Simulate with
fireEvent.keyDown(element, { key: 'Escape' })
Performance Notes
- Debounced Search: 300ms delay prevents excessive API calls
- SWR Caching: Search results cached, no refetch on focus/reconnect
- Memoization: All submenu components use
memo()to prevent re-renders - Portal: Renders outside main React tree for better performance
- Conditional Rendering: Only renders when
open && mounted
Future Improvements
Potential areas for enhancement:
- Recent Commands: Track and show recently used commands
- Custom Shortcuts: Allow users to assign custom keyboard shortcuts
- Command History: Navigate through previous searches
- AI Integration: Actually connect AI mode to backend chat service
- Plugin System: Allow extensions to register custom commands
- Themes: More theme options beyond light/dark/auto
- Search Scoping: Filter search by type (agents only, files only, etc.)
- Workspace-specific: Different commands per workspace/project
Quick Reference
Open Menu: Cmd/Ctrl + K
Main Files:
index.tsx- Main orchestrationuseCommandMenu.ts- Business logicMainMenu.tsx- Default commandsSearchResults.tsx- Search displayutils/context.ts- Context detectionutils/contextCommands.ts- Context commands
Key Dependencies:
cmdk- Command palette primitivesreact-router-dom- Navigationzustand- Global stateswr- Data fetchinglucide-react- Icons
Related Documentation: