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

78 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-12-17 13:10:38 +00:00
import {
Dispatch,
FunctionComponent,
SetStateAction,
useEffect,
useState,
} from "react";
2022-12-19 08:30:00 +00:00
import { ChainItemType } from "../../types";
2022-12-15 10:06:44 +00:00
2022-12-19 08:30:00 +00:00
import styled from "styled-components";
import AllChainsIcon from "../../public/images/svg/all-chains-icon.svg";
import color from "../../styles/color";
2022-12-15 10:06:44 +00:00
import { Flex1 } from "../../styles/flex-1";
2022-12-19 08:30:00 +00:00
import { Checkbox } from "../checkbox";
import {
ChainInfoContainer,
ChainItemContainer,
2022-12-15 10:06:44 +00:00
ChainName,
WalletAddress,
} from "./chain-item";
interface Props {
2022-12-17 13:10:38 +00:00
chainList: ChainItemType[];
checkedItems: Set<unknown>;
setCheckedItems: Dispatch<SetStateAction<Set<unknown>>>;
}
export const AllChainsItem: FunctionComponent<Props> = (props) => {
2022-12-17 13:10:38 +00:00
const { chainList, checkedItems, setCheckedItems } = props;
const [checked, setChecked] = useState(false);
const checkHandler = () => {
2022-12-17 13:10:38 +00:00
if (checked) {
setCheckedItems(new Set());
} else if (chainList.length !== checkedItems.size) {
setCheckedItems(new Set(chainList));
}
};
2022-12-17 13:10:38 +00:00
useEffect(() => {
if (chainList.length === checkedItems.size && checkedItems.size !== 0) {
setChecked(true);
} else {
setChecked(false);
}
}, [checkedItems]);
return (
<AllChainsContainer>
<ChainItemContainer
2022-12-17 13:10:38 +00:00
key="all chains"
isLoading={false}
2022-12-17 13:10:38 +00:00
checked={checked}
onClick={checkHandler}
>
2022-12-19 08:30:00 +00:00
<AllChainsIcon />
<ChainInfoContainer>
2022-12-19 10:46:52 +00:00
<ChainName>{`All chains(${chainList.length})`}</ChainName>
2022-12-17 13:10:38 +00:00
<WalletAddress>
{chainList.map((chain) => chain.chainName).join(", ")}
</WalletAddress>
</ChainInfoContainer>
<Flex1 />
2022-12-17 13:10:38 +00:00
<Checkbox checked={checked} />
</ChainItemContainer>
</AllChainsContainer>
);
};
const AllChainsContainer = styled.div`
width: 100%;
2022-12-15 15:43:18 +00:00
background-color: ${color.grey["900"]};
`;