mirror of https://github.com/msgbyte/tailchat
perf: 优化消息已读容器逻辑,方便第三方复用
parent
b938fcb12c
commit
66a67bf02b
@ -0,0 +1,27 @@
|
||||
import { Intersection } from '@/components/Intersection';
|
||||
import React from 'react';
|
||||
import { useConverseAck, useMemoizedFn } from 'tailchat-shared';
|
||||
|
||||
/**
|
||||
* 消息已读回调容器
|
||||
* 套在消息体外面可以实现消息出现在视野内就发送已读提示
|
||||
*/
|
||||
interface MessageAckContainerProps extends React.PropsWithChildren {
|
||||
converseId: string;
|
||||
messageId: string;
|
||||
}
|
||||
export const MessageAckContainer: React.FC<MessageAckContainerProps> =
|
||||
React.memo((props) => {
|
||||
const { updateConverseAck } = useConverseAck(props.converseId);
|
||||
|
||||
const handleIntersection = useMemoizedFn(() => {
|
||||
updateConverseAck(props.messageId);
|
||||
});
|
||||
|
||||
return (
|
||||
<Intersection onIntersection={handleIntersection}>
|
||||
{props.children}
|
||||
</Intersection>
|
||||
);
|
||||
});
|
||||
MessageAckContainer.displayName = 'MessageAckContainer';
|
@ -1,24 +0,0 @@
|
||||
import { useEffect } from 'react';
|
||||
import { ChatMessage, sharedEvent, useConverseAck } from 'tailchat-shared';
|
||||
|
||||
/**
|
||||
* 消息已读的回调
|
||||
*/
|
||||
export function useMessageAck(converseId: string) {
|
||||
const { updateConverseAck } = useConverseAck(converseId);
|
||||
|
||||
useEffect(() => {
|
||||
const handleReadMessage = (message: ChatMessage | null) => {
|
||||
const messageId = message?._id;
|
||||
if (messageId && converseId === message.converseId) {
|
||||
updateConverseAck(messageId);
|
||||
}
|
||||
};
|
||||
|
||||
sharedEvent.on('readMessage', handleReadMessage);
|
||||
|
||||
return () => {
|
||||
sharedEvent.off('readMessage', handleReadMessage);
|
||||
};
|
||||
}, [converseId]);
|
||||
}
|
Loading…
Reference in New Issue