1
0
mirror of https://github.com/funamitech/mastodon synced 2024-12-25 12:08:29 +09:00
YuruToot/app/javascript/mastodon/store/middlewares/errors.ts

36 lines
920 B
TypeScript
Raw Normal View History

2024-01-08 19:57:40 +09:00
import { isAction } from '@reduxjs/toolkit';
import type { Action, Middleware } from '@reduxjs/toolkit';
import type { RootState } from '..';
import { showAlertForError } from '../../actions/alerts';
const defaultFailSuffix = 'FAIL';
2024-01-08 19:57:40 +09:00
const isFailedAction = new RegExp(`${defaultFailSuffix}$`, 'g');
2024-01-08 19:57:40 +09:00
interface ActionWithMaybeAlertParams extends Action {
skipAlert?: boolean;
skipNotFound?: boolean;
error?: unknown;
}
function isActionWithmaybeAlertParams(
action: unknown,
): action is ActionWithMaybeAlertParams {
return isAction(action);
}
export const errorsMiddleware: Middleware<Record<string, never>, RootState> =
({ dispatch }) =>
(next) =>
2024-01-08 19:57:40 +09:00
(action) => {
if (
isActionWithmaybeAlertParams(action) &&
!action.skipAlert &&
action.type.match(isFailedAction)
) {
dispatch(showAlertForError(action.error, action.skipNotFound));
}
return next(action);
};