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.
23 lines
703 B
TypeScript
23 lines
703 B
TypeScript
3 years ago
|
import { useEffect } from 'react';
|
||
|
import { ChatMessage, useConverseAck, useUpdateRef } from 'tailchat-shared';
|
||
4 years ago
|
import _debounce from 'lodash/debounce';
|
||
3 years ago
|
import _last from 'lodash/last';
|
||
4 years ago
|
|
||
|
export function useMessageAck(converseId: string, messages: ChatMessage[]) {
|
||
3 years ago
|
const { updateConverseAck } = useConverseAck(converseId);
|
||
4 years ago
|
const messagesRef = useUpdateRef(messages);
|
||
3 years ago
|
const updateConverseAckRef = useUpdateRef(updateConverseAck);
|
||
4 years ago
|
|
||
|
useEffect(() => {
|
||
|
// 设置当前
|
||
|
if (messagesRef.current.length === 0) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
3 years ago
|
const lastMessageId = _last(messagesRef.current)!._id;
|
||
|
updateConverseAckRef.current(lastMessageId);
|
||
4 years ago
|
}, [converseId]);
|
||
|
|
||
|
return { updateConverseAck };
|
||
|
}
|