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

184 lines
5.8 KiB
TypeScript
Raw Normal View History

2020-01-17 22:23:19 +00:00
import * as React from 'react';
import { groupBy, mapValues, orderBy, sortBy } from 'lodash';
2020-01-17 22:23:19 +00:00
import classNames from 'classnames';
import { ContactName } from './ContactName';
2020-01-17 22:23:19 +00:00
import { Avatar, Props as AvatarProps } from '../Avatar';
import { Emoji } from '../emoji/Emoji';
import { useRestoreFocus } from '../../util/hooks';
import { ColorType } from '../../types/Util';
2020-01-17 22:23:19 +00:00
export type Reaction = {
emoji: string;
timestamp: number;
from: {
id: string;
color?: ColorType;
avatarPath?: string;
2020-01-17 22:23:19 +00:00
name?: string;
profileName?: string;
2020-01-17 22:23:19 +00:00
isMe?: boolean;
phoneNumber?: string;
2020-01-17 22:23:19 +00:00
};
};
export type OwnProps = {
reactions: Array<Reaction>;
pickedReaction?: string;
2020-01-17 22:23:19 +00:00
onClose?: () => unknown;
};
export type Props = OwnProps &
Pick<React.HTMLProps<HTMLDivElement>, 'style'> &
Pick<AvatarProps, 'i18n'>;
const emojisOrder = ['❤️', '👍', '👎', '😂', '😮', '😢', '😡'];
2020-01-17 22:23:19 +00:00
export const ReactionViewer = React.forwardRef<HTMLDivElement, Props>(
// tslint:disable-next-line max-func-body-length
({ i18n, reactions, onClose, pickedReaction, ...rest }, ref) => {
2020-01-17 22:23:19 +00:00
const grouped = mapValues(groupBy(reactions, 'emoji'), res =>
orderBy(res, ['timestamp'], ['desc'])
);
const [selected, setSelected] = React.useState(pickedReaction || 'all');
2020-01-17 22:23:19 +00:00
const focusRef = React.useRef<HTMLButtonElement>(null);
// Handle escape key
React.useEffect(() => {
const handler = (e: KeyboardEvent) => {
if (onClose && e.key === 'Escape') {
onClose();
}
};
document.addEventListener('keydown', handler);
return () => {
document.removeEventListener('keydown', handler);
};
}, [onClose]);
// Focus first button and restore focus on unmount
useRestoreFocus(focusRef);
// Create sorted reaction categories, supporting reaction types we don't
// explicitly know about yet
const renderedEmojis = React.useMemo(() => {
const emojiSet = new Set<string>();
reactions.forEach(re => emojiSet.add(re.emoji));
2020-02-05 23:14:25 +00:00
const arr = sortBy(Array.from(emojiSet), emoji => {
const idx = emojisOrder.indexOf(emoji);
if (idx > -1) {
return idx;
}
return Infinity;
});
2020-02-05 23:14:25 +00:00
return ['all', ...arr];
}, [reactions]);
const allSorted = React.useMemo(() => {
return orderBy(reactions, ['timestamp'], ['desc']);
}, [reactions]);
// If we have previously selected a reaction type that is no longer present
// (removed on another device, for instance) we should select another
// reaction type
React.useEffect(() => {
if (!grouped[selected]) {
const toSelect = renderedEmojis[0];
if (toSelect) {
setSelected(toSelect);
} else if (onClose) {
// We have nothing to render!
onClose();
}
}
}, [grouped, onClose, renderedEmojis, selected, setSelected]);
2020-02-05 23:14:25 +00:00
const selectedReactions = grouped[selected] || allSorted;
2020-01-17 22:23:19 +00:00
return (
<div {...rest} ref={ref} className="module-reaction-viewer">
<header className="module-reaction-viewer__header">
{renderedEmojis
2020-02-05 23:14:25 +00:00
.filter(e => e === 'all' || Boolean(grouped[e]))
.map((cat, index) => {
const re = grouped[cat] || reactions;
2020-01-17 22:23:19 +00:00
const maybeFocusRef = index === 0 ? focusRef : undefined;
2020-02-05 23:14:25 +00:00
const isAll = cat === 'all';
2020-01-17 22:23:19 +00:00
return (
<button
2020-02-05 23:14:25 +00:00
key={cat}
2020-01-17 22:23:19 +00:00
ref={maybeFocusRef}
className={classNames(
'module-reaction-viewer__header__button',
2020-02-05 23:14:25 +00:00
selected === cat
2020-01-17 22:23:19 +00:00
? 'module-reaction-viewer__header__button--selected'
: null
)}
2020-01-23 19:59:29 +00:00
onClick={event => {
event.stopPropagation();
2020-02-05 23:14:25 +00:00
setSelected(cat);
2020-01-17 22:23:19 +00:00
}}
>
2020-02-05 23:14:25 +00:00
{isAll ? (
<span className="module-reaction-viewer__header__button__all">
{i18n('ReactionsViewer--all')}&thinsp;&middot;&thinsp;
{re.length}
</span>
) : (
<>
<Emoji size={18} emoji={cat} />
<span className="module-reaction-viewer__header__button__count">
{re.length}
</span>
</>
)}
2020-01-17 22:23:19 +00:00
</button>
);
})}
</header>
<main className="module-reaction-viewer__body">
{selectedReactions.map(({ from, emoji }) => (
2020-01-17 22:23:19 +00:00
<div
key={`${from.id}-${emoji}`}
2020-01-17 22:23:19 +00:00
className="module-reaction-viewer__body__row"
>
<div className="module-reaction-viewer__body__row__avatar">
<Avatar
avatarPath={from.avatarPath}
2020-01-17 22:23:19 +00:00
conversationType="direct"
size={32}
color={from.color}
name={from.name}
profileName={from.profileName}
phoneNumber={from.phoneNumber}
2020-01-17 22:23:19 +00:00
i18n={i18n}
/>
</div>
2020-02-05 23:14:25 +00:00
<div className="module-reaction-viewer__body__row__name">
{from.isMe ? (
i18n('you')
) : (
<ContactName
module="module-reaction-viewer__body__row__name__contact-name"
name={from.name}
profileName={from.profileName}
phoneNumber={from.phoneNumber}
/>
)}
2020-02-05 23:14:25 +00:00
</div>
<div className="module-reaction-viewer__body__row__emoji">
<Emoji size={18} emoji={emoji} />
</div>
2020-01-17 22:23:19 +00:00
</div>
))}
</main>
</div>
);
}
);