2020-10-23 03:53:59 +05:30
|
|
|
import React, { ComponentType, MouseEventHandler, useCallback, useState } from 'react';
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
|
|
|
import { PseudoAvatar } from 'app/components/ui';
|
|
|
|
import { ComponentLoader } from 'app/components/ui/loader';
|
|
|
|
import { Account } from 'app/components/accounts/reducer';
|
|
|
|
|
|
|
|
import styles from './accountSwitcher.scss';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
accounts: ReadonlyArray<Account>;
|
|
|
|
onAccountClick?: (account: Account) => Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AccountSwitcher: ComponentType<Props> = ({ accounts, onAccountClick }) => {
|
|
|
|
const [selectedAccount, setSelectedAccount] = useState<number>();
|
|
|
|
const onAccountClickCallback = useCallback(
|
2021-07-14 02:10:31 +05:30
|
|
|
(account: Account): MouseEventHandler =>
|
|
|
|
async (event) => {
|
|
|
|
event.stopPropagation();
|
2020-10-23 03:53:59 +05:30
|
|
|
|
2021-07-14 02:10:31 +05:30
|
|
|
setSelectedAccount(account.id);
|
|
|
|
try {
|
|
|
|
if (onAccountClick) {
|
|
|
|
await onAccountClick(account);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
setSelectedAccount(undefined);
|
2020-10-23 03:53:59 +05:30
|
|
|
}
|
2021-07-14 02:10:31 +05:30
|
|
|
},
|
2020-10-23 03:53:59 +05:30
|
|
|
[onAccountClick],
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.accountSwitcher} data-testid="account-switcher">
|
|
|
|
{accounts.map((account, index) => (
|
|
|
|
<div
|
|
|
|
className={clsx(styles.item, {
|
|
|
|
[styles.inactiveItem]: selectedAccount && selectedAccount !== account.id,
|
2020-10-27 04:16:57 +05:30
|
|
|
[styles.deletedAccount]: account.isDeleted,
|
2020-10-23 03:53:59 +05:30
|
|
|
})}
|
|
|
|
key={account.id}
|
|
|
|
data-e2e-account-id={account.id}
|
|
|
|
onClick={onAccountClickCallback(account)}
|
|
|
|
>
|
2020-10-27 04:16:57 +05:30
|
|
|
<PseudoAvatar index={index} deleted={account.isDeleted} className={styles.accountAvatar} />
|
2020-10-23 03:53:59 +05:30
|
|
|
<div className={styles.accountInfo}>
|
|
|
|
<div className={styles.accountUsername}>{account.username}</div>
|
|
|
|
<div className={styles.accountEmail}>{account.email}</div>
|
|
|
|
</div>
|
|
|
|
{selectedAccount === account.id ? (
|
|
|
|
<ComponentLoader skin="light" className={styles.accountLoader} />
|
|
|
|
) : (
|
|
|
|
<div className={styles.nextIcon} />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AccountSwitcher;
|