[sms-center-prototype-migration-SMS-05] expose unread and history failures

This commit is contained in:
yibaiba committed 2026-08-13 19:59:48 +08:00
1 parent 15adfe8534
commit e1c4eab446
7 files changed
+73 -24

No files matched your search

@@ -3,9 +3,11 @@ import { onUnmounted } from 'vue'
import { Delete24Regular } from '@vicons/fluent'
import type { SMSMessage } from '../../types/api'
import { formatDeviceDateTime } from '../../utils/deviceTime'
import EmptyState from '../EmptyState.vue'
defineProps<{
groups: readonly Readonly<{ date: string; items: SMSMessage[] }>[]
loading: boolean
canLoadMore: boolean
loadingMore: boolean
deletingId: number | null
@@ -67,6 +69,14 @@ onUnmounted(clearLongPress)
<template>
<div class="sms-timeline">
<div v-if="loading && groups.length === 0" class="sms-timeline-notice" role="status">正在加载短信历史…</div>
<EmptyState
v-else-if="groups.length === 0"
class="sms-timeline-empty"
title="暂无短信记录"
subtitle="当前接口未返回此会话的历史短信"
/>
<div v-if="canLoadMore" class="sms-load-more">
<el-button text type="primary" :loading="loadingMore" @click="emit('loadMore')">加载更多</el-button>
</div>
@@ -106,6 +116,8 @@ onUnmounted(clearLongPress)
<style scoped>
.sms-timeline { padding: 18px 22px 28px; }
.sms-timeline-notice,
.sms-timeline-empty { min-height: 240px; display: grid; place-items: center; color: var(--ui-text-muted); font-size: 12px; }
.sms-load-more { display: flex; justify-content: center; margin-bottom: 12px; }
.sms-day-group + .sms-day-group { margin-top: 22px; }
.sms-day-label { width: max-content; margin: 0 auto 18px; padding: 4px 10px; display: block; border: 1px solid var(--ui-border); border-radius: 999px; color: var(--ui-text-muted); font: 10px "v-mono", monospace; }
+2
View File
@@ -3,6 +3,7 @@ import { callService } from './http'
import type { ServiceResult } from '../types/domain'
import type { DeviceMgmtListItem } from '../types/api'
import type { SMSContactDTO, SMSMessageDTO, SmsThreadVM } from '../types/view-model'
import { normalizeSmsUnreadCount } from '../utils/smsPresentation'
export type SmsThreadQueryParams = {
peer: string
@@ -42,6 +43,7 @@ function normalizeThread(contact: SMSContactDTO): SmsThreadVM {
lastMessage: String(contact.last_content || '').slice(0, 80),
lastDeviceName: contact.device_name,
localPhone: contact.local_phone || '',
unreadCount: normalizeSmsUnreadCount(contact.unread_count),
peerLower: String(contact.peer || '').toLowerCase(),
lastMessageLower: String(contact.last_content || '').toLowerCase()
}
+1
View File
@@ -18,6 +18,7 @@ export type SmsThreadVM = {
lastMessage: string
lastDeviceName?: string
localPhone?: string
unreadCount: number
peerLower: string
lastMessageLower: string
}
+4
View File
@@ -61,6 +61,10 @@ export function smsUnreadBadge(unreadCount: number, readLocally = false): number
return Math.max(0, Number(unreadCount) || 0)
}
export function normalizeSmsUnreadCount(value: unknown): number {
return Math.max(0, Number(value) || 0)
}
export function smsPeerInitial(peer: string): string {
return Array.from(cleanValue(peer))[0]?.toLocaleUpperCase() || '—'
}
+36 -24
View File
@@ -30,6 +30,7 @@ type SmsThread = {
lastMessage: string
lastDeviceName?: string
localPhone?: string
unreadCount: number
peerLower: string
lastMessageLower: string
}
@@ -50,6 +51,7 @@ const messagesError = ref<{ message: string; status?: number; method?: string; u
const selectedDevice = ref<string>(typeof route.query.device === 'string' ? route.query.device : 'all')
const selectedThreadKey = ref<string>(typeof route.query.contact === 'string' ? route.query.contact : '')
const searchQuery = ref('')
const unreadPresentationVersion = ref(0)
const smsPageRef = ref<HTMLElement | null>(null)
const smsPageWidth = ref(0)
@@ -73,15 +75,6 @@ function lastSeenKey(threadKey: string) {
return `sms_thread_last_seen:${selectedDevice.value}:${threadKey}`
}
function getLastSeen(threadKey: string) {
try {
const v = localStorage.getItem(lastSeenKey(threadKey))
return v ? Number(v) || 0 : 0
} catch {
return 0
}
}
function setLastSeen(threadKey: string, ts: number) {
try {
localStorage.setItem(lastSeenKey(threadKey), String(ts || Date.now()))
@@ -99,6 +92,14 @@ const filteredThreads = computed(() => {
})
})
const presentedThreads = computed(() => {
void unreadPresentationVersion.value
return filteredThreads.value.map(thread => ({
...thread,
unreadCount: isThreadUnreadForDisplay(thread) ? Math.max(0, Number(thread.unreadCount) || 0) : 0
}))
})
const selectedThread = computed(() => {
return threads.value.find(t => t.key === selectedThreadKey.value) || null
})
@@ -190,6 +191,16 @@ const conversationContext = computed(() => createSmsConversationContext({
devices: devices.value
}))
function isThreadUnreadForDisplay(thread: SmsThread): boolean {
try {
const key = `sms_thread_last_seen:${selectedDevice.value}:${thread.key}`
const lastSeen = Number(localStorage.getItem(key) || 0)
return thread.lastTs > lastSeen
} catch {
return true
}
}
function normalizeQueryDevice(device: string) {
const v = String(device || '').trim()
if (!v || v === 'all') return undefined
@@ -210,15 +221,6 @@ function markThreadSeen(t: SmsThread | null) {
setLastSeen(t.key, t.lastTs)
}
function isUnread(t: SmsThread) {
return t.lastTs > getLastSeen(t.key)
}
function backToList() {
if (!selectedThreadKey.value) return
clearSelectedThread(true)
}
function scrollThreadToBottom() {
const doScroll = () => {
const wrap = detailScrollbar.value
@@ -268,7 +270,10 @@ async function loadMoreHistory() {
params.imsi = selectedThread.value.imsi
}
const result = await smsStore.fetchThread(params)
if (!result.ok) throw new Error(result.error.message)
if (!result.ok) {
messagesError.value = result.error
return
}
const list = (result.data || []) as SMSMessage[]
const merged = list.slice().sort((a, b) => parseTs(a.timestamp) - parseTs(b.timestamp) || a.id - b.id).concat(threadMessages.value)
threadMessages.value = merged
@@ -281,8 +286,9 @@ async function loadMoreHistory() {
const delta = Math.max(0, nextHeight - prevHeight)
w.scrollTop = prevTop + delta
})
} catch {
// Ignore history load errors to keep current thread state unchanged.
messagesError.value = null
} catch (error: unknown) {
messagesError.value = toAppError(error)
} finally {
loadingHistoryMore.value = false
}
@@ -411,7 +417,7 @@ async function ensureThreadSelection(options: { syncRoute?: boolean; silent?: bo
if (current) {
const ok = await fetchThreadLatest(silent)
if (ok) {
markThreadSeen(current)
await applyThreadSeen(current)
if (scrollToBottom) scrollThreadToBottom()
}
return
@@ -451,10 +457,15 @@ async function selectThread(key: string, options: { syncRoute?: boolean; silent?
const ok = await fetchThreadLatest(silent)
if (!ok) return
markThreadSeen(t)
await applyThreadSeen(t)
if (scrollToBottom) scrollThreadToBottom()
}
async function applyThreadSeen(thread: SmsThread) {
await markThreadSeen(thread)
unreadPresentationVersion.value += 1
}
async function handleSelectDevice(deviceId: string, options: { syncRoute?: boolean; silent?: boolean } = {}) {
const nextDevice = String(deviceId || 'all').trim() || 'all'
const syncRoute = options.syncRoute !== false
@@ -710,7 +721,7 @@ async function confirmDeleteThread(thread: SmsThread) {
<SmsThreadListPane
v-model="searchQuery"
:items="filteredThreads"
:items="presentedThreads"
:selected-key="selectedThreadKey"
:loading="loading"
:deleting-key="deletingThreadKey"
@@ -738,6 +749,7 @@ async function confirmDeleteThread(thread: SmsThread) {
<div v-else ref="detailScrollbar" class="flex-1 min-h-0 overflow-y-auto sms-detail-scroll" @scroll="onDetailScroll">
<SmsMessageTimeline
:groups="selectedThreadGroups"
:loading="threadLoading"
:can-load-more="canLoadMoreHistory"
:loading-more="loadingHistoryMore"
:deleting-id="deletingMessageId"
+7
View File
@@ -4,6 +4,7 @@ import type { DeviceMgmtListItem } from '../src/types/api'
import {
createSmsConversationContext,
createSmsDeviceChannels,
normalizeSmsUnreadCount,
smsPeerInitial,
smsUnreadBadge
} from '../src/utils/smsPresentation'
@@ -67,3 +68,9 @@ test('unread badges and initials are explicit and bounded', () => {
assert.equal(smsPeerInitial(' giffgaff '), 'G')
assert.equal(smsPeerInitial(''), '—')
})
test('normalizes backend unread counts without inventing negative values', () => {
assert.equal(normalizeSmsUnreadCount(7), 7)
assert.equal(normalizeSmsUnreadCount(-3), 0)
assert.equal(normalizeSmsUnreadCount(undefined), 0)
})
+11
View File
@@ -8,6 +8,7 @@ const threadList = readFileSync(new URL('../src/components/sms/SmsThreadListPane
const conversationHeader = readFileSync(new URL('../src/components/sms/SmsConversationHeader.vue', import.meta.url), 'utf8')
const messageTimeline = readFileSync(new URL('../src/components/sms/SmsMessageTimeline.vue', import.meta.url), 'utf8')
const composer = readFileSync(new URL('../src/components/sms/SmsComposer.vue', import.meta.url), 'utf8')
const smsService = readFileSync(new URL('../src/services/sms.ts', import.meta.url), 'utf8')
test('SMS workspace uses one continuous shell with dedicated navigation panes', () => {
assert.match(smsView, /<SmsDeviceRail/)
@@ -59,3 +60,13 @@ test('narrow SMS workspace keeps all three panes visible in the prototype compos
assert.match(smsView, /grid-template-columns:\s*52px minmax\(0, 1fr\)/)
assert.match(smsView, /@media \(prefers-reduced-motion: reduce\)/)
})
test('unread presentation and history failures stay explicit', () => {
assert.match(smsService, /unreadCount: normalizeSmsUnreadCount\(contact\.unread_count\)/)
assert.match(smsView, /:items="presentedThreads"/)
assert.match(smsView, /unreadCount: isThreadUnreadForDisplay\(thread\)/)
assert.match(smsView, /messagesError\.value = result\.error/)
assert.match(smsView, /messagesError\.value = toAppError\(error\)/)
assert.doesNotMatch(smsView, /Ignore history load errors/)
assert.match(messageTimeline, /当前接口未返回此会话的历史短信/)
})