import { useState } from 'react'; import { useRecoilValue } from 'recoil'; import { useOpenActivityRightDrawer } from '@/activities/hooks/useOpenActivityRightDrawer'; import { IconNotes } from '@/ui/icon'; import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys'; import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope'; import { Avatar } from '@/users/components/Avatar'; import { QueryMode, useSearchActivityQuery, useSearchCompanyQuery, useSearchPeopleQuery, } from '~/generated/graphql'; 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 } = useCommandMenu(); const openActivityRightDrawer = useOpenActivityRightDrawer(); const isCommandMenuOpened = useRecoilValue(isCommandMenuOpenedState); const [search, setSearch] = useState(''); const commandMenuCommands = useRecoilValue(commandMenuCommandsState); useScopedHotkeys( 'ctrl+k,meta+k', () => { openCommandMenu(); }, AppHotkeyScope.CommandMenu, [openCommandMenu], ); const { data: peopleData } = useSearchPeopleQuery({ variables: { where: { OR: [ { firstName: { contains: search, mode: QueryMode.Insensitive } }, { lastName: { contains: search, mode: QueryMode.Insensitive } }, ], }, limit: 3, }, }); const people = peopleData?.searchResults ?? []; const { data: companyData } = useSearchCompanyQuery({ variables: { where: { OR: [{ name: { contains: search, mode: QueryMode.Insensitive } }], }, limit: 3, }, }); const companies = companyData?.searchResults ?? []; const { data: activityData } = useSearchActivityQuery({ variables: { where: { OR: [ { title: { contains: search, mode: QueryMode.Insensitive } }, { body: { contains: search, mode: QueryMode.Insensitive } }, ], }, limit: 3, }, }); const activities = activityData?.searchResults ?? []; const checkInShortcuts = (cmd: Command, search: string) => { if (cmd.shortcuts && cmd.shortcuts.length > 0) { return cmd.shortcuts .join('') .toLowerCase() .includes(search.toLowerCase()); } return false; }; 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.length === 1 && matchingCreateCommand.map((cmd) => ( ))} {matchingNavigateCommand.length === 1 && matchingNavigateCommand.map((cmd) => ( ))} {people.map((person) => ( ( )} /> ))} {companies.map((company) => ( ( )} /> ))} {activities.map((activity) => ( openActivityRightDrawer(activity.id)} /> ))} ); };