dsh-brain/src-client/format.ts

99 lines
2.4 KiB
TypeScript

// dsh-brain client formatting helpers and label tables.
export function formatTime(value) {
if (typeof value !== 'number' || value <= 0) return '—'
var date = new Date(value)
var pad = function (n) {
return n < 10 ? '0' + String(n) : String(n)
}
return pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds())
}
export function formatIso(value) {
if (typeof value !== 'string' || value === '') return '—'
var stamp = Date.parse(value)
if (!Number.isFinite(stamp)) return value
return formatTime(stamp)
}
export function formatBytes(value) {
if (typeof value !== 'number' || value < 0) return '—'
if (value < 1024) return String(value) + ' B'
if (value < 1024 * 1024) return (value / 1024).toFixed(1) + ' KB'
return (value / (1024 * 1024)).toFixed(2) + ' MB'
}
export function shortHash(value) {
if (typeof value !== 'string' || value === '') return '—'
return value.slice(0, 12)
}
export function readStored(key, fallback) {
try {
var raw = window.localStorage.getItem('dsh-brain:' + key)
if (raw === null || raw === '') raw = window.localStorage.getItem('dsh-cloud-memory:' + key)
return raw === null || raw === '' ? fallback : raw
} catch {
return fallback
}
}
export function store(key, value) {
try {
window.localStorage.setItem('dsh-brain:' + key, String(value))
} catch {
/* a locked-down window keeps the value in memory only */
}
}
export const OUTCOME_LABELS = {
recorded: '已上报',
duplicate: '重复忽略',
failed: '失败',
queued: '待重试',
skipped: '已跳过',
}
export const OUTCOME_TONES = {
recorded: 'ok',
duplicate: 'muted',
failed: 'bad',
queued: 'warn',
skipped: 'muted',
}
export const ORIGIN_LABELS = {
cloud: '云端下发',
uploaded: '本地已上传',
local: '本机安装',
unknown: '来源未知',
}
export const CHECK_TONES = {
ok: 'ok',
unconfigured: 'muted',
invalid: 'bad',
forbidden: 'bad',
unreachable: 'bad',
error: 'bad',
}
export const CHECK_LABELS = {
ok: '可用',
unconfigured: '未配置',
invalid: '令牌失效',
forbidden: '权限不足',
unreachable: '不可达',
error: '出错',
}
export const MCP_STATE_LABELS = {
connected: '已连接',
connecting: '连接中',
disabled: '已停用',
error: '异常',
expired: '授权过期',
unknown: '未知',
needs_auth: '待授权',
}