Files
element-call/src/reactions/RaisedHandIndicator.tsx
T

105 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-11-04 08:54:13 -01:00
/*
Copyright 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
2024-11-04 08:54:13 -01:00
Please see LICENSE in the repository root for full details.
*/
import {
type MouseEventHandler,
type ReactNode,
useCallback,
useEffect,
useState,
useMemo,
} from "react";
2024-11-08 17:36:40 +00:00
import { useTranslation } from "react-i18next";
2024-11-04 08:54:13 -01:00
2024-11-08 17:36:40 +00:00
import { ReactionIndicator } from "./ReactionIndicator";
2024-11-04 08:54:13 -01:00
export function RaisedHandIndicator({
raisedHandTime,
2024-11-06 15:54:58 +00:00
miniature,
2024-11-04 08:54:13 -01:00
showTimer,
onClick,
2024-11-04 08:54:13 -01:00
}: {
raisedHandTime?: Date;
2024-11-06 15:54:58 +00:00
miniature?: boolean;
2024-11-04 08:54:13 -01:00
showTimer?: boolean;
onClick?: () => void;
2024-11-04 08:54:13 -01:00
}): ReactNode {
2024-11-08 17:36:40 +00:00
const { t } = useTranslation();
2024-11-04 08:54:13 -01:00
const [raisedHandDuration, setRaisedHandDuration] = useState("");
const durationFormatter = useMemo(
() =>
new Intl.DurationFormat(undefined, {
minutesDisplay: "always",
secondsDisplay: "always",
hoursDisplay: "auto",
style: "digital",
}),
[],
);
const clickCallback = useCallback<MouseEventHandler<HTMLButtonElement>>(
(event) => {
if (!onClick) {
return;
}
event.preventDefault();
onClick();
},
[onClick],
);
2024-11-04 08:54:13 -01:00
// This effect creates a simple timer effect.
useEffect(() => {
if (!raisedHandTime || !showTimer) {
return;
}
const calculateTime = (): void => {
const totalSeconds = Math.ceil(
(new Date().getTime() - raisedHandTime.getTime()) / 1000,
);
setRaisedHandDuration(
durationFormatter.format({
seconds: totalSeconds % 60,
minutes: Math.floor(totalSeconds / 60),
}),
);
};
calculateTime();
const to = setInterval(calculateTime, 1000);
return (): void => clearInterval(to);
}, [setRaisedHandDuration, raisedHandTime, showTimer, durationFormatter]);
2024-11-04 08:54:13 -01:00
if (!raisedHandTime) {
return;
}
const content = (
2024-11-08 17:36:40 +00:00
<ReactionIndicator emoji="✋" miniature={miniature}>
{showTimer && <p>{raisedHandDuration}</p>}
2024-11-08 17:36:40 +00:00
</ReactionIndicator>
);
if (onClick) {
return (
<button
2024-11-08 17:36:40 +00:00
aria-label={t("action.lower_hand")}
style={{
display: "contents",
background: "none",
}}
onClick={clickCallback}
>
{content}
</button>
2024-11-04 08:54:13 -01:00
);
}
return content;
2024-11-04 08:54:13 -01:00
}