fix(admin-orders): 修复实时 SSE 订单推送未按时间倒序排列导致历史单跳到第 1 行的闪烁问题
1. 在 AdminLayout 的 patchAdminOrderListEnvelope 中接入 sortAdminOrdersDesc 排序引擎,严格按 paidAt / createdAt 倒序及 orderId 倒序排列; 2. 修复后台同步历史订单(如 15/16 号)时,SSE 事件盲目将其插入第一页顶部并在手动刷新后消失的跳跃与闪烁现象; 3. 新订单按时间自然沉降到正确的页码位置,只有属于当前页时间范围的新订单才会展示在第一页顶部。
This commit is contained in:
@@ -527,6 +527,28 @@ function patchAdminOrderCaches(
|
||||
}
|
||||
}
|
||||
|
||||
function getOrderSortTimestamp(item: unknown): number {
|
||||
if (!item || typeof item !== 'object') return 0
|
||||
const record = item as Record<string, unknown>
|
||||
const timeStr = String(record.paidAt || record.createdAt || '').trim()
|
||||
if (!timeStr) return 0
|
||||
const t = dayjs(timeStr).valueOf()
|
||||
return Number.isFinite(t) ? t : 0
|
||||
}
|
||||
|
||||
function sortAdminOrdersDesc(items: unknown[]): unknown[] {
|
||||
return [...items].sort((a, b) => {
|
||||
const timeA = getOrderSortTimestamp(a)
|
||||
const timeB = getOrderSortTimestamp(b)
|
||||
if (timeB !== timeA) {
|
||||
return timeB - timeA
|
||||
}
|
||||
const idA = Number((a as Record<string, unknown>)?.orderId || 0)
|
||||
const idB = Number((b as Record<string, unknown>)?.orderId || 0)
|
||||
return idB - idA
|
||||
})
|
||||
}
|
||||
|
||||
function patchAdminOrderListEnvelope(
|
||||
current: unknown,
|
||||
event: RealtimeEvent,
|
||||
@@ -544,6 +566,9 @@ function patchAdminOrderListEnvelope(
|
||||
? items.filter((entry) => Number((entry as { orderId?: unknown })?.orderId) !== id)
|
||||
: upsertById(items, event.data, 'orderId', insert)
|
||||
|
||||
// 严格保持按支付/创建时间倒序排列,避免后台同步历史单(如 15/16 号)时盲目插在第 1 行
|
||||
nextItems = sortAdminOrdersDesc(nextItems)
|
||||
|
||||
if (insert && nextItems.length > pageSize) {
|
||||
nextItems = nextItems.slice(0, pageSize)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user