icns-frontend/components/chain-list/chain-item.tsx

148 lines
3.6 KiB
TypeScript
Raw Normal View History

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";
import { ChainImage } from "./chain-image";
import { Checkbox } from "../checkbox";
2022-12-09 11:59:52 +00:00
interface Props {
chainItem: ChainItemType | DisabledChainItemType;
checkedItemHandler: (chainItem: ChainItemType, isChecked: boolean) => void;
checkedItems: Set<unknown>;
2022-12-09 11:59:52 +00:00
}
export const ChainItem: FunctionComponent<Props> = (props) => {
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]);
const checkHandler = () => {
2022-12-15 10:06:44 +00:00
if (!disabled) {
setChecked(!checked);
checkedItemHandler(chainItem, !checked);
}
};
useEffect(() => {
2022-12-15 10:06:44 +00:00
if (!disabled) {
setChecked(checkedItems.has(chainItem));
}
}, [checkedItems]);
2022-12-09 11:59:52 +00:00
return (
<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}
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>
<ChainName>{`.${chainItem.prefix}`}</ChainName>
{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 />
<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;
`;
export const ChainName = styled.div`
2022-12-09 11:59:52 +00:00
font-weight: 600;
font-size: 0.8rem;
line-height: 1rem;
color: ${color.white};
2022-12-09 11:59:52 +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;
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"]};
`;
export const DisabledReason = styled.div`
color: ${color.grey["200"]};
font-weight: 500;
font-size: 14px;
line-height: 17px;
`;