Skip to content
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

fix: [M3-7249] - Linode Landing flickering #9836

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ interface RenderChildrenProps<T> {
type RenderChildren<T> = (props: RenderChildrenProps<T>) => JSX.Element;

interface Props<T = PreferenceValue> {
children: RenderChildren<T>;
children: RenderChildren<T | undefined>;
initialSetCallbackFn?: (value: T) => void;
localStorageKey?: string;
preferenceKey: string;
preferenceOptions: [T, T];
toggleCallbackFn?: (value: T) => void;
Expand All @@ -39,18 +38,18 @@ export const PreferenceToggle = <T extends unknown>(props: Props<T>) => {
value,
} = props;

/** will be undefined and render-block children unless otherwise specified */
const [currentlySetPreference, setPreference] = React.useState<T | undefined>(
value
);
const [lastUpdated, setLastUpdated] = React.useState<number>(0);

const {
data: preferences,
error: preferencesError,
refetch: refetchUserPreferences,
} = usePreferences();

const [currentlySetPreference, setPreference] = React.useState<T | undefined>(
value ?? preferences?.[preferenceKey]
);

const [lastUpdated, setLastUpdated] = React.useState<number>(0);

const { mutateAsync: updateUserPreferences } = useMutatePreferences();

React.useEffect(() => {
Expand Down Expand Up @@ -192,17 +191,6 @@ export const PreferenceToggle = <T extends unknown>(props: Props<T>) => {
return newPreferenceToSet;
};

/**
* render-block the children. We can prevent
* render-blocking by passing a default value as a prop
*
* So if you want to handle local state outside of this component,
* you can do so and pass the value explicitly with the _value_ prop
*/
if (isNullOrUndefined(currentlySetPreference)) {
return null;
}

return typeof children === 'function'
? children({ preference: currentlySetPreference, togglePreference })
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ class ListLinodes extends React.Component<CombinedProps, State> {
<DocumentTitleSegment segment="Linodes" />
<ProductInformationBanner bannerLocation="Linodes" />
<PreferenceToggle<boolean>
localStorageKey="GROUP_LINODES"
Copy link
Member Author

Choose a reason for hiding this comment

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

This prop was unused and did nothing

preferenceKey="linodes_group_by_tag"
preferenceOptions={[false, true]}
toggleCallbackFnDebounced={sendGroupByAnalytic}
Copy link
Member Author

@bnussman-akamai bnussman-akamai Oct 24, 2023

Choose a reason for hiding this comment

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

Because we now rely on React Query's state directly, we won't debounce because React Query performs optimistic updates. I don't think this is an issue considering how and where this component is used.

Expand All @@ -199,7 +198,6 @@ class ListLinodes extends React.Component<CombinedProps, State> {
}: PreferenceToggleProps<boolean>) => {
return (
<PreferenceToggle<'grid' | 'list'>
localStorageKey="LINODE_VIEW"
preferenceKey="linodes_view_style"
preferenceOptions={['list', 'grid']}
toggleCallbackFn={this.changeViewInstant}
Expand Down
Loading