icns-frontend/components/connect-wallet-modal/wallet-item.tsx

192 lines
4.7 KiB
TypeScript
Raw Normal View History

import {
Dispatch,
FunctionComponent,
SetStateAction,
useEffect,
useState,
} from "react";
2022-12-12 06:51:25 +00:00
import ArrowRightIcon from "../../public/images/svg/arrow-right.svg";
import color from "../../styles/color";
import { Flex1 } from "../../styles/flex-1";
import styled from "styled-components";
2022-12-14 07:52:05 +00:00
import {
2022-12-15 15:43:18 +00:00
MINIMUM_VERSION,
2022-12-14 07:52:05 +00:00
SELECTED_WALLET_KEY,
WALLET_INSTALL_URL,
WalletType,
} from "../../constants/wallet";
import { getKeplrFromWindow, KeplrWallet } from "../../wallets";
2022-12-15 15:43:18 +00:00
import {
KEPLR_NO_ACCOUNT_ERROR,
KEPLR_NO_ACCOUNT_MESSAGE,
2022-12-15 15:43:18 +00:00
KEPLR_NOT_FOUND_ERROR,
KEPLR_VERSION_ERROR,
} from "../../constants/error-message";
import semver from "semver/preload";
import { ErrorMessage } from "../../types";
import { MAIN_CHAIN_ID } from "../../constants/icns";
2022-12-12 06:51:25 +00:00
interface Props {
wallet: WalletType;
setErrorMessage: Dispatch<SetStateAction<ErrorMessage | undefined>>;
setErrorModalOpen: Dispatch<SetStateAction<boolean>>;
2022-12-20 07:58:21 +00:00
setBeforeYouStartModalOpen: Dispatch<SetStateAction<boolean>>;
setWalletKeyName: Dispatch<SetStateAction<string>>;
setSelectedWalletItem: Dispatch<SetStateAction<WalletType | undefined>>;
2022-12-12 06:51:25 +00:00
}
export const WalletItem: FunctionComponent<Props> = (props: Props) => {
2022-12-20 07:58:21 +00:00
const {
wallet,
setErrorModalOpen,
setErrorMessage,
setBeforeYouStartModalOpen,
setWalletKeyName,
setSelectedWalletItem,
} = props;
2022-12-15 15:43:18 +00:00
const [isInstalled, setIsInstalled] = useState<boolean>();
useEffect(() => {
setIsInstalled(!!window.keplr);
}, []);
2022-12-12 06:51:25 +00:00
const onClickWalletItem = async () => {
2022-12-20 07:58:21 +00:00
setSelectedWalletItem(wallet);
try {
if (wallet.name === "Keplr") {
await connectKeplr();
localStorage.setItem(SELECTED_WALLET_KEY, wallet.name);
}
2022-12-12 06:51:25 +00:00
2022-12-20 07:58:21 +00:00
setBeforeYouStartModalOpen(true);
} catch (error) {
if (error instanceof Error) {
console.log(error.message);
if (error.message === KEPLR_NO_ACCOUNT_ERROR) {
setErrorMessage({ message: KEPLR_NO_ACCOUNT_MESSAGE });
setErrorModalOpen(true);
return;
}
setErrorMessage({ message: error.message });
setErrorModalOpen(true);
}
}
};
const connectKeplr = async () => {
const keplr = await getKeplrFromWindow();
if (keplr === undefined) {
2022-12-14 07:52:05 +00:00
window.location.href = WALLET_INSTALL_URL;
2022-12-15 15:43:18 +00:00
throw new Error(KEPLR_NOT_FOUND_ERROR);
}
if (semver.lt(keplr.version, MINIMUM_VERSION)) {
throw new Error(KEPLR_VERSION_ERROR);
}
if (keplr) {
const wallet = new KeplrWallet(keplr);
2022-12-20 07:58:21 +00:00
const chainIds = (await wallet.getChainInfosWithoutEndpoints()).map(
(c) => c.chainId,
);
await wallet.init(chainIds);
const walletKey = await wallet.getKey(MAIN_CHAIN_ID);
2022-12-20 07:58:21 +00:00
setWalletKeyName(walletKey.name);
// FIXME: Probably able to make memory leak.
// setWalletKeyName should be persistent, so it is fine for now.
// We should remove event listener well.
window.addEventListener("keplr_keystorechange", async () => {
const walletKey = await wallet.getKey(MAIN_CHAIN_ID);
setWalletKeyName(walletKey.name);
});
}
2022-12-12 06:51:25 +00:00
};
return (
2022-12-19 07:38:50 +00:00
<WalleButton disabled={!wallet.isReady} onClick={onClickWalletItem}>
2022-12-19 08:30:00 +00:00
<WalletIconContainer>
<wallet.IconComponent width="60" height="60" />
</WalletIconContainer>
2022-12-12 06:51:25 +00:00
<WalletContentContainer>
<WalletName>{wallet.name}</WalletName>
2022-12-15 15:43:18 +00:00
{wallet.isReady ? (
isInstalled ? null : (
<WalletDescription>Go to install Keplr Extension</WalletDescription>
)
) : (
2022-12-19 07:38:50 +00:00
<WalletDescription>Coming soon</WalletDescription>
2022-12-12 06:51:25 +00:00
)}
</WalletContentContainer>
<Flex1 />
2022-12-19 08:30:00 +00:00
<ArrowRightIcon />
2022-12-19 07:38:50 +00:00
</WalleButton>
2022-12-12 06:51:25 +00:00
);
};
2022-12-19 07:38:50 +00:00
const WalleButton = styled.button`
2022-12-12 06:51:25 +00:00
display: flex;
flex-direction: row;
align-items: center;
2022-12-19 07:38:50 +00:00
border: none;
2022-12-12 06:51:25 +00:00
height: 5.8rem;
padding: 1rem;
background-color: ${color.grey["600"]};
2022-12-19 07:38:50 +00:00
&:hover:not(:disabled) {
background-color: ${color.grey["700"]};
cursor: pointer;
}
&:disabled {
opacity: 0.3;
cursor: not-allowed;
}
2022-12-12 06:51:25 +00:00
`;
2022-12-19 08:30:00 +00:00
const WalletIconContainer = styled.div`
2022-12-12 06:51:25 +00:00
position: relative;
width: 3.75rem;
height: 3.75rem;
`;
const WalletContentContainer = styled.div`
display: flex;
flex-direction: column;
align-items: flex-start;
padding-left: 1rem;
`;
const WalletName = styled.div`
font-family: "Inter", serif;
font-style: normal;
font-weight: 600;
font-size: 1.3rem;
line-height: 1.5rem;
text-align: center;
color: ${color.white};
`;
const WalletDescription = styled.div`
font-family: "Inter", serif;
font-style: normal;
font-weight: 600;
font-size: 0.8rem;
line-height: 1.1rem;
color: ${color.grey["300"]};
`;