Skip to content

Commit

Permalink
fix: refactor lastMessageMap computation
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Oct 31, 2024
1 parent e6b504a commit 11ebb70
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/composables/useDocumentTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function useDocumentTitle() {
const defaultPageTitle = ref<string>(getDefaultPageTitle())
const savedLastMessageMap = ref<Record<string, number>>({})

const actorId = computed(() => store.getters.getActorId())
const actorType = computed(() => store.getters.getActorType())
/**
* Keeps a list for all last message ids
*
Expand All @@ -36,27 +38,27 @@ export function useDocumentTitle() {
return {}
}

const lastMessage: Record<string, number> = {}
conversationList.forEach((conversation: Conversation) => {
lastMessage[conversation.token] = 0
if (!Array.isArray(conversation.lastMessage)) {
const currentActorIsAuthor = conversation.lastMessage.actorType === store.getters.getActorType()
&& conversation.lastMessage.actorId === store.getters.getActorId()
if (currentActorIsAuthor) {
// Set a special value when the actor is the author so we can skip it.
// Can't use 0 though because hidden commands result in 0
// and they would hide other previously posted new messages
lastMessage[conversation.token] = -1
} else {
lastMessage[conversation.token] = Math.max(
// @ts-expect-error: id is missing for federated conversations
conversation.lastMessage?.id ? conversation.lastMessage.id : 0,
store.getters.getLastKnownMessageId(conversation.token) ? store.getters.getLastKnownMessageId(conversation.token) : 0,
)
}
return conversationList.reduce((acc: Record<string, number>, conversation: Conversation) => {
const { token, lastMessage } = conversation
// Default to 0 for messages without valid lastMessage
if (!lastMessage || Array.isArray(lastMessage)) {
acc[token] = 0
return acc
}
})
return lastMessage

if (lastMessage.actorId === actorId.value && lastMessage.actorType === actorType.value) {
// Set a special value when the actor is the author so we can skip it.
// Can't use 0 though because hidden commands result in 0,
// and they would hide other previously posted new messages
acc[token] = -1
} else {
// @ts-expect-error: Property 'id' does not exist on type ChatProxyMessage
const lastMessageId = lastMessage.id ?? 0
const lastKnownMessageId = store.getters.getLastKnownMessageId(token) ?? 0
acc[token] = Math.max(lastMessageId, lastKnownMessageId)
}
return acc
}, {})
})

watch(lastMessageMap, (newValue) => {
Expand Down

0 comments on commit 11ebb70

Please sign in to comment.