Files
yunke/yunkes/src/utils/format.ts
T

48 lines
1.6 KiB
TypeScript
Raw Normal View History

/** Formatting helpers for chat timeline + customer list. */
/** ms-epoch → `YYYY-MM-DD HH:mm:ss` in local time. `--` for missing/invalid. */
export function formatTime(ts?: number | null): string {
if (!ts || !Number.isFinite(ts) || ts <= 0) return '--'
const d = new Date(ts)
if (Number.isNaN(d.getTime())) return '--'
const pad = (n: number) => String(n).padStart(2, '0')
return (
`${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ` +
`${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
)
}
/** ms-epoch → short `MM-DD HH:mm` for dense timelines. */
export function formatTimeShort(ts?: number | null): string {
if (!ts || !Number.isFinite(ts) || ts <= 0) return ''
const d = new Date(ts)
if (Number.isNaN(d.getTime())) return ''
const pad = (n: number) => String(n).padStart(2, '0')
return `${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}
/**
* For voice messages the backend stores duration in seconds (best-effort).
* Render as `N"` or `M'SS"` to mirror the messaging-app look.
*/
export function formatDuration(seconds?: number | null): string {
if (!seconds || !Number.isFinite(seconds) || seconds <= 0) return ''
const s = Math.round(seconds)
if (s < 60) return `${s}"`
const m = Math.floor(s / 60)
const rem = s % 60
return `${m}'${String(rem).padStart(2, '0')}"`
}
/** Friendly label for a Channel value. */
export function channelLabel(channel?: string | null): string {
switch (channel) {
case 'wechat':
return '微信'
case 'whatsapp':
return 'WhatsApp'
default:
return channel ?? '未知'
}
}