'use client' import React, {useEffect, useState} from 'react'; import {AUTH_TOKEN_KEY} from '../../constants/common'; import {getChatList, getChatMessages} from "../../actions/chat/groups"; import {useLocalStorage} from "../../hooks/useLocalStorage"; import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import {message} from "antd"; import {Loader} from "../view/Loader"; import SignalrConnection from "../../lib/signalr-connection"; import {CheckOutlined} from "@ant-design/icons"; dayjs.extend(relativeTime); type CompProps = { locale: string; groupId: number }; export const ChatMessages = ({ locale, groupId }: CompProps) => { const { newMessage, joinChat, readMessages, addListener } = SignalrConnection(); const [jwt] = useLocalStorage(AUTH_TOKEN_KEY, ''); //const messages = await getChatMessages(locale, jwt, groupId) const [loading, setLoading] = useState(false); const [text, setText] = useState(''); const [me, setMe] = useState(); const [notMe, setNotMe] = useState(); const [messages, setMessages] = useState([]); useEffect(() => { if (jwt) { setLoading(true); Promise.all([ getChatList(locale, jwt), getChatMessages(locale, jwt, groupId), ]) .then(([_groups, _messages]) => { // const _group = _groups.directs.find( (el: any) => el.group.id === groupId) if (_group.group.members[0].userId === parseInt(_group.userId)){ setMe(_group.group.members[0].user) setNotMe(_group.group.members[1].user) } else { setMe(_group.group.members[1].user) setNotMe(_group.group.members[0].user) } setMessages(_messages.messages); }) .catch((e) => { console.log(e) message.error('Не удалось загрузить данные'); }) .finally(() => { setLoading(false); }) } },[jwt]) const onConnected = (flag: boolean) =>{ if (flag) { joinChat(groupId) readUreaded() } } const readUreaded = () => { const msgs = [] as number[]; messages.forEach((message: any) => { if (!message.seen && message.sentByMe === false){ msgs.push(message.id) } }) readMessages(msgs) } const onReceiveMessage = (payload: any) => { const _messages = [... messages]; let flag = false; const msg = { data : payload.data, dataType: null, id: payload.id, receiverId:null, seen: false, senderId: payload.creatorId, sentAt: payload.createdUtc+'.000Z', sentByMe: payload.creatorId === me.id, text: payload.content, type:"text" } _messages.forEach((item: any, i) => { if (item.id === msg.id){ _messages[i] = msg flag = true } }) //console.log(payload, flag) if (flag){ setMessages([..._messages]); } else { setMessages([msg, ..._messages]); } readMessages([msg.id]) } const onOpponentRead = (payload: any) => { console.log('onOpponentRead', payload) const _messages = [... messages] as any[]; _messages.forEach((item: any, i) => { if (item.id === payload.messageId){ _messages[i].seen = true } }) setMessages([..._messages]) } useEffect(() => { addListener('onConnected', onConnected) addListener('ReceiveMessage', onReceiveMessage) addListener('MessageWasRead', onOpponentRead) }, [messages, me, setMessages]); const handleSendMessages = () => { newMessage({ GroupId: groupId.toString(), CreatorId: me.id, Content: text }) setText('') } const onEnterPress = (e: any) => { if(e.keyCode == 13 && e.shiftKey == false) { e.preventDefault(); handleSendMessages(); } } const handleChangeMessage = (e: any) =>{ const { value } = e.target; e.preventDefault(); setText(value); return true } const messageRender = (message: any, i:number) => { const item = message.sentByMe ? me : notMe const date = dayjs(message.sentAt).fromNow() const imgSrc = item?.faceImage?.descriptor ? 'http://static.bbuddy.expert/' + item.faceImage.descriptor.split(':')[1] : '' return (
{imgSrc && ()}
{message.text} { message?.seen && ()} {date}
) } return (
{messages.map((el, i)=> (messageRender(el, i)))}