2021-09-09 16:29:01 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
|
|
|
import { omit } from 'lodash';
|
|
|
|
import * as log from '../../logging/log';
|
|
|
|
import * as Errors from '../../types/errors';
|
|
|
|
import { replaceIndex } from '../../util/replaceIndex';
|
2021-09-17 22:24:21 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2021-09-09 16:29:01 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2021-09-09 23:47:30 +00:00
|
|
|
import { DEFAULT_PREFERRED_REACTION_EMOJI_SHORT_NAMES } from '../../reactions/constants';
|
2021-09-15 18:59:51 +00:00
|
|
|
import { getPreferredReactionEmoji } from '../../reactions/preferredReactionEmoji';
|
2021-09-09 23:47:30 +00:00
|
|
|
import { getEmojiSkinTone } from '../selectors/items';
|
|
|
|
import { convertShortName } from '../../components/emoji/lib';
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
|
|
|
export type PreferredReactionsStateType = {
|
|
|
|
customizePreferredReactionsModal?: {
|
|
|
|
draftPreferredReactions: Array<string>;
|
|
|
|
originalPreferredReactions: Array<string>;
|
|
|
|
selectedDraftEmojiIndex: undefined | number;
|
|
|
|
} & (
|
|
|
|
| { isSaving: true; hadSaveError: false }
|
|
|
|
| { isSaving: false; hadSaveError: boolean }
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
const CANCEL_CUSTOMIZE_PREFERRED_REACTIONS_MODAL =
|
|
|
|
'preferredReactions/CANCEL_CUSTOMIZE_PREFERRED_REACTIONS_MODAL';
|
|
|
|
const DESELECT_DRAFT_EMOJI = 'preferredReactions/DESELECT_DRAFT_EMOJI';
|
|
|
|
const OPEN_CUSTOMIZE_PREFERRED_REACTIONS_MODAL =
|
|
|
|
'preferredReactions/OPEN_CUSTOMIZE_PREFERRED_REACTIONS_MODAL';
|
|
|
|
const REPLACE_SELECTED_DRAFT_EMOJI =
|
|
|
|
'preferredReactions/REPLACE_SELECTED_DRAFT_EMOJI';
|
|
|
|
const RESET_DRAFT_EMOJI = 'preferredReactions/RESET_DRAFT_EMOJI';
|
|
|
|
const SAVE_PREFERRED_REACTIONS_FULFILLED =
|
|
|
|
'preferredReactions/SAVE_PREFERRED_REACTIONS_FULFILLED';
|
|
|
|
const SAVE_PREFERRED_REACTIONS_PENDING =
|
|
|
|
'preferredReactions/SAVE_PREFERRED_REACTIONS_PENDING';
|
|
|
|
const SAVE_PREFERRED_REACTIONS_REJECTED =
|
|
|
|
'preferredReactions/SAVE_PREFERRED_REACTIONS_REJECTED';
|
|
|
|
const SELECT_DRAFT_EMOJI_TO_BE_REPLACED =
|
|
|
|
'preferredReactions/SELECT_DRAFT_EMOJI_TO_BE_REPLACED';
|
|
|
|
|
|
|
|
type CancelCustomizePreferredReactionsModalActionType = {
|
|
|
|
type: typeof CANCEL_CUSTOMIZE_PREFERRED_REACTIONS_MODAL;
|
|
|
|
};
|
|
|
|
|
|
|
|
type DeselectDraftEmojiActionType = { type: typeof DESELECT_DRAFT_EMOJI };
|
|
|
|
|
|
|
|
type OpenCustomizePreferredReactionsModalActionType = {
|
|
|
|
type: typeof OPEN_CUSTOMIZE_PREFERRED_REACTIONS_MODAL;
|
|
|
|
payload: {
|
|
|
|
originalPreferredReactions: Array<string>;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
type ReplaceSelectedDraftEmojiActionType = {
|
|
|
|
type: typeof REPLACE_SELECTED_DRAFT_EMOJI;
|
|
|
|
payload: string;
|
|
|
|
};
|
|
|
|
|
2021-09-09 23:47:30 +00:00
|
|
|
type ResetDraftEmojiActionType = {
|
|
|
|
type: typeof RESET_DRAFT_EMOJI;
|
|
|
|
payload: { skinTone: number };
|
|
|
|
};
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
type SavePreferredReactionsFulfilledActionType = {
|
|
|
|
type: typeof SAVE_PREFERRED_REACTIONS_FULFILLED;
|
|
|
|
};
|
|
|
|
|
|
|
|
type SavePreferredReactionsPendingActionType = {
|
|
|
|
type: typeof SAVE_PREFERRED_REACTIONS_PENDING;
|
|
|
|
};
|
|
|
|
|
|
|
|
type SavePreferredReactionsRejectedActionType = {
|
|
|
|
type: typeof SAVE_PREFERRED_REACTIONS_REJECTED;
|
|
|
|
};
|
|
|
|
|
|
|
|
type SelectDraftEmojiToBeReplacedActionType = {
|
|
|
|
type: typeof SELECT_DRAFT_EMOJI_TO_BE_REPLACED;
|
|
|
|
payload: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Action creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
cancelCustomizePreferredReactionsModal,
|
|
|
|
deselectDraftEmoji,
|
|
|
|
openCustomizePreferredReactionsModal,
|
|
|
|
replaceSelectedDraftEmoji,
|
|
|
|
resetDraftEmoji,
|
|
|
|
savePreferredReactions,
|
|
|
|
selectDraftEmojiToBeReplaced,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useActions = (): typeof actions => useBoundActions(actions);
|
|
|
|
|
|
|
|
function cancelCustomizePreferredReactionsModal(): CancelCustomizePreferredReactionsModalActionType {
|
|
|
|
return { type: CANCEL_CUSTOMIZE_PREFERRED_REACTIONS_MODAL };
|
|
|
|
}
|
|
|
|
|
|
|
|
function deselectDraftEmoji(): DeselectDraftEmojiActionType {
|
|
|
|
return { type: DESELECT_DRAFT_EMOJI };
|
|
|
|
}
|
|
|
|
|
|
|
|
function openCustomizePreferredReactionsModal(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
OpenCustomizePreferredReactionsModalActionType
|
|
|
|
> {
|
|
|
|
return (dispatch, getState) => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const state = getState();
|
2021-09-09 16:29:01 +00:00
|
|
|
const originalPreferredReactions = getPreferredReactionEmoji(
|
2021-09-09 23:47:30 +00:00
|
|
|
getState().items.preferredReactionEmoji,
|
|
|
|
getEmojiSkinTone(state)
|
2021-09-09 16:29:01 +00:00
|
|
|
);
|
|
|
|
dispatch({
|
|
|
|
type: OPEN_CUSTOMIZE_PREFERRED_REACTIONS_MODAL,
|
|
|
|
payload: { originalPreferredReactions },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceSelectedDraftEmoji(
|
|
|
|
newEmoji: string
|
|
|
|
): ReplaceSelectedDraftEmojiActionType {
|
|
|
|
return {
|
|
|
|
type: REPLACE_SELECTED_DRAFT_EMOJI,
|
|
|
|
payload: newEmoji,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:47:30 +00:00
|
|
|
function resetDraftEmoji(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
ResetDraftEmojiActionType
|
|
|
|
> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const skinTone = getEmojiSkinTone(getState());
|
|
|
|
dispatch({ type: RESET_DRAFT_EMOJI, payload: { skinTone } });
|
|
|
|
};
|
2021-09-09 16:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function savePreferredReactions(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
| SavePreferredReactionsFulfilledActionType
|
|
|
|
| SavePreferredReactionsPendingActionType
|
|
|
|
| SavePreferredReactionsRejectedActionType
|
|
|
|
> {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const { draftPreferredReactions } =
|
|
|
|
getState().preferredReactions.customizePreferredReactionsModal || {};
|
|
|
|
if (!draftPreferredReactions) {
|
|
|
|
log.error(
|
|
|
|
"savePreferredReactions won't work because the modal is not open"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-15 18:59:51 +00:00
|
|
|
let succeeded = false;
|
|
|
|
|
2021-09-09 16:29:01 +00:00
|
|
|
dispatch({ type: SAVE_PREFERRED_REACTIONS_PENDING });
|
|
|
|
try {
|
|
|
|
await window.storage.put(
|
|
|
|
'preferredReactionEmoji',
|
|
|
|
draftPreferredReactions
|
|
|
|
);
|
2021-09-15 18:59:51 +00:00
|
|
|
succeeded = true;
|
2021-09-09 16:29:01 +00:00
|
|
|
} catch (err: unknown) {
|
|
|
|
log.warn(Errors.toLogFormat(err));
|
2021-09-15 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (succeeded) {
|
|
|
|
dispatch({ type: SAVE_PREFERRED_REACTIONS_FULFILLED });
|
|
|
|
window.ConversationController.getOurConversationOrThrow().captureChange(
|
|
|
|
'preferredReactionEmoji'
|
|
|
|
);
|
|
|
|
} else {
|
2021-09-09 16:29:01 +00:00
|
|
|
dispatch({ type: SAVE_PREFERRED_REACTIONS_REJECTED });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectDraftEmojiToBeReplaced(
|
|
|
|
index: number
|
|
|
|
): SelectDraftEmojiToBeReplacedActionType {
|
|
|
|
return {
|
|
|
|
type: SELECT_DRAFT_EMOJI_TO_BE_REPLACED,
|
|
|
|
payload: index,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
export function getInitialState(): PreferredReactionsStateType {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
|
|
|
state: Readonly<PreferredReactionsStateType> = getInitialState(),
|
|
|
|
action: Readonly<
|
|
|
|
| CancelCustomizePreferredReactionsModalActionType
|
|
|
|
| DeselectDraftEmojiActionType
|
|
|
|
| OpenCustomizePreferredReactionsModalActionType
|
|
|
|
| ReplaceSelectedDraftEmojiActionType
|
|
|
|
| ResetDraftEmojiActionType
|
|
|
|
| SavePreferredReactionsFulfilledActionType
|
|
|
|
| SavePreferredReactionsPendingActionType
|
|
|
|
| SavePreferredReactionsRejectedActionType
|
|
|
|
| SelectDraftEmojiToBeReplacedActionType
|
|
|
|
>
|
|
|
|
): PreferredReactionsStateType {
|
|
|
|
switch (action.type) {
|
|
|
|
case CANCEL_CUSTOMIZE_PREFERRED_REACTIONS_MODAL:
|
|
|
|
case SAVE_PREFERRED_REACTIONS_FULFILLED:
|
|
|
|
return omit(state, ['customizePreferredReactionsModal']);
|
|
|
|
case DESELECT_DRAFT_EMOJI:
|
|
|
|
if (!state.customizePreferredReactionsModal) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...state.customizePreferredReactionsModal,
|
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
case OPEN_CUSTOMIZE_PREFERRED_REACTIONS_MODAL: {
|
|
|
|
const { originalPreferredReactions } = action.payload;
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
draftPreferredReactions: originalPreferredReactions,
|
|
|
|
originalPreferredReactions,
|
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
isSaving: false,
|
|
|
|
hadSaveError: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case REPLACE_SELECTED_DRAFT_EMOJI: {
|
|
|
|
const newEmoji = action.payload;
|
|
|
|
|
|
|
|
const { customizePreferredReactionsModal } = state;
|
|
|
|
if (!customizePreferredReactionsModal) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const { draftPreferredReactions, selectedDraftEmojiIndex } =
|
|
|
|
customizePreferredReactionsModal;
|
2021-09-13 17:04:45 +00:00
|
|
|
if (selectedDraftEmojiIndex === undefined) {
|
2021-09-09 16:29:01 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...customizePreferredReactionsModal,
|
|
|
|
draftPreferredReactions: replaceIndex(
|
|
|
|
draftPreferredReactions,
|
|
|
|
selectedDraftEmojiIndex,
|
|
|
|
newEmoji
|
|
|
|
),
|
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-09-09 23:47:30 +00:00
|
|
|
case RESET_DRAFT_EMOJI: {
|
|
|
|
const { skinTone } = action.payload;
|
2021-09-09 16:29:01 +00:00
|
|
|
if (!state.customizePreferredReactionsModal) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...state.customizePreferredReactionsModal,
|
2021-11-11 22:43:05 +00:00
|
|
|
draftPreferredReactions:
|
|
|
|
DEFAULT_PREFERRED_REACTION_EMOJI_SHORT_NAMES.map(shortName =>
|
|
|
|
convertShortName(shortName, skinTone)
|
|
|
|
),
|
2021-09-09 16:29:01 +00:00
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
},
|
|
|
|
};
|
2021-09-09 23:47:30 +00:00
|
|
|
}
|
2021-09-09 16:29:01 +00:00
|
|
|
case SAVE_PREFERRED_REACTIONS_PENDING:
|
|
|
|
if (!state.customizePreferredReactionsModal) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...state.customizePreferredReactionsModal,
|
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
isSaving: true,
|
|
|
|
hadSaveError: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
case SAVE_PREFERRED_REACTIONS_REJECTED:
|
|
|
|
if (!state.customizePreferredReactionsModal) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...state.customizePreferredReactionsModal,
|
|
|
|
isSaving: false,
|
|
|
|
hadSaveError: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
case SELECT_DRAFT_EMOJI_TO_BE_REPLACED: {
|
|
|
|
const index = action.payload;
|
|
|
|
if (
|
|
|
|
!state.customizePreferredReactionsModal ||
|
|
|
|
!(
|
|
|
|
index in
|
|
|
|
state.customizePreferredReactionsModal.draftPreferredReactions
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...state.customizePreferredReactionsModal,
|
|
|
|
selectedDraftEmojiIndex: index,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|