diff --git a/app/soapbox/middleware/errors.js b/app/soapbox/middleware/errors.js deleted file mode 100644 index b5d9a36a3f..0000000000 Binary files a/app/soapbox/middleware/errors.js and /dev/null differ diff --git a/app/soapbox/middleware/errors.ts b/app/soapbox/middleware/errors.ts new file mode 100644 index 0000000000..b87c502498 --- /dev/null +++ b/app/soapbox/middleware/errors.ts @@ -0,0 +1,29 @@ +import { showAlertForError } from '../actions/alerts'; + +import type { AnyAction } from 'redux'; +import type { ThunkMiddleware } from 'redux-thunk'; + +/** Whether the action is considered a failure. */ +const isFailType = (type: string): boolean => type.endsWith('_FAIL'); + +/** Whether the action is a failure to fetch from browser storage. */ +const isRememberFailType = (type: string): boolean => type.endsWith('_REMEMBER_FAIL'); + +/** Whether the error contains an Axios response. */ +const hasResponse = (error: any): boolean => Boolean(error && error.response); + +/** Whether the error should be shown to the user. */ +const shouldShowError = ({ type, skipAlert, error }: AnyAction): boolean => { + return !skipAlert && hasResponse(error) && isFailType(type) && !isRememberFailType(type); +}; + +/** Middleware to display Redux errors to the user. */ +export default function errorsMiddleware(): ThunkMiddleware { + return ({ dispatch }) => next => action => { + if (shouldShowError(action)) { + dispatch(showAlertForError(action.error)); + } + + return next(action); + }; +} diff --git a/app/soapbox/middleware/sounds.js b/app/soapbox/middleware/sounds.ts similarity index 63% rename from app/soapbox/middleware/sounds.js rename to app/soapbox/middleware/sounds.ts index 6950e76188..94ba153131 100644 Binary files a/app/soapbox/middleware/sounds.js and b/app/soapbox/middleware/sounds.ts differ