ChatScreen.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { StackScreenProps } from '@react-navigation/stack';
  2. import React, { useState, useCallback, useEffect } from 'react';
  3. import { Platform, View, KeyboardAvoidingView } from 'react-native';
  4. import { Div, Button, Image, Text, Avatar } from 'react-native-magnus';
  5. import { GiftedChat } from 'react-native-gifted-chat';
  6. import { ScrollView } from 'react-native-gesture-handler';
  7. import { useRequest, useMount } from '@umijs/hooks';
  8. import { useTranslation } from 'react-i18next';
  9. import * as Localization from 'expo-localization';
  10. import useModel from 'flooks';
  11. import User from '../flooks/User';
  12. import IM from './model.ts';
  13. import { parse } from '../Utils/TimeFnUtils.ts';
  14. export default function ChatScreen({ navigation, route }: StackScreenProps) {
  15. const { params } = route;
  16. const { toUserId, toUserName, type } = params;
  17. const { t } = useTranslation();
  18. if (toUserName) {
  19. navigation.setOptions({
  20. title: toUserName,
  21. });
  22. }
  23. const { id, userInfo, chatInfo } = useModel(User, [
  24. 'id',
  25. 'chatInfo',
  26. 'userInfo',
  27. ]);
  28. const [messages, setMessages] = useState([]);
  29. const { sendMessage } = useModel(IM, []);
  30. const { loading, run } = useRequest(
  31. `/chat/showChat?userOne=${id}&userTwo=${toUserId}`,
  32. {
  33. manual: true,
  34. defaultLoading: false,
  35. initialData: [],
  36. onSuccess: (data) => {
  37. const list = data.map((item, index) => {
  38. return {
  39. _id: index,
  40. text: item.content,
  41. createdAt: parse(item.sendTime),
  42. user: {
  43. _id: item.sendUserId,
  44. name: item.sendNickName,
  45. avatar: item.sendAvatar,
  46. },
  47. };
  48. });
  49. setMessages(list);
  50. },
  51. }
  52. );
  53. React.useEffect(() => {
  54. if (chatInfo && chatInfo.from === toUserId.toString()) {
  55. run();
  56. }
  57. }, [chatInfo]);
  58. useMount(() => {
  59. run().then(() => {
  60. console.log(type);
  61. if (type === 'Reminder') {
  62. onSend([
  63. {
  64. text: `${userInfo.nickname}催单了`,
  65. user: {
  66. _id: userInfo.id,
  67. avatar: userInfo.avatar,
  68. },
  69. },
  70. ]);
  71. }
  72. });
  73. });
  74. // eslint-disable-next-line no-shadow
  75. const onSend = useCallback((messages = []) => {
  76. console.log(messages);
  77. sendMessage(messages[0].text, toUserId);
  78. setMessages((previousMessages) =>
  79. GiftedChat.append(previousMessages, messages)
  80. );
  81. }, []);
  82. return (
  83. <Div flex={1} bg="gray100">
  84. <GiftedChat
  85. placeholder={t('qing-shu-ru-liao-tian-nei-rong')}
  86. messages={messages}
  87. // eslint-disable-next-line no-shadow
  88. onSend={(messages) => onSend(messages)}
  89. showUserAvatar
  90. user={{
  91. _id: userInfo.id,
  92. avatar: userInfo.avatar,
  93. }}
  94. />
  95. {/* {Platform.OS === 'android' && <KeyboardAvoidingView behavior="padding" />} */}
  96. </Div>
  97. );
  98. }