signal-desktop/ts/components/AvatarPopup.tsx

93 lines
2.5 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;
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,
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">
<div className="module-avatar-popup__profile">
<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>
</div>
<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
ref={focusRef}
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>
</div>
);
};