fix(tui): order messages by creation time (#40994)
Co-authored-by: Dax <mail@thdxr.com>
This commit is contained in:
committed by
GitHub
parent
a6d88d99d5
commit
b173ba95bd
@@ -51,6 +51,12 @@ function search<T>(items: T[], target: string, key: (item: T) => string) {
|
||||
return { found: false, index: left }
|
||||
}
|
||||
|
||||
function compareMessage(a: Message, b: Message) {
|
||||
return a.time.created - b.time.created || a.id.localeCompare(b.id)
|
||||
}
|
||||
|
||||
const messageKey = (message: Message) => message.time.created + message.id
|
||||
|
||||
export const {
|
||||
context: SyncContext,
|
||||
use: useSync,
|
||||
@@ -319,7 +325,7 @@ export const {
|
||||
setStore("message", event.properties.info.sessionID, [event.properties.info])
|
||||
break
|
||||
}
|
||||
const result = search(messages, event.properties.info.id, (m) => m.id)
|
||||
const result = search(messages, messageKey(event.properties.info), messageKey)
|
||||
if (result.found) {
|
||||
setStore("message", event.properties.info.sessionID, result.index, reconcile(event.properties.info))
|
||||
break
|
||||
@@ -355,13 +361,13 @@ export const {
|
||||
case "message.removed": {
|
||||
touchMessage(event.properties.sessionID, event.properties.messageID)
|
||||
const messages = store.message[event.properties.sessionID]
|
||||
const result = search(messages, event.properties.messageID, (m) => m.id)
|
||||
if (result.found) {
|
||||
const index = messages.findIndex((message) => message.id === event.properties.messageID)
|
||||
if (index !== -1) {
|
||||
setStore(
|
||||
"message",
|
||||
event.properties.sessionID,
|
||||
produce((draft) => {
|
||||
draft.splice(result.index, 1)
|
||||
draft.splice(index, 1)
|
||||
}),
|
||||
)
|
||||
}
|
||||
@@ -374,7 +380,7 @@ export const {
|
||||
setStore("part", event.properties.part.messageID, [event.properties.part])
|
||||
break
|
||||
}
|
||||
const result = search(parts, event.properties.part.id, (p) => p.id)
|
||||
const result = search(parts, event.properties.part.id, (part) => part.id)
|
||||
if (result.found) {
|
||||
setStore("part", event.properties.part.messageID, result.index, reconcile(event.properties.part))
|
||||
break
|
||||
@@ -392,7 +398,7 @@ export const {
|
||||
case "message.part.delta": {
|
||||
const parts = store.part[event.properties.messageID]
|
||||
if (!parts) break
|
||||
const result = search(parts, event.properties.partID, (p) => p.id)
|
||||
const result = search(parts, event.properties.partID, (part) => part.id)
|
||||
if (!result.found) break
|
||||
touchPart(event.properties.sessionID, event.properties.partID)
|
||||
setStore(
|
||||
@@ -411,7 +417,7 @@ export const {
|
||||
case "message.part.removed": {
|
||||
touchPart(event.properties.sessionID, event.properties.partID)
|
||||
const parts = store.part[event.properties.messageID]
|
||||
const result = search(parts, event.properties.partID, (p) => p.id)
|
||||
const result = search(parts, event.properties.partID, (part) => part.id)
|
||||
if (result.found) {
|
||||
setStore(
|
||||
"part",
|
||||
@@ -615,6 +621,7 @@ export const {
|
||||
(message) => tracker.messages.has(message.id) && !infos.some((item) => item.id === message.id),
|
||||
),
|
||||
)
|
||||
infos.sort(compareMessage)
|
||||
const removed = infos.slice(0, -100)
|
||||
const visible = infos.slice(-100)
|
||||
const visibleIDs = new Set(visible.map((message) => message.id))
|
||||
|
||||
Reference in New Issue
Block a user