/* Copyright 2024 New Vector Ltd. SPDX-License-Identifier: AGPL-3.0-only Please see LICENSE in the repository root for full details. */ import { Button as CpdButton, Tooltip, Alert } from "@vector-im/compound-web"; import { RaisedHandSolidIcon, ReactionIcon, ChevronDownIcon, ChevronUpIcon, } from "@vector-im/compound-design-tokens/assets/web/icons"; import { ComponentPropsWithoutRef, FC, ReactNode, useCallback, useEffect, useMemo, useState, } from "react"; import { useTranslation } from "react-i18next"; import { logger } from "matrix-js-sdk/src/logger"; import { EventType, RelationType } from "matrix-js-sdk/src/matrix"; import { MatrixClient } from "matrix-js-sdk/src/client"; import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession"; import classNames from "classnames"; import { useReactions } from "../useReactions"; import { useMatrixRTCSessionMemberships } from "../useMatrixRTCSessionMemberships"; import styles from "./ReactionToggleButton.module.css"; import { ReactionOption, ReactionSet, ElementCallReactionEventType, } from "../reactions"; import { Modal } from "../Modal"; interface InnerButtonProps extends ComponentPropsWithoutRef<"button"> { raised: boolean; open: boolean; } const InnerButton: FC = ({ raised, open, ...props }) => { const { t } = useTranslation(); return ( ); }; export function ReactionPopupMenu({ sendReaction, toggleRaisedHand, isHandRaised, canReact, errorText, }: { sendReaction: (reaction: ReactionOption) => void; toggleRaisedHand: () => void; errorText?: string; isHandRaised: boolean; canReact: boolean; }): ReactNode { const { t } = useTranslation(); const [isFullyExpanded, setExpanded] = useState(false); const filteredReactionSet = useMemo( () => (isFullyExpanded ? ReactionSet : ReactionSet.slice(0, 5)), [isFullyExpanded], ); const label = isHandRaised ? t("action.lower_hand") : t("action.raise_hand"); return ( <> {errorText && ( {errorText} )}
toggleRaisedHand()} iconOnly Icon={RaisedHandSolidIcon} />
{filteredReactionSet.map((reaction) => (
  • sendReaction(reaction)} > {reaction.emoji}
  • ))}
    setExpanded(!isFullyExpanded)} />
    ); } interface ReactionToggleButtonProps extends ComponentPropsWithoutRef<"button"> { rtcSession: MatrixRTCSession; client: MatrixClient; } export function ReactionToggleButton({ client, rtcSession, ...props }: ReactionToggleButtonProps): ReactNode { const { t } = useTranslation(); const { raisedHands, lowerHand, reactions } = useReactions(); const [busy, setBusy] = useState(false); const userId = client.getUserId()!; const isHandRaised = !!raisedHands[userId]; const memberships = useMatrixRTCSessionMemberships(rtcSession); const [showReactionsMenu, setShowReactionsMenu] = useState(false); const [errorText, setErrorText] = useState(); useEffect(() => { // Clear whenever the reactions menu state changes. setErrorText(undefined); }, [showReactionsMenu]); const canReact = !reactions[userId]; const sendRelation = useCallback( async (reaction: ReactionOption) => { try { const myMembership = memberships.find((m) => m.sender === userId); if (!myMembership?.eventId) { throw new Error("Cannot find own membership event"); } const parentEventId = myMembership.eventId; setBusy(true); await client.sendEvent( rtcSession.room.roomId, ElementCallReactionEventType, { "m.relates_to": { rel_type: RelationType.Reference, event_id: parentEventId, }, emoji: reaction.emoji, name: reaction.name, }, ); setErrorText(undefined); setShowReactionsMenu(false); } catch (ex) { setErrorText(ex instanceof Error ? ex.message : "Unknown error"); logger.error("Failed to send reaction", ex); } finally { setBusy(false); } }, [memberships, client, userId, rtcSession], ); const toggleRaisedHand = useCallback(() => { const raiseHand = async (): Promise => { if (isHandRaised) { try { setBusy(true); await lowerHand(); setShowReactionsMenu(false); } finally { setBusy(false); } } else { try { const myMembership = memberships.find((m) => m.sender === userId); if (!myMembership?.eventId) { throw new Error("Cannot find own membership event"); } const parentEventId = myMembership.eventId; setBusy(true); const reaction = await client.sendEvent( rtcSession.room.roomId, EventType.Reaction, { "m.relates_to": { rel_type: RelationType.Annotation, event_id: parentEventId, key: "🖐️", }, }, ); logger.debug("Sent raise hand event", reaction.event_id); setErrorText(undefined); setShowReactionsMenu(false); } catch (ex) { setErrorText(ex instanceof Error ? ex.message : "Unknown error"); logger.error("Failed to raise hand", ex); } finally { setBusy(false); } } }; void raiseHand(); }, [ client, isHandRaised, memberships, lowerHand, rtcSession.room.roomId, userId, ]); return ( <> setShowReactionsMenu((show) => !show)} raised={isHandRaised} open={showReactionsMenu} {...props} /> setShowReactionsMenu(false)} > void sendRelation(reaction)} toggleRaisedHand={toggleRaisedHand} /> ); }