import React from 'react'; import { compact, flatten } from 'lodash'; import { ContactName } from './ContactName'; import { Intl } from '../Intl'; import { LocalizerType } from '../../types/Util'; import { missingCaseError } from '../../util/missingCaseError'; interface Contact { phoneNumber?: string; profileName?: string; name?: string; title: string; isMe?: boolean; } interface Change { type: 'add' | 'remove' | 'name' | 'avatar' | 'general'; newName?: string; contacts?: Array; } export type PropsData = { from: Contact; changes: Array; }; type PropsHousekeeping = { i18n: LocalizerType; }; export type Props = PropsData & PropsHousekeeping; export class GroupNotification extends React.Component { public renderChange(change: Change, from: Contact) { const { contacts, type, newName } = change; const { i18n } = this.props; const otherPeople: Array = compact( (contacts || []).map(contact => { if (contact.isMe) { return null; } return ( ); }) ); const otherPeopleWithCommas: Array = compact( flatten( otherPeople.map((person, index) => [index > 0 ? ', ' : null, person]) ) ); const contactsIncludesMe = (contacts || []).length !== otherPeople.length; switch (type) { case 'name': return ( ); case 'avatar': return ; case 'add': if (!contacts || !contacts.length) { throw new Error('Group update is missing contacts'); } const otherPeopleNotifMsg = otherPeople.length === 1 ? 'joinedTheGroup' : 'multipleJoinedTheGroup'; return ( <> {otherPeople.length > 0 && ( )} {contactsIncludesMe && (
)} ); case 'remove': if (from && from.isMe) { return i18n('youLeftTheGroup'); } if (!contacts || !contacts.length) { throw new Error('Group update is missing contacts'); } const leftKey = contacts.length > 1 ? 'multipleLeftTheGroup' : 'leftTheGroup'; return ( ); case 'general': return; default: throw missingCaseError(type); } } public render() { const { changes, i18n, from } = this.props; // Leave messages are always from the person leaving, so we omit the fromLabel if // the change is a 'leave.' const isLeftOnly = changes && changes.length === 1 && changes[0].type === 'remove'; const fromContact = ( ); const fromLabel = from.isMe ? ( ) : ( ); return (
{isLeftOnly ? null : ( <> {fromLabel}
)} {(changes || []).map((change, index) => (
{this.renderChange(change, from)}
))}
); } }