signal-desktop/ts/components/AvatarPopup.tsx

116 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2019-2020 Signal Messenger, LLC
// 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
2019-10-17 18:22:07 +00:00
import { Avatar, Props as AvatarProps } from './Avatar';
import { useRestoreFocus } from '../util/hooks';
2019-10-17 18:22:07 +00:00
2020-01-17 22:23:19 +00:00
import { LocalizerType } from '../types/Util';
2019-10-17 18:22:07 +00:00
export type Props = {
readonly i18n: LocalizerType;
2021-07-19 19:26:06 +00:00
onEditProfile: () => unknown;
2021-05-28 16:15:17 +00:00
onSetChatColor: () => 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;
} & AvatarProps;
2020-09-12 00:46:52 +00:00
export const AvatarPopup = (props: Props): JSX.Element => {
2019-11-07 21:36:16 +00:00
const focusRef = React.useRef<HTMLButtonElement>(null);
2019-10-17 18:22:07 +00:00
const {
i18n,
2020-07-24 01:35:32 +00:00
name,
2019-10-17 18:22:07 +00:00
profileName,
phoneNumber,
2020-07-24 01:35:32 +00:00
title,
2021-07-19 19:26:06 +00:00
onEditProfile,
2021-05-28 16:15:17 +00:00
onSetChatColor,
2019-10-17 18:22:07 +00:00
onViewPreferences,
onViewArchive,
style,
} = 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
2020-01-17 22:23:19 +00:00
useRestoreFocus(focusRef);
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>
2021-05-28 16:15:17 +00:00
<button
type="button"
className="module-avatar-popup__item"
onClick={onSetChatColor}
>
<div
className={classNames(
'module-avatar-popup__item__icon',
'module-avatar-popup__item__icon-colors'
)}
/>
<div className="module-avatar-popup__item__text">
{i18n('avatarMenuChatColors')}
</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>
</div>
);
};