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';
import { isStationeryTheme, useTheme } from '../../hooks/useTheme';
const MAX_STICKER_STACK = 4;
export const Reaction = as<
'button',
{
mx: MatrixClient;
count: number;
reaction: string;
useAuthentication?: boolean;
}
>(({ className, mx, count, reaction, useAuthentication, ...props }, ref) => {
const theme = useTheme();
const stationery = isStationeryTheme(theme);
const isCustomEmoji = reaction.startsWith('mxc://');
const customSrc = isCustomEmoji
? mxcUrlToHttp(mx, reaction, useAuthentication) ?? reaction
: undefined;
const emoji = isCustomEmoji ? (
) : (
{reaction}
);
// Stationery: fanned sticker stack. Everywhere else: compact emoji + count.
if (!stationery) {
return (
{emoji}
{count}
);
}
const stackCount = Math.min(Math.max(count, 1), MAX_STICKER_STACK);
const showCount = count > 2;
return (
{Array.from({ length: stackCount }, (_, i) => {
const layer = stackCount - 1 - i;
return (
{isCustomEmoji ? (
) : (
{reaction}
)}
);
})}
{showCount && (
{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}:
>
);
}