import React, { useEffect, useMemo, useState } from 'react' import { Image, Keyboard, ScrollView, StyleSheet, TextInput, TouchableOpacity, View } from 'react-native' import { Text } from '../Text' import { useLoginState, useGroupState, MessageInputState } from 'tuikit-atomicx-react-native' import { rpxToPx } from '../../utils/rpxToPx' import { GROUP_TYPES, DEFAULT_GROUP_AVATARS, getGroupTypeById, type GroupType } from './constants/groupTypes' import { safeJsonParse } from './utils/utsUtils' import { useTranslation } from 'react-i18next' import { iconAssets } from '../../static/iconBase64' import { showToast } from '../../utils/toast' type SendMessagePayload = | { type: 'text'; text: string } | { type: 'custom'; customData: string; description?: string; extensionInfo?: string } | { type: 'image'; imagePath: string; imageWidth?: number; imageHeight?: number } | { type: 'audio'; audioFilePath: string; duration: number } | { type: 'video'; videoFilePath: string; videoType?: string; duration?: number; snapshotPath: string; snapshotWidth?: number; snapshotHeight?: number; } | { type: 'file'; filePath: string; fileName: string; fileSize?: number } | { type: 'face'; index: number; data: string } export interface CreateGroupUserLite { userID: string nickname: string } export interface CreateGroupProps { selectedUsers?: CreateGroupUserLite[] controlledGroupType?: GroupType } export interface CreateGroupEmits { onBack?: () => void onCreateSuccess?: (info: { groupID: string conversationID: string }) => void onNavigateToGroupType?: (currentTypeId: string) => void onGroupTypeSelected?: (groupType: GroupType) => void } const ARROW_RIGHT_ICON = iconAssets['components/ChatSetting/assets/nvue_arrow-right.png'] const GROUP_TYPE_NAME_KEYS: Record = { Work: 'createGroup.workName', Public: 'createGroup.publicName', Meeting: 'createGroup.meetingName', AVChatRoom: 'createGroup.avchatroomName', } const GROUP_TYPE_DESC_KEYS: Record = { Work: 'createGroup.workDescription', Public: 'createGroup.publicDescription', Meeting: 'createGroup.meetingDescription', AVChatRoom: 'createGroup.avchatroomDescription', } type TFunction = (key: string, options?: any) => string const resolveGroupTypeName = (id: string, t: TFunction): string => { const key = GROUP_TYPE_NAME_KEYS[id] return key ? t(key) : t('createGroup.unknownGroupType') } const resolveGroupTypeDescription = (id: string, t: TFunction): string => { const key = GROUP_TYPE_DESC_KEYS[id] return key ? t(key) : '' } export const CreateGroup: React.FC = ({ selectedUsers = [], controlledGroupType, onBack, onCreateSuccess, onNavigateToGroupType, onGroupTypeSelected }) => { const { t } = useTranslation() const { loginUserInfo } = useLoginState() const { createGroup } = useGroupState() const [groupName, setGroupName] = useState('') const [groupID, setGroupID] = useState('') const [selectedAvatarIndex, setSelectedAvatarIndex] = useState(0) const [avatarLoadedMap, setAvatarLoadedMap] = useState>({}) const [selectedGroupType, setSelectedGroupType] = useState( GROUP_TYPES[0] ) const [isCreating, setIsCreating] = useState(false) const canCreate = useMemo( () => groupName.trim().length > 0 && !isCreating, [groupName, isCreating] ) useEffect((): void => { setIsCreating(false) }, []) useEffect((): (() => void) | void => { if (!onGroupTypeSelected) return return (): void => { } }, [onGroupTypeSelected]) useEffect((): void => { if (controlledGroupType != null) { setSelectedGroupType(controlledGroupType) } }, [controlledGroupType]) const onAvatarLoad = (index: number): void => { setAvatarLoadedMap((prev) => ({ ...prev, [index]: true })) } const onAvatarError = (index: number): void => { setAvatarLoadedMap((prev) => ({ ...prev, [index]: true })) console.warn(t('createGroup.avatarLoadFailed', { index })) } const blurAllInputs = (inputRefs: Array<{ blur: () => void } | null>): void => { inputRefs.forEach((ref) => { if (ref != null && typeof ref.blur === 'function') { try { ref.blur() } catch (e) { } } }) } const handleGroupTypeSelect = (): void => { Keyboard.dismiss() if (onNavigateToGroupType) { onNavigateToGroupType(selectedGroupType.id) } } const handleAvatarSelect = (index: number): void => { setSelectedAvatarIndex(index) } const handleGroupCreatedSuccess = async (createdGroupID: string): Promise => { const conversationID = `group_${createdGroupID}` const messageInputState = MessageInputState.getInstance(conversationID) try { const customPayload: SendMessagePayload = { type: 'custom', customData: JSON.stringify({ version: 1, businessID: 'group_create', opUser: loginUserInfo?.userID ?? '', content: t('createGroup.systemMessageContent'), cmd: 0 }), } await messageInputState?.sendMessage(customPayload) } catch (e) { console.error('[CreateGroup] sendMessage(custom) failed:', e) } if (onCreateSuccess) { onCreateSuccess({ groupID: createdGroupID, conversationID }) } } const handleCreate = async (): Promise => { if (!canCreate || isCreating) return setIsCreating(true) try { const memberList: string[] = (selectedUsers || []).map((u) => u.userID) const createdGroupID = await createGroup({ groupType: selectedGroupType.id, groupName, groupID, avatarURL: DEFAULT_GROUP_AVATARS[selectedAvatarIndex], memberList } as any) if (!createdGroupID) { showToast(t('createGroup.createFailed')) setIsCreating(false) return } handleGroupCreatedSuccess(createdGroupID) } catch (error: any) { let errMsg: string = error?.message || t('createGroup.createFailed') const rawMsg: string = String(error?.message || '') if (error && error.errCode === 80001) { const match = rawMsg.match(/beat word:\s*([^|]+?)\s*(\||$)/i) const beatWord: string = match ? match[1].trim() : '' errMsg = beatWord ? t('createGroup.createFailedBanned', { word: beatWord }) : t('createGroup.createFailedBannedShort') } showToast(errMsg) setIsCreating(false) } } void onGroupTypeSelected return ( {t('createGroup.groupName')} {t('createGroup.groupId')} {t('createGroup.groupType')} {resolveGroupTypeName(selectedGroupType.id, t)} › {resolveGroupTypeDescription(selectedGroupType.id, t)} {t('createGroup.groupAvatar')} {DEFAULT_GROUP_AVATARS.map((avatarURL, index) => ( handleAvatarSelect(index)} > onAvatarLoad(index)} onError={(): void => onAvatarError(index)} /> {!avatarLoadedMap[index] && ( )} ))} {t('createGroup.create')} ) } const styles = StyleSheet.create({ createGroup: { flex: 1, backgroundColor: '#F5F5F5' }, formContainer: { flex: 1, flexDirection: 'column' }, formContent: { flex: 1, paddingBottom: rpxToPx(180) }, formItem: { height: rpxToPx(120), backgroundColor: '#FFFFFF', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: rpxToPx(32), marginTop: rpxToPx(20) }, formItemConnected: { marginTop: rpxToPx(2) }, formItemClickable: { height: rpxToPx(120), backgroundColor: '#FFFFFF', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: rpxToPx(32), marginTop: rpxToPx(2) }, formLabel: { fontSize: rpxToPx(32), color: '#000000', fontWeight: '400', opacity: 0.55 }, formInput: { flex: 1, fontSize: rpxToPx(32), color: '#333333', textAlign: 'right', paddingVertical: 0 }, formValue: { flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-end' }, formText: { fontSize: rpxToPx(32), color: '#333333', marginRight: rpxToPx(16) }, arrowIcon: { fontSize: rpxToPx(40), color: '#999999', lineHeight: rpxToPx(40) }, groupTypeDesc: { paddingHorizontal: rpxToPx(32), paddingVertical: rpxToPx(10), borderRadius: rpxToPx(16) }, descText: { fontSize: rpxToPx(28), color: '#666666', lineHeight: rpxToPx(40) }, avatarSection: { marginTop: rpxToPx(20), backgroundColor: '#FFFFFF', borderRadius: rpxToPx(16), paddingHorizontal: rpxToPx(32), paddingVertical: rpxToPx(32) }, sectionTitle: { fontSize: rpxToPx(32), color: '#000000', fontWeight: '400', opacity: 0.55, marginBottom: rpxToPx(20) }, avatarGrid: { flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between' }, avatarWrapper: { width: rpxToPx(128), height: rpxToPx(128), marginBottom: rpxToPx(24), borderRadius: rpxToPx(20), alignItems: 'center', justifyContent: 'center', backgroundColor: 'transparent' }, avatarWrapperSelected: { backgroundColor: '#1C66E5' }, avatarItem: { width: rpxToPx(120), height: rpxToPx(120), borderRadius: rpxToPx(16), overflow: 'hidden', position: 'relative' }, avatarImage: { width: rpxToPx(120), height: rpxToPx(120), backgroundColor: '#F5F5F5', borderRadius: rpxToPx(16) }, avatarLoading: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, alignItems: 'center', justifyContent: 'center', backgroundColor: '#F8F9FA' }, loadingPlaceholder: { width: rpxToPx(60), height: rpxToPx(60), borderRadius: rpxToPx(30), backgroundColor: '#E9ECEF' }, footer: { height: rpxToPx(180), backgroundColor: '#FFFFFF', alignItems: 'flex-end', justifyContent: 'center', paddingHorizontal: rpxToPx(32), borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: '#E5E5E5' }, createButton: { width: rpxToPx(153), height: rpxToPx(60), borderRadius: rpxToPx(4), backgroundColor: '#1C66E5', alignItems: 'center', justifyContent: 'center' }, createButtonDisabled: { backgroundColor: '#CCE2FF' }, createButtonText: { fontSize: rpxToPx(32), color: '#FFFFFF', fontWeight: '500' }, }) export default CreateGroup