2022-12-18 12:01:34 +00:00
|
|
|
import { ChainItemType, DisabledChainItemType } from "../../types";
|
2022-12-15 10:06:44 +00:00
|
|
|
import { FunctionComponent, useEffect, useState } from "react";
|
2022-12-09 11:59:52 +00:00
|
|
|
|
|
|
|
import color from "../../styles/color";
|
2022-12-12 06:51:25 +00:00
|
|
|
import { Flex1 } from "../../styles/flex-1";
|
2022-12-09 11:59:52 +00:00
|
|
|
import styled from "styled-components";
|
2022-12-12 13:45:24 +00:00
|
|
|
import { ChainImage } from "./chain-image";
|
2022-12-17 08:50:59 +00:00
|
|
|
import { Checkbox } from "../checkbox";
|
2022-12-09 11:59:52 +00:00
|
|
|
|
|
|
|
interface Props {
|
2022-12-18 12:01:34 +00:00
|
|
|
chainItem: ChainItemType | DisabledChainItemType;
|
2022-12-12 13:45:24 +00:00
|
|
|
checkedItemHandler: (chainItem: ChainItemType, isChecked: boolean) => void;
|
2022-12-13 12:21:16 +00:00
|
|
|
checkedItems: Set<unknown>;
|
2022-12-09 11:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const ChainItem: FunctionComponent<Props> = (props) => {
|
2022-12-18 12:01:34 +00:00
|
|
|
const { chainItem, checkedItemHandler, checkedItems } = props;
|
|
|
|
const disabled = "disabled" in chainItem && chainItem.disabled;
|
2022-12-18 12:09:34 +00:00
|
|
|
// XXX: Currently, this component can't handle `checked` state well,
|
|
|
|
// If chain is disabled, it should be disabled in general.
|
|
|
|
// However, if it is disabled due to the limitation of ethermint and ledger,
|
|
|
|
// it should be not checked.
|
|
|
|
// To solve this problem, for now, just use dumb way.
|
|
|
|
// If chain is disabled with explicit reason, it should be unchecked.
|
|
|
|
const [checked, setChecked] = useState(!!disabled && !chainItem.reason);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (disabled) {
|
|
|
|
if (chainItem.reason) {
|
|
|
|
setChecked(false);
|
|
|
|
} else {
|
|
|
|
setChecked(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [chainItem, disabled]);
|
2022-12-12 13:45:24 +00:00
|
|
|
|
2022-12-13 12:21:16 +00:00
|
|
|
const checkHandler = () => {
|
2022-12-15 10:06:44 +00:00
|
|
|
if (!disabled) {
|
|
|
|
setChecked(!checked);
|
|
|
|
checkedItemHandler(chainItem, !checked);
|
|
|
|
}
|
2022-12-12 13:45:24 +00:00
|
|
|
};
|
2022-12-13 12:21:16 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-12-15 10:06:44 +00:00
|
|
|
if (!disabled) {
|
|
|
|
setChecked(checkedItems.has(chainItem));
|
|
|
|
}
|
2022-12-13 12:21:16 +00:00
|
|
|
}, [checkedItems]);
|
|
|
|
|
2022-12-09 11:59:52 +00:00
|
|
|
return (
|
2022-12-13 12:21:16 +00:00
|
|
|
<ChainItemContainer
|
|
|
|
key={chainItem.prefix}
|
|
|
|
isLoading={false}
|
2022-12-15 10:06:44 +00:00
|
|
|
disabled={disabled}
|
2022-12-15 15:43:18 +00:00
|
|
|
checked={checked}
|
2022-12-13 12:21:16 +00:00
|
|
|
onClick={checkHandler}
|
|
|
|
>
|
2022-12-17 12:34:14 +00:00
|
|
|
<ChainImage
|
|
|
|
src={chainItem.chainImageUrl}
|
|
|
|
fill={true}
|
|
|
|
alt={`${chainItem.prefix} chain image`}
|
|
|
|
/>
|
2022-12-09 11:59:52 +00:00
|
|
|
<ChainInfoContainer>
|
2022-12-12 13:45:24 +00:00
|
|
|
<ChainName>{`.${chainItem.prefix}`}</ChainName>
|
2022-12-18 12:01:34 +00:00
|
|
|
{chainItem.address ? (
|
|
|
|
<WalletAddress>{chainItem.address}</WalletAddress>
|
|
|
|
) : null}
|
|
|
|
{disabled && chainItem.reason ? (
|
|
|
|
<DisabledReason>{chainItem.reason.message}</DisabledReason>
|
|
|
|
) : null}
|
2022-12-09 11:59:52 +00:00
|
|
|
</ChainInfoContainer>
|
|
|
|
|
|
|
|
<Flex1 />
|
|
|
|
|
2022-12-17 08:50:59 +00:00
|
|
|
<Checkbox checked={checked} />
|
2022-12-09 11:59:52 +00:00
|
|
|
</ChainItemContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-15 10:06:44 +00:00
|
|
|
export const ChainItemContainer = styled.div<{
|
|
|
|
isLoading: boolean;
|
|
|
|
checked?: boolean;
|
|
|
|
disabled?: boolean;
|
2022-12-17 07:48:35 +00:00
|
|
|
isSkeleton?: boolean;
|
2022-12-15 10:06:44 +00:00
|
|
|
}>`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
gap: 1rem;
|
|
|
|
|
|
|
|
padding: 1.5rem;
|
|
|
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
2022-12-15 15:43:18 +00:00
|
|
|
opacity: ${(props) => (props.disabled ? "0.5" : "1")};
|
|
|
|
|
|
|
|
background-color: ${(props) =>
|
|
|
|
props.disabled
|
|
|
|
? color.black
|
|
|
|
: props.checked
|
|
|
|
? color.grey["800"]
|
2022-12-17 07:48:35 +00:00
|
|
|
: props.isSkeleton
|
|
|
|
? color.grey["800"]
|
2022-12-15 15:43:18 +00:00
|
|
|
: color.grey["900"]};
|
2022-12-15 10:06:44 +00:00
|
|
|
|
|
|
|
&:hover {
|
2022-12-15 15:43:18 +00:00
|
|
|
background: ${(props) => (props.isLoading ? null : color.grey["700"])};
|
2022-12-15 10:06:44 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
export const ChainInfoContainer = styled.div`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
gap: 0.5rem;
|
|
|
|
`;
|
|
|
|
|
2022-12-13 12:21:16 +00:00
|
|
|
export const ChainName = styled.div`
|
2022-12-09 11:59:52 +00:00
|
|
|
font-weight: 600;
|
|
|
|
font-size: 0.8rem;
|
|
|
|
line-height: 1rem;
|
|
|
|
|
2022-12-13 12:21:16 +00:00
|
|
|
color: ${color.white};
|
2022-12-09 11:59:52 +00:00
|
|
|
`;
|
|
|
|
|
2022-12-13 12:21:16 +00:00
|
|
|
export const WalletAddress = styled.div`
|
2022-12-09 11:59:52 +00:00
|
|
|
font-weight: 500;
|
|
|
|
font-size: 0.8rem;
|
|
|
|
line-height: 1rem;
|
|
|
|
|
2022-12-13 12:21:16 +00:00
|
|
|
max-height: 2rem;
|
|
|
|
max-width: 27rem;
|
|
|
|
display: -webkit-box;
|
|
|
|
-webkit-line-clamp: 2;
|
|
|
|
-webkit-box-orient: vertical;
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
2022-12-09 11:59:52 +00:00
|
|
|
color: ${color.grey["400"]};
|
|
|
|
`;
|
2022-12-18 12:01:34 +00:00
|
|
|
|
|
|
|
export const DisabledReason = styled.div`
|
|
|
|
color: ${color.grey["200"]};
|
|
|
|
font-weight: 500;
|
|
|
|
font-size: 14px;
|
|
|
|
line-height: 17px;
|
|
|
|
`;
|