This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: cleanup notifications system (#3926)
* refactor: cleanup notifications system * fix: disable lint rule * fix: notification styles * fix: update notifications * fix: update notification route
- Loading branch information
Showing
39 changed files
with
444 additions
and
697 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,31 @@ | ||
import { ADDRESS_BOOK_ACTIONS } from 'src/logic/addressBook/store/actions' | ||
import { enhanceSnackbarForAction, getNotificationsFromTxType } from 'src/logic/notifications' | ||
import enqueueSnackbar from 'src/logic/notifications/store/actions/enqueueSnackbar' | ||
import { TX_NOTIFICATION_TYPES } from 'src/logic/safe/transactions' | ||
import { showNotification } from 'src/logic/notifications/store/notifications' | ||
import { NOTIFICATIONS } from 'src/logic/notifications' | ||
import { AppReduxState } from 'src/store' | ||
import { ThunkMiddleware } from 'redux-thunk' | ||
|
||
const watchedActions = Object.values(ADDRESS_BOOK_ACTIONS) | ||
export const addressBookMiddleware: ThunkMiddleware<AppReduxState> = | ||
({ dispatch }) => | ||
(next) => | ||
(action) => { | ||
const handledAction = next(action) | ||
|
||
export const addressBookMiddleware = (store) => (next) => async (action) => { | ||
const handledAction = next(action) | ||
if (watchedActions.includes(action.type)) { | ||
const { dispatch } = store | ||
switch (action.type) { | ||
case ADDRESS_BOOK_ACTIONS.ADD_OR_UPDATE: { | ||
const { shouldAvoidUpdatesNotifications } = action.payload | ||
if (!shouldAvoidUpdatesNotifications) { | ||
const notification = getNotificationsFromTxType(TX_NOTIFICATION_TYPES.ADDRESS_BOOK_NEW_ENTRY) | ||
dispatch(enqueueSnackbar(enhanceSnackbarForAction(notification.afterExecution.noMoreConfirmationsNeeded))) | ||
} | ||
dispatch(showNotification(NOTIFICATIONS.ADDRESS_BOOK_NEW_ENTRY_SUCCESS)) | ||
break | ||
} | ||
case ADDRESS_BOOK_ACTIONS.REMOVE: { | ||
const notification = getNotificationsFromTxType(TX_NOTIFICATION_TYPES.ADDRESS_BOOK_DELETE_ENTRY) | ||
dispatch(enqueueSnackbar(enhanceSnackbarForAction(notification.afterExecution.noMoreConfirmationsNeeded))) | ||
dispatch(showNotification(NOTIFICATIONS.ADDRESS_BOOK_DELETE_ENTRY_SUCCESS)) | ||
break | ||
} | ||
case ADDRESS_BOOK_ACTIONS.IMPORT: { | ||
const notification = getNotificationsFromTxType(TX_NOTIFICATION_TYPES.ADDRESS_BOOK_IMPORT_ENTRIES) | ||
dispatch(enqueueSnackbar(enhanceSnackbarForAction(notification.afterExecution.noMoreConfirmationsNeeded))) | ||
dispatch(showNotification(NOTIFICATIONS.ADDRESS_BOOK_IMPORT_ENTRIES_SUCCESS)) | ||
break | ||
} | ||
default: | ||
break | ||
} | ||
} | ||
|
||
return handledAction | ||
} | ||
return handledAction | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useEffect } from 'react' | ||
import { useSelector, useDispatch } from 'react-redux' | ||
import { SnackbarKey, useSnackbar } from 'notistack' | ||
import { IconButton } from '@material-ui/core' | ||
import CloseIcon from '@material-ui/icons/Close' | ||
|
||
import { selectNotifications, closeNotification } from 'src/logic/notifications/store/notifications' | ||
|
||
let onScreenKeys: SnackbarKey[] = [] | ||
|
||
const useNotifier = (): void => { | ||
const notifications = useSelector(selectNotifications) | ||
const { closeSnackbar, enqueueSnackbar } = useSnackbar() | ||
const dispatch = useDispatch() | ||
|
||
useEffect(() => { | ||
for (const notification of notifications) { | ||
// Unspecified keys are automatically generated in `enqueueSnackbar` thunk | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const key = notification.options!.key! | ||
|
||
if (notification.dismissed) { | ||
closeSnackbar(key) | ||
continue | ||
} | ||
|
||
if (onScreenKeys.includes(key)) { | ||
continue | ||
} | ||
|
||
enqueueSnackbar(notification.message, { | ||
...notification.options, | ||
onExited: () => { | ||
// Cleanup store/cache when notification has unmounted | ||
dispatch(closeNotification({ key })) | ||
onScreenKeys = onScreenKeys.filter((onScreenKey) => onScreenKey !== key) | ||
}, | ||
action: ( | ||
<IconButton onClick={() => dispatch(closeNotification({ key }))}> | ||
<CloseIcon /> | ||
</IconButton> | ||
), | ||
}) | ||
|
||
onScreenKeys = [...onScreenKeys, key] | ||
} | ||
}, [notifications, closeSnackbar, enqueueSnackbar, dispatch]) | ||
} | ||
|
||
export default useNotifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './notificationTypes' | ||
export * from './notificationBuilder' | ||
export * from './txNotificationBuilder' |
Oops, something went wrong.