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

104 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-01-03 11:55:46 -08:00
// Copyright 2019 Signal Messenger, LLC
2020-10-30 15:34:04 -05:00
// SPDX-License-Identifier: AGPL-3.0-only
2019-01-14 13:49:58 -08:00
import React from 'react';
import classNames from 'classnames';
import type { ReadonlyDeep } from 'type-fest';
2019-01-14 13:49:58 -08:00
2021-05-07 17:21:10 -05:00
import { Avatar, AvatarBlur } from '../Avatar';
import { AvatarColors } from '../../types/Colors';
import { getName } from '../../types/EmbeddedContact';
import { AttachmentStatusIcon } from './AttachmentStatusIcon';
2019-01-14 13:49:58 -08:00
import type { LocalizerType } from '../../types/Util';
2025-03-25 15:59:34 -07:00
import type { EmbeddedContactForUIType } from '../../types/EmbeddedContact';
2019-01-14 13:49:58 -08:00
export function renderAvatar({
contact,
direction,
2019-01-14 13:49:58 -08:00
i18n,
size,
}: {
2025-03-25 15:59:34 -07:00
contact: ReadonlyDeep<EmbeddedContactForUIType>;
direction?: 'outgoing' | 'incoming';
i18n: LocalizerType;
size: 52 | 80;
2020-09-14 12:51:27 -07:00
}): JSX.Element {
2019-01-14 13:49:58 -08:00
const { avatar } = contact;
2024-07-11 12:44:09 -07:00
const avatarUrl = avatar && avatar.avatar && avatar.avatar.path;
2020-07-23 18:35:32 -07:00
const title = getName(contact) || '';
const isAttachmentNotAvailable = Boolean(
2025-03-25 15:59:34 -07:00
avatar?.avatar?.isPermanentlyUndownloadable
);
2019-01-14 13:49:58 -08:00
const renderAttachmentDownloaded = () => (
2019-01-14 13:49:58 -08:00
<Avatar
2024-07-11 12:44:09 -07:00
avatarUrl={avatarUrl}
badge={undefined}
2021-05-07 17:21:10 -05:00
blur={AvatarBlur.NoBlur}
2021-08-05 20:17:05 -04:00
color={AvatarColors[0]}
2019-01-14 13:49:58 -08:00
conversationType="direct"
i18n={i18n}
2020-07-23 18:35:32 -07:00
title={title}
2021-05-07 17:21:10 -05:00
sharedGroupNames={[]}
2019-01-14 13:49:58 -08:00
size={size}
/>
);
return (
<AttachmentStatusIcon
attachment={avatar?.avatar}
isAttachmentNotAvailable={isAttachmentNotAvailable}
isIncoming={direction === 'incoming'}
renderAttachmentDownloaded={renderAttachmentDownloaded}
/>
);
2019-01-14 13:49:58 -08:00
}
export function renderName({
contact,
isIncoming,
module,
}: {
2025-03-25 15:59:34 -07:00
contact: ReadonlyDeep<EmbeddedContactForUIType>;
2019-01-14 13:49:58 -08:00
isIncoming: boolean;
module: string;
2020-09-14 12:51:27 -07:00
}): JSX.Element {
2019-01-14 13:49:58 -08:00
return (
<div
className={classNames(
`module-${module}__contact-name`,
isIncoming ? `module-${module}__contact-name--incoming` : null
)}
>
{getName(contact)}
</div>
);
}
export function renderContactShorthand({
contact,
isIncoming,
module,
}: {
2025-03-25 15:59:34 -07:00
contact: ReadonlyDeep<EmbeddedContactForUIType>;
2019-01-14 13:49:58 -08:00
isIncoming: boolean;
module: string;
2020-09-14 12:51:27 -07:00
}): JSX.Element {
2019-01-14 13:49:58 -08:00
const { number: phoneNumber, email } = contact;
const firstNumber = phoneNumber && phoneNumber[0] && phoneNumber[0].value;
const firstEmail = email && email[0] && email[0].value;
return (
<div
className={classNames(
`module-${module}__contact-method`,
isIncoming ? `module-${module}__contact-method--incoming` : null
)}
>
{firstNumber || firstEmail}
</div>
);
}