Skip to content

Commit 92c628c

Browse files
committed
Add debounce
1 parent 76bf6ec commit 92c628c

File tree

2 files changed

+56
-53
lines changed

2 files changed

+56
-53
lines changed

site/src/pages/WorkspacesPage/WorkspacesPageView.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable eslint-comments/disable-enable-pair -- ignore */
2+
/* eslint-disable @typescript-eslint/no-explicit-any -- We don't care about any here */
13
import { ComponentMeta, Story } from "@storybook/react"
24
import { DEFAULT_RECORDS_PER_PAGE } from "components/PaginationWidget/utils"
35
import dayjs from "dayjs"
@@ -86,6 +88,7 @@ const defaultFilterProps = {
8688
filter: {
8789
query: `owner:${MockUser.username}`,
8890
update: () => action("update"),
91+
debounceUpdate: action("debounce") as any,
8992
values: {
9093
owner: MockUser.username,
9194
template: undefined,

site/src/pages/WorkspacesPage/filter/filter.tsx

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
StatusOption,
3535
BaseOption,
3636
} from "./options"
37+
import debounce from "just-debounce-it"
3738

3839
export type FilterValues = {
3940
owner?: string // User["username"]
@@ -64,9 +65,15 @@ export const useFilter = ({
6465
}
6566
}
6667

68+
const debounceUpdate = debounce(
69+
(values: string | FilterValues) => update(values),
70+
500,
71+
)
72+
6773
return {
6874
query,
6975
update,
76+
debounceUpdate,
7077
values,
7178
}
7279
}
@@ -160,61 +167,54 @@ export const Filter = ({
160167

161168
return (
162169
<Box display="flex" sx={{ gap: 1, mb: 2 }}>
163-
<Box
164-
component="form"
165-
sx={{ width: "100%" }}
166-
onSubmit={(e) => {
167-
e.preventDefault()
168-
const formData = new FormData(e.currentTarget)
169-
const query = formData.get("query") as string
170-
filter.update(query)
171-
}}
172-
>
173-
<TextField
174-
fullWidth
175-
error={shouldDisplayError}
176-
helperText={
177-
shouldDisplayError ? getValidationErrorMessage(error) : undefined
178-
}
179-
size="small"
180-
InputProps={{
181-
name: "query",
182-
placeholder: "Search...",
183-
value: searchQuery,
184-
onChange: (e) => setSearchQuery(e.target.value),
185-
sx: {
186-
borderRadius: "6px",
187-
"& input::placeholder": {
188-
color: (theme) => theme.palette.text.secondary,
189-
},
170+
<TextField
171+
fullWidth
172+
error={shouldDisplayError}
173+
helperText={
174+
shouldDisplayError ? getValidationErrorMessage(error) : undefined
175+
}
176+
size="small"
177+
InputProps={{
178+
name: "query",
179+
placeholder: "Search...",
180+
value: searchQuery,
181+
onChange: (e) => {
182+
setSearchQuery(e.target.value)
183+
filter.debounceUpdate(e.target.value)
184+
},
185+
sx: {
186+
borderRadius: "6px",
187+
"& input::placeholder": {
188+
color: (theme) => theme.palette.text.secondary,
190189
},
191-
startAdornment: (
192-
<InputAdornment position="start">
193-
<SearchOutlined
194-
sx={{
195-
fontSize: 14,
196-
color: (theme) => theme.palette.text.secondary,
190+
},
191+
startAdornment: (
192+
<InputAdornment position="start">
193+
<SearchOutlined
194+
sx={{
195+
fontSize: 14,
196+
color: (theme) => theme.palette.text.secondary,
197+
}}
198+
/>
199+
</InputAdornment>
200+
),
201+
endAdornment: hasFilterQuery && (
202+
<InputAdornment position="end">
203+
<Tooltip title="Clear filter">
204+
<IconButton
205+
size="small"
206+
onClick={() => {
207+
filter.update("")
197208
}}
198-
/>
199-
</InputAdornment>
200-
),
201-
endAdornment: hasFilterQuery && (
202-
<InputAdornment position="end">
203-
<Tooltip title="Clear filter">
204-
<IconButton
205-
size="small"
206-
onClick={() => {
207-
filter.update("")
208-
}}
209-
>
210-
<CloseOutlined sx={{ fontSize: 14 }} />
211-
</IconButton>
212-
</Tooltip>
213-
</InputAdornment>
214-
),
215-
}}
216-
/>
217-
</Box>
209+
>
210+
<CloseOutlined sx={{ fontSize: 14 }} />
211+
</IconButton>
212+
</Tooltip>
213+
</InputAdornment>
214+
),
215+
}}
216+
/>
217+
218218
{autocomplete.users && <OwnerFilter autocomplete={autocomplete.users} />}
219219
<TemplatesFilter autocomplete={autocomplete.templates} />
220220
<StatusFilter autocomplete={autocomplete.status} />

0 commit comments

Comments
 (0)