element-call/src/room/PTTButton.jsx

74 lines
1.9 KiB
React
Raw Normal View History

import React, { useCallback, useEffect, useState } from "react";
2022-04-29 00:44:50 +00:00
import classNames from "classnames";
2022-04-23 01:05:48 +00:00
import styles from "./PTTButton.module.css";
import { ReactComponent as MicIcon } from "../icons/Mic.svg";
import { Avatar } from "../Avatar";
2022-04-29 00:44:50 +00:00
export function PTTButton({
showTalkOverError,
activeSpeakerUserId,
activeSpeakerDisplayName,
activeSpeakerAvatarUrl,
activeSpeakerIsLocalUser,
size,
startTalking,
stopTalking,
2022-04-29 00:44:50 +00:00
}) {
const [isHeld, setHeld] = useState(false);
const onDocumentMouseUp = useCallback(() => {
if (isHeld) stopTalking();
setHeld(false);
}, [isHeld, setHeld]);
const onWindowBlur = useCallback(() => {
if (isHeld) stopTalking();
setHeld(false);
}, [isHeld, setHeld]);
const onButtonMouseDown = useCallback(() => {
setHeld(true);
startTalking();
}, [setHeld]);
useEffect(() => {
window.addEventListener("mouseup", onDocumentMouseUp);
window.addEventListener("blur", onWindowBlur);
return () => {
window.removeEventListener("mouseup", onDocumentMouseUp);
window.removeEventListener("blur", onWindowBlur);
};
}, [onDocumentMouseUp, onWindowBlur]);
2022-04-23 01:05:48 +00:00
return (
<button
className={classNames(styles.pttButton, {
2022-04-29 00:44:50 +00:00
[styles.talking]: activeSpeakerUserId,
[styles.error]: showTalkOverError,
2022-04-23 01:05:48 +00:00
})}
onMouseDown={onButtonMouseDown}
2022-04-23 01:05:48 +00:00
>
2022-04-29 00:44:50 +00:00
{activeSpeakerIsLocalUser || !activeSpeakerUserId ? (
2022-04-23 01:05:48 +00:00
<MicIcon
2022-04-29 00:44:50 +00:00
className={styles.micIcon}
2022-04-23 01:05:48 +00:00
width={size / 3}
height={size / 3}
/>
) : (
<Avatar
2022-04-29 00:44:50 +00:00
key={activeSpeakerUserId}
2022-04-23 01:05:48 +00:00
style={{
2022-04-29 00:44:50 +00:00
width: size - 12,
height: size - 12,
borderRadius: size - 12,
fontSize: Math.round((size - 12) / 2),
2022-04-23 01:05:48 +00:00
}}
2022-04-29 00:44:50 +00:00
src={activeSpeakerAvatarUrl}
fallback={activeSpeakerDisplayName.slice(0, 1).toUpperCase()}
2022-04-23 01:05:48 +00:00
className={styles.avatar}
/>
)}
</button>
);
}