// Copyright 2020-2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useEffect, useRef, useState } from 'react'; import Measure from 'react-measure'; import { Avatar, AvatarBlur, Props as AvatarProps } from '../Avatar'; import { ContactName } from './ContactName'; import { About } from './About'; import { SharedGroupNames } from '../SharedGroupNames'; import { LocalizerType } from '../../types/Util'; import { ConfirmationDialog } from '../ConfirmationDialog'; import { Button, ButtonSize, ButtonVariant } from '../Button'; import { assert } from '../../util/assert'; import { shouldBlurAvatar } from '../../util/shouldBlurAvatar'; export type Props = { about?: string; acceptedMessageRequest?: boolean; i18n: LocalizerType; isMe: boolean; membersCount?: number; onHeightChange?: () => unknown; phoneNumber?: string; sharedGroupNames?: Array; unblurAvatar: () => void; unblurredAvatarPath?: string; updateSharedGroups: () => unknown; } & Omit; const renderMembershipRow = ({ acceptedMessageRequest, conversationType, i18n, isMe, onClickMessageRequestWarning, phoneNumber, sharedGroupNames, }: Pick< Props, | 'acceptedMessageRequest' | 'conversationType' | 'i18n' | 'isMe' | 'phoneNumber' > & Required> & { onClickMessageRequestWarning: () => void; }) => { const className = 'module-conversation-hero__membership'; if (conversationType !== 'direct') { return null; } if (isMe) { return
{i18n('noteToSelfHero')}
; } if (sharedGroupNames.length > 0) { return (
); } if (acceptedMessageRequest) { if (phoneNumber) { return null; } return
{i18n('no-groups-in-common')}
; } return (
{i18n('no-groups-in-common-warning')}
); }; export const ConversationHero = ({ i18n, about, acceptedMessageRequest, avatarPath, color, conversationType, isMe, membersCount, sharedGroupNames = [], name, phoneNumber, profileName, title, onHeightChange, unblurAvatar, unblurredAvatarPath, updateSharedGroups, }: Props): JSX.Element => { const firstRenderRef = useRef(true); const previousHeightRef = useRef(); const [height, setHeight] = useState(); const [ isShowingMessageRequestWarning, setIsShowingMessageRequestWarning, ] = useState(false); const closeMessageRequestWarning = () => { setIsShowingMessageRequestWarning(false); }; useEffect(() => { // Kick off the expensive hydration of the current sharedGroupNames updateSharedGroups(); }, [updateSharedGroups]); useEffect(() => { firstRenderRef.current = false; }, []); useEffect(() => { // We only want to kick off a "height change" when the height goes from number to // number. We DON'T want to do it when we measure the height for the first time, as // this will cause a re-render loop. const previousHeight = previousHeightRef.current; if (previousHeight && height && previousHeight !== height) { onHeightChange?.(); } }, [height, onHeightChange]); useEffect(() => { previousHeightRef.current = height; }, [height]); let avatarBlur: AvatarBlur; let avatarOnClick: undefined | (() => void); if ( shouldBlurAvatar({ acceptedMessageRequest, avatarPath, isMe, sharedGroupNames, unblurredAvatarPath, }) ) { avatarBlur = AvatarBlur.BlurPictureWithClickToView; avatarOnClick = unblurAvatar; } else { avatarBlur = AvatarBlur.NoBlur; } const phoneNumberOnly = Boolean( !name && !profileName && conversationType === 'direct' ); /* eslint-disable no-nested-ternary */ return ( <> { assert(bounds, 'We should be measuring the bounds'); setHeight(bounds.height); }} > {({ measureRef }) => (

{isMe ? ( i18n('noteToSelf') ) : ( )}

{about && !isMe && (
)} {!isMe ? (
{membersCount === 1 ? i18n('ConversationHero--members-1') : membersCount !== undefined ? i18n('ConversationHero--members', [`${membersCount}`]) : phoneNumberOnly ? null : phoneNumber}
) : null} {renderMembershipRow({ acceptedMessageRequest, conversationType, i18n, isMe, onClickMessageRequestWarning() { setIsShowingMessageRequestWarning(true); }, phoneNumber, sharedGroupNames, })}
)}
{isShowingMessageRequestWarning && ( { window.location.href = 'https://support.signal.org/hc/articles/360007459591'; closeMessageRequestWarning(); }, }, ]} > {i18n('MessageRequestWarning__dialog__details')} )} ); /* eslint-enable no-nested-ternary */ };