import { StackScreenProps } from '@react-navigation/stack'; import React, { useState, useCallback, useEffect } from 'react'; import { Platform, View, KeyboardAvoidingView } from 'react-native'; import { Div, Button, Image, Text, Avatar } from 'react-native-magnus'; import { GiftedChat } from 'react-native-gifted-chat'; import { ScrollView } from 'react-native-gesture-handler'; import { useRequest, useMount } from '@umijs/hooks'; import { useTranslation } from 'react-i18next'; import * as Localization from 'expo-localization'; import useModel from 'flooks'; import User from '../flooks/User'; import IM from './model.ts'; import { parse } from '../Utils/TimeFnUtils.ts'; export default function ChatScreen({ navigation, route }: StackScreenProps) { const { params } = route; const { toUserId, toUserName, type } = params; const { t } = useTranslation(); if (toUserName) { navigation.setOptions({ title: toUserName, }); } const { id, userInfo, chatInfo } = useModel(User, [ 'id', 'chatInfo', 'userInfo', ]); const [messages, setMessages] = useState([]); const { sendMessage } = useModel(IM, []); const { loading, run } = useRequest( `/chat/showChat?userOne=${id}&userTwo=${toUserId}`, { manual: true, defaultLoading: false, initialData: [], onSuccess: (data) => { const list = data.map((item, index) => { return { _id: index, text: item.content, createdAt: parse(item.sendTime), user: { _id: item.sendUserId, name: item.sendNickName, avatar: item.sendAvatar, }, }; }); setMessages(list); }, } ); React.useEffect(() => { if (chatInfo && chatInfo.from === toUserId.toString()) { run(); } }, [chatInfo]); useMount(() => { run().then(() => { console.log(type); if (type === 'Reminder') { onSend([ { text: `${userInfo.nickname}催单了`, user: { _id: userInfo.id, avatar: userInfo.avatar, }, }, ]); } }); }); // eslint-disable-next-line no-shadow const onSend = useCallback((messages = []) => { console.log(messages); sendMessage(messages[0].text, toUserId); setMessages((previousMessages) => GiftedChat.append(previousMessages, messages) ); }, []); return (
onSend(messages)} showUserAvatar user={{ _id: userInfo.id, avatar: userInfo.avatar, }} /> {/* {Platform.OS === 'android' && } */}
); }