|
| 1 | +import { useRef, useState } from "react" |
| 2 | +import { useTranslation } from "react-i18next" |
| 3 | +import { useNavigate } from "react-router" |
| 4 | +import { Modal } from "~/components/modal" |
| 5 | +import type { Version } from "~/utils/version-resolvers" |
| 6 | +import { fuzzySearch } from "../hooks/use-fuzzy-search" |
| 7 | +import { useKeyboardNavigation } from "../hooks/use-keyboard-navigation" |
| 8 | +import { useModalState } from "../hooks/use-modal-state" |
| 9 | +import { useSearchHistory } from "../hooks/use-search-history" |
| 10 | +import type { SearchDoc, SearchResult } from "../search-types" |
| 11 | +import { EmptyState } from "./empty-state" |
| 12 | +import { ResultsFooter } from "./results-footer" |
| 13 | +import { SearchHistory } from "./search-history" |
| 14 | +import { SearchInput } from "./search-input" |
| 15 | +import { SearchResultRow } from "./search-result" |
| 16 | +import { TriggerButton } from "./trigger-button" |
| 17 | + |
| 18 | +interface CommandPaletteProps { |
| 19 | + searchIndex: SearchDoc[] |
| 20 | + placeholder?: string |
| 21 | + version: Version |
| 22 | +} |
| 23 | + |
| 24 | +interface HistoryItem extends SearchDoc { |
| 25 | + type?: string |
| 26 | + slug?: string |
| 27 | + highlightedText?: string |
| 28 | +} |
| 29 | + |
| 30 | +const withVersion = (version: string, id: string) => `/${version}${id}` |
| 31 | + |
| 32 | +const adaptToRowItem = (doc: SearchDoc): SearchDoc => ({ |
| 33 | + id: doc.id, |
| 34 | + title: doc.title, |
| 35 | + subtitle: doc.subtitle, |
| 36 | + paragraphs: doc.paragraphs, |
| 37 | +}) |
| 38 | + |
| 39 | +export const CommandK = ({ searchIndex, placeholder, version }: CommandPaletteProps) => { |
| 40 | + const { t } = useTranslation() |
| 41 | + const navigate = useNavigate() |
| 42 | + const inputRef = useRef<HTMLInputElement>(null) |
| 43 | + |
| 44 | + const [query, setQuery] = useState("") |
| 45 | + |
| 46 | + const { isOpen, openModal, closeModal } = useModalState() |
| 47 | + const { history, addToHistory, clearHistory, removeFromHistory } = useSearchHistory() |
| 48 | + |
| 49 | + const results = fuzzySearch(searchIndex, query, { |
| 50 | + threshold: 0.8, |
| 51 | + minMatchCharLength: 3, |
| 52 | + }) |
| 53 | + |
| 54 | + const hasQuery = query.trim().length > 0 |
| 55 | + const hasResults = results.length > 0 |
| 56 | + const hasHistory = history.length > 0 |
| 57 | + const searchPlaceholder = placeholder ?? t("placeholders.search_documentation") |
| 58 | + |
| 59 | + const handleClose = () => { |
| 60 | + closeModal() |
| 61 | + setQuery("") |
| 62 | + } |
| 63 | + |
| 64 | + const handleNavigateAndClose = (doc: SearchDoc) => { |
| 65 | + navigate(withVersion(version, doc.id)) |
| 66 | + handleClose() |
| 67 | + } |
| 68 | + |
| 69 | + const handleResultSelect = (result: SearchResult) => { |
| 70 | + if (!isOpen) return |
| 71 | + |
| 72 | + const rowItem = adaptToRowItem(result.item) |
| 73 | + const matchType = result.refIndex === 0 ? "heading" : "paragraph" |
| 74 | + |
| 75 | + const historyItem: HistoryItem = { |
| 76 | + ...rowItem, |
| 77 | + type: matchType, |
| 78 | + slug: rowItem.id, |
| 79 | + highlightedText: result.highlightedText, |
| 80 | + } |
| 81 | + |
| 82 | + addToHistory(historyItem) |
| 83 | + handleNavigateAndClose(result.item) |
| 84 | + } |
| 85 | + |
| 86 | + const handleHistorySelect = (item: { slug?: string; id?: string }) => { |
| 87 | + const id = item.slug || item.id |
| 88 | + if (!id) return |
| 89 | + |
| 90 | + navigate(withVersion(version, id)) |
| 91 | + handleClose() |
| 92 | + } |
| 93 | + |
| 94 | + const handleToggle = () => { |
| 95 | + isOpen ? handleClose() : openModal() |
| 96 | + } |
| 97 | + |
| 98 | + const { selectedIndex } = useKeyboardNavigation({ |
| 99 | + isOpen, |
| 100 | + results, |
| 101 | + onSelect: handleResultSelect, |
| 102 | + onClose: handleClose, |
| 103 | + onToggle: handleToggle, |
| 104 | + }) |
| 105 | + |
| 106 | + if (!isOpen) { |
| 107 | + return <TriggerButton onOpen={openModal} placeholder={searchPlaceholder} /> |
| 108 | + } |
| 109 | + |
| 110 | + const renderBody = () => { |
| 111 | + if (hasQuery) { |
| 112 | + if (!hasResults) { |
| 113 | + return <EmptyState query={query} /> |
| 114 | + } |
| 115 | + |
| 116 | + return results.map((result, index) => ( |
| 117 | + <SearchResultRow |
| 118 | + key={`${result.item.id}-${result.refIndex}`} |
| 119 | + item={adaptToRowItem(result.item)} |
| 120 | + highlightedText={result.highlightedText} |
| 121 | + isSelected={index === selectedIndex} |
| 122 | + onClick={() => handleResultSelect(result)} |
| 123 | + matchType={result.refIndex === 0 ? "heading" : "paragraph"} |
| 124 | + /> |
| 125 | + )) |
| 126 | + } |
| 127 | + |
| 128 | + if (hasHistory) { |
| 129 | + return ( |
| 130 | + <SearchHistory |
| 131 | + history={history} |
| 132 | + onSelect={handleHistorySelect} |
| 133 | + onRemove={removeFromHistory} |
| 134 | + onClear={clearHistory} |
| 135 | + /> |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + return <EmptyState /> |
| 140 | + } |
| 141 | + |
| 142 | + return ( |
| 143 | + <Modal isOpen={isOpen} onClose={handleClose} getInitialFocus={() => inputRef.current} ariaLabel={searchPlaceholder}> |
| 144 | + <SearchInput ref={inputRef} value={query} onChange={setQuery} placeholder={searchPlaceholder} /> |
| 145 | + |
| 146 | + <div className="max-h-96 overflow-y-auto overscroll-contain" aria-label={searchPlaceholder}> |
| 147 | + {renderBody()} |
| 148 | + </div> |
| 149 | + |
| 150 | + <ResultsFooter resultsCount={results.length} query={query} /> |
| 151 | + </Modal> |
| 152 | + ) |
| 153 | +} |
0 commit comments