1
0
mirror of https://github.com/funamitech/mastodon synced 2024-12-16 23:59:11 +09:00
YuruToot/app/javascript/mastodon/store/middlewares/errors.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import {
isAction,
isAsyncThunkAction,
isRejectedWithValue,
} from '@reduxjs/toolkit';
2024-01-08 19:57:40 +09:00
import type { Action, Middleware } from '@reduxjs/toolkit';
import type { RootState } from '..';
import { showAlertForError } from '../../actions/alerts';
import type { AsyncThunkRejectValue } from '../typed_functions';
const defaultFailSuffix = 'FAIL';
2024-01-08 19:57:40 +09:00
const isFailedAction = new RegExp(`${defaultFailSuffix}$`, 'g');
interface ActionWithMaybeAlertParams extends Action, AsyncThunkRejectValue {}
interface RejectedAction extends Action {
payload: AsyncThunkRejectValue;
}
function isRejectedActionWithPayload(
action: unknown,
): action is RejectedAction {
return isAsyncThunkAction(action) && isRejectedWithValue(action);
2024-01-08 19:57:40 +09:00
}
function isActionWithmaybeAlertParams(
action: unknown,
): action is ActionWithMaybeAlertParams {
return isAction(action);
}
// eslint-disable-next-line @typescript-eslint/no-empty-object-type -- we need to use `{}` here to ensure the dispatch types can be merged
2024-04-02 18:56:03 +09:00
export const errorsMiddleware: Middleware<{}, RootState> =
({ dispatch }) =>
(next) =>
2024-01-08 19:57:40 +09:00
(action) => {
if (isRejectedActionWithPayload(action) && !action.payload.skipAlert) {
dispatch(
showAlertForError(action.payload.error, action.payload.skipNotFound),
);
} else if (
2024-01-08 19:57:40 +09:00
isActionWithmaybeAlertParams(action) &&
!action.skipAlert &&
action.type.match(isFailedAction)
) {
dispatch(showAlertForError(action.error, action.skipNotFound));
}
return next(action);
};