import React, { useState } from 'react' import { ScrollView, StyleSheet, TouchableOpacity, View } from 'react-native' import { Text } from '../../Text' import { useContactState } from 'tuikit-atomicx-react-native' import { useTranslation } from 'react-i18next' import { Avatar as DefaultAvatar } from '../../Avatar/Avatar' import { rpxToPx } from '../../../utils/rpxToPx' import type { FriendApplicationInfo } from '../types' import type { NewContactInfoProps, NewContactInfoEmits } from '../types' import { showToast } from '../../../utils/toast' export interface NewContactInfoComponentProps extends NewContactInfoProps { onAccept?: NewContactInfoEmits['onAccept'] onReject?: NewContactInfoEmits['onReject'] } export const NewContactInfo: React.FC = ({ application = null, Avatar = DefaultAvatar, onAccept, onReject }) => { const { t } = useTranslation() const { acceptFriendApplication, refuseFriendApplication } = useContactState() const [isHandled, setIsHandled] = useState(false) const [handledResult, setHandledResult] = useState<'accepted' | 'rejected' | null>(null) const handledText = handledResult === 'accepted' ? t('contact.accepted') : handledResult === 'rejected' ? t('contact.rejected') : '' const handleAccept = async (): Promise => { if (!application || isHandled) return try { await acceptFriendApplication(application as any) setIsHandled(true) setHandledResult('accepted') showToast(t('contact.accepted')) onAccept?.(application) } catch (error: any) { const msg = (error && error.message) || t('toast.operationFailed') showToast(`${msg}`) } } const handleReject = async (): Promise => { if (!application || isHandled) return try { await refuseFriendApplication(application as any) setIsHandled(true) setHandledResult('rejected') showToast(t('contact.rejected')) onReject?.(application) } catch (error: any) { const msg = (error && error.message) || t('toast.operationFailed') showToast(`${msg}`) } } return ( {application?.title} ID: {application?.userID} {application?.signature ? ( {t('contact.signature')}{application.signature} ) : null} {t('contact.verifyMessage')} {application?.addWording || t('common.none')} {!isHandled ? ( { handleAccept() }} > {t('contact.accept')} { handleReject() }} > {t('contact.reject')} ) : ( {handledText} )} ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5F5F5' }, scroll: { flex: 1 }, header: { flexDirection: 'row', padding: rpxToPx(32), backgroundColor: '#FFFFFF' }, headerInfo: { flex: 1, marginLeft: rpxToPx(30), paddingBottom: rpxToPx(20) }, name: { fontSize: rpxToPx(36), fontWeight: '500', color: 'rgba(0, 0, 0, 0.9)', lineHeight: rpxToPx(48) }, id: { fontSize: rpxToPx(26), color: 'rgba(0, 0, 0, 0.4)', lineHeight: rpxToPx(40), marginTop: rpxToPx(8) }, signature: { fontSize: rpxToPx(26), color: 'rgba(0, 0, 0, 0.4)', lineHeight: rpxToPx(40), marginTop: rpxToPx(4) }, section: { marginBottom: rpxToPx(20), paddingHorizontal: rpxToPx(32), backgroundColor: '#FFFFFF' }, row: { flexDirection: 'row', alignItems: 'center', paddingVertical: rpxToPx(20), borderTopWidth: 1, borderTopColor: '#E5E5E5' }, label: { fontSize: rpxToPx(28), fontWeight: '400', lineHeight: rpxToPx(40), color: '#888888' }, value: { flex: 1, marginLeft: rpxToPx(10), fontSize: rpxToPx(28), fontWeight: '400', lineHeight: rpxToPx(40), color: '#444444' }, actions: { backgroundColor: '#FFFFFF' }, btn: { alignItems: 'center', justifyContent: 'center', paddingVertical: rpxToPx(30), borderBottomWidth: 2, borderBottomColor: '#E5E5E5' }, btnText: { fontSize: rpxToPx(34), fontWeight: '400', lineHeight: rpxToPx(52) }, btnTextPrimary: { color: '#1C66E5' }, btnTextDanger: { color: '#E54545' }, handled: { paddingVertical: rpxToPx(40), paddingHorizontal: rpxToPx(32), alignItems: 'center', backgroundColor: '#FFFFFF', marginTop: rpxToPx(20) }, handledText: { fontSize: rpxToPx(28), color: '#999999' }, }) export default NewContactInfo