mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-12 16:29:01 +05:30
8075192472
Improve auth's ChooseAccount visual behavior Rework the ComponentLoader component. Make it more stylable
65 lines
2.3 KiB
TypeScript
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;
|