import React, { useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Conversation } from '@tencentcloud/chat'; import { Input } from '../Input'; import { Icon, IconTypes } from '../Icon'; import { ConversationCreateSelectView, ConversationCreateSelectViewProps } from './ConversationCreateSelectView'; import { Avatar, defaultUserAvatar } from '../Avatar'; import { useConversationCreate } from './hooks/useConversationCreate'; import { useTUIKitContext } from '../../context'; import { PageStateTypes } from './ConversationCreate'; import { useConversation } from '../../hooks'; import { Toast } from '../Toast'; export interface ConversationCreateUserSelectListProps extends ConversationCreateSelectViewProps{ isCreateGroup: boolean, setIsCreateGroup: React.Dispatch>, setConversationCreated: React.Dispatch>, className: string, conversationList: Array, setPageState: React.Dispatch>, } export function ConversationCreateUserSelectList(props: ConversationCreateUserSelectListProps) { const { isCreateGroup, selectList, setSelectList, className, conversationList, setIsCreateGroup, setConversationCreated, setPageState, } = props; const { t } = useTranslation(); const [searchValue, setSearchValue] = useState(''); const { chat, setActiveConversation } = useTUIKitContext(); const [friendList, setFriendList] = useState({}); const { getFriendListSortSearchResult, } = useConversationCreate(chat, conversationList, (newFriendListResult) => { setFriendList(newFriendListResult); }); const { createConversation } = useConversation(chat); const userCheckedList = useRef(new Map()); const searchValueChange = async (e) => { const { value } = e.target; setSearchValue(value); setFriendList(await getFriendListSortSearchResult(value)); }; const createGroup = () => { setIsCreateGroup(true); userCheckedList.current.clear(); setSelectList([]); }; const getUserChecked = (userID, dom) => { if (!dom) return; if (!userCheckedList.current.has(userID)) { userCheckedList.current.set(userID, []); } const list = userCheckedList.current.get(userID); if (list.length !== 0 && list.some((item) => item.id === dom.id)) { return; } list.push(dom); }; const userSelectListChange = (e, profile, domList = []) => { const { userID } = profile; const { checked } = e.target; // eslint-disable-next-line no-param-reassign domList.forEach((dom) => { dom.checked = checked; }); if (checked) { selectList.push({ profile, domList }); } else { selectList.splice(selectList.findIndex((item) => item.profile.userID === userID), 1); } setSelectList([...selectList]); }; const createC2CConversation = async (profile) => { if (isCreateGroup) return; const { userID } = profile; const conversation = await createConversation(`C2C${userID}`); setConversationCreated(false); setActiveConversation(conversation); }; const next = () => { if (selectList && selectList.length === 0) { Toast({ text: t('TUIConversation.Participant cannot be empty'), type: 'error' }); return; } setPageState(PageStateTypes.CREATE_DETAIL); }; return ( <> } /> {(isCreateGroup) && ( )} {!isCreateGroup && (
{t('TUIConversation.New group chat')}
)}
{Object.keys(friendList).map( (key) => friendList[key].length !== 0 && (
{key}
{friendList[key].map((profile, index) => { const { userID, nick, avatar } = profile; return ( ); })}
), )}
{isCreateGroup && (
{t('TUIConversation.Next')}
)} ); }