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';
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2021-09-09 16:29:01 +00:00
|
|
|
import * as log from '../../logging/log';
|
|
|
|
import * as Errors from '../../types/errors';
|
|
|
|
import { replaceIndex } from '../../util/replaceIndex';
|
2022-12-09 17:35:34 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
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
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type PreferredReactionsStateType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
customizePreferredReactionsModal?: {
|
|
|
|
draftPreferredReactions: Array<string>;
|
|
|
|
originalPreferredReactions: Array<string>;
|
|
|
|
selectedDraftEmojiIndex: undefined | number;
|
|
|
|
} & (
|
|
|
|
| { isSaving: true; hadSaveError: false }
|
|
|
|
| { isSaving: false; hadSaveError: boolean }
|
|
|
|
);
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
// 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';
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type CancelCustomizePreferredReactionsModalActionType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
type: typeof CANCEL_CUSTOMIZE_PREFERRED_REACTIONS_MODAL;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type DeselectDraftEmojiActionType = ReadonlyDeep<{
|
|
|
|
type: typeof DESELECT_DRAFT_EMOJI;
|
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type OpenCustomizePreferredReactionsModalActionType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
type: typeof OPEN_CUSTOMIZE_PREFERRED_REACTIONS_MODAL;
|
|
|
|
payload: {
|
|
|
|
originalPreferredReactions: Array<string>;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ReplaceSelectedDraftEmojiActionType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
type: typeof REPLACE_SELECTED_DRAFT_EMOJI;
|
|
|
|
payload: string;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ResetDraftEmojiActionType = ReadonlyDeep<{
|
2021-09-09 23:47:30 +00:00
|
|
|
type: typeof RESET_DRAFT_EMOJI;
|
|
|
|
payload: { skinTone: number };
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SavePreferredReactionsFulfilledActionType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
type: typeof SAVE_PREFERRED_REACTIONS_FULFILLED;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SavePreferredReactionsPendingActionType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
type: typeof SAVE_PREFERRED_REACTIONS_PENDING;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SavePreferredReactionsRejectedActionType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
type: typeof SAVE_PREFERRED_REACTIONS_REJECTED;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SelectDraftEmojiToBeReplacedActionType = ReadonlyDeep<{
|
2021-09-09 16:29:01 +00:00
|
|
|
type: typeof SELECT_DRAFT_EMOJI_TO_BE_REPLACED;
|
|
|
|
payload: number;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
// Action creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
cancelCustomizePreferredReactionsModal,
|
|
|
|
deselectDraftEmoji,
|
|
|
|
openCustomizePreferredReactionsModal,
|
|
|
|
replaceSelectedDraftEmoji,
|
|
|
|
resetDraftEmoji,
|
|
|
|
savePreferredReactions,
|
|
|
|
selectDraftEmojiToBeReplaced,
|
|
|
|
};
|
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
export const usePreferredReactionsActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-02-23 18:48:40 +00:00
|
|
|
export function getEmptyState(): PreferredReactionsStateType {
|
2021-09-09 16:29:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2022-02-23 18:48:40 +00:00
|
|
|
state: Readonly<PreferredReactionsStateType> = getEmptyState(),
|
2021-09-09 16:29:01 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|