2022-02-08 18:30:33 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-05-09 23:51:57 +00:00
|
|
|
import classNames from 'classnames';
|
2022-02-25 15:24:05 +00:00
|
|
|
import { noop } from 'lodash';
|
2022-02-08 18:30:33 +00:00
|
|
|
import type { ReactElement } from 'react';
|
2022-02-25 15:24:05 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-02-08 18:30:33 +00:00
|
|
|
import animationData from '../../images/lottie-animations/CallingSpeakingIndicator.json';
|
|
|
|
import { Lottie } from './Lottie';
|
|
|
|
|
2022-02-25 15:24:05 +00:00
|
|
|
const SPEAKING_LINGER_MS = 100;
|
|
|
|
|
2022-05-09 23:51:57 +00:00
|
|
|
const BASE_CLASS_NAME = 'CallingAudioIndicator';
|
|
|
|
const CONTENT_CLASS_NAME = `${BASE_CLASS_NAME}__content`;
|
|
|
|
|
2022-02-08 18:30:33 +00:00
|
|
|
export function CallingAudioIndicator({
|
2022-02-25 15:24:05 +00:00
|
|
|
hasAudio,
|
2022-02-08 18:30:33 +00:00
|
|
|
isSpeaking,
|
2022-02-25 15:24:05 +00:00
|
|
|
}: Readonly<{ hasAudio: boolean; isSpeaking: boolean }>): ReactElement {
|
2022-05-12 21:03:43 +00:00
|
|
|
const [shouldShowSpeaking, setShouldShowSpeaking] = useState(isSpeaking);
|
2022-02-25 15:24:05 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isSpeaking) {
|
2022-05-12 21:03:43 +00:00
|
|
|
setShouldShowSpeaking(true);
|
|
|
|
} else if (shouldShowSpeaking) {
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
setShouldShowSpeaking(false);
|
2022-02-25 15:24:05 +00:00
|
|
|
}, SPEAKING_LINGER_MS);
|
2022-05-12 21:03:43 +00:00
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
};
|
2022-02-25 15:24:05 +00:00
|
|
|
}
|
2022-05-12 21:03:43 +00:00
|
|
|
return noop;
|
|
|
|
}, [isSpeaking, shouldShowSpeaking]);
|
2022-02-25 15:24:05 +00:00
|
|
|
|
|
|
|
if (!hasAudio) {
|
2022-02-08 18:30:33 +00:00
|
|
|
return (
|
2022-05-09 23:51:57 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
BASE_CLASS_NAME,
|
|
|
|
`${BASE_CLASS_NAME}--with-content`
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
CONTENT_CLASS_NAME,
|
|
|
|
`${CONTENT_CLASS_NAME}--muted`
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-02-08 18:30:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-12 21:03:43 +00:00
|
|
|
if (shouldShowSpeaking) {
|
2022-02-08 18:30:33 +00:00
|
|
|
return (
|
2022-05-09 23:51:57 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
BASE_CLASS_NAME,
|
|
|
|
`${BASE_CLASS_NAME}--with-content`
|
|
|
|
)}
|
|
|
|
>
|
2022-05-12 21:03:43 +00:00
|
|
|
<Lottie animationData={animationData} className={CONTENT_CLASS_NAME} />
|
2022-05-09 23:51:57 +00:00
|
|
|
</div>
|
2022-02-08 18:30:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render an empty spacer so that names don't move around.
|
2022-05-09 23:51:57 +00:00
|
|
|
return <div className={BASE_CLASS_NAME} />;
|
2022-02-08 18:30:33 +00:00
|
|
|
}
|