import { useState } from 'react'; import { useRecoilValue } from 'recoil'; import { useOpenActivityRightDrawer } from '@/activities/hooks/useOpenActivityRightDrawer'; import { useFindManyObjectRecords } from '@/object-record/hooks/useFindManyObjectRecords'; import { Person } from '@/people/types/Person'; import { IconNotes } from '@/ui/display/icon'; import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys'; import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope'; import { Avatar } from '@/users/components/Avatar'; import { getLogoUrlFromDomainName } from '~/utils'; import { useCommandMenu } from '../hooks/useCommandMenu'; import { commandMenuCommandsState } from '../states/commandMenuCommandsState'; import { isCommandMenuOpenedState } from '../states/isCommandMenuOpenedState'; import { Command, CommandType } from '../types/Command'; import { CommandGroup } from './CommandGroup'; import { CommandMenuItem } from './CommandMenuItem'; import { StyledDialog, StyledEmpty, StyledInput, StyledList, } from './CommandMenuStyles'; export const CommandMenu = () => { const { openCommandMenu, closeCommandMenu, toggleCommandMenu } = useCommandMenu(); const openActivityRightDrawer = useOpenActivityRightDrawer(); const isCommandMenuOpened = useRecoilValue(isCommandMenuOpenedState); const [search, setSearch] = useState(''); const commandMenuCommands = useRecoilValue(commandMenuCommandsState); useScopedHotkeys( 'ctrl+k,meta+k', () => { setSearch(''); toggleCommandMenu(); }, AppHotkeyScope.CommandMenu, [openCommandMenu, setSearch], ); const { objects: people } = useFindManyObjectRecords({ skip: !isCommandMenuOpened, objectNamePlural: 'people', filter: { or: [ { name: { firstName: { like: `%${search}%` } } }, { name: { firstName: { like: `%${search}%` } } }, ], }, limit: 3, }); const { objects: companies } = useFindManyObjectRecords({ skip: !isCommandMenuOpened, objectNamePlural: 'companies', filter: { name: { like: `%${search}%` }, }, limit: 3, }); const { objects: activities } = useFindManyObjectRecords({ skip: !isCommandMenuOpened, objectNamePlural: 'activities', filter: { or: [ { title: { like: `%${search}%` } }, { body: { like: `%${search}%` } }, ], }, limit: 3, }); const checkInShortcuts = (cmd: Command, search: string) => { return (cmd.firstHotKey + (cmd.secondHotKey ?? '')) .toLowerCase() .includes(search.toLowerCase()); }; const checkInLabels = (cmd: Command, search: string) => { if (cmd.label) { return cmd.label.toLowerCase().includes(search.toLowerCase()); } return false; }; const matchingNavigateCommand = commandMenuCommands.filter( (cmd) => (search.length > 0 ? checkInShortcuts(cmd, search) || checkInLabels(cmd, search) : true) && cmd.type === CommandType.Navigate, ); const matchingCreateCommand = commandMenuCommands.filter( (cmd) => (search.length > 0 ? checkInShortcuts(cmd, search) || checkInLabels(cmd, search) : true) && cmd.type === CommandType.Create, ); return ( { if (!opened) { closeCommandMenu(); } }} shouldFilter={false} label="Global Command Menu" > No results found. {matchingCreateCommand.map((cmd) => ( ))} {matchingNavigateCommand.map((cmd) => ( ))} {people.map((person) => ( ( )} /> ))} {companies.map((company) => ( ( )} /> ))} {activities.map((activity) => ( openActivityRightDrawer(activity.id)} /> ))} ); };