2021-09-09 16:29:01 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { usePopper } from 'react-popper';
|
|
|
|
import { isEqual, noop } from 'lodash';
|
|
|
|
|
|
|
|
import type { LocalizerType } from '../types/Util';
|
|
|
|
import { Modal } from './Modal';
|
|
|
|
import { Button, ButtonVariant } from './Button';
|
|
|
|
import {
|
2021-09-13 17:04:45 +00:00
|
|
|
ReactionPickerPicker,
|
|
|
|
ReactionPickerPickerEmojiButton,
|
|
|
|
ReactionPickerPickerStyle,
|
|
|
|
} from './ReactionPickerPicker';
|
2021-09-09 16:29:01 +00:00
|
|
|
import { EmojiPicker } from './emoji/EmojiPicker';
|
2021-09-09 23:47:30 +00:00
|
|
|
import { DEFAULT_PREFERRED_REACTION_EMOJI_SHORT_NAMES } from '../reactions/constants';
|
2021-09-09 16:29:01 +00:00
|
|
|
import { convertShortName } from './emoji/lib';
|
|
|
|
import { offsetDistanceModifier } from '../util/popperUtil';
|
|
|
|
|
|
|
|
type PropsType = {
|
|
|
|
draftPreferredReactions: Array<string>;
|
|
|
|
hadSaveError: boolean;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
isSaving: boolean;
|
|
|
|
originalPreferredReactions: Array<string>;
|
2021-09-10 18:43:17 +00:00
|
|
|
recentEmojis: Array<string>;
|
2021-09-09 16:29:01 +00:00
|
|
|
selectedDraftEmojiIndex: undefined | number;
|
|
|
|
skinTone: number;
|
|
|
|
|
|
|
|
cancelCustomizePreferredReactionsModal(): unknown;
|
|
|
|
deselectDraftEmoji(): unknown;
|
|
|
|
onSetSkinTone(tone: number): unknown;
|
|
|
|
replaceSelectedDraftEmoji(newEmoji: string): unknown;
|
|
|
|
resetDraftEmoji(): unknown;
|
|
|
|
savePreferredReactions(): unknown;
|
|
|
|
selectDraftEmojiToBeReplaced(index: number): unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function CustomizingPreferredReactionsModal({
|
|
|
|
cancelCustomizePreferredReactionsModal,
|
|
|
|
deselectDraftEmoji,
|
|
|
|
draftPreferredReactions,
|
|
|
|
hadSaveError,
|
|
|
|
i18n,
|
|
|
|
isSaving,
|
|
|
|
onSetSkinTone,
|
|
|
|
originalPreferredReactions,
|
2021-09-10 18:43:17 +00:00
|
|
|
recentEmojis,
|
2021-09-09 16:29:01 +00:00
|
|
|
replaceSelectedDraftEmoji,
|
|
|
|
resetDraftEmoji,
|
|
|
|
savePreferredReactions,
|
|
|
|
selectDraftEmojiToBeReplaced,
|
|
|
|
selectedDraftEmojiIndex,
|
|
|
|
skinTone,
|
|
|
|
}: Readonly<PropsType>): JSX.Element {
|
2021-11-11 22:43:05 +00:00
|
|
|
const [referenceElement, setReferenceElement] =
|
|
|
|
useState<null | HTMLDivElement>(null);
|
2021-09-09 16:29:01 +00:00
|
|
|
const [popperElement, setPopperElement] = useState<null | HTMLDivElement>(
|
|
|
|
null
|
|
|
|
);
|
|
|
|
const emojiPickerPopper = usePopper(referenceElement, popperElement, {
|
|
|
|
placement: 'bottom',
|
|
|
|
modifiers: [
|
|
|
|
offsetDistanceModifier(8),
|
|
|
|
{
|
|
|
|
name: 'preventOverflow',
|
|
|
|
options: { altAxis: true },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const isSomethingSelected = selectedDraftEmojiIndex !== undefined;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isSomethingSelected) {
|
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
|
|
|
const onBodyClick = (event: MouseEvent) => {
|
|
|
|
const { target } = event;
|
|
|
|
if (!(target instanceof HTMLElement) || !popperElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isClickOutsidePicker = !popperElement.contains(target);
|
|
|
|
if (isClickOutsidePicker) {
|
|
|
|
deselectDraftEmoji();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.body.addEventListener('click', onBodyClick);
|
|
|
|
return () => {
|
|
|
|
document.body.removeEventListener('click', onBodyClick);
|
|
|
|
};
|
|
|
|
}, [isSomethingSelected, popperElement, deselectDraftEmoji]);
|
|
|
|
|
|
|
|
const hasChanged = !isEqual(
|
|
|
|
originalPreferredReactions,
|
|
|
|
draftPreferredReactions
|
|
|
|
);
|
|
|
|
const canReset =
|
|
|
|
!isSaving &&
|
2021-09-09 23:47:30 +00:00
|
|
|
!isEqual(
|
|
|
|
DEFAULT_PREFERRED_REACTION_EMOJI_SHORT_NAMES.map(shortName =>
|
|
|
|
convertShortName(shortName, skinTone)
|
|
|
|
),
|
|
|
|
draftPreferredReactions
|
|
|
|
);
|
2021-09-09 16:29:01 +00:00
|
|
|
const canSave = !isSaving && hasChanged;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
2022-03-17 21:21:50 +00:00
|
|
|
moduleClassName="module-CustomizingPreferredReactionsModal"
|
2021-09-09 16:29:01 +00:00
|
|
|
hasXButton
|
|
|
|
i18n={i18n}
|
|
|
|
onClose={() => {
|
|
|
|
cancelCustomizePreferredReactionsModal();
|
|
|
|
}}
|
|
|
|
title={i18n('CustomizingPreferredReactions__title')}
|
|
|
|
>
|
2021-09-13 17:04:45 +00:00
|
|
|
<div className="module-CustomizingPreferredReactionsModal__small-emoji-picker-wrapper">
|
|
|
|
<ReactionPickerPicker
|
|
|
|
isSomethingSelected={isSomethingSelected}
|
|
|
|
pickerStyle={ReactionPickerPickerStyle.Menu}
|
2021-09-09 16:29:01 +00:00
|
|
|
ref={setReferenceElement}
|
2021-09-13 17:04:45 +00:00
|
|
|
>
|
|
|
|
{draftPreferredReactions.map((emoji, index) => (
|
|
|
|
<ReactionPickerPickerEmojiButton
|
|
|
|
emoji={emoji}
|
|
|
|
// The index is the only thing that uniquely identifies the emoji, because
|
|
|
|
// there can be duplicates in the list.
|
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
|
|
|
key={index}
|
|
|
|
onClick={() => {
|
|
|
|
selectDraftEmojiToBeReplaced(index);
|
|
|
|
}}
|
|
|
|
isSelected={index === selectedDraftEmojiIndex}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ReactionPickerPicker>
|
2021-09-09 16:29:01 +00:00
|
|
|
{hadSaveError
|
|
|
|
? i18n('CustomizingPreferredReactions__had-save-error')
|
|
|
|
: i18n('CustomizingPreferredReactions__subtitle')}
|
|
|
|
</div>
|
|
|
|
{isSomethingSelected && (
|
|
|
|
<div
|
|
|
|
ref={setPopperElement}
|
|
|
|
style={emojiPickerPopper.styles.popper}
|
|
|
|
{...emojiPickerPopper.attributes.popper}
|
|
|
|
>
|
|
|
|
<EmojiPicker
|
|
|
|
i18n={i18n}
|
2021-09-09 23:47:30 +00:00
|
|
|
onPickEmoji={pickedEmoji => {
|
|
|
|
const emoji = convertShortName(
|
|
|
|
pickedEmoji.shortName,
|
|
|
|
pickedEmoji.skinTone
|
|
|
|
);
|
|
|
|
replaceSelectedDraftEmoji(emoji);
|
2021-09-09 16:29:01 +00:00
|
|
|
}}
|
2021-09-10 18:43:17 +00:00
|
|
|
recentEmojis={recentEmojis}
|
2021-09-09 16:29:01 +00:00
|
|
|
skinTone={skinTone}
|
|
|
|
onSetSkinTone={onSetSkinTone}
|
|
|
|
onClose={() => {
|
|
|
|
deselectDraftEmoji();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<Modal.ButtonFooter>
|
|
|
|
<Button
|
|
|
|
disabled={!canReset}
|
|
|
|
onClick={() => {
|
|
|
|
resetDraftEmoji();
|
|
|
|
}}
|
2022-09-07 18:29:08 +00:00
|
|
|
onKeyDown={event => {
|
|
|
|
if (event.key === 'Enter' || event.key === 'Space') {
|
|
|
|
resetDraftEmoji();
|
|
|
|
}
|
|
|
|
}}
|
2021-09-09 16:29:01 +00:00
|
|
|
variant={ButtonVariant.SecondaryAffirmative}
|
|
|
|
>
|
|
|
|
{i18n('reset')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
disabled={!canSave}
|
|
|
|
onClick={() => {
|
|
|
|
savePreferredReactions();
|
|
|
|
}}
|
2022-09-07 18:29:08 +00:00
|
|
|
onKeyDown={event => {
|
|
|
|
if (event.key === 'Enter' || event.key === 'Space') {
|
|
|
|
savePreferredReactions();
|
|
|
|
}
|
|
|
|
}}
|
2021-09-09 16:29:01 +00:00
|
|
|
>
|
|
|
|
{i18n('save')}
|
|
|
|
</Button>
|
|
|
|
</Modal.ButtonFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|