1
1
import { useEffect , useMemo , useRef , useState } from "react" ;
2
2
import "./Gallery.css" ;
3
- import { Badge , Button , IconButton , Select } from "@stratakit/bricks" ;
3
+ import { Badge , IconButton , Select , Spinner , TextBox } from "@stratakit/bricks" ;
4
4
import { getBaseUrl } from "./util/getBaseUrl" ;
5
- import { Input } from "@stratakit/bricks/TextBox" ;
6
5
import classNames from "classnames" ;
7
- import { script } from "./icons" ;
6
+ import { close , script , search } from "./icons" ;
7
+ import { ProcessStatus } from "./util/ProcessStatus" ;
8
8
9
9
const GALLERY_BASE = __GALLERY_BASE_URL__ ;
10
10
@@ -32,6 +32,7 @@ export function GallerySearch({
32
32
33
33
const [ searchTerm , setSearchTerm ] = useState ( "" ) ;
34
34
const [ currentTag , setCurrentTag ] = useState < string | "All" > ( "All" ) ;
35
+ const [ searchState , setSearchState ] = useState < ProcessStatus > ( "NOT_STARTED" ) ;
35
36
36
37
const requestId = useRef < number > ( 0 ) ;
37
38
@@ -40,9 +41,12 @@ export function GallerySearch({
40
41
if ( ! pagefind . current ) {
41
42
return ;
42
43
}
44
+
45
+ setSearchState ( "IN_PROGRESS" ) ;
43
46
const searchString = searchTerm ;
44
47
if ( ! searchString || searchString === "" ) {
45
48
setSearchResults ( null ) ;
49
+ setSearchState ( "COMPLETE" ) ;
46
50
return ;
47
51
}
48
52
const thisRequestId = ++ requestId . current ;
@@ -66,6 +70,7 @@ export function GallerySearch({
66
70
67
71
if ( thisRequestId === requestId . current ) {
68
72
setSearchResults ( resultData ) ;
73
+ setSearchState ( "COMPLETE" ) ;
69
74
}
70
75
}
71
76
@@ -104,35 +109,49 @@ export function GallerySearch({
104
109
105
110
return (
106
111
< >
107
- { ! ! searchTerm && (
108
- < Button
109
- onClick = { ( ) => {
110
- setSearchTerm ( "" ) ;
111
- if ( inputRef . current ) {
112
- inputRef . current . value = "" ;
112
+ { searchState === "IN_PROGRESS" && < Spinner /> }
113
+ < TextBox . Root className = "search-input" >
114
+ < TextBox . Icon href = { search } />
115
+ < TextBox . Input
116
+ ref = { inputRef }
117
+ disabled = { ! pagefindLoaded }
118
+ onChange = { ( e ) => {
119
+ if ( debounceTimeout . current ) {
120
+ clearInterval ( debounceTimeout . current ) ;
113
121
}
122
+ const newSearchTerm = e . target . value ;
123
+ pagefind . current ?. preload ( newSearchTerm ) ;
124
+ if ( newSearchTerm === "" ) {
125
+ // Instantly update for a more reactive feel when clearing the input
126
+ console . log ( "early clear" ) ;
127
+ setSearchTerm ( "" ) ;
128
+ return ;
129
+ }
130
+ debounceTimeout . current = setTimeout ( ( ) => {
131
+ setSearchTerm ( newSearchTerm ) ;
132
+ } , 300 ) ;
114
133
} }
115
- >
116
- Clear
117
- </ Button >
118
- ) }
119
- < Input
120
- ref = { inputRef }
121
- disabled = { ! pagefindLoaded }
122
- onChange = { ( e ) => {
123
- if ( debounceTimeout . current ) {
124
- clearInterval ( debounceTimeout . current ) ;
125
- }
126
- const newSearchTerm = e . target . value ;
127
- pagefind . current ?. preload ( newSearchTerm ) ;
128
- debounceTimeout . current = setTimeout ( ( ) => {
129
- setSearchTerm ( newSearchTerm ) ;
130
- } , 300 ) ;
131
- } }
132
- placeholder = "Filter demos..."
133
- />
134
+ placeholder = "Search gallery"
135
+ />
136
+ { ! ! searchTerm && (
137
+ < IconButton
138
+ className = "clear-btn"
139
+ icon = { close }
140
+ label = "Clear"
141
+ onClick = { ( e ) => {
142
+ console . log ( e ) ;
143
+ setSearchTerm ( "" ) ;
144
+ if ( inputRef . current ) {
145
+ inputRef . current . value = "" ;
146
+ inputRef . current . focus ( ) ;
147
+ }
148
+ } }
149
+ > </ IconButton >
150
+ ) }
151
+ </ TextBox . Root >
134
152
< Select . Root >
135
153
< Select . HtmlSelect
154
+ className = "tag-select"
136
155
disabled = { ! pagefindLoaded }
137
156
onChange = { ( e ) => {
138
157
setCurrentTag ( e . target . value ) ;
0 commit comments