2022-02-08 18:30:33 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
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-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 {
|
|
|
|
const [shouldShowSpeaking, setShouldShowSpeaking] = useState(isSpeaking);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isSpeaking) {
|
|
|
|
setShouldShowSpeaking(true);
|
|
|
|
} else if (shouldShowSpeaking) {
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
setShouldShowSpeaking(false);
|
|
|
|
}, SPEAKING_LINGER_MS);
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return noop;
|
|
|
|
}, [isSpeaking, shouldShowSpeaking]);
|
|
|
|
|
|
|
|
if (!hasAudio) {
|
2022-02-08 18:30:33 +00:00
|
|
|
return (
|
|
|
|
<div className="CallingAudioIndicator CallingAudioIndicator--muted" />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:05 +00:00
|
|
|
if (shouldShowSpeaking) {
|
2022-02-08 18:30:33 +00:00
|
|
|
return (
|
|
|
|
<Lottie animationData={animationData} className="CallingAudioIndicator" />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render an empty spacer so that names don't move around.
|
|
|
|
return <div className="CallingAudioIndicator" />;
|
|
|
|
}
|