Files
AI-CS/frontend/utils/format.ts
T
2026-04-01 13:40:42 +08:00

80 lines
2.0 KiB
TypeScript

export function formatConversationTime(dateStr: string | null | undefined): string {
if (!dateStr) {
return "-";
}
const date = new Date(dateStr);
if (Number.isNaN(date.getTime())) {
return "-";
}
const now = new Date();
const diff = now.getTime() - date.getTime();
if (diff < 24 * 3600 * 1000 && date.getDate() === now.getDate()) {
return date.toLocaleTimeString("zh-CN", {
hour: "2-digit",
minute: "2-digit",
});
}
return date.toLocaleString("zh-CN", {
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
});
}
export function formatMessageTime(dateStr: string | null | undefined): string {
if (!dateStr) {
return "";
}
const date = new Date(dateStr);
if (Number.isNaN(date.getTime())) {
return "";
}
const now = new Date();
const diff = now.getTime() - date.getTime();
if (diff < 24 * 3600 * 1000 && date.getDate() === now.getDate()) {
return date.toLocaleTimeString("zh-CN", {
hour: "2-digit",
minute: "2-digit",
});
}
return date.toLocaleString("zh-CN", {
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
});
}
export function buildMessagePreview(content: string, maxLength = 50): string {
if (!content) {
return "";
}
if (content.length <= maxLength) {
return content;
}
return `${content.substring(0, maxLength)}...`;
}
// 判断访客是否在线(根据 last_seen_at 字段)
// 说明:10 秒阈值在公网环境(代理、弱网、移动端切后台)容易抖动,体验上会“刚说完就离线”。
// 这里放宽到 90 秒,减少误判闪断。
export function isVisitorOnline(lastSeenAt: string | null | undefined): boolean {
if (!lastSeenAt) {
return false;
}
const lastSeen = new Date(lastSeenAt);
if (Number.isNaN(lastSeen.getTime())) {
return false;
}
const now = new Date();
const diff = now.getTime() - lastSeen.getTime();
// 90 秒内认为在线
return diff < 90 * 1000;
}