@@ -7,7 +7,7 @@ import { fuzzySearch } from "../hooks/use-fuzzy-search"
77import { useKeyboardNavigation } from "../hooks/use-keyboard-navigation"
88import { useModalState } from "../hooks/use-modal-state"
99import { useSearchHistory } from "../hooks/use-search-history"
10- import type { SearchDoc , SearchResult } from "../search-types"
10+ import type { MatchType , SearchDoc , SearchResult } from "../search-types"
1111import { EmptyState } from "./empty-state"
1212import { ResultsFooter } from "./results-footer"
1313import { SearchHistory } from "./search-history"
@@ -21,24 +21,8 @@ interface CommandPaletteProps {
2121 version : Version
2222}
2323
24- type MatchType = "heading" | "paragraph"
25-
26- interface HistoryItem extends SearchDoc {
27- type ?: MatchType
28- slug ?: string
29- highlightedText ?: string
30- version ?: string
31- }
32-
3324const withVersion = ( version : string , id : string ) => `/${ version } ${ id } `
3425
35- const adaptToRowItem = ( doc : SearchDoc ) : SearchDoc => ( {
36- id : doc . id ,
37- title : doc . title ,
38- subtitle : doc . subtitle ,
39- paragraphs : doc . paragraphs ,
40- } )
41-
4226export const CommandK = ( { searchIndex, placeholder, version } : CommandPaletteProps ) => {
4327 const { t } = useTranslation ( )
4428 const navigate = useNavigate ( )
@@ -54,28 +38,23 @@ export const CommandK = ({ searchIndex, placeholder, version }: CommandPalettePr
5438 minMatchCharLength : 3 ,
5539 } )
5640
57- const hasQuery = query . trim ( ) . length > 0
58- const hasResults = results . length > 0
59- const hasHistory = history . length > 0
41+ const hasQuery = ! ! query . trim ( )
42+ const hasResults = ! ! results . length
43+ const hasHistory = ! ! history . length
6044 const searchPlaceholder = placeholder ?? t ( "placeholders.search_documentation" )
6145
6246 const handleClose = ( ) => {
6347 closeModal ( )
6448 setQuery ( "" )
6549 }
6650
67- const handleNavigateAndClose = ( doc : SearchDoc , v : string ) => {
68- navigate ( withVersion ( v , doc . id ) )
69- handleClose ( )
70- }
71-
7251 const handleResultSelect = ( result : SearchResult ) => {
7352 if ( ! isOpen ) return
7453
75- const rowItem = adaptToRowItem ( result . item )
54+ const rowItem = result . item
7655 const matchType : MatchType = result . refIndex === 0 ? "heading" : "paragraph"
7756
78- const historyItem : HistoryItem = {
57+ const historyItem = {
7958 ...rowItem ,
8059 type : matchType ,
8160 slug : rowItem . id ,
@@ -84,7 +63,8 @@ export const CommandK = ({ searchIndex, placeholder, version }: CommandPalettePr
8463 }
8564
8665 addToHistory ( historyItem )
87- handleNavigateAndClose ( result . item , version )
66+ navigate ( withVersion ( version , rowItem . id ) )
67+ handleClose ( )
8868 }
8969
9070 const handleHistorySelect = ( item : { slug ?: string ; id ?: string ; version ?: string } ) => {
@@ -119,7 +99,7 @@ export const CommandK = ({ searchIndex, placeholder, version }: CommandPalettePr
11999 return results . map ( ( result , index ) => (
120100 < SearchResultRow
121101 key = { `${ result . item . id } -${ result . refIndex } ` }
122- item = { adaptToRowItem ( result . item ) }
102+ item = { result . item }
123103 highlightedText = { result . highlightedText }
124104 isSelected = { index === selectedIndex }
125105 onClick = { ( ) => handleResultSelect ( result ) }
0 commit comments