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

refactor: [M3-7960] - Update Notistack to 3.0.1 #10357

Merged
merged 8 commits into from
Apr 9, 2024
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
---

Update Notistack to 3.0.1 ([#10357](https://github.com/linode/manager/pull/10357))
12 changes: 10 additions & 2 deletions packages/manager/cypress/support/ui/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export const toast = {
message: string,
options?: ToastFindOptions | undefined
): void => {
cy.contains('[data-qa-toast]', message, options).should('be.visible');
cy.contains(
'[aria-describedby="notistack-snackbar"]',
message,
options
).should('be.visible');
},

/**
Expand All @@ -42,6 +46,10 @@ export const toast = {
message: string,
options?: ToastFindOptions | undefined
): Cypress.Chainable => {
return cy.contains('[data-qa-toast]', message, options);
return cy.contains(
'[aria-describedby="notistack-snackbar"]',
message,
options
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bnussman-akamai i went with using an existing attribute as a CY selector instead, removing the need to pass a non type-safe one

Copy link
Member

Choose a reason for hiding this comment

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

Nice! looks good

},
};
2 changes: 1 addition & 1 deletion packages/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"luxon": "3.4.4",
"markdown-it": "^12.3.2",
"md5": "^2.2.1",
"notistack": "^2.0.5",
"notistack": "^3.0.1",
"patch-package": "^7.0.0",
"qrcode.react": "^0.8.0",
"ramda": "~0.25.0",
Expand Down
44 changes: 27 additions & 17 deletions packages/manager/src/components/Snackbar/Snackbar.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { Theme } from '@mui/material/styles';
import { styled } from '@mui/material/styles';
import { SnackbarProvider, SnackbarProviderProps } from 'notistack';
import { MaterialDesignContent } from 'notistack';
import * as React from 'react';
import { makeStyles } from 'tss-react/mui';

import { CloseSnackbar } from './CloseSnackbar';

import type { Theme } from '@mui/material/styles';

const StyledMaterialDesignContent = styled(MaterialDesignContent)(
({ theme }: { theme: Theme }) => ({
'&.notistack-MuiContent-error': {
borderLeft: `6px solid ${theme.palette.error.dark}`,
},
'&.notistack-MuiContent-info': {
borderLeft: `6px solid ${theme.palette.primary.main}`,
},
'&.notistack-MuiContent-success': {
borderLeft: `6px solid ${theme.palette.success.main}`, // corrected to palette.success
},
'&.notistack-MuiContent-warning': {
borderLeft: `6px solid ${theme.palette.warning.dark}`,
},
})
);

const useStyles = makeStyles()((theme: Theme) => ({
error: {
borderLeft: `6px solid ${theme.palette.error.dark}`,
},
info: {
borderLeft: `6px solid ${theme.palette.primary.main}`,
},
root: {
'& div': {
backgroundColor: `${theme.bg.white} !important`,
Expand All @@ -32,12 +46,6 @@ const useStyles = makeStyles()((theme: Theme) => ({
},
},
},
success: {
borderLeft: `6px solid ${theme.color.green}`,
},
warning: {
borderLeft: `6px solid ${theme.palette.warning.dark}`,
},
}));

export const Snackbar = (props: SnackbarProviderProps) => {
Expand All @@ -59,6 +67,12 @@ export const Snackbar = (props: SnackbarProviderProps) => {
<SnackbarProvider
ref={notistackRef}
{...rest}
Components={{
error: StyledMaterialDesignContent,
info: StyledMaterialDesignContent,
success: StyledMaterialDesignContent,
warning: StyledMaterialDesignContent,
}}
action={(key) => (
<CloseSnackbar
onClick={onClickDismiss(key)}
Expand All @@ -67,10 +81,6 @@ export const Snackbar = (props: SnackbarProviderProps) => {
)}
classes={{
root: classes.root,
variantError: classes.error,
variantInfo: classes.info,
variantSuccess: classes.success,
variantWarning: classes.warning,
}}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { APIError } from '@linode/api-v4/lib/types';
import { vpcsValidateIP } from '@linode/validation';
import { CreateLinodeSchema } from '@linode/validation/lib/linodes.schema';
import Grid from '@mui/material/Unstable_Grid2';
import { WithSnackbarProps, withSnackbar } from 'notistack';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

withSnackbar HOC has been deprecated in favor of direct imports.

In short:

  • direct imports for class components
  • useSnackbar for functional components

CC @bnussman-akamai for Linode Create v2, tho I guess you handle this already the right way, or will

import { enqueueSnackbar } from 'notistack';
import * as React from 'react';
import { connect } from 'react-redux';
import { RouteComponentProps } from 'react-router-dom';
Expand Down Expand Up @@ -63,11 +63,11 @@ import {
} from 'src/utilities/analytics';
import { getAPIErrorOrDefault } from 'src/utilities/errorUtils';
import { ExtendedType, extendType } from 'src/utilities/extendType';
import { isEURegion } from 'src/utilities/formatRegion';
import {
getGDPRDetails,
getSelectedRegionGroup,
} from 'src/utilities/formatRegion';
import { isEURegion } from 'src/utilities/formatRegion';
import { ExtendedIP } from 'src/utilities/ipUtils';
import { UNKNOWN_PRICE } from 'src/utilities/pricing/constants';
import { getLinodeRegionPrice } from 'src/utilities/pricing/linodes';
Expand Down Expand Up @@ -127,8 +127,7 @@ interface State {
vpcIPv4AddressOfLinode?: string;
}

type CombinedProps = WithSnackbarProps &
CreateType &
type CombinedProps = CreateType &
WithImagesProps &
WithTypesProps &
WithLinodesProps &
Expand Down Expand Up @@ -896,12 +895,9 @@ class LinodeCreateContainer extends React.PureComponent<CombinedProps, State> {
);

/** show toast */
this.props.enqueueSnackbar(
`Your Linode ${response.label} is being created.`,
{
variant: 'success',
}
);
enqueueSnackbar(`Your Linode ${response.label} is being created.`, {
variant: 'success',
});

/** reset the Events polling */
this.props.checkForNewEvents();
Expand Down Expand Up @@ -977,7 +973,6 @@ export default recompose<CombinedProps, {}>(
withRegions,
withTypes,
connected,
withSnackbar,
withFeatureFlags,
withProfile,
withAgreements,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
useAllPlacementGroupsQuery,
useAssignLinodesToPlacementGroup,
} from 'src/queries/placementGroups';
import { getErrorStringOrDefault } from 'src/utilities/errorUtils';

import { LinodeSelect } from '../Linodes/LinodeSelect/LinodeSelect';
import {
Expand Down Expand Up @@ -126,13 +127,13 @@ export const PlacementGroupsAssignLinodesDrawer = (
variant: 'success',
});
handleDrawerClose();
} catch (error) {
setGeneralError(
error?.[0]?.reason
? error[0].reason
: 'An error occurred while adding the Linode to the group'
} catch (errorResponse) {
const error = getErrorStringOrDefault(
errorResponse,
'An error occurred while adding the Linode to the group'
);
enqueueSnackbar(error[0]?.reason, { variant: 'error' });
setGeneralError(error);
enqueueSnackbar(error, { variant: 'error' });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only test to complain from running the suite

Copy link
Member

Choose a reason for hiding this comment

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

Is it safe to pass an APIError[] to enqueueSnackbar? I'm worried this could cause a crash.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it could be an issue, the first parameter for enqueueSnackbar has a type of SnackbarMessage

Screenshot 2024-04-09 at 11 58 46β€―AM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely not safe 🀦

fixed

Copy link
Contributor Author

@abailly-akamai abailly-akamai Apr 9, 2024

Choose a reason for hiding this comment

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

actually it takes React Node as type, which is why is accepted an array. Need to figure out what is does with it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, so yeah, since catch error is any it'll take it.
That being said I tested it and it won't throw with ApiError (just an empty toast, good to know I guess), but it isn't type safe

enqueueSnackbar([{ field: 'label', reason: 'Label is too long' }], {
  variant,
});

}
};

Expand Down
21 changes: 7 additions & 14 deletions packages/manager/src/features/Users/UserPermissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { APIError } from '@linode/api-v4/lib/types';
import { Paper } from '@mui/material';
import Grid from '@mui/material/Unstable_Grid2';
import { QueryClient } from '@tanstack/react-query';
import { WithSnackbarProps, withSnackbar } from 'notistack';
import { enqueueSnackbar } from 'notistack';
import { compose, flatten, lensPath, omit, set } from 'ramda';
import * as React from 'react';
import { compose as recompose } from 'recompose';
Expand Down Expand Up @@ -89,10 +89,7 @@ interface State {
userType: null | string;
}

type CombinedProps = Props &
WithSnackbarProps &
WithQueryClientProps &
WithFeatureFlagProps;
type CombinedProps = Props & WithQueryClientProps & WithFeatureFlagProps;

class UserPermissions extends React.Component<CombinedProps, State> {
componentDidMount() {
Expand Down Expand Up @@ -308,7 +305,7 @@ class UserPermissions extends React.Component<CombinedProps, State> {
);
// unconditionally sets this.state.loadingGrants to false
this.getUserGrants();
this.props.enqueueSnackbar('User permissions successfully saved.', {
enqueueSnackbar('User permissions successfully saved.', {
variant: 'success',
});
})
Expand Down Expand Up @@ -720,12 +717,9 @@ class UserPermissions extends React.Component<CombinedProps, State> {
const { tabs } = this.getTabInformation(grantsResponse);
this.setState({ isSavingGlobal: false, tabs });

this.props.enqueueSnackbar(
'General user permissions successfully saved.',
{
variant: 'success',
}
);
enqueueSnackbar('General user permissions successfully saved.', {
variant: 'success',
});

// Update the user's grants directly in the cache
this.props.queryClient.setQueriesData<Grants>(
Expand Down Expand Up @@ -782,7 +776,7 @@ class UserPermissions extends React.Component<CombinedProps, State> {
if (updateFns.length) {
this.setState((compose as any)(...updateFns));
}
this.props.enqueueSnackbar(
enqueueSnackbar(
'Entity-specific user permissions successfully saved.',
{
variant: 'success',
Expand Down Expand Up @@ -833,7 +827,6 @@ class UserPermissions extends React.Component<CombinedProps, State> {
}

export default recompose<CombinedProps, Props>(
withSnackbar,
withQueryClient,
withFeatureFlags
)(UserPermissions);
1 change: 0 additions & 1 deletion packages/manager/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const Main = () => {
<Snackbar
anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }}
autoHideDuration={4000}
data-qa-toast
hideIconVariant={true}
maxSnack={3}
>
Expand Down
15 changes: 10 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7502,6 +7502,11 @@ globby@^11.0.1, globby@^11.0.2, globby@^11.0.3, globby@^11.1.0:
merge2 "^1.4.1"
slash "^3.0.0"

goober@^2.0.33:
version "2.1.14"
resolved "https://registry.yarnpkg.com/goober/-/goober-2.1.14.tgz#4a5c94fc34dc086a8e6035360ae1800005135acd"
integrity sha512-4UpC0NdGyAFqLNPnhCT2iHpza2q+RAY3GV85a/mRPdzyPQMsj0KmMMuetdIkzWRbJ+Hgau1EZztq8ImmiMGhsg==

gopd@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.0.1.tgz#29ff76de69dac7489b7c0918a5788e56477c332c"
Expand Down Expand Up @@ -9777,13 +9782,13 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==

notistack@^2.0.5:
version "2.0.8"
resolved "https://registry.yarnpkg.com/notistack/-/notistack-2.0.8.tgz#78cdf34c64e311bf1d1d71c2123396bcdea5e95b"
integrity sha512-/IY14wkFp5qjPgKNvAdfL5Jp6q90+MjgKTPh4c81r/lW70KeuX6b9pE/4f8L4FG31cNudbN9siiFS5ql1aSLRw==
notistack@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/notistack/-/notistack-3.0.1.tgz#daf59888ab7e2c30a1fa8f71f9cba2978773236e"
integrity sha512-ntVZXXgSQH5WYfyU+3HfcXuKaapzAJ8fBLQ/G618rn3yvSzEbnOB8ZSOwhX+dAORy/lw+GC2N061JA0+gYWTVA==
dependencies:
clsx "^1.1.0"
hoist-non-react-statics "^3.3.0"
goober "^2.0.33"

npm-run-all@^4.1.5:
version "4.1.5"
Expand Down
Loading