import React from 'react';
import { Box, Text, as } from 'folds';
import classNames from 'classnames';
import { MatrixClient, MatrixEvent, Room } from 'matrix-js-sdk';
import * as css from './Reaction.css';
import { getHexcodeForEmoji, getShortcodeFor } from '../../plugins/emoji';
import { getMemberDisplayName } from '../../utils/room';
import { eventWithShortcode, getMxIdLocalPart, mxcUrlToHttp } from '../../utils/matrix';
export const Reaction = as<
'button',
{
mx: MatrixClient;
count: number;
reaction: string;
/** Custom-emoji shortcode from the reaction event, used for its label. */
shortcode?: string;
useAuthentication?: boolean;
}
>(({ className, mx, count, reaction, shortcode, useAuthentication, ...props }, ref) => {
// Name a unicode reaction by the emoji itself: screen readers speak it by its
// proper name ("party popper"). Don't use getShortcodeFor here — it returns
// undefined until the lazy emoji data loads, so the same button would flip
// between "🎉 reaction" and "tada reaction" depending on timing (#179).
const name = reaction.startsWith('mxc://')
? shortcode
? `:${shortcode}:`
: 'custom emoji'
: reaction;
const label = `${name} reaction, ${count} ${count === 1 ? 'person' : 'people'}`;
return (
{reaction.startsWith('mxc://') ? (
) : (
{reaction}
)}
{count}
);
});
type ReactionTooltipMsgProps = {
room: Room;
reaction: string;
events: MatrixEvent[];
};
export function ReactionTooltipMsg({ room, reaction, events }: ReactionTooltipMsgProps) {
const shortCodeEvt = events.find(eventWithShortcode);
const shortcode =
shortCodeEvt?.getContent().shortcode ??
getShortcodeFor(getHexcodeForEmoji(reaction)) ??
reaction;
const names = events.map(
(ev: MatrixEvent) =>
getMemberDisplayName(room, ev.getSender() ?? 'Unknown') ??
getMxIdLocalPart(ev.getSender() ?? 'Unknown') ??
'Unknown',
);
return (
<>
{names.length === 1 && {names[0]}}
{names.length === 2 && (
<>
{names[0]}
{' and '}
{names[1]}
>
)}
{names.length === 3 && (
<>
{names[0]}
{', '}
{names[1]}
{' and '}
{names[2]}
>
)}
{names.length > 3 && (
<>
{names[0]}
{', '}
{names[1]}
{', '}
{names[2]}
{' and '}
{names.length - 3} others
>
)}
{' reacted with '}
:{shortcode}:
>
);
}