// Copyright 2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import * as React from 'react'; import { take } from 'lodash'; import { Avatar, Props as AvatarProps } from '../Avatar'; import { ContactName } from './ContactName'; import { Emojify } from './Emojify'; import { Intl } from '../Intl'; import { LocalizerType } from '../../types/Util'; export type Props = { i18n: LocalizerType; isMe?: boolean; sharedGroupNames?: Array; membersCount?: number; phoneNumber?: string; onHeightChange?: () => unknown; updateSharedGroups?: () => unknown; } & Omit; const renderMembershipRow = ({ i18n, sharedGroupNames, conversationType, isMe, }: Pick) => { const className = 'module-conversation-hero__membership'; const nameClassName = `${className}__name`; if (isMe) { return
{i18n('noteToSelfHero')}
; } if ( conversationType === 'direct' && sharedGroupNames && sharedGroupNames.length > 0 ) { const firstThreeGroups = take(sharedGroupNames, 3).map((group, i) => ( // We cannot guarantee uniqueness of group names // eslint-disable-next-line react/no-array-index-key )); if (sharedGroupNames.length > 3) { const remainingCount = sharedGroupNames.length - 3; return (
); } if (firstThreeGroups.length === 3) { return (
); } if (firstThreeGroups.length >= 2) { return (
); } if (firstThreeGroups.length >= 1) { return (
); } } return null; }; export const ConversationHero = ({ i18n, avatarPath, color, conversationType, isMe, membersCount, sharedGroupNames = [], name, phoneNumber, profileName, title, onHeightChange, updateSharedGroups, }: Props): JSX.Element => { const firstRenderRef = React.useRef(true); // TODO: DESKTOP-686 /* eslint-disable react-hooks/exhaustive-deps */ React.useEffect(() => { // If any of the depenencies for this hook change then the height of this // component may have changed. The cleanup function notifies listeners of // any potential height changes. return () => { // Kick off the expensive hydration of the current sharedGroupNames if (updateSharedGroups) { updateSharedGroups(); } if (onHeightChange && !firstRenderRef.current) { onHeightChange(); } else { firstRenderRef.current = false; } }; }, [ firstRenderRef, onHeightChange, // Avoid collisions in these dependencies by prefixing them // These dependencies may be dynamic, and therefore may cause height changes `mc-${membersCount}`, `n-${name}`, `pn-${profileName}`, sharedGroupNames.map(g => `g-${g}`).join(' '), ]); /* eslint-enable react-hooks/exhaustive-deps */ const phoneNumberOnly = Boolean( !name && !profileName && conversationType === 'direct' ); /* eslint-disable no-nested-ternary */ return (

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

{!isMe ? (
{membersCount === 1 ? i18n('ConversationHero--members-1') : membersCount !== undefined ? i18n('ConversationHero--members', [`${membersCount}`]) : phoneNumberOnly ? null : phoneNumber}
) : null} {renderMembershipRow({ isMe, sharedGroupNames, conversationType, i18n })}
); /* eslint-enable no-nested-ternary */ };