accounts-frontend/packages/app/components/userbar/LoggedInPanel.tsx
ErickSkrauch 8075192472 Split the AccountSwitcher component on 2 independent implementations
Improve auth's ChooseAccount visual behavior
Rework the ComponentLoader component. Make it more stylable
2020-10-26 22:09:01 +03:00

65 lines
2.3 KiB
TypeScript

import React, { ComponentType, useCallback, useState } from 'react';
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],
);
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)}
>
<span className={styles.userIcon} />
<span className={styles.userName}>{activeAccount.username}</span>
<span className={styles.expandIcon} />
</button>
<div className={styles.accountSwitcherContainer}>
<AccountSwitcher
activeAccount={activeAccount}
accounts={accounts}
onAccountClick={onAccountClick}
onRemoveClick={onRemoveAccount}
onLoginClick={hideAccountSwitcher}
/>
</div>
</ClickAwayListener>
</div>
</div>
);
};
export default LoggedInPanel;