'use client'; import IconSearch from '@geist-ui/icons/search'; import React from 'react'; import { useHotkeys } from 'react-hotkeys-hook'; import { tString, useLanguage } from '@/intl/client'; import { tcls } from '@/lib/tailwind'; import { SearchAskAnswer } from './SearchAskAnswer'; import { SearchResults, SearchResultsRef } from './SearchResults'; import { SearchState, useSearch } from './useSearch'; interface SearchModalProps { spaceId: string; withAsk: boolean; } export function SearchModal(props: SearchModalProps) { const [state, setSearchState] = useSearch(); useHotkeys( 'ctrl+k, command+k', (e) => { e.preventDefault(); //might be inadvisable as it interferes with expected browser behavior. setSearchState({ ask: false, query: '' }); }, [], ); if (state === null) { return null; } const onChangeQuery = (newQuery: SearchState) => { setSearchState(newQuery); }; const onClose = () => { setSearchState(null); }; return (
); } function SearchModalBody( props: SearchModalProps & { state: SearchState; onChangeQuery: (newQuery: SearchState) => void; onClose: () => void; }, ) { const { spaceId, withAsk, state, onChangeQuery, onClose } = props; const language = useLanguage(); const resultsRef = React.useRef(null); const inputRef = React.useRef(null); React.useEffect(() => { inputRef.current?.focus(); }, []); const onKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Escape' || (event.key === 'Backspace' && state.query === '')) { onClose(); } else if (event.key === 'ArrowUp') { event.preventDefault(); resultsRef.current?.moveUp(); } else if (event.key === 'ArrowDown') { event.preventDefault(); resultsRef.current?.moveDown(); } else if (event.key === 'Enter') { event.preventDefault(); resultsRef.current?.select(); } }; const onChange = (event: React.ChangeEvent) => { onChangeQuery({ ask: false, // When typing, we go back to the default search mode query: event.target.value, }); }; return (
{ event.stopPropagation(); }} >
{!state.ask || !withAsk ? ( { onChangeQuery({ ask: true, query: state.query, }); }} /> ) : null} {state.query && state.ask && withAsk ? ( ) : null}
); }