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

86 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Dispatch, FunctionComponent, SetStateAction, useEffect } from "react";
2022-12-15 10:06:44 +00:00
import { ChainItemType } from "../../types";
2022-12-09 11:59:52 +00:00
import color from "../../styles/color";
import styled from "styled-components";
import { ChainItem } from "./chain-item";
interface Props {
allChecked: boolean;
setAllChecked: Dispatch<SetStateAction<boolean>>;
chainList: ChainItemType[];
2022-12-15 10:06:44 +00:00
disabledChainList: ChainItemType[];
checkedItems: Set<unknown>;
setCheckedItems: Dispatch<SetStateAction<Set<unknown>>>;
2022-12-09 11:59:52 +00:00
}
export const ChainList: FunctionComponent<Props> = (props) => {
const {
allChecked,
setAllChecked,
chainList,
2022-12-15 10:06:44 +00:00
disabledChainList,
checkedItems,
setCheckedItems,
} = props;
const checkedItemHandler = (chainItem: ChainItemType, isChecked: boolean) => {
const tempSet = new Set(checkedItems);
if (isChecked) {
tempSet.add(chainItem);
} else if (!isChecked && checkedItems.has(chainItem)) {
tempSet.delete(chainItem);
}
setCheckedItems(tempSet);
};
useEffect(() => {
if (allChecked) {
setCheckedItems(new Set(chainList));
} else if (chainList.length === checkedItems.size) {
setCheckedItems(new Set());
}
}, [allChecked]);
useEffect(() => {
if (chainList.length === checkedItems.size && checkedItems.size !== 0) {
setAllChecked(true);
} else {
setAllChecked(false);
}
}, [checkedItems]);
2022-12-09 11:59:52 +00:00
return (
<ChainContainer color={color.grey["800"]}>
{chainList.map((chainItem) => (
<ChainItem
key={chainItem.address}
chainItem={chainItem}
checkedItemHandler={checkedItemHandler}
checkedItems={checkedItems}
/>
2022-12-09 11:59:52 +00:00
))}
2022-12-15 10:06:44 +00:00
{disabledChainList.map((chainItem) => (
<ChainItem
key={chainItem.address}
chainItem={chainItem}
checkedItemHandler={checkedItemHandler}
checkedItems={checkedItems}
disabled={true}
/>
))}
2022-12-09 11:59:52 +00:00
</ChainContainer>
);
};
export const ChainContainer = styled.div`
display: flex;
flex-direction: column;
width: 100%;
max-height: 33rem;
overflow: scroll;
background-color: ${(props) => props.color};
`;