~cytrogen/masto-fe

ref: 6d0b964e2bab79ee0cb448c7772883f57b2818ec masto-fe/app/javascript/flavours/glitch/store/middlewares/errors.ts -rw-r--r-- 681 bytes
6d0b964e — Sqx. Flann [feature] Query media description limit (#62) 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import type { AnyAction, Middleware } from 'redux';

import { showAlertForError } from 'flavours/glitch/actions/alerts';

import type { RootState } from '..';

const defaultFailSuffix = 'FAIL';

export const errorsMiddleware: Middleware<Record<string, never>, RootState> =
  ({ dispatch }) =>
  (next) =>
  (action: AnyAction & { skipAlert?: boolean; skipNotFound?: boolean }) => {
    if (action.type && !action.skipAlert) {
      const isFail = new RegExp(`${defaultFailSuffix}$`, 'g');

      if (typeof action.type === 'string' && action.type.match(isFail)) {
        dispatch(showAlertForError(action.error, action.skipNotFound));
      }
    }

    return next(action);
  };