import React, { type FC } from 'react'; import type { SearchResultCommonType, SearchResult, SearchFlatCommonData } from '../SearchBar'; import { Command } from 'cmdk'; import { Modal, ModalOverlay, ModalContent, ModalCloseButton, Button, InputGroup, Input, Divider, Hide, Spinner, chakra, Stack, Box, Heading, Highlight, type ComponentWithAs } from '@chakra-ui/react'; import { NotAllowedIcon, LinkIcon, InfoIcon, ViewIcon, EditIcon } from '@chakra-ui/icons'; import { useSiteSearch, useIntl, FormattedMessage, Link } from 'dumi'; import useFlatSearchData from '../../hooks/useFlatSearchData'; import './index.less'; const { Empty, Item, List, Loading } = Command; const ChakraCommand = chakra(Command); const ChakraItem = chakra(Item); const ChakraList = chakra(List); const ChakraEmpty = chakra(Empty); const ChakraLoading = chakra(Loading); const IconMap: Record> = { title: InfoIcon, page: LinkIcon, content: EditIcon, demo: ViewIcon }; type Highlights = SearchResult[0]['hints'][0]['highlightTexts']; function generateHighlightQuery(highlight: Highlights): string[] { return highlight?.filter?.((v) => v.highlighted)?.map?.((v) => v.text) ?? []; } function generateHighlightText(highlight: Highlights): string { return highlight.map?.((v) => v.text)?.join?.('') ?? ''; } const Cmdk: FC = ({ isOpen, onClose }) => { const intl = useIntl(); const { keywords, setKeywords, result, loading } = useSiteSearch(); const [data] = useFlatSearchData(result); return ( setKeywords(ev.target.value)} placeholder={intl.formatMessage({ id: 'header.search.placeholder' })} /> {loading && !!keywords?.length && ( )} {!loading && data?.map?.(({ type, value }, idx) => { const { title, link, highlightTitleTexts, highlightTexts, type: valueType } = value as SearchFlatCommonData; const Icon = IconMap[valueType]; return ( {type === 'title' ? ( {title} ) : ( )} ); })} ); }; export default Cmdk;