Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 6x 6x 6x 6x 6x 6x 6x | import NiceModal from "@ebay/nice-modal-react";
import { useAccountSelection } from "@pages/Login/modals/hooks/useAccountSelectionModal";
import { toast } from "react-toastify";
import { ToastMessage } from "@common/Toast/ToastMessage";
import { SwitchAccountToastIcon } from "@assets/icons/SwitchAccountToastIcon";
import { useAppTheme } from "@theme/v2/Provider";
import { LOGOUT_MODAL } from "modals/modal_names";
import useRedirectionModal from "./useRedirectionModal";
type AccountSwitcherOpts = {
showToast?: boolean;
notificationMessageID?: string;
redirectUrl?: string;
};
export default function useAccountSwitcher() {
const { accountList, chooseAccount } = useAccountSelection();
const { showRedirectionModal } = useRedirectionModal();
const { palette } = useAppTheme();
const findAccountIndex = (id: string | number) =>
accountList?.findIndex((acc) => `${acc.id}` === `${id}`) ?? -1;
const switchToAccountByIndex = (
accountIndex: number,
opts?: AccountSwitcherOpts,
) => {
const account = accountList?.[accountIndex];
if (!account || account.selected) return;
chooseAccount(accountIndex, opts?.notificationMessageID, opts?.redirectUrl);
if (opts?.showToast) {
toast.info(
<ToastMessage
message={`Switched to ${account.name}`}
type="Info"
description=""
icon={<SwitchAccountToastIcon fill={palette.neutral.white} />}
/>,
{ autoClose: 2000 },
);
}
};
const handleAccountSwitch = (
targetAccountID: string | number,
options: AccountSwitcherOpts = { showToast: true },
onCancel?: () => void,
) => {
const idx = findAccountIndex(targetAccountID);
if (idx === -1) {
NiceModal.show(LOGOUT_MODAL, { onCancel });
return;
}
showRedirectionModal(() => {
switchToAccountByIndex(idx, options);
}, onCancel);
};
return { switchToAccountByIndex, handleAccountSwitch, findAccountIndex };
}
|