import React, { useEffect, useMemo, useState } from 'react' import { ScrollView, StyleSheet, View } from 'react-native' import { Text } from '../Text' import { useTranslation } from 'react-i18next' import { useContactState, useConversationListState, ReceiveMessageOpt, } from 'tuikit-atomicx-react-native' import { Avatar } from '../Avatar/Avatar' import { rpxToPx } from '../../utils/rpxToPx' import { ButtonCell, NavigateCell, ToggleCell } from '../Cell' import { TextInputPopup } from '../TextInputPopup/TextInputPopup' import { ConfirmDialog } from '../ConfirmDialog/ConfirmDialog' import type { ContactInfo, ConversationInfo } from './types/contact' import { showToast } from '../../utils' export interface C2CChatSettingProps { conversationID: string showType?: number onBack?: () => void } export const C2CChatSetting: React.FC = ({ conversationID, showType = 0, onBack, }) => { const { t } = useTranslation() const userID = useMemo( () => (conversationID.startsWith('c2c_') ? conversationID.replace('c2c_', '') : ''), [conversationID] ) const contactState = useContactState('c2cSetting') const convListState = useConversationListState('c2cSetting') const [userInfo, setUserInfo] = useState(null) const [currentConversation, setCurrentConversation] = useState(null) const [isFirstLoading, setIsFirstLoading] = useState(true) const [showRemarkPopup, setShowRemarkPopup] = useState(false) const [showDeleteConfirm, setShowDeleteConfirm] = useState(false) const friendList: any[] = Array.isArray(contactState.friendList) ? (contactState.friendList as any[]) : [] const blackList: any[] = Array.isArray(contactState.blackList) ? (contactState.blackList as any[]) : [] const friendRemark = useMemo(() => { const fromFriend = friendList.find((f) => f?.userID === userID)?.friendRemark return fromFriend || userInfo?.friendRemark || '' }, [friendList, userID, userInfo]) const isFriend: boolean = userInfo?.isFriend ?? false const isInBlacklist: boolean = useMemo(() => { const inBL = blackList.find((f) => f?.userID === userID) return !!inBL || userInfo?.isInBlacklist === true }, [blackList, userID, userInfo]) const isNotDisturb: boolean = useMemo(() => { const opt = currentConversation?.receiveOption if (opt === undefined) return false return opt !== ReceiveMessageOpt.RECEIVE }, [currentConversation]) const fetchPeerUserInfo = async (): Promise => { if (!userID) return try { const list: any[] = await contactState.getContactInfo([userID]) if (Array.isArray(list) && list.length > 0) { const first = list[0] setUserInfo({ userID: first.userID ?? userID, nickname: first.nickname, friendRemark: first.friendRemark, avatarURL: first.avatarURL, aboutMe: first.aboutMe, isFriend: first.isFriend, isInBlacklist: first.isInBlacklist, }) } } catch (e: any | null) { console.error(`[C2CChatSetting] fetchPeerUserInfo failed: ${e != null ? `${e}` : 'null'}`) } } const fetchConversationInfo = async (): Promise => { if (!conversationID) return try { const info: any = await convListState.getConversationInfo(conversationID) if (info) { setCurrentConversation({ conversationID: info.conversationID, showName: info.showName, conversationType: info.conversationType, receiveOption: info.receiveOption, isPinned: info.isPinned, unreadCount: info.unreadCount, markList: info.markList, }) } } catch (e: any | null) { console.error(`[C2CChatSetting] fetchConversationInfo failed: ${e != null ? `${e}` : 'null'}`) } } useEffect((): (() => void) | void => { if (!userID) return if (currentConversation != null) { setIsFirstLoading(false) return } void fetchPeerUserInfo() void fetchConversationInfo().then((): void => { setIsFirstLoading(false) }) return undefined }, [userID, conversationID]) const handleRemarkClick = (): void => { setShowRemarkPopup(true) } const handleRemarkConfirm = async (newRemark: string): Promise => { if (!userID) return try { await contactState.setFriendRemark(userID, newRemark) await fetchPeerUserInfo() } catch (e: any | null) { console.error(`[C2CChatSetting] setFriendRemark failed: ${e != null ? `${e}` : 'null'}`) } } const handleNotDisturbChange = async (value: boolean): Promise => { if (!userID) return try { const opt: number = value ? ReceiveMessageOpt.NOT_NOTIFY : ReceiveMessageOpt.RECEIVE await convListState.setReceiveMessageOpt(`c2c_${userID}`, opt) await fetchConversationInfo() } catch (e: any | null) { console.error(`[C2CChatSetting] setReceiveMessageOpt failed: ${e != null ? `${e}` : 'null'}`) } } const handleBlacklistChange = async (value: boolean): Promise => { if (!userID) return try { if (value) { await contactState.addToBlacklist(userID) } else { await contactState.removeFromBlacklist(userID) } await fetchPeerUserInfo() } catch (e: any | null) { console.error(`[C2CChatSetting] blacklist operation failed: ${e != null ? `${e}` : 'null'}`) } } const handleSendMessage = (): void => { if (showType === 0) { onBack?.() } } const handleDeleteFriendClick = (): void => { setShowDeleteConfirm(true) } const handleDeleteCancel = (): void => { setShowDeleteConfirm(false) } const handleDeleteConfirm = async (): Promise => { setShowDeleteConfirm(false) if (!userID) return try { await contactState.deleteFriend(userID) onBack?.() } catch (e: any | null) { console.error(`[C2CChatSetting] deleteFriend failed: ${e != null ? `${e}` : 'null'}`) } } const nickname = userInfo?.nickname || '' const avatarURL = userInfo?.avatarURL || '' const signature = userInfo?.aboutMe || '' const isPinned: boolean = currentConversation?.isPinned ?? false return ( {isFirstLoading && userInfo == null ? ( ) : ( {nickname || userID} ID: {userID || '-'} {signature ? `${t('contact.signature')}${signature}` : `${t('contact.signature')}${t('common.unset')}`} {isFriend && ( )} { void handleNotDisturbChange(v) }} /> { try { await convListState.pinConversation(conversationID, v) await fetchConversationInfo() } catch (e: any | null) { console.error(`[C2CChatSetting] pinConversation failed: ${e != null ? `${e}` : 'null'}`) const msg: string = `${e?.message ?? e ?? ''}` if (msg === 'conversation not exists') { showToast(t('conversation.notExists')) } else { showToast(msg) } } }} /> { void handleBlacklistChange(v) }} /> {isFriend && ( )} )} {} { void handleRemarkConfirm(v) }} onCancel={(): void => setShowRemarkPopup(false)} /> { void handleDeleteConfirm() }} onCancel={handleDeleteCancel} /> ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5F5F5', }, content: { flex: 1, }, userInfo: { flexDirection: 'row', paddingTop: rpxToPx(24), paddingBottom: rpxToPx(24), paddingHorizontal: rpxToPx(32), backgroundColor: '#FFFFFF', }, userInfoAvatarWrapper: { marginRight: rpxToPx(30), }, userInfoDetail: { flex: 1, justifyContent: 'center', }, userInfoNickname: { fontSize: rpxToPx(36), fontWeight: '500', color: 'rgba(0, 0, 0, 0.9)', marginBottom: rpxToPx(8), maxWidth: rpxToPx(400), }, userInfoId: { fontSize: rpxToPx(26), color: 'rgba(0, 0, 0, 0.5)', marginBottom: rpxToPx(4), }, userInfoSignature: { fontSize: rpxToPx(26), color: 'rgba(0, 0, 0, 0.5)', maxWidth: rpxToPx(550), }, sectionGap: { height: rpxToPx(20), backgroundColor: '#F0F2F7', }, safeAreaBottom: { height: rpxToPx(68), }, skeletonBlock: { height: rpxToPx(36), width: rpxToPx(280), backgroundColor: '#E5E5E5', borderRadius: rpxToPx(8), marginBottom: rpxToPx(12), }, skeletonBlockThin: { height: rpxToPx(26), width: rpxToPx(200), backgroundColor: '#E5E5E5', borderRadius: rpxToPx(8), }, skeletonAvatar: { width: rpxToPx(96), height: rpxToPx(96), borderRadius: rpxToPx(8), backgroundColor: '#E5E5E5', marginRight: rpxToPx(30), }, skeletonRow: { height: rpxToPx(96), backgroundColor: '#FFFFFF', borderBottomWidth: rpxToPx(1), borderBottomColor: '#E5E5E5', paddingHorizontal: rpxToPx(32), justifyContent: 'center', }, }) export default C2CChatSetting