61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { t } from '@/i18n'
|
|
import { formatAbsoluteDateFromIso } from '@/lib/relativeDate'
|
|
import type { ApiKey } from '@/composables/useApiKeys'
|
|
|
|
const VISIBLE_PREFIX_LENGTH = 9
|
|
const END_OF_DAY = { hours: 23, minutes: 59, seconds: 59 }
|
|
const MINUTES_PER_HOUR = 60
|
|
|
|
export function displayPrefix(prefix: string) {
|
|
return `${prefix.slice(0, VISIBLE_PREFIX_LENGTH)}…`
|
|
}
|
|
|
|
function twoDigits(value: number) {
|
|
return String(value).padStart(2, '0')
|
|
}
|
|
|
|
function localEndOfDay(dateValue: string) {
|
|
const [year, month, day] = dateValue.split('-').map(Number)
|
|
return new Date(
|
|
year!,
|
|
month! - 1,
|
|
day,
|
|
END_OF_DAY.hours,
|
|
END_OF_DAY.minutes,
|
|
END_OF_DAY.seconds,
|
|
)
|
|
}
|
|
|
|
function localOffset(date: Date) {
|
|
const totalMinutes = -date.getTimezoneOffset()
|
|
const sign = totalMinutes < 0 ? '-' : '+'
|
|
const absolute = Math.abs(totalMinutes)
|
|
return `${sign}${twoDigits(Math.floor(absolute / MINUTES_PER_HOUR))}:${twoDigits(absolute % MINUTES_PER_HOUR)}`
|
|
}
|
|
|
|
export function expiryInstant(dateValue: string) {
|
|
const date = localEndOfDay(dateValue)
|
|
const day = `${date.getFullYear()}-${twoDigits(date.getMonth() + 1)}-${twoDigits(date.getDate())}`
|
|
return `${day}T${twoDigits(date.getHours())}:${twoDigits(date.getMinutes())}:${twoDigits(date.getSeconds())}${localOffset(date)}`
|
|
}
|
|
|
|
export function isFutureExpiry(dateValue: string, now: Date = new Date()) {
|
|
return localEndOfDay(dateValue).getTime() > now.getTime()
|
|
}
|
|
|
|
export function formatKeyDate(iso: string | null, emptyLabel: string) {
|
|
return iso ? formatAbsoluteDateFromIso(iso) : emptyLabel
|
|
}
|
|
|
|
export function lastUsedLabel(key: ApiKey) {
|
|
return formatKeyDate(key.last_used_at, t('Never'))
|
|
}
|
|
|
|
export function expiresLabel(key: ApiKey) {
|
|
return formatKeyDate(key.expires_at, t('None'))
|
|
}
|
|
|
|
export function creatorLabel(key: ApiKey) {
|
|
return key.created_by_name || '—'
|
|
}
|