signal-desktop/ts/components/AvatarPopup.tsx

84 lines
2.3 KiB
TypeScript
Raw Normal View History

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 { isEmpty } from 'lodash';
2019-10-17 18:22:07 +00:00
import { Avatar, Props as AvatarProps } from './Avatar';
2020-01-17 22:23:19 +00:00
import { useRestoreFocus } from './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;
export const AvatarPopup = (props: Props) => {
2019-11-07 21:36:16 +00:00
const focusRef = React.useRef<HTMLButtonElement>(null);
2019-10-17 18:22:07 +00:00
const {
i18n,
profileName,
phoneNumber,
onViewPreferences,
onViewArchive,
style,
} = props;
const hasProfileName = !isEmpty(profileName);
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">
{hasProfileName ? profileName : phoneNumber}
</div>
{hasProfileName ? (
<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
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>
<button className="module-avatar-popup__item" onClick={onViewArchive}>
<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>
);
};