// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 import React, { useState, useEffect } from 'react'; import { Modal, ModalHeader, ModalBody, ModalButtonGroup, ModalButton, Checkbox, } from 'amazon-chime-sdk-component-library-react'; import { useIdentityService } from '../../providers/IdentityProvider'; import { useAuthContext } from '../../providers/AuthProvider'; import { useChatChannelState } from '../../providers/ChatMessagesProvider'; import { listSubChannels } from '../../api/ChimeAPI'; import './ChannelModals.css'; export const ListSubChannelModel = ({ onClose, channel, subChannelList, leaveSubChannel, joinSubChannel, }) => { const [formatedSubChannelList, setFormatedSubChannelList] = useState([]); const identityClient = useIdentityService(); const { useCognitoIdp, member: { userId } } = useAuthContext(); const { subChannelIds } = useChatChannelState(); const getAllSubChannelsForElasticChannel = () => { listSubChannels(channel.ChannelArn, userId, null) .then((subs) => { const list = subs.SubChannels.map((subChannel) => { return { SubChannelId: subChannel.SubChannelId, MembershipCount: subChannel.MembershipCount, joined: subChannelList.length ? subChannelList.findIndex((x) => { if (x.SubChannelId == subChannel.SubChannelId) { return true; } }) != -1 : false, }; }); setFormatedSubChannelList(list); }) .catch((err) => { throw new Error( `Failed at getAllSubChannelsForElasticChannel() with error: ${err}` ); }); }; useEffect(() => { if (!identityClient) return; if (useCognitoIdp) { identityClient.setupClient(); } getAllSubChannelsForElasticChannel(); }, [identityClient, useCognitoIdp]); const handleCheckClick = (e) => { const selected = formatedSubChannelList.filter( (subChannel) => subChannel && subChannel.SubChannelId == e.target.value )[0]; const isJoined = selected.joined; if (isJoined) { leaveSubChannel(selected.SubChannelId); } else { joinSubChannel(selected.SubChannelId); } selected.joined = !isJoined; }; const subChannelItems = formatedSubChannelList.map((subChannel) => (
{subChannel.SubChannelId} {subChannel.MembershipCount} handleCheckClick(e)} />
)); return (
Name Member Count Join
    {subChannelItems}
, ]} />
); }; export default ListSubChannelModel;