Files
rhema-together/web/app/utils/format.ts
T
vhaudiquet 2216f37422 feat: pick the room model from a catalog dialog
The room model was a free-text field. It now opens a dialog that lists the provider's live catalog through the same adapter a turn uses, with context, output, price, and vision from models.dev. Rows filter by text, vision, or free price, and a custom id remains available when the provider lists nothing.

The dialog is nested over room settings, so BaseDialog now gives Escape and the focus trap to the top dialog only. Clearing the model stores NULL, which returns the room to the provider's default.
2026-11-28 17:21:48 +01:00

80 lines
2.6 KiB
TypeScript

const MINUTE = 60_000
const HOUR = 3_600_000
const DAY = 86_400_000
export function relativeTime(at: number | null | undefined): string {
if (!at) return ''
const diff = Date.now() - at
if (diff < 45_000) return 'just now'
if (diff < HOUR) return `${Math.max(1, Math.round(diff / MINUTE))}m ago`
if (diff < DAY) return `${Math.round(diff / HOUR)}h ago`
if (diff < 7 * DAY) return `${Math.round(diff / DAY)}d ago`
return new Date(at).toLocaleDateString('en', { month: 'short', day: 'numeric' })
}
export function fullTime(at: number | null | undefined): string {
if (!at) return ''
return new Date(at).toLocaleString('en', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
})
}
export function formatDate(at: number | null | undefined): string {
if (!at) return ''
return new Date(at).toLocaleDateString('en', { year: 'numeric', month: 'short', day: 'numeric' })
}
export function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`
if (bytes < 1024 * 1024) return `${Math.round((bytes / 1024) * 10) / 10} KB`
return `${Math.round((bytes / (1024 * 1024)) * 10) / 10} MB`
}
export function initials(name: string): string {
const parts = name.trim().split(/\s+/).filter(Boolean)
if (parts.length === 0) return '?'
const first = parts[0]?.[0] ?? ''
const second = parts.length > 1 ? (parts[parts.length - 1]?.[0] ?? '') : ''
return (first + second).toUpperCase()
}
export function formatCost(cost: number | null | undefined): string {
if (cost === null || cost === undefined) return ''
if (cost === 0) return '$0.00'
if (cost < 0.01) return `$${cost.toFixed(4)}`
return `$${cost.toFixed(2)}`
}
export function plural(count: number, one: string, many: string): string {
return count === 1 ? one : many
}
export function formatTokens(tokens: number | null | undefined): string {
if (tokens === null || tokens === undefined || tokens <= 0) return ''
if (tokens >= 1_000_000) {
const value = tokens / 1_000_000
return `${Number.isInteger(value) ? value : value.toFixed(1)}M`
}
if (tokens >= 1_000) {
const value = tokens / 1_000
return `${Number.isInteger(value) ? value : value.toFixed(0)}k`
}
return String(tokens)
}
export function formatModelPrice(value: number | null | undefined): string {
if (value === null || value === undefined) return ''
if (value === 0) return 'Free'
const digits = value < 0.01 ? 4 : 2
return new Intl.NumberFormat('en', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
maximumFractionDigits: digits,
}).format(value)
}