2023-05-25 22:42:37 +09:00
|
|
|
import { Record as ImmutableRecord, Stack } from 'immutable';
|
|
|
|
|
|
|
|
import type { PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
|
|
|
|
import { COMPOSE_UPLOAD_CHANGE_SUCCESS } from '../actions/compose';
|
|
|
|
import type { ModalType } from '../actions/modal';
|
|
|
|
import { openModal, closeModal } from '../actions/modal';
|
|
|
|
import { TIMELINE_DELETE } from '../actions/timelines';
|
|
|
|
|
|
|
|
type ModalProps = Record<string, unknown>;
|
|
|
|
interface Modal {
|
|
|
|
modalType: ModalType;
|
|
|
|
modalProps: ModalProps;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Modal = ImmutableRecord<Modal>({
|
|
|
|
modalType: 'ACTIONS',
|
|
|
|
modalProps: ImmutableRecord({})(),
|
|
|
|
});
|
|
|
|
|
|
|
|
interface ModalState {
|
|
|
|
ignoreFocus: boolean;
|
|
|
|
stack: Stack<ImmutableRecord<Modal>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState = ImmutableRecord<ModalState>({
|
|
|
|
ignoreFocus: false,
|
|
|
|
stack: Stack(),
|
|
|
|
})();
|
|
|
|
type State = typeof initialState;
|
|
|
|
|
|
|
|
interface PopModalOption {
|
|
|
|
modalType: ModalType | undefined;
|
|
|
|
ignoreFocus: boolean;
|
|
|
|
}
|
|
|
|
const popModal = (
|
|
|
|
state: State,
|
2023-07-13 18:26:45 +09:00
|
|
|
{ modalType, ignoreFocus }: PopModalOption,
|
2023-05-25 22:42:37 +09:00
|
|
|
): State => {
|
|
|
|
if (
|
|
|
|
modalType === undefined ||
|
|
|
|
modalType === state.get('stack').get(0)?.get('modalType')
|
|
|
|
) {
|
|
|
|
return state
|
|
|
|
.set('ignoreFocus', !!ignoreFocus)
|
|
|
|
.update('stack', (stack) => stack.shift());
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const pushModal = (
|
|
|
|
state: State,
|
|
|
|
modalType: ModalType,
|
2023-07-13 18:26:45 +09:00
|
|
|
modalProps: ModalProps,
|
2023-05-25 22:42:37 +09:00
|
|
|
): State => {
|
|
|
|
return state.withMutations((record) => {
|
|
|
|
record.set('ignoreFocus', false);
|
|
|
|
record.update('stack', (stack) =>
|
2023-07-13 18:26:45 +09:00
|
|
|
stack.unshift(Modal({ modalType, modalProps })),
|
2023-05-25 22:42:37 +09:00
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export function modalReducer(
|
|
|
|
state: State = initialState,
|
|
|
|
action: PayloadAction<{
|
|
|
|
modalType: ModalType;
|
|
|
|
ignoreFocus: boolean;
|
|
|
|
modalProps: Record<string, unknown>;
|
2023-07-13 18:26:45 +09:00
|
|
|
}>,
|
2023-05-25 22:42:37 +09:00
|
|
|
) {
|
|
|
|
switch (action.type) {
|
|
|
|
case openModal.type:
|
|
|
|
return pushModal(
|
|
|
|
state,
|
|
|
|
action.payload.modalType,
|
2023-07-13 18:26:45 +09:00
|
|
|
action.payload.modalProps,
|
2023-05-25 22:42:37 +09:00
|
|
|
);
|
|
|
|
case closeModal.type:
|
|
|
|
return popModal(state, action.payload);
|
|
|
|
case COMPOSE_UPLOAD_CHANGE_SUCCESS:
|
|
|
|
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.
|
2023-07-13 18:26:45 +09:00
|
|
|
(modal) => modal.get('modalProps').statusId === action.id,
|
|
|
|
),
|
2023-05-25 22:42:37 +09:00
|
|
|
);
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|