import clsx from 'clsx' import { useEffect } from 'react' import { useTranslation } from 'react-i18next' import { CommandModalProps } from '@dao-dao/types/components/CommandModal' import { SearchBar } from '../inputs/SearchBar' import { Modal } from '../modals/Modal' import { ContextPill } from './ContextPill' export const CommandModal = ({ visible, setVisible, filter, setFilter, contexts, closeCurrentContext, children, searchBarRef, }: CommandModalProps) => { const { t } = useTranslation() // Auto focus search bar on open and blur on close. useEffect(() => { if (visible) { searchBarRef.current?.focus() } else { searchBarRef.current?.blur() } }, [searchBarRef, visible]) return (
1 ? 'max-h-12 overflow-y-auto' : 'max-h-0' )} > {/* Don't show the root context. */} {contexts.slice(1).map((context, index) => ( ))}
setFilter(event.currentTarget.value)} onKeyDown={(event) => { // Handle in list. if (event.key === 'ArrowUp' || event.key === 'ArrowDown') { return event.preventDefault() } else if ( (event.key === 'Backspace' && !filter.length) || // If escape and not at root context, go back instead of close // modal. (event.key === 'Escape' && contexts.length > 1) ) { event.stopPropagation() return closeCurrentContext() } // If hits Escape and there are no contexts, this event will // propagate to the Modal, which will then close itself. }} placeholder={t('info.whatAreYouLookingForPrompt')} ref={searchBarRef} value={filter} /> } onClose={() => setVisible(false)} smallCloseButton visible={visible} > {children}
) }