mirror of https://github.com/msgbyte/tailchat
refactor: refactor ack logic to not fetch all acks but fetch only the ones that are needed
parent
5c338b5bb4
commit
14613cc973
@ -0,0 +1,41 @@
|
||||
import { useAppDispatch, useAppSelector } from './useAppSelector';
|
||||
import { chatActions } from '../slices';
|
||||
import { useEvent } from '../../hooks/useEvent';
|
||||
import { getCachedAckInfo } from '../../cache/cache';
|
||||
|
||||
export function useAckInfoChecker() {
|
||||
const ack = useAppSelector((state) => state.chat.ack);
|
||||
const lastMessageMap = useAppSelector((state) => state.chat.lastMessageMap);
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
const ensureAckInfo = useEvent((converseId: string) => {
|
||||
if (
|
||||
ack[converseId] === undefined ||
|
||||
lastMessageMap[converseId] === undefined
|
||||
) {
|
||||
getCachedAckInfo(converseId).then((info) => {
|
||||
if (info.ack?.lastMessageId) {
|
||||
dispatch(
|
||||
chatActions.setConverseAck({
|
||||
converseId,
|
||||
lastMessageId: info.ack.lastMessageId,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (info.lastMessage?.lastMessageId) {
|
||||
dispatch(
|
||||
chatActions.setLastMessageMap([
|
||||
{
|
||||
converseId,
|
||||
lastMessageId: info.lastMessage.lastMessageId,
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return { ensureAckInfo };
|
||||
}
|
Loading…
Reference in New Issue