mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 22:18:43 +00:00
* feat(upload): simplify upload file list display and add live speed and ETA * fix(upload): retain authoritative server search results and handle local failed upload state gracefully
116 lines
3.2 KiB
TypeScript
116 lines
3.2 KiB
TypeScript
import { differenceInDays, format, isToday, isYesterday, parseISO } from 'date-fns'
|
|
import { TRASH_RETENTION_DAYS } from '@shumai/dtos'
|
|
import { m } from '@/ui/paraglide/messages.js'
|
|
|
|
const MS_PER_DAY = 24 * 60 * 60 * 1000
|
|
|
|
export const formatTimeAgo = (dateString: string) => {
|
|
const date = parseISO(dateString)
|
|
const now = new Date()
|
|
|
|
if (isNaN(date.getTime())) {
|
|
return dateString
|
|
}
|
|
|
|
const time = format(date, 'HH:mm')
|
|
|
|
if (isToday(date)) {
|
|
return m.today_with_time({ time })
|
|
}
|
|
if (isYesterday(date)) {
|
|
return m.yesterday_with_time({ time })
|
|
}
|
|
|
|
const daysAgo = differenceInDays(now, date)
|
|
const count = daysAgo >= 0 ? daysAgo : 0
|
|
return count === 1 ? m.n_days_ago_singular({ count }) : m.n_days_ago_plural({ count })
|
|
}
|
|
|
|
export const formatDateAgo = (dateString: string) => {
|
|
const date = parseISO(dateString)
|
|
const now = new Date()
|
|
|
|
if (isNaN(date.getTime())) {
|
|
return dateString
|
|
}
|
|
|
|
if (isToday(date)) {
|
|
return m.today()
|
|
}
|
|
if (isYesterday(date)) {
|
|
return m.yesterday()
|
|
}
|
|
|
|
const daysAgo = differenceInDays(now, date)
|
|
const count = daysAgo >= 0 ? daysAgo : 0
|
|
return count === 1 ? m.n_days_ago_singular({ count }) : m.n_days_ago_plural({ count })
|
|
}
|
|
|
|
export const formatRemainingTime = (targetDate: string | Date, now: Date = new Date()): string => {
|
|
const targetMs =
|
|
typeof targetDate === 'string'
|
|
? parseISO(targetDate).getTime()
|
|
: targetDate instanceof Date
|
|
? targetDate.getTime()
|
|
: NaN
|
|
if (isNaN(targetMs)) {
|
|
return '< 1m'
|
|
}
|
|
const diffMs = targetMs - now.getTime()
|
|
if (diffMs <= 0) {
|
|
return '< 1m'
|
|
}
|
|
|
|
const totalSeconds = Math.floor(diffMs / 1000)
|
|
const days = Math.floor(totalSeconds / 86400)
|
|
const hours = Math.floor((totalSeconds % 86400) / 3600)
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60)
|
|
|
|
if (days > 0) {
|
|
return hours > 0 ? `${days}d ${hours}h` : `${days}d`
|
|
}
|
|
if (hours > 0) {
|
|
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`
|
|
}
|
|
return minutes > 0 ? `${minutes}m` : '< 1m'
|
|
}
|
|
|
|
export const formatRemainingTimeSeconds = (totalSeconds: number): string => {
|
|
if (!isFinite(totalSeconds) || totalSeconds <= 0) {
|
|
return '< 1s'
|
|
}
|
|
const total = Math.round(totalSeconds)
|
|
const days = Math.floor(total / 86400)
|
|
const hours = Math.floor((total % 86400) / 3600)
|
|
const minutes = Math.floor((total % 3600) / 60)
|
|
const seconds = total % 60
|
|
|
|
if (days > 0) {
|
|
return hours > 0 ? `${days}d ${hours}h` : `${days}d`
|
|
}
|
|
if (hours > 0) {
|
|
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`
|
|
}
|
|
if (minutes > 0) {
|
|
return `${minutes}m`
|
|
}
|
|
return `${seconds}s`
|
|
}
|
|
|
|
/**
|
|
* Whole days remaining before a soft-deleted asset is permanently purged.
|
|
* Returns `null` when there is no valid `deletedAt` timestamp, and `0` once the
|
|
* retention window has elapsed.
|
|
*/
|
|
export const getTrashDaysLeft = (
|
|
deletedAt: string | null | undefined,
|
|
now: Date = new Date(),
|
|
): number | null => {
|
|
if (!deletedAt) return null
|
|
const deleted = parseISO(deletedAt)
|
|
if (isNaN(deleted.getTime())) return null
|
|
|
|
const expiresAtMs = deleted.getTime() + TRASH_RETENTION_DAYS * MS_PER_DAY
|
|
return Math.max(0, Math.ceil((expiresAtMs - now.getTime()) / MS_PER_DAY))
|
|
}
|