1
- import React , { useEffect , useState } from "react" ;
1
+ import React , { act , useEffect , useState } from "react" ;
2
2
import { SearchChunksReqPayload , TrieveSDK } from "trieve-ts-sdk" ;
3
3
import { Chunk } from "../utils/types" ;
4
4
import * as Dialog from "@radix-ui/react-dialog" ;
@@ -10,10 +10,15 @@ type Props = {
10
10
onResultClick ?: ( chunk : Chunk ) => void ;
11
11
showImages ?: boolean ;
12
12
theme ?: "light" | "dark" ;
13
- searchOptions ?: Omit < SearchChunksReqPayload , "query" > ;
13
+ searchOptions ?: Omit <
14
+ Omit < SearchChunksReqPayload , "query" > ,
15
+ "highlight_options"
16
+ > ;
14
17
placeholder ?: string ;
15
18
} ;
16
19
20
+ export type ChunkWithHighlights = { chunk : Chunk ; highlights : string [ ] } ;
21
+
17
22
export const TrieveModalSearch = ( {
18
23
placeholder = "Search..." ,
19
24
onResultClick,
@@ -22,23 +27,23 @@ export const TrieveModalSearch = ({
22
27
theme = "light" ,
23
28
searchOptions = {
24
29
search_type : "hybrid" ,
25
- highlight_options : {
26
- highlight_delimiters : [ "?" , "," , "." , "!" , "↵" ] ,
27
- highlight_max_length : 2 ,
28
- highlight_max_num : 2 ,
29
- highlight_strategy : "exactmatch" ,
30
- highlight_window : 100 ,
31
- } ,
32
30
} ,
33
31
} : Props ) => {
34
32
const [ query , setQuery ] = useState ( "" ) ;
35
- const [ results , setResults ] = useState < { chunk : Chunk } [ ] > ( [ ] ) ;
33
+ const [ results , setResults ] = useState < ChunkWithHighlights [ ] > ( [ ] ) ;
36
34
const [ open , setOpen ] = useState ( false ) ;
37
35
38
36
const search = async ( ) => {
39
37
const results = await trieve . search ( {
40
38
...searchOptions ,
41
39
query,
40
+ highlight_options : {
41
+ highlight_delimiters : [ "?" , "," , "." , "!" , "↵" ] ,
42
+ highlight_max_length : 2 ,
43
+ highlight_max_num : 1 ,
44
+ highlight_strategy : "exactmatch" ,
45
+ highlight_window : 10 ,
46
+ } ,
42
47
} ) ;
43
48
const resultsWithHighlight = results . chunks . map ( ( chunk ) => {
44
49
const c = chunk . chunk as unknown as Chunk ;
@@ -47,18 +52,10 @@ export const TrieveModalSearch = ({
47
52
chunk : {
48
53
...chunk . chunk ,
49
54
highlight : highlightText ( query , c . chunk_html ) ,
50
- highlightTitle : highlightText (
51
- query ,
52
- c . metadata ?. title || c . metadata ?. page_title || c . metadata ?. name
53
- ) ,
54
- highlightDescription : highlightText (
55
- query ,
56
- c . metadata ?. description || c . metadata ?. page_description
57
- ) ,
58
55
} ,
59
56
} ;
60
57
} ) ;
61
- setResults ( resultsWithHighlight as unknown as { chunk : Chunk } [ ] ) ;
58
+ setResults ( resultsWithHighlight as unknown as ChunkWithHighlights [ ] ) ;
62
59
} ;
63
60
useEffect ( ( ) => {
64
61
if ( query ) {
@@ -70,6 +67,23 @@ export const TrieveModalSearch = ({
70
67
if ( e . code === "KeyK" && e . metaKey && ! open ) setOpen ( true ) ;
71
68
} ;
72
69
70
+ const onUpOrDownClicked = ( index : number , code : string ) => {
71
+ console . log ( "clicked" ) ;
72
+ if ( code === "ArrowDown" ) {
73
+ document
74
+ . getElementsByClassName ( "trieve-elements-search" ) [ 0 ]
75
+ . getElementsByClassName ( "item" )
76
+ [ index + 1 ] ?. focus ( ) ;
77
+ }
78
+
79
+ if ( code === "ArrowUp" ) {
80
+ document
81
+ . getElementsByClassName ( "trieve-elements-search" ) [ 0 ]
82
+ . getElementsByClassName ( "item" )
83
+ [ index - 1 ] ?. focus ( ) ;
84
+ }
85
+ } ;
86
+
73
87
useEffect ( ( ) => {
74
88
document . addEventListener ( "keydown" , checkForCMDK ) ;
75
89
return ( ) => {
@@ -106,6 +120,7 @@ export const TrieveModalSearch = ({
106
120
</ button >
107
121
</ Dialog . Trigger >
108
122
< Dialog . Portal >
123
+ < Dialog . DialogTitle className = "sr-only" > Search</ Dialog . DialogTitle >
109
124
< Dialog . Overlay id = "trieve-search-modal-overlay" />
110
125
< Dialog . Content
111
126
id = "trieve-search-modal"
@@ -136,10 +151,12 @@ export const TrieveModalSearch = ({
136
151
< kbd > ESC</ kbd >
137
152
</ div >
138
153
</ div >
139
- < ul >
140
- { results . map ( ( result ) => (
154
+ < ul className = "trieve-elements-search" >
155
+ { results . map ( ( result , index ) => (
141
156
< Item
157
+ onUpOrDownClicked = { onUpOrDownClicked }
142
158
item = { result }
159
+ index = { index }
143
160
onResultClick = { onResultClick }
144
161
showImages = { showImages }
145
162
key = { result . chunk . id }
0 commit comments