import React from 'react';
import { compact, flatten } from 'lodash';
import { Intl } from '../Intl';
import { missingCaseError } from '../../util/missingCaseError';
import {
PropsForGroupUpdate,
PropsForGroupUpdateAdd,
PropsForGroupUpdateKicked,
PropsForGroupUpdateRemove,
PropsForGroupUpdateType,
} from '../../state/ducks/conversations';
import _ from 'underscore';
// This component is used to display group updates in the conversation view.
// This is a not a "notification" as the name suggests, but a message inside the conversation
type TypeWithContacts =
| PropsForGroupUpdateAdd
| PropsForGroupUpdateKicked
| PropsForGroupUpdateRemove;
function isTypeWithContact(change: PropsForGroupUpdateType): change is TypeWithContacts {
return (change as TypeWithContacts).contacts !== undefined;
}
function getPeople(change: TypeWithContacts) {
return _.compact(
flatten(
(change.contacts || []).map((contact, index) => {
const element = (
{contact.profileName || contact.phoneNumber}
);
return [index > 0 ? ', ' : null, element];
})
)
);
}
function renderChange(change: PropsForGroupUpdateType) {
const people = isTypeWithContact(change) ? getPeople(change) : [];
switch (change.type) {
case 'name':
return `${window.i18n('titleIsNow', [change.newName || ''])}.`;
case 'add':
if (!change.contacts || !change.contacts.length) {
throw new Error('Group update add is missing contacts');
}
const joinKey = change.contacts.length > 1 ? 'multipleJoinedTheGroup' : 'joinedTheGroup';
return