1
- import React , { useMemo } from 'react' ;
1
+ import React , { ChangeEvent , useCallback , useMemo } from 'react' ;
2
2
import { FilterIcon } from 'lucide-react' ;
3
3
import { useQuery } from 'urql' ;
4
4
import { Button } from '@/components/ui/button' ;
@@ -111,24 +111,33 @@ export function TypeFilter(props: {
111
111
[ allNamedTypes ] ,
112
112
) ;
113
113
114
+ const onChange = useCallback (
115
+ ( option : SelectOption | null ) => {
116
+ void router . navigate ( {
117
+ search : router . latestLocation . search ,
118
+ to : '/$organizationSlug/$projectSlug/$targetSlug/explorer/$typename' ,
119
+ params : {
120
+ organizationSlug : props . organizationSlug ,
121
+ projectSlug : props . projectSlug ,
122
+ targetSlug : props . targetSlug ,
123
+ typename : option ?. value ?? '' ,
124
+ } ,
125
+ } ) ;
126
+ } ,
127
+ [ router ] ,
128
+ ) ;
129
+
130
+ const defaultValue = useMemo ( ( ) => {
131
+ return props . typename ? { value : props . typename , label : props . typename } : null ;
132
+ } , [ props . typename ] ) ;
133
+
114
134
return (
115
135
< Autocomplete
116
136
className = "min-w-[200px] grow cursor-text"
117
137
placeholder = "Search for a type"
118
- defaultValue = { props . typename ? { value : props . typename , label : props . typename } : null }
138
+ defaultValue = { defaultValue }
119
139
options = { types }
120
- onChange = { ( option : SelectOption | null ) => {
121
- void router . navigate ( {
122
- search : router . latestLocation . search ,
123
- to : '/$organizationSlug/$projectSlug/$targetSlug/explorer/$typename' ,
124
- params : {
125
- organizationSlug : props . organizationSlug ,
126
- projectSlug : props . projectSlug ,
127
- targetSlug : props . targetSlug ,
128
- typename : option ?. value ?? '' ,
129
- } ,
130
- } ) ;
131
- } }
140
+ onChange = { onChange }
132
141
loading = { query . fetching }
133
142
/>
134
143
) ;
@@ -137,37 +146,48 @@ export function TypeFilter(props: {
137
146
export function FieldByNameFilter ( ) {
138
147
const router = useRouter ( ) ;
139
148
149
+ const onChange = useCallback (
150
+ ( e : ChangeEvent < HTMLInputElement > ) => {
151
+ void router . navigate ( {
152
+ search : {
153
+ ...router . latestLocation . search ,
154
+ search : e . target . value === '' ? undefined : e . target . value ,
155
+ } ,
156
+ replace : true ,
157
+ } ) ;
158
+ } ,
159
+ [ router ] ,
160
+ ) ;
161
+
162
+ const initialValue =
163
+ 'search' in router . latestLocation . search &&
164
+ typeof router . latestLocation . search . search === 'string'
165
+ ? router . latestLocation . search . search
166
+ : '' ;
167
+
140
168
return (
141
169
< Input
142
170
className = "w-[200px] grow cursor-text"
143
171
placeholder = "Filter by field name"
144
- onChange = { e => {
145
- void router . navigate ( {
146
- search : {
147
- ...router . latestLocation . search ,
148
- search : e . target . value === '' ? undefined : e . target . value ,
149
- } ,
150
- } ) ;
151
- } }
152
- value = {
153
- 'search' in router . latestLocation . search &&
154
- typeof router . latestLocation . search . search === 'string'
155
- ? router . latestLocation . search . search
156
- : ''
157
- }
172
+ onChange = { onChange }
173
+ defaultValue = { initialValue }
158
174
/>
159
175
) ;
160
176
}
161
177
162
178
export function DateRangeFilter ( ) {
163
179
const periodSelector = usePeriodSelector ( ) ;
180
+ const onUpdate = useCallback (
181
+ ( value : { preset : { range : { from : string ; to : string } } } ) => {
182
+ periodSelector . setPeriod ( value . preset . range ) ;
183
+ } ,
184
+ [ periodSelector ] ,
185
+ ) ;
164
186
165
187
return (
166
188
< DateRangePicker
167
189
validUnits = { [ 'y' , 'M' , 'w' , 'd' ] }
168
- onUpdate = { value => {
169
- periodSelector . setPeriod ( value . preset . range ) ;
170
- } }
190
+ onUpdate = { onUpdate }
171
191
selectedRange = { periodSelector . period }
172
192
startDate = { periodSelector . startDate }
173
193
align = "end"
0 commit comments