signal-desktop/ts/components/AvatarPopup.tsx

123 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-08-05 16:14:57 +00:00
// Copyright 2019-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2019-10-17 18:22:07 +00:00
import * as React from 'react';
import classNames from 'classnames';
2020-01-17 22:23:19 +00:00
import type { Props as AvatarProps } from './Avatar';
import { Avatar } from './Avatar';
2021-09-17 22:24:21 +00:00
import { useRestoreFocus } from '../hooks/useRestoreFocus';
2019-10-17 18:22:07 +00:00
import type { LocalizerType, ThemeType } from '../types/Util';
2019-10-17 18:22:07 +00:00
export type Props = {
readonly i18n: LocalizerType;
readonly theme: ThemeType;
2019-10-17 18:22:07 +00:00
hasPendingUpdate: boolean;
startUpdate: () => unknown;
2021-07-19 19:26:06 +00:00
onEditProfile: () => unknown;
2019-10-17 18:22:07 +00:00
onViewPreferences: () => unknown;
onViewArchive: () => unknown;
// Matches Popper's RefHandler type
innerRef?: React.Ref<HTMLDivElement>;
2019-10-17 18:22:07 +00:00
style: React.CSSProperties;
2021-08-05 16:14:57 +00:00
} & Omit<AvatarProps, 'onClick'>;
2019-10-17 18:22:07 +00:00
2020-09-12 00:46:52 +00:00
export const AvatarPopup = (props: Props): JSX.Element => {
2019-10-17 18:22:07 +00:00
const {
hasPendingUpdate,
2019-10-17 18:22:07 +00:00
i18n,
2020-07-24 01:35:32 +00:00
name,
2021-07-19 19:26:06 +00:00
onEditProfile,
2019-10-17 18:22:07 +00:00
onViewArchive,
onViewPreferences,
phoneNumber,
profileName,
startUpdate,
2019-10-17 18:22:07 +00:00
style,
title,
2019-10-17 18:22:07 +00:00
} = props;
2020-07-24 01:35:32 +00:00
const shouldShowNumber = Boolean(name || profileName);
2019-10-17 18:22:07 +00:00
2019-11-07 21:36:16 +00:00
// Note: mechanisms to dismiss this view are all in its host, MainHeader
// Focus first button after initial render, restore focus on teardown
const [focusRef] = useRestoreFocus();
2019-11-07 21:36:16 +00:00
2019-10-17 18:22:07 +00:00
return (
<div style={style} className="module-avatar-popup">
2021-07-19 19:26:06 +00:00
<button
className="module-avatar-popup__profile"
onClick={onEditProfile}
ref={focusRef}
type="button"
>
2019-10-17 18:22:07 +00:00
<Avatar {...props} size={52} />
<div className="module-avatar-popup__profile__text">
<div className="module-avatar-popup__profile__name">
{profileName || title}
</div>
2020-07-24 01:35:32 +00:00
{shouldShowNumber ? (
2019-10-17 18:22:07 +00:00
<div className="module-avatar-popup__profile__number">
{phoneNumber}
</div>
) : null}
</div>
2021-07-19 19:26:06 +00:00
</button>
2019-10-17 18:22:07 +00:00
<hr className="module-avatar-popup__divider" />
2019-11-07 21:36:16 +00:00
<button
2020-09-12 00:46:52 +00:00
type="button"
2019-11-07 21:36:16 +00:00
className="module-avatar-popup__item"
onClick={onViewPreferences}
>
2019-10-17 18:22:07 +00:00
<div
className={classNames(
'module-avatar-popup__item__icon',
'module-avatar-popup__item__icon-settings'
)}
/>
<div className="module-avatar-popup__item__text">
{i18n('mainMenuSettings')}
</div>
</button>
2020-09-12 00:46:52 +00:00
<button
type="button"
className="module-avatar-popup__item"
onClick={onViewArchive}
>
2019-10-17 18:22:07 +00:00
<div
className={classNames(
'module-avatar-popup__item__icon',
'module-avatar-popup__item__icon-archive'
)}
/>
<div className="module-avatar-popup__item__text">
{i18n('avatarMenuViewArchive')}
</div>
</button>
{hasPendingUpdate && (
<button
type="button"
className="module-avatar-popup__item"
onClick={startUpdate}
>
<div
className={classNames(
'module-avatar-popup__item__icon',
'module-avatar-popup__item__icon--update'
)}
/>
<div className="module-avatar-popup__item__text">
{i18n('avatarMenuUpdateAvailable')}
</div>
<div className="module-avatar-popup__item--badge" />
</button>
)}
2019-10-17 18:22:07 +00:00
</div>
);
};