2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2021-05-20 21:54:03 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-10-26 18:26:25 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ActiveCallType } from '../types/Calling';
|
2023-10-16 17:58:51 +00:00
|
|
|
import { CallMode } from '../types/Calling';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
|
|
|
import type { LocalizerType } from '../types/Util';
|
2023-10-16 17:58:51 +00:00
|
|
|
import { isReconnecting } from '../util/callingIsReconnecting';
|
2023-10-26 18:26:25 +00:00
|
|
|
import { CallingToastProvider, useCallingToasts } from './CallingToast';
|
2023-10-19 18:59:21 +00:00
|
|
|
import { Spinner } from './Spinner';
|
2023-10-26 18:26:25 +00:00
|
|
|
import { usePrevious } from '../hooks/usePrevious';
|
2021-05-20 21:54:03 +00:00
|
|
|
|
|
|
|
type PropsType = {
|
|
|
|
activeCall: ActiveCallType;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
};
|
|
|
|
|
2023-10-19 18:59:21 +00:00
|
|
|
export function useReconnectingToast({ activeCall, i18n }: PropsType): void {
|
|
|
|
const { showToast, hideToast } = useCallingToasts();
|
|
|
|
const RECONNECTING_TOAST_KEY = 'reconnecting';
|
2021-05-20 21:54:03 +00:00
|
|
|
|
2023-10-19 18:59:21 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (isReconnecting(activeCall)) {
|
|
|
|
showToast({
|
|
|
|
key: RECONNECTING_TOAST_KEY,
|
|
|
|
content: (
|
|
|
|
<span className="CallingToast__reconnecting">
|
|
|
|
<Spinner svgSize="small" size="16px" />
|
|
|
|
{i18n('icu:callReconnecting')}
|
|
|
|
</span>
|
|
|
|
),
|
|
|
|
autoClose: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
hideToast(RECONNECTING_TOAST_KEY);
|
|
|
|
}
|
|
|
|
}, [activeCall, i18n, showToast, hideToast]);
|
2021-05-20 21:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ME = Symbol('me');
|
|
|
|
|
|
|
|
function getCurrentPresenter(
|
|
|
|
activeCall: Readonly<ActiveCallType>
|
|
|
|
): ConversationType | typeof ME | undefined {
|
|
|
|
if (activeCall.presentingSource) {
|
|
|
|
return ME;
|
|
|
|
}
|
|
|
|
if (activeCall.callMode === CallMode.Direct) {
|
|
|
|
const isOtherPersonPresenting = activeCall.remoteParticipants.some(
|
|
|
|
participant => participant.presenting
|
|
|
|
);
|
|
|
|
return isOtherPersonPresenting ? activeCall.conversation : undefined;
|
|
|
|
}
|
|
|
|
if (activeCall.callMode === CallMode.Group) {
|
|
|
|
return activeCall.remoteParticipants.find(
|
|
|
|
participant => participant.presenting
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2023-10-19 18:59:21 +00:00
|
|
|
export function useScreenSharingStoppedToast({
|
|
|
|
activeCall,
|
|
|
|
i18n,
|
|
|
|
}: PropsType): void {
|
2021-05-20 21:54:03 +00:00
|
|
|
const [previousPresenter, setPreviousPresenter] = useState<
|
|
|
|
undefined | { id: string | typeof ME; title?: string }
|
|
|
|
>(undefined);
|
2023-10-19 18:59:21 +00:00
|
|
|
const { showToast } = useCallingToasts();
|
2021-05-20 21:54:03 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const currentPresenter = getCurrentPresenter(activeCall);
|
|
|
|
if (currentPresenter === ME) {
|
|
|
|
setPreviousPresenter({
|
|
|
|
id: ME,
|
|
|
|
});
|
|
|
|
} else if (!currentPresenter) {
|
|
|
|
setPreviousPresenter(undefined);
|
|
|
|
} else {
|
|
|
|
const { id, title } = currentPresenter;
|
|
|
|
setPreviousPresenter({ id, title });
|
|
|
|
}
|
|
|
|
}, [activeCall]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-10-19 18:59:21 +00:00
|
|
|
const currentPresenter = getCurrentPresenter(activeCall);
|
2021-05-20 21:54:03 +00:00
|
|
|
|
2023-10-19 18:59:21 +00:00
|
|
|
if (!currentPresenter && previousPresenter && previousPresenter.title) {
|
|
|
|
showToast({
|
|
|
|
content:
|
|
|
|
previousPresenter.id === ME
|
|
|
|
? i18n('icu:calling__presenting--you-stopped')
|
|
|
|
: i18n('icu:calling__presenting--person-stopped', {
|
|
|
|
name: previousPresenter.title,
|
|
|
|
}),
|
|
|
|
autoClose: true,
|
|
|
|
});
|
2021-05-20 21:54:03 +00:00
|
|
|
}
|
2023-10-19 18:59:21 +00:00
|
|
|
}, [activeCall, previousPresenter, showToast, i18n]);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|
2023-10-25 13:40:22 +00:00
|
|
|
|
2023-10-26 18:26:25 +00:00
|
|
|
function useMutedToast({
|
|
|
|
hasLocalAudio,
|
|
|
|
i18n,
|
|
|
|
}: {
|
|
|
|
hasLocalAudio: boolean;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
}): void {
|
|
|
|
const previousHasLocalAudio = usePrevious(hasLocalAudio, hasLocalAudio);
|
2023-10-25 13:40:22 +00:00
|
|
|
const { showToast, hideToast } = useCallingToasts();
|
|
|
|
const MUTED_TOAST_KEY = 'muted';
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
previousHasLocalAudio !== undefined &&
|
|
|
|
hasLocalAudio !== previousHasLocalAudio
|
|
|
|
) {
|
|
|
|
hideToast(MUTED_TOAST_KEY);
|
|
|
|
showToast({
|
|
|
|
key: MUTED_TOAST_KEY,
|
|
|
|
content: hasLocalAudio
|
|
|
|
? i18n('icu:CallControls__MutedToast--unmuted')
|
|
|
|
: i18n('icu:CallControls__MutedToast--muted'),
|
|
|
|
autoClose: true,
|
|
|
|
dismissable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [hasLocalAudio, previousHasLocalAudio, hideToast, showToast, i18n]);
|
|
|
|
}
|
2023-10-26 18:26:25 +00:00
|
|
|
|
|
|
|
function useOutgoingRingToast({
|
|
|
|
outgoingRing,
|
|
|
|
i18n,
|
|
|
|
}: {
|
|
|
|
outgoingRing?: boolean;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
}): void {
|
|
|
|
const { showToast, hideToast } = useCallingToasts();
|
|
|
|
const previousOutgoingRing = usePrevious(outgoingRing, outgoingRing);
|
|
|
|
const RINGING_TOAST_KEY = 'ringing';
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (outgoingRing === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
previousOutgoingRing !== undefined &&
|
|
|
|
outgoingRing !== previousOutgoingRing
|
|
|
|
) {
|
|
|
|
hideToast(RINGING_TOAST_KEY);
|
|
|
|
showToast({
|
|
|
|
key: RINGING_TOAST_KEY,
|
|
|
|
content: outgoingRing
|
|
|
|
? i18n('icu:CallControls__RingingToast--ringing-on')
|
|
|
|
: i18n('icu:CallControls__RingingToast--ringing-off'),
|
|
|
|
autoClose: true,
|
|
|
|
dismissable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [outgoingRing, previousOutgoingRing, hideToast, showToast, i18n]);
|
|
|
|
}
|
|
|
|
|
|
|
|
type CallingButtonToastsType = {
|
|
|
|
hasLocalAudio: boolean;
|
|
|
|
outgoingRing: boolean | undefined;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function CallingButtonToastsContainer(
|
|
|
|
props: CallingButtonToastsType
|
|
|
|
): JSX.Element {
|
|
|
|
const toastRegionRef = useRef<HTMLDivElement>(null);
|
|
|
|
return (
|
|
|
|
<CallingToastProvider
|
|
|
|
i18n={props.i18n}
|
|
|
|
maxToasts={1}
|
|
|
|
region={toastRegionRef}
|
|
|
|
>
|
|
|
|
<div className="CallingButtonToasts" ref={toastRegionRef} />
|
|
|
|
<CallingButtonToasts {...props} />
|
|
|
|
</CallingToastProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function CallingButtonToasts({
|
|
|
|
hasLocalAudio,
|
|
|
|
outgoingRing,
|
|
|
|
i18n,
|
|
|
|
}: CallingButtonToastsType) {
|
|
|
|
useMutedToast({ hasLocalAudio, i18n });
|
|
|
|
useOutgoingRingToast({ outgoingRing, i18n });
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|