accounts-frontend/packages/app/components/userbar/LoggedInPanel.tsx

65 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

import React, { ComponentType, useCallback, useState } from 'react';
2019-12-08 01:13:08 +05:30
import clsx from 'clsx';
import ClickAwayListener from 'react-click-away-listener';
import { Account } from 'app/components/accounts';
import AccountSwitcher from './AccountSwitcher';
import styles from './loggedInPanel.scss';
interface Props {
activeAccount: Account;
accounts: ReadonlyArray<Account>;
onSwitchAccount?: (account: Account) => Promise<any>;
onRemoveAccount?: (account: Account) => Promise<any>;
}
const LoggedInPanel: ComponentType<Props> = ({ activeAccount, accounts, onSwitchAccount, onRemoveAccount }) => {
const [isAccountSwitcherActive, setAccountSwitcherState] = useState(false);
const hideAccountSwitcher = useCallback(() => setAccountSwitcherState(false), []);
const onAccountClick = useCallback(
async (account: Account) => {
if (onSwitchAccount) {
await onSwitchAccount(account);
}
setAccountSwitcherState(false);
},
[onSwitchAccount],
);
2020-05-24 04:38:24 +05:30
return (
<div className={styles.loggedInPanel}>
<div
className={clsx(styles.activeAccount, {
[styles.activeAccountExpanded]: isAccountSwitcherActive,
})}
>
<ClickAwayListener onClickAway={hideAccountSwitcher}>
<button
className={styles.activeAccountButton}
onClick={setAccountSwitcherState.bind(null, !isAccountSwitcherActive)}
>
2020-05-24 04:38:24 +05:30
<span className={styles.userIcon} />
<span className={styles.userName}>{activeAccount.username}</span>
2020-05-24 04:38:24 +05:30
<span className={styles.expandIcon} />
</button>
<div className={styles.accountSwitcherContainer}>
<AccountSwitcher
activeAccount={activeAccount}
accounts={accounts}
onAccountClick={onAccountClick}
onRemoveClick={onRemoveAccount}
onLoginClick={hideAccountSwitcher}
/>
2020-05-24 04:38:24 +05:30
</div>
</ClickAwayListener>
2020-05-24 04:38:24 +05:30
</div>
</div>
2020-05-24 04:38:24 +05:30
);
};
export default LoggedInPanel;