mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
784 B
TypeScript
33 lines
784 B
TypeScript
import { useEffect } from 'react';
|
|
import {
|
|
ChatMessage,
|
|
useConverseAck,
|
|
useMemoizedFn,
|
|
useUpdateRef,
|
|
} from 'tailchat-shared';
|
|
import _last from 'lodash/last';
|
|
|
|
/**
|
|
* 消息已读的回调
|
|
*/
|
|
export function useMessageAck(converseId: string, messages: ChatMessage[]) {
|
|
const { updateConverseAck } = useConverseAck(converseId);
|
|
const messagesRef = useUpdateRef(messages);
|
|
const updateConverseAckMemo = useMemoizedFn(updateConverseAck);
|
|
|
|
useEffect(() => {
|
|
// 设置当前
|
|
if (messagesRef.current.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const lastMessage = _last(messagesRef.current);
|
|
if (lastMessage) {
|
|
const lastMessageId = lastMessage?._id;
|
|
updateConverseAckMemo(lastMessageId);
|
|
}
|
|
}, [converseId]);
|
|
|
|
return { updateConverseAck };
|
|
}
|