signal-desktop/ts/components/conversation/ReactionPicker.tsx

155 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2020-01-23 23:57:37 +00:00
import * as React from 'react';
import { convertShortName } from '../emoji/lib';
import type { Props as EmojiPickerProps } from '../emoji/EmojiPicker';
import { useDelayedRestoreFocus } from '../../hooks/useRestoreFocus';
import type { LocalizerType } from '../../types/Util';
2021-09-13 17:04:45 +00:00
import {
ReactionPickerPicker,
ReactionPickerPickerEmojiButton,
ReactionPickerPickerMoreButton,
ReactionPickerPickerStyle,
} from '../ReactionPickerPicker';
export type RenderEmojiPickerProps = Pick<Props, 'onClose' | 'style'> &
Pick<
EmojiPickerProps,
'onClickSettings' | 'onPickEmoji' | 'onSetSkinTone'
> & {
ref: React.Ref<HTMLDivElement>;
};
2020-01-23 23:57:37 +00:00
export type OwnProps = {
i18n: LocalizerType;
2020-01-23 23:57:37 +00:00
selected?: string;
onClose?: () => unknown;
onPick: (emoji: string) => unknown;
onSetSkinTone: (tone: number) => unknown;
openCustomizePreferredReactionsModal?: () => unknown;
preferredReactionEmoji: ReadonlyArray<string>;
renderEmojiPicker: (props: RenderEmojiPickerProps) => React.ReactElement;
2020-01-23 23:57:37 +00:00
};
export type Props = OwnProps & Pick<React.HTMLProps<HTMLDivElement>, 'style'>;
export const ReactionPicker = React.forwardRef<HTMLDivElement, Props>(
2022-11-18 00:45:19 +00:00
function ReactionPickerInner(
{
i18n,
onClose,
onPick,
onSetSkinTone,
openCustomizePreferredReactionsModal,
preferredReactionEmoji,
renderEmojiPicker,
selected,
style,
},
2020-10-02 20:05:09 +00:00
ref
2022-11-18 00:45:19 +00:00
) {
const [pickingOther, setPickingOther] = React.useState(false);
2020-01-23 23:57:37 +00:00
// Handle escape key
React.useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (onClose && e.key === 'Escape' && !pickingOther) {
2020-01-23 23:57:37 +00:00
onClose();
}
};
document.addEventListener('keydown', handler);
return () => {
document.removeEventListener('keydown', handler);
};
}, [onClose, pickingOther]);
2020-01-23 23:57:37 +00:00
// Handle EmojiPicker::onPickEmoji
const onPickEmoji: EmojiPickerProps['onPickEmoji'] = React.useCallback(
2020-10-02 20:05:09 +00:00
({ shortName, skinTone: pickedSkinTone }) => {
onPick(convertShortName(shortName, pickedSkinTone));
},
[onPick]
);
2021-09-07 21:30:58 +00:00
// Focus first button and restore focus on unmount
const [focusRef] = useDelayedRestoreFocus();
2021-09-07 21:30:58 +00:00
if (pickingOther) {
return renderEmojiPicker({
onClickSettings: openCustomizePreferredReactionsModal,
onClose,
onPickEmoji,
onSetSkinTone,
ref,
style,
});
2021-09-07 21:30:58 +00:00
}
const otherSelected =
selected && !preferredReactionEmoji.includes(selected);
2021-09-07 21:30:58 +00:00
let moreButton: React.ReactNode;
2021-09-13 17:04:45 +00:00
if (otherSelected) {
2021-09-07 21:30:58 +00:00
moreButton = (
2021-09-13 17:04:45 +00:00
<ReactionPickerPickerEmojiButton
2021-09-07 21:30:58 +00:00
emoji={selected}
2021-09-13 17:04:45 +00:00
onClick={() => {
2021-09-07 21:30:58 +00:00
onPick(selected);
}}
2021-09-13 17:04:45 +00:00
isSelected
2023-03-30 00:03:25 +00:00
title={i18n('icu:Reactions--remove')}
2021-09-07 21:30:58 +00:00
/>
);
} else {
moreButton = (
2021-09-13 17:04:45 +00:00
<ReactionPickerPickerMoreButton
i18n={i18n}
onClick={() => {
2021-09-07 21:30:58 +00:00
setPickingOther(true);
}}
2021-09-13 17:04:45 +00:00
/>
2021-09-07 21:30:58 +00:00
);
}
2021-09-13 17:04:45 +00:00
// This logic is here to avoid selecting duplicate emoji.
let hasSelectedSomething = false;
2021-09-07 21:30:58 +00:00
return (
2021-09-13 17:04:45 +00:00
<ReactionPickerPicker
isSomethingSelected={typeof selected === 'number'}
pickerStyle={ReactionPickerPickerStyle.Picker}
ref={ref}
style={style}
>
{preferredReactionEmoji.map((emoji, index) => {
2020-01-23 23:57:37 +00:00
const maybeFocusRef = index === 0 ? focusRef : undefined;
2021-09-13 17:04:45 +00:00
const isSelected = !hasSelectedSomething && emoji === selected;
if (isSelected) {
hasSelectedSomething = true;
}
2020-01-23 23:57:37 +00:00
return (
2021-09-13 17:04:45 +00:00
<ReactionPickerPickerEmojiButton
2021-09-07 21:30:58 +00:00
emoji={emoji}
2021-09-13 17:04:45 +00:00
isSelected={isSelected}
// 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={() => {
2020-01-23 23:57:37 +00:00
onPick(emoji);
}}
2021-09-07 21:30:58 +00:00
ref={maybeFocusRef}
/>
2020-01-23 23:57:37 +00:00
);
})}
2021-09-07 21:30:58 +00:00
{moreButton}
2021-09-13 17:04:45 +00:00
</ReactionPickerPicker>
2020-01-23 23:57:37 +00:00
);
}
);