signal-desktop/ts/components/conversation/CallingNotification.tsx

179 lines
4.7 KiB
TypeScript
Raw Normal View History

// Copyright 2020-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useState, useEffect } from 'react';
import Measure from 'react-measure';
2021-08-26 20:51:55 +00:00
import { noop } from 'lodash';
2020-06-04 18:16:19 +00:00
import { SystemMessage } from './SystemMessage';
2021-08-26 20:51:55 +00:00
import { Button, ButtonSize, ButtonVariant } from '../Button';
2020-06-04 18:16:19 +00:00
import { Timestamp } from './Timestamp';
import { LocalizerType } from '../../types/Util';
import { CallMode } from '../../types/Calling';
import {
CallingNotificationType,
2021-08-26 20:51:55 +00:00
getCallingIcon,
getCallingNotificationText,
} from '../../util/callingNotification';
import { usePrevious } from '../../util/hooks';
import { missingCaseError } from '../../util/missingCaseError';
import { Tooltip, TooltipPlacement } from '../Tooltip';
2020-06-04 18:16:19 +00:00
export type PropsActionsType = {
messageSizeChanged: (messageId: string, conversationId: string) => void;
returnToActiveCall: () => void;
startCallingLobby: (_: {
conversationId: string;
isVideoCall: boolean;
}) => void;
};
2020-06-04 18:16:19 +00:00
type PropsHousekeeping = {
i18n: LocalizerType;
conversationId: string;
messageId: string;
2020-06-04 18:16:19 +00:00
};
type PropsType = CallingNotificationType & PropsActionsType & PropsHousekeeping;
2020-06-04 18:16:19 +00:00
export const CallingNotification: React.FC<PropsType> = React.memo(props => {
const { conversationId, i18n, messageId, messageSizeChanged } = props;
const [height, setHeight] = useState<null | number>(null);
const previousHeight = usePrevious<null | number>(null, height);
useEffect(() => {
if (height === null) {
return;
2020-06-04 18:16:19 +00:00
}
if (previousHeight !== null && height !== previousHeight) {
messageSizeChanged(messageId, conversationId);
2020-09-14 19:51:27 +00:00
}
}, [height, previousHeight, conversationId, messageId, messageSizeChanged]);
2021-08-26 20:51:55 +00:00
let hasButton = false;
let timestamp: number;
2021-08-26 20:51:55 +00:00
let wasMissed = false;
switch (props.callMode) {
case CallMode.Direct:
timestamp = props.acceptedTime || props.endedTime;
2021-08-26 20:51:55 +00:00
wasMissed =
props.wasIncoming && !props.acceptedTime && !props.wasDeclined;
break;
case CallMode.Group:
2021-08-26 20:51:55 +00:00
hasButton = !props.ended;
timestamp = props.startedTime;
break;
default:
window.log.error(
`CallingNotification missing case: ${missingCaseError(props)}`
);
return null;
2020-06-04 18:16:19 +00:00
}
2021-08-26 20:51:55 +00:00
const icon = getCallingIcon(props);
return (
<Measure
bounds
onResize={({ bounds }) => {
if (!bounds) {
window.log.error('We should be measuring the bounds');
return;
}
setHeight(bounds.height);
}}
>
{({ measureRef }) => (
<SystemMessage
button={
hasButton ? <CallingNotificationButton {...props} /> : undefined
}
contents={
<>
2021-08-26 20:51:55 +00:00
{getCallingNotificationText(props, i18n)} &middot;{' '}
<Timestamp
direction="outgoing"
extended
i18n={i18n}
timestamp={timestamp}
withImageNoCaption={false}
withSticker={false}
withTapToViewExpired={false}
/>
</>
}
icon={icon}
isError={wasMissed}
ref={measureRef}
/>
)}
</Measure>
);
});
function CallingNotificationButton(props: PropsType) {
if (props.callMode !== CallMode.Group || props.ended) {
2020-06-04 18:16:19 +00:00
return null;
}
const {
activeCallConversationId,
conversationId,
deviceCount,
i18n,
maxDevices,
returnToActiveCall,
startCallingLobby,
} = props;
let buttonText: string;
let disabledTooltipText: undefined | string;
2021-08-26 20:51:55 +00:00
let onClick: () => void;
if (activeCallConversationId) {
if (activeCallConversationId === conversationId) {
buttonText = i18n('calling__return');
onClick = returnToActiveCall;
} else {
buttonText = i18n('calling__join');
disabledTooltipText = i18n(
'calling__call-notification__button__in-another-call-tooltip'
);
2021-08-26 20:51:55 +00:00
onClick = noop;
}
} else if (deviceCount >= maxDevices) {
buttonText = i18n('calling__call-is-full');
disabledTooltipText = i18n(
'calling__call-notification__button__call-full-tooltip',
[String(deviceCount)]
);
2021-08-26 20:51:55 +00:00
onClick = noop;
} else {
buttonText = i18n('calling__join');
onClick = () => {
startCallingLobby({ conversationId, isVideoCall: true });
};
}
const button = (
2021-08-26 20:51:55 +00:00
<Button
disabled={Boolean(disabledTooltipText)}
onClick={onClick}
2021-08-26 20:51:55 +00:00
size={ButtonSize.Small}
variant={ButtonVariant.SystemMessage}
2020-06-04 18:16:19 +00:00
>
{buttonText}
2021-08-26 20:51:55 +00:00
</Button>
2020-06-04 18:16:19 +00:00
);
if (disabledTooltipText) {
return (
<Tooltip content={disabledTooltipText} direction={TooltipPlacement.Top}>
{button}
</Tooltip>
);
}
return button;
}