Files
RenewCMS/web/src/lib/updateFormat.ts
T

107 lines
3.3 KiB
TypeScript

import { t } from '@/i18n'
import { formatAbsoluteDateFromIso, formatRelativeDate } from '@/lib/relativeDate'
export const ENABLE_BROWSER_UPDATES_COMMAND = 'renewcms enable-browser-updates'
export const AUTO_CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000
export function displayVersion(tag: string) {
return tag.replace(/^v/, '')
}
function isSameLocalDay(a: Date, b: Date) {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
)
}
export function formatClock(date: Date) {
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
return `${hours}:${minutes}`
}
export function formatLastChecked(iso: string, now: Date = new Date()) {
const date = new Date(iso)
const yesterday = new Date(now)
yesterday.setDate(now.getDate() - 1)
if (isSameLocalDay(date, now)) {
return t('Last checked today at {0}', [formatClock(date)])
}
if (isSameLocalDay(date, yesterday)) {
return t('Last checked yesterday at {0}', [formatClock(date)])
}
return t('Last checked {0} at {1}', [formatAbsoluteDateFromIso(iso), formatClock(date)])
}
export function isCheckDue(checkedAt: string | null, now: number = Date.now()) {
if (checkedAt === null) {
return true
}
return now - new Date(checkedAt).getTime() > AUTO_CHECK_INTERVAL_MS
}
export function formatReleased(iso: string) {
return t('Released {0}', [formatAbsoluteDateFromIso(iso)])
}
const BYTES_PER_MB = 1024 * 1024
const ANNOUNCEMENT_STEP_PERCENT = 25
const LAST_ANNOUNCED_PERCENT = 75
export function formatDownloadProgress(progress: {
percent: number
downloaded_bytes: number
total_bytes: number
}) {
const done = Math.round(progress.downloaded_bytes / BYTES_PER_MB)
const total = Math.round(progress.total_bytes / BYTES_PER_MB)
return t('{0}% · {1} of {2} MB', [progress.percent, done, total])
}
export function downloadAnnouncement(version: string, percent: number) {
const step = Math.min(
Math.floor(percent / ANNOUNCEMENT_STEP_PERCENT) * ANNOUNCEMENT_STEP_PERCENT,
LAST_ANNOUNCED_PERCENT,
)
const base = t('Downloading version {0}', [version])
return step > 0 ? t('{0}, {1} percent', [base, step]) : base
}
export const VERIFYING_ANNOUNCEMENT = 'Checking the download'
const BROWSER_UPDATES_DOCS_PATH = '/src/branch/main/docs/deployment.md#browser-updates'
const RELEASES_SEGMENT = /^(.*)\/releases\//
export function browserUpdatesDocsUrl(releaseUrl: string) {
try {
const url = new URL(releaseUrl)
const repository = RELEASES_SEGMENT.exec(url.pathname)
return repository ? `${url.origin}${repository[1]}${BROWSER_UPDATES_DOCS_PATH}` : releaseUrl
} catch {
return releaseUrl
}
}
export function formatStartedAt(iso: string) {
return t('Started at {0}', [formatClock(new Date(iso))])
}
export function formatStartedWithAge(iso: string, now: Date = new Date()) {
return `${formatStartedAt(iso)} · ${formatRelativeDate(iso, now)}`
}
export function formatUpdatedAt(iso: string, now: Date = new Date()) {
const date = new Date(iso)
const yesterday = new Date(now)
yesterday.setDate(now.getDate() - 1)
if (isSameLocalDay(date, now)) {
return t('Updated today at {0}', [formatClock(date)])
}
if (isSameLocalDay(date, yesterday)) {
return t('Updated yesterday at {0}', [formatClock(date)])
}
return t('Updated {0} at {1}', [formatAbsoluteDateFromIso(iso), formatClock(date)])
}