Skip to content

Commit

Permalink
Better way to check if messageId is included
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Nov 8, 2023
1 parent 5fbd1d4 commit 9cd4bbb
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/components/chats/ChatList/hooks/useGetChatElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function useGetMessageElement({

waitingMessageDataLoadedIds.forEach((messageId) => {
if (
checkIfMessageIdIsIncluded(messageId, renderedMessageIds) ||
checkIfMessageIdIsIncluded(client, messageId, renderedMessageIds) ||
!hasMore
) {
const resolvers = promiseRef.current.resolvers.get(messageId)
Expand All @@ -55,11 +55,12 @@ export default function useGetMessageElement({
resolvers.forEach((resolve) => resolve())
})
}
}, [renderedMessageIds, hasMore])
}, [client, renderedMessageIds, hasMore])

const loadMoreUntilMessageIdIsLoaded = useCallback(
async (messageId: string) => {
const isMessageIdIncluded = checkIfMessageIdIsIncluded(
client,
messageId,
renderedIdsRef.current
)
Expand All @@ -68,7 +69,7 @@ export default function useGetMessageElement({
await awaitableLoadMore()
await loadMoreUntilMessageIdIsLoaded(messageId)
},
[awaitableLoadMore, renderedIdsRef]
[client, awaitableLoadMore, renderedIdsRef]
)

const getMessageElementById = useCallback(
Expand All @@ -87,7 +88,9 @@ export default function useGetMessageElement({
await loadMoreUntilMessageIdIsLoaded(messageId)

// if its already rendered, get and return it
if (checkIfMessageIdIsIncluded(messageId, renderedIdsRef.current)) {
if (
checkIfMessageIdIsIncluded(client, messageId, renderedIdsRef.current)
) {
return document.getElementById(elementId)
}

Expand All @@ -105,7 +108,12 @@ export default function useGetMessageElement({

return document.getElementById(elementId)
},
[loadMoreUntilMessageIdIsLoaded, renderedIdsRef, waitAllMessagesLoadedRef]
[
client,
loadMoreUntilMessageIdIsLoaded,
renderedIdsRef,
waitAllMessagesLoadedRef,
]
)

const getMessageElementByTime = useCallback(
Expand Down Expand Up @@ -191,12 +199,18 @@ function useWaitMessagesLoading(isLoading: boolean) {
}
}

function checkIfMessageIdIsIncluded(messageId: string, messageIds: string[]) {
const parsedMessageId = Number(messageId)
if (isNaN(parsedMessageId)) return true
function checkIfMessageIdIsIncluded(
client: QueryClient,
messageId: string,
messageIds: string[]
) {
const oldestRenderedId = messageIds[messageIds.length - 1]
const oldestMessage = getPostQuery.getQueryData(client, oldestRenderedId)
const oldestMessageTime = oldestMessage?.struct.createdAtTime

const smallestId = Number(messageIds[messageIds.length - 1])
return parsedMessageId >= smallestId
const messageTime = getPostQuery.getQueryData(client, messageId)?.struct
.createdAtTime
return oldestMessageTime && messageTime && oldestMessageTime <= messageTime
}

export function getNearestMessageIdToTimeFromRenderedIds(
Expand Down

0 comments on commit 9cd4bbb

Please sign in to comment.