-
Notifications
You must be signed in to change notification settings - Fork 374
refactor: [M3-9647] - Reduce api requests made for every keystroke in Volume attach drawer #12052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
112597b
a8746fe
5d9c009
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/manager": Tech Stories | ||
--- | ||
|
||
Reduce api requests made for every keystroke in Volume attach drawer ([#12052](https://github.com/linode/manager/pull/12052)) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,28 +16,13 @@ export const VolumeSelect = (props: Props) => { | |||||
const { disabled, error, name, onBlur, onChange, region, value } = props; | ||||||
|
||||||
const [inputValue, setInputValue] = React.useState<string>(''); | ||||||
|
||||||
const searchFilter = inputValue | ||||||
? { | ||||||
'+or': [ | ||||||
{ label: { '+contains': inputValue } }, | ||||||
{ tags: { '+contains': inputValue } }, | ||||||
], | ||||||
} | ||||||
: {}; | ||||||
Comment on lines
-20
to
-27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the API searching functionality makes it so that if the user searches for a result that isn't on the first page of paginated results, the result does not show. Screen.Recording.2025-04-17.at.10.15.43.AM.movWe need to decide if we want to keep the API pagination/filtering or go full client side search/filtering. My vote would be to keep this component using API pagination/filtering. Even better, we could use The implementation would look something like const [inputValue, setInputValue] = React.useState<string>('');
const debouncedSearchValue = useDebouncedValue(inputValue);
const { filter: searchFilter } = getAPIFilterFromQuery(debouncedSearchValue, {
searchableFieldsWithoutOperator: ['label', 'tags'],
});
const { data, fetchNextPage, hasNextPage, isLoading } =
useInfiniteVolumesQuery({
...searchFilter,
...(region ? { region } : {}),
'+order': 'asc',
// linode_id: null, <- if the API let us, we would do this
'+order_by': 'label',
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if I'm using API side filtering instead of client side filtering, the searched/user-typed option is not shown if it is not present on the first page π€ Screen.Recording.2025-04-22.at.11.16.02.PM.movI'm not able to think about an approach to solve this other than fetching all pages data at once(which obviously isn't very efficient) |
||||||
|
||||||
const { | ||||||
data, | ||||||
fetchNextPage, | ||||||
hasNextPage, | ||||||
isLoading, | ||||||
} = useInfiniteVolumesQuery({ | ||||||
...searchFilter, | ||||||
...(region ? { region } : {}), | ||||||
'+order': 'asc', | ||||||
// linode_id: null, <- if the API let us, we would do this | ||||||
'+order_by': 'label', | ||||||
}); | ||||||
const { data, fetchNextPage, hasNextPage, isLoading } = | ||||||
useInfiniteVolumesQuery({ | ||||||
...(region ? { region } : {}), | ||||||
'+order': 'asc', | ||||||
// linode_id: null, <- if the API let us, we would do this | ||||||
'+order_by': 'label', | ||||||
}); | ||||||
|
||||||
const options = data?.pages | ||||||
.flatMap((page) => page.data) | ||||||
|
@@ -47,6 +32,15 @@ export const VolumeSelect = (props: Props) => { | |||||
|
||||||
return ( | ||||||
<Autocomplete | ||||||
disabled={disabled} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we retain API filtering, we should add this prop
Suggested change
to the Autocomplete to ensure it doesn't do any client side filtering |
||||||
errorText={error} | ||||||
helperText={ | ||||||
region && "Only volumes in this Linode's region are attachable." | ||||||
} | ||||||
id={name} | ||||||
inputValue={selectedVolume ? selectedVolume.label : inputValue} | ||||||
isOptionEqualToValue={(option) => option.id === selectedVolume?.id} | ||||||
label="Volume" | ||||||
ListboxProps={{ | ||||||
onScroll: (event: React.SyntheticEvent) => { | ||||||
const listboxNode = event.currentTarget; | ||||||
|
@@ -59,9 +53,8 @@ export const VolumeSelect = (props: Props) => { | |||||
} | ||||||
}, | ||||||
}} | ||||||
helperText={ | ||||||
region && "Only volumes in this Linode's region are attachable." | ||||||
} | ||||||
loading={isLoading} | ||||||
onBlur={onBlur} | ||||||
onChange={(event, value) => { | ||||||
onChange(value?.id ?? -1); | ||||||
setInputValue(''); | ||||||
|
@@ -73,14 +66,6 @@ export const VolumeSelect = (props: Props) => { | |||||
setInputValue(''); | ||||||
} | ||||||
}} | ||||||
disabled={disabled} | ||||||
errorText={error} | ||||||
id={name} | ||||||
inputValue={selectedVolume ? selectedVolume.label : inputValue} | ||||||
isOptionEqualToValue={(option) => option.id === selectedVolume?.id} | ||||||
label="Volume" | ||||||
loading={isLoading} | ||||||
onBlur={onBlur} | ||||||
options={options ?? []} | ||||||
placeholder="Select a Volume" | ||||||
value={selectedVolume} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enabled the
useVolumeQuery
only whenVolume_id
is set and not -1(default value)