Skip to content

Commit

Permalink
initial refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bnussman committed Dec 9, 2024
1 parent 7609507 commit ad02ddb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
13 changes: 6 additions & 7 deletions packages/manager/src/components/PrimaryNav/PrimaryNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ export const PrimaryNav = (props: PrimaryNavProps) => {

const { isIAMEnabled, isIAMBeta } = useIsIAMEnabled();

const { data: preferences } = usePreferences();
const { data: collapsedSideNavPreference } = usePreferences(
(preferences) => preferences?.collapsedSideNavProductFamilies
);

const collapsedAccordions = collapsedSideNavPreference ?? [];

const { mutateAsync: updatePreferences } = useMutatePreferences();

const productFamilyLinkGroups: ProductFamilyLinkGroup<
Expand Down Expand Up @@ -253,10 +258,6 @@ export const PrimaryNav = (props: PrimaryNavProps) => {
]
);

const [collapsedAccordions, setCollapsedAccordions] = React.useState<
number[]
>(preferences?.collapsedSideNavProductFamilies ?? []);

const accordionClicked = (index: number) => {
let updatedCollapsedAccordions;
if (collapsedAccordions.includes(index)) {
Expand All @@ -266,13 +267,11 @@ export const PrimaryNav = (props: PrimaryNavProps) => {
updatePreferences({
collapsedSideNavProductFamilies: updatedCollapsedAccordions,
});
setCollapsedAccordions(updatedCollapsedAccordions);
} else {
updatedCollapsedAccordions = [...collapsedAccordions, index];
updatePreferences({
collapsedSideNavProductFamilies: updatedCollapsedAccordions,
});
setCollapsedAccordions(updatedCollapsedAccordions);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ export const LinodeResize = (props: Props) => {
);

const { data: types } = useAllTypes(open);
const { data: preferences } = usePreferences(open);

const { data: typeToConfirmPreference } = usePreferences(
(preferences) => preferences?.type_to_confirm,
open
);

const { enqueueSnackbar } = useSnackbar();
const [confirmationText, setConfirmationText] = React.useState('');
const [resizeError, setResizeError] = React.useState<string>('');
Expand Down Expand Up @@ -162,8 +167,7 @@ export const LinodeResize = (props: Props) => {
const tableDisabled = hostMaintenance || isLinodesGrantReadOnly;

const submitButtonDisabled =
preferences?.type_to_confirm !== false &&
confirmationText !== linode?.label;
typeToConfirmPreference !== false && confirmationText !== linode?.label;

const type = types?.find((t) => t.id === linode?.type);

Expand Down Expand Up @@ -323,7 +327,7 @@ export const LinodeResize = (props: Props) => {
title="Confirm"
typographyStyle={{ marginBottom: 8 }}
value={confirmationText}
visible={preferences?.type_to_confirm}
visible={typeToConfirmPreference}
/>
</Box>
<Box display="flex" justifyContent="flex-end">
Expand Down
35 changes: 19 additions & 16 deletions packages/manager/src/queries/profile/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import { updateUserPreferences } from '@linode/api-v4';
import {
QueryClient,
useMutation,
useQuery,
useQueryClient,
} from '@tanstack/react-query';

import { ManagerPreferences } from 'src/types/ManagerPreferences';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';

import { queryPresets } from '../base';
import { profileQueries } from './profile';

import type { APIError } from '@linode/api-v4';
import type { QueryClient } from '@tanstack/react-query';
import type { ManagerPreferences } from 'src/types/ManagerPreferences';

export const usePreferences = (enabled = true) =>
useQuery<ManagerPreferences, APIError[]>({
export const usePreferences = <TData = ManagerPreferences>(
select?: (data: ManagerPreferences | undefined) => TData,
enabled = true
) =>
useQuery({
...profileQueries.preferences,
...queryPresets.oneTimeFetch,
enabled,
select,
});

export const useMutatePreferences = (replace = false) => {
const { data: preferences } = usePreferences(!replace);
const queryClient = useQueryClient();

return useMutation<
ManagerPreferences,
APIError[],
Partial<ManagerPreferences>
>({
mutationFn: (data) =>
updateUserPreferences({
...(!replace && preferences !== undefined ? preferences : {}),
...data,
}),
async mutationFn(data) {
if (replace) {
return updateUserPreferences(data);
} else {
const existingPreferences = await queryClient.ensureQueryData<ManagerPreferences>(
profileQueries.preferences
);
return updateUserPreferences({ ...existingPreferences, ...data });
}
},
onMutate: (data) => updatePreferenceData(data, replace, queryClient),
});
};
Expand Down
1 change: 1 addition & 0 deletions packages/manager/src/types/ManagerPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface DismissedNotification {
export interface ManagerPreferences extends UserPreferences {
avatarColor?: string;
backups_cta_dismissed?: boolean;
collapsedSideNavProductFamilies?: number[];
desktop_sidebar_open?: boolean;
dismissed_notifications?: Record<string, DismissedNotification>;
domains_group_by_tag?: boolean;
Expand Down
12 changes: 9 additions & 3 deletions packages/manager/src/utilities/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ export const getThemeFromPreferenceValue = (
};

export const useColorMode = () => {
// Make sure we are authenticated before we fetch preferences.
const isAuthenticated = !!useAuthentication().token;
const { data: preferences } = usePreferences(isAuthenticated);

const { data: themePreference } = usePreferences(
(preferences) => preferences?.theme,
// Make sure we are authenticated before we fetch preferences.
// If we don't, we get an authentication loop.
isAuthenticated
);

const isSystemInDarkMode = useMediaQuery('(prefers-color-scheme: dark)');

const colorMode = getThemeFromPreferenceValue(
preferences?.theme,
themePreference,
isSystemInDarkMode
);

Expand Down

0 comments on commit ad02ddb

Please sign in to comment.