106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
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 `Last checked today at ${formatClock(date)}`
|
|
}
|
|
if (isSameLocalDay(date, yesterday)) {
|
|
return `Last checked yesterday at ${formatClock(date)}`
|
|
}
|
|
return `Last checked ${formatAbsoluteDateFromIso(iso)} at ${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 `Released ${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 `${progress.percent}% · ${done} of ${total} MB`
|
|
}
|
|
|
|
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 = `Downloading version ${version}`
|
|
return step > 0 ? `${base}, ${step} percent` : 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 `Started at ${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 `Updated today at ${formatClock(date)}`
|
|
}
|
|
if (isSameLocalDay(date, yesterday)) {
|
|
return `Updated yesterday at ${formatClock(date)}`
|
|
}
|
|
return `Updated ${formatAbsoluteDateFromIso(iso)} at ${formatClock(date)}`
|
|
}
|