Skip to content

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -97,7 +97,10 @@ export const LinodeVolumeAttachForm = (props: Props) => {
validationSchema: AttachVolumeValidationSchema,
});

const { data: volume } = useVolumeQuery(values.volume_id);
const { data: volume } = useVolumeQuery(
values.volume_id,
values.volume_id !== -1
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabled the useVolumeQuery only when Volume_id is set and not -1(default value)


const linodeRequiresClientLibraryUpdate =
volume?.encryption === 'enabled' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useInfiniteVolumesQuery } from '@linode/queries';
import { useInfiniteVolumesQuery, useVolumeQuery } from '@linode/queries';
import { Autocomplete } from '@linode/ui';
import { useDebouncedValue } from '@linode/utilities';
import * as React from 'react';

interface Props {
Expand All @@ -17,11 +18,13 @@ export const VolumeSelect = (props: Props) => {

const [inputValue, setInputValue] = React.useState<string>('');

const searchFilter = inputValue
const debouncedInputValue = useDebouncedValue(inputValue);

const searchFilter = debouncedInputValue
? {
'+or': [
{ label: { '+contains': inputValue } },
{ tags: { '+contains': inputValue } },
{ label: { '+contains': debouncedInputValue } },
{ tags: { '+contains': debouncedInputValue } },
],
}
: {};
Expand All @@ -35,16 +38,24 @@ export const VolumeSelect = (props: Props) => {
'+order_by': 'label',
});

const options = data?.pages
.flatMap((page) => page.data)
.map(({ id, label }) => ({ id, label }));
const options = data?.pages.flatMap((page) => page.data);

const { data: volume, isLoading: isLoadingSelected } = useVolumeQuery(
value,
value > 0
);

if (value && volume && !options?.some((item) => item.id === volume.id)) {
options?.push(volume);
}

const selectedVolume = options?.find((option) => option.id === value) ?? null;

return (
<Autocomplete
disabled={disabled}
errorText={error}
filterOptions={(options) => options}
helperText={
region && "Only volumes in this Linode's region are attachable."
}
Expand All @@ -64,13 +75,13 @@ export const VolumeSelect = (props: Props) => {
}
},
}}
loading={isLoading}
loading={isLoading || isLoadingSelected}
onBlur={onBlur}
onChange={(event, value) => {
onChange(value?.id ?? -1);
onChange={(_, value) => {
setInputValue('');
onChange(value?.id ?? -1);
}}
onInputChange={(event, value, reason) => {
onInputChange={(_, value, reason) => {
if (reason === 'input') {
setInputValue(value);
} else {
Expand Down