Add support for displaying the duration of a raised hand.

This commit is contained in:
Half-Shot
2024-10-25 17:09:45 +01:00
parent 16afb568fd
commit 1c8e547b9a
5 changed files with 113 additions and 28 deletions

View File

@@ -310,10 +310,11 @@ export const InCallView: FC<InCallViewProps> = ({
); );
const memberships = useMatrixRTCSessionMemberships(rtcSession); const memberships = useMatrixRTCSessionMemberships(rtcSession);
const { raisedHands, setRaisedHands } = useReactions(); const { raisedHands, addRaisedHand, removeRaisedHand } = useReactions();
const [reactionId, setReactionId] = useState<string | null>(null); const [reactionId, setReactionId] = useState<string | null>(null);
const userId = client.getUserId()!; const userId = client.getUserId()!;
const isHandRaised = raisedHands.includes(userId);
const isHandRaised = !!raisedHands[userId];
useEffect(() => { useEffect(() => {
const getLastReactionEvent = async ( const getLastReactionEvent = async (
@@ -335,17 +336,21 @@ export const InCallView: FC<InCallViewProps> = ({
}; };
const fetchReactions = async (): Promise<void> => { const fetchReactions = async (): Promise<void> => {
const newRaisedHands = [...raisedHands];
for (const m of memberships) { for (const m of memberships) {
if (!m.sender) {
continue;
}
const reaction = await getLastReactionEvent(m.eventId!); const reaction = await getLastReactionEvent(m.eventId!);
if (reaction && reaction.getType() === EventType.Reaction) { if (reaction && reaction.getType() === EventType.Reaction) {
const content = reaction.getContent() as ReactionEventContent; const content = reaction.getContent() as ReactionEventContent;
if (content?.["m.relates_to"].key === "🖐️") { if (content?.["m.relates_to"].key === "🖐️") {
newRaisedHands.push(m.sender!); addRaisedHand(m.sender, new Date(m.createdTs()));
if (m.sender === userId) {
setReactionId(m.eventId!);
}
} }
} }
} }
setRaisedHands(newRaisedHands);
}; };
void fetchReactions(); void fetchReactions();
@@ -354,16 +359,21 @@ export const InCallView: FC<InCallViewProps> = ({
useEffect(() => { useEffect(() => {
const handleReactionEvent = (event: MatrixEvent): void => { const handleReactionEvent = (event: MatrixEvent): void => {
const sender = event.getSender();
if (!sender) {
// Weird, skip.
return;
}
if (event.getType() === EventType.Reaction) { if (event.getType() === EventType.Reaction) {
// TODO: check if target of reaction is a call membership event // TODO: check if target of reaction is a call membership event
const content = event.getContent() as ReactionEventContent; const content = event.getContent() as ReactionEventContent;
if (content?.["m.relates_to"].key === "🖐️") { if (content?.["m.relates_to"].key === "🖐️") {
setRaisedHands([...raisedHands, event.getSender()!]); addRaisedHand(sender, new Date(event.localTimestamp));
} }
} }
if (event.getType() === EventType.RoomRedaction) { if (event.getType() === EventType.RoomRedaction && event.getSender()) {
// TODO: check target of redaction event // TODO: check target of redaction event
setRaisedHands(raisedHands.filter((id) => id !== event.getSender())); removeRaisedHand(sender);
} }
}; };
@@ -374,7 +384,7 @@ export const InCallView: FC<InCallViewProps> = ({
client.on(MatrixRoomEvent.Timeline, handleReactionEvent); client.on(MatrixRoomEvent.Timeline, handleReactionEvent);
client.off(MatrixRoomEvent.Redaction, handleReactionEvent); client.off(MatrixRoomEvent.Redaction, handleReactionEvent);
}; };
}, [client, raisedHands, setRaisedHands]); }, [client, raisedHands, addRaisedHand, removeRaisedHand]);
useEffect(() => { useEffect(() => {
widget?.api.transport widget?.api.transport
@@ -564,7 +574,7 @@ export const InCallView: FC<InCallViewProps> = ({
.redactEvent(rtcSession.room.roomId, reactionId) .redactEvent(rtcSession.room.roomId, reactionId)
.then(() => { .then(() => {
setReactionId(null); setReactionId(null);
setRaisedHands(raisedHands.filter((id) => id !== userId)); removeRaisedHand(userId);
logger.debug("Redacted reaction event"); logger.debug("Redacted reaction event");
}) })
.catch((e) => { .catch((e) => {
@@ -584,7 +594,7 @@ export const InCallView: FC<InCallViewProps> = ({
}) })
.then((reaction) => { .then((reaction) => {
setReactionId(reaction.event_id); setReactionId(reaction.event_id);
setRaisedHands([...raisedHands, userId]); addRaisedHand(userId, new Date());
logger.debug("Sent reaction event", reaction.event_id); logger.debug("Sent reaction event", reaction.event_id);
}) })
.catch((e) => { .catch((e) => {
@@ -595,10 +605,10 @@ export const InCallView: FC<InCallViewProps> = ({
client, client,
isHandRaised, isHandRaised,
memberships, memberships,
raisedHands,
reactionId, reactionId,
rtcSession.room.roomId, rtcSession.room.roomId,
setRaisedHands, addRaisedHand,
removeRaisedHand,
setReactionId, setReactionId,
userId, userId,
]); ]);

View File

@@ -92,7 +92,6 @@ const UserMediaTile = forwardRef<HTMLDivElement, UserMediaTileProps>(
[vm], [vm],
); );
const { raisedHands } = useReactions(); const { raisedHands } = useReactions();
const raisedHand = raisedHands.includes(vm.member?.userId ?? "");
const MicIcon = audioEnabled ? MicOnSolidIcon : MicOffSolidIcon; const MicIcon = audioEnabled ? MicOnSolidIcon : MicOffSolidIcon;
@@ -147,7 +146,7 @@ const UserMediaTile = forwardRef<HTMLDivElement, UserMediaTileProps>(
{menu} {menu}
</Menu> </Menu>
} }
raisedHand={raisedHand} raisedHandTime={raisedHands[vm.member?.userId ?? ""]}
{...props} {...props}
/> />
); );

View File

@@ -90,6 +90,21 @@ unconditionally select the container so we can use cqmin units */
place-items: start; place-items: start;
} }
.raisedHandWidget {
display: flex;
background-color: var(--cpd-color-bg-subtle-primary);
border-radius: var(--cpd-radius-pill-effect);
color: var(--cpd-color-icon-secondary);
border: 1px solid var(--cpd-color-yellow-1200);
}
.raisedHandWidget > p {
padding: var(--cpd-space-2x);
margin-top: auto;
margin-bottom: auto;
width: 4em;
}
.raisedHand { .raisedHand {
margin: var(--cpd-space-2x); margin: var(--cpd-space-2x);
padding: var(--cpd-space-2x); padding: var(--cpd-space-2x);
@@ -104,6 +119,7 @@ unconditionally select the container so we can use cqmin units */
box-shadow: var(--small-drop-shadow); box-shadow: var(--small-drop-shadow);
box-sizing: border-box; box-sizing: border-box;
max-inline-size: 100%; max-inline-size: 100%;
max-width: fit-content;
} }
.raisedHand > span { .raisedHand > span {
@@ -114,6 +130,10 @@ unconditionally select the container so we can use cqmin units */
font-size: 22px; font-size: 22px;
} }
.raisedHandBorder {
border: var(--cpd-space-1x) solid var(--cpd-color-yellow-1200);
}
.nameTag { .nameTag {
grid-area: nameTag; grid-area: nameTag;
padding: var(--cpd-space-1x); padding: var(--cpd-space-1x);

View File

@@ -8,7 +8,13 @@ Please see LICENSE in the repository root for full details.
import { TrackReferenceOrPlaceholder } from "@livekit/components-core"; import { TrackReferenceOrPlaceholder } from "@livekit/components-core";
import { animated } from "@react-spring/web"; import { animated } from "@react-spring/web";
import { RoomMember } from "matrix-js-sdk/src/matrix"; import { RoomMember } from "matrix-js-sdk/src/matrix";
import { ComponentProps, ReactNode, forwardRef } from "react"; import {
ComponentProps,
ReactNode,
forwardRef,
useEffect,
useState,
} from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import classNames from "classnames"; import classNames from "classnames";
import { VideoTrack } from "@livekit/components-react"; import { VideoTrack } from "@livekit/components-react";
@@ -32,7 +38,7 @@ interface Props extends ComponentProps<typeof animated.div> {
nameTagLeadingIcon?: ReactNode; nameTagLeadingIcon?: ReactNode;
displayName: string; displayName: string;
primaryButton?: ReactNode; primaryButton?: ReactNode;
raisedHand: boolean; raisedHandTime?: Date;
} }
export const MediaView = forwardRef<HTMLDivElement, Props>( export const MediaView = forwardRef<HTMLDivElement, Props>(
@@ -51,18 +57,39 @@ export const MediaView = forwardRef<HTMLDivElement, Props>(
nameTagLeadingIcon, nameTagLeadingIcon,
displayName, displayName,
primaryButton, primaryButton,
raisedHand, raisedHandTime,
...props ...props
}, },
ref, ref,
) => { ) => {
const { t } = useTranslation(); const { t } = useTranslation();
const [raisedHandDuration, setRaisedHandDuration] = useState("");
useEffect(() => {
if (!raisedHandTime) {
return;
}
setRaisedHandDuration("00:00");
const to = setInterval(() => {
const totalSeconds = Math.ceil(
(new Date().getTime() - raisedHandTime.getTime()) / 1000,
);
const seconds = totalSeconds % 60;
const minutes = Math.floor(totalSeconds / 60);
setRaisedHandDuration(
`${minutes < 10 ? "0" : ""}${minutes}:${seconds < 10 ? "0" : ""}${seconds}`,
);
}, 1000);
return (): void => clearInterval(to);
}, [setRaisedHandDuration, raisedHandTime]);
return ( return (
<animated.div <animated.div
className={classNames(styles.media, className, { className={classNames(styles.media, className, {
[styles.mirror]: mirror, [styles.mirror]: mirror,
[styles.videoMuted]: !videoEnabled, [styles.videoMuted]: !videoEnabled,
[styles.raisedHandBorder]: !!raisedHandTime,
})} })}
style={style} style={style}
ref={ref} ref={ref}
@@ -88,11 +115,14 @@ export const MediaView = forwardRef<HTMLDivElement, Props>(
)} )}
</div> </div>
<div className={styles.fg}> <div className={styles.fg}>
{raisedHand && ( {raisedHandTime && (
<div className={styles.raisedHand}> <div className={styles.raisedHandWidget}>
<span role="img" aria-label="raised hand"> <div className={styles.raisedHand}>
<span role="img" aria-label="raised hand">
</span>
</span>
</div>
<p>{raisedHandDuration}</p>
</div> </div>
)} )}
<div className={styles.nameTag}> <div className={styles.nameTag}>

View File

@@ -5,11 +5,18 @@ SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details. Please see LICENSE in the repository root for full details.
*/ */
import React, { createContext, useContext, useState, ReactNode } from "react"; import React, {
createContext,
useContext,
useState,
ReactNode,
useCallback,
} from "react";
interface ReactionsContextType { interface ReactionsContextType {
raisedHands: string[]; raisedHands: Record<string, Date>;
setRaisedHands: React.Dispatch<React.SetStateAction<string[]>>; addRaisedHand: (userId: string, date: Date) => void;
removeRaisedHand: (userId: string) => void;
supportsReactions: boolean; supportsReactions: boolean;
setSupportsReactions: React.Dispatch<React.SetStateAction<boolean>>; setSupportsReactions: React.Dispatch<React.SetStateAction<boolean>>;
} }
@@ -31,14 +38,33 @@ export const ReactionsProvider = ({
}: { }: {
children: ReactNode; children: ReactNode;
}): JSX.Element => { }): JSX.Element => {
const [raisedHands, setRaisedHands] = useState<string[]>([]); const [raisedHands, setRaisedHands] = useState<Record<string, Date>>({});
const [supportsReactions, setSupportsReactions] = useState<boolean>(true); const [supportsReactions, setSupportsReactions] = useState<boolean>(true);
const addRaisedHand = useCallback(
(userId: string, time: Date) => {
setRaisedHands({
...raisedHands,
[userId]: time,
});
},
[raisedHands],
);
const removeRaisedHand = useCallback(
(userId: string) => {
delete raisedHands[userId];
setRaisedHands(raisedHands);
},
[raisedHands],
);
return ( return (
<ReactionsContext.Provider <ReactionsContext.Provider
value={{ value={{
raisedHands, raisedHands,
setRaisedHands, addRaisedHand,
removeRaisedHand,
supportsReactions, supportsReactions,
setSupportsReactions, setSupportsReactions,
}} }}