Files
shumai/packages/webui/lib/time.ts
T
Yiling-J 09b65a1eb6 feat(asset): Recently Deleted previews, retention countdown, and version stack delete/restore (#418)
* feat(asset): improve Recently Deleted previews and retention indicators

- Include soft-deleted children when building folder previews for the
  Recently Deleted view, and resolve deleted version stacks to their
  latest version so a trashed folder shows what was inside it.
- Add a "days left" badge (clock + Nd) to file and folder cards in the
  Recently Deleted view, placed to the left of the comments badge.
- Fix an issue where deleting or restoring a version stack from the file
  list did nothing because the WebUI dropped version_stack items before
  calling the API.
- Share the 30-day trash retention window through TRASH_RETENTION_DAYS.

* test(asset): use typed MediaInfo in recently-deleted preview tests

Replace the explicit any cast with a fully typed MediaInfo object so the new tests comply with the no-explicit-any policy.
2026-09-19 11:36:14 +08:00

94 lines
2.6 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'
}
/**
* 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))
}