|
| 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 { useKeyboardNavigation } from "../hooks/use-keyboard-navigation" |
| 7 | +import { useModalState } from "../hooks/use-modal-state" |
| 8 | +import { useSearch } from "../hooks/use-search" |
| 9 | +import { useSearchHistory } from "../hooks/use-search-history" |
| 10 | +import type { HistoryItem, MatchType, 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 | + placeholder?: string |
| 20 | + version: Version |
| 21 | +} |
| 22 | + |
| 23 | +export const CommandK = ({ placeholder, version }: CommandPaletteProps) => { |
| 24 | + const { t } = useTranslation() |
| 25 | + const navigate = useNavigate() |
| 26 | + const inputRef = useRef<HTMLInputElement>(null) |
| 27 | + const [query, setQuery] = useState("") |
| 28 | + const { isOpen, openModal, closeModal } = useModalState() |
| 29 | + const { history, addToHistory, clearHistory, removeFromHistory } = useSearchHistory(version) |
| 30 | + const { results, search } = useSearch({ version }) |
| 31 | + |
| 32 | + const hasQuery = !!query.trim() |
| 33 | + const hasResults = !!results.length |
| 34 | + const hasHistory = !!history.length |
| 35 | + const searchPlaceholder = placeholder ?? t("placeholders.search_documentation") |
| 36 | + |
| 37 | + const handleClose = () => { |
| 38 | + closeModal() |
| 39 | + setQuery("") |
| 40 | + search("") |
| 41 | + } |
| 42 | + |
| 43 | + const navigateToPage = (id: string) => { |
| 44 | + const path = [version, id] |
| 45 | + .filter(Boolean) |
| 46 | + .map((s) => s.replace(/^\/+|\/+$/g, "")) |
| 47 | + .join("/") |
| 48 | + |
| 49 | + navigate(`/${path}`) |
| 50 | + } |
| 51 | + |
| 52 | + const handleResultSelect = (result: SearchResult) => { |
| 53 | + if (!isOpen) return |
| 54 | + const rowItem = result.item |
| 55 | + const matchType: MatchType = result.refIndex === 0 ? "heading" : "paragraph" |
| 56 | + const historyItem = { |
| 57 | + ...rowItem, |
| 58 | + type: matchType, |
| 59 | + highlightedText: result.highlightedText, |
| 60 | + } |
| 61 | + |
| 62 | + addToHistory(historyItem) |
| 63 | + navigateToPage(rowItem.id) |
| 64 | + handleClose() |
| 65 | + } |
| 66 | + |
| 67 | + const handleHistorySelect = (item: HistoryItem) => { |
| 68 | + navigateToPage(item.id) |
| 69 | + handleClose() |
| 70 | + } |
| 71 | + |
| 72 | + const handleToggle = () => { |
| 73 | + isOpen ? handleClose() : openModal() |
| 74 | + } |
| 75 | + |
| 76 | + const { selectedIndex } = useKeyboardNavigation({ |
| 77 | + isOpen, |
| 78 | + results, |
| 79 | + onSelect: handleResultSelect, |
| 80 | + onClose: handleClose, |
| 81 | + onToggle: handleToggle, |
| 82 | + }) |
| 83 | + |
| 84 | + if (!isOpen) { |
| 85 | + return <TriggerButton onOpen={openModal} placeholder={searchPlaceholder} /> |
| 86 | + } |
| 87 | + |
| 88 | + const renderBody = () => { |
| 89 | + if (hasQuery) { |
| 90 | + if (!hasResults) return <EmptyState query={query} /> |
| 91 | + |
| 92 | + return results.map((result, index) => ( |
| 93 | + <SearchResultRow |
| 94 | + key={`${result.item.id}-${result.refIndex}`} |
| 95 | + item={result.item} |
| 96 | + highlightedText={result.highlightedText} |
| 97 | + isSelected={index === selectedIndex} |
| 98 | + onClick={() => handleResultSelect(result)} |
| 99 | + matchType={result.refIndex === 0 ? "heading" : "paragraph"} |
| 100 | + /> |
| 101 | + )) |
| 102 | + } |
| 103 | + |
| 104 | + if (hasHistory) { |
| 105 | + return ( |
| 106 | + <SearchHistory |
| 107 | + history={history} |
| 108 | + onSelect={handleHistorySelect} |
| 109 | + onRemove={removeFromHistory} |
| 110 | + onClear={clearHistory} |
| 111 | + /> |
| 112 | + ) |
| 113 | + } |
| 114 | + |
| 115 | + return <EmptyState /> |
| 116 | + } |
| 117 | + |
| 118 | + return ( |
| 119 | + <Modal isOpen={isOpen} onClose={handleClose} getInitialFocus={() => inputRef.current} ariaLabel={searchPlaceholder}> |
| 120 | + <SearchInput |
| 121 | + ref={inputRef} |
| 122 | + value={query} |
| 123 | + onChange={(val) => { |
| 124 | + setQuery(val) |
| 125 | + search(val.trim()) |
| 126 | + }} |
| 127 | + placeholder={searchPlaceholder} |
| 128 | + /> |
| 129 | + <div className="max-h-96 overflow-y-auto overscroll-contain" aria-label={searchPlaceholder}> |
| 130 | + {renderBody()} |
| 131 | + </div> |
| 132 | + <ResultsFooter resultsCount={results.length} query={query} /> |
| 133 | + </Modal> |
| 134 | + ) |
| 135 | +} |
0 commit comments