Improve modals reducer types (#26610)

This commit is contained in:
Renaud Chaput 2023-09-22 16:41:50 +02:00 committed by GitHub
parent 39da3d86f8
commit b93ffb74bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 33 deletions

View file

@ -1,12 +1,14 @@
import { createAction } from '@reduxjs/toolkit'; import { createAction } from '@reduxjs/toolkit';
import type { ModalProps } from 'mastodon/reducers/modal';
import type { MODAL_COMPONENTS } from '../features/ui/components/modal_root'; import type { MODAL_COMPONENTS } from '../features/ui/components/modal_root';
export type ModalType = keyof typeof MODAL_COMPONENTS; export type ModalType = keyof typeof MODAL_COMPONENTS;
interface OpenModalPayload { interface OpenModalPayload {
modalType: ModalType; modalType: ModalType;
modalProps: unknown; modalProps: ModalProps;
} }
export const openModal = createAction<OpenModalPayload>('MODAL_OPEN'); export const openModal = createAction<OpenModalPayload>('MODAL_OPEN');

View file

@ -1,13 +1,13 @@
import { Record as ImmutableRecord, Stack } from 'immutable'; import { Record as ImmutableRecord, Stack } from 'immutable';
import type { PayloadAction } from '@reduxjs/toolkit'; import type { Reducer } from '@reduxjs/toolkit';
import { COMPOSE_UPLOAD_CHANGE_SUCCESS } from '../actions/compose'; import { COMPOSE_UPLOAD_CHANGE_SUCCESS } from '../actions/compose';
import type { ModalType } from '../actions/modal'; import type { ModalType } from '../actions/modal';
import { openModal, closeModal } from '../actions/modal'; import { openModal, closeModal } from '../actions/modal';
import { TIMELINE_DELETE } from '../actions/timelines'; import { TIMELINE_DELETE } from '../actions/timelines';
type ModalProps = Record<string, unknown>; export type ModalProps = Record<string, unknown>;
interface Modal { interface Modal {
modalType: ModalType; modalType: ModalType;
modalProps: ModalProps; modalProps: ModalProps;
@ -62,33 +62,22 @@ const pushModal = (
}); });
}; };
export function modalReducer( export const modalReducer: Reducer<State> = (state = initialState, action) => {
state: State = initialState, if (openModal.match(action))
action: PayloadAction<{ return pushModal(
modalType: ModalType; state,
ignoreFocus: boolean; action.payload.modalType,
modalProps: Record<string, unknown>; action.payload.modalProps,
}>, );
) { else if (closeModal.match(action)) return popModal(state, action.payload);
switch (action.type) { // TODO: type those actions
case openModal.type: else if (action.type === COMPOSE_UPLOAD_CHANGE_SUCCESS)
return pushModal( return popModal(state, { modalType: 'FOCAL_POINT', ignoreFocus: false });
state, else if (action.type === TIMELINE_DELETE)
action.payload.modalType, return state.update('stack', (stack) =>
action.payload.modalProps, stack.filterNot(
); (modal) => modal.get('modalProps').statusId === action.id,
case closeModal.type: ),
return popModal(state, action.payload); );
case COMPOSE_UPLOAD_CHANGE_SUCCESS: else return state;
return popModal(state, { modalType: 'FOCAL_POINT', ignoreFocus: false }); };
case TIMELINE_DELETE:
return state.update('stack', (stack) =>
stack.filterNot(
// @ts-expect-error TIMELINE_DELETE action is not typed yet.
(modal) => modal.get('modalProps').statusId === action.id,
),
);
default:
return state;
}
}