mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-27 19:34:38 +00:00
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.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { formatRemainingTime } from './time'
|
||||
import { formatRemainingTime, getTrashDaysLeft } from './time'
|
||||
|
||||
describe('formatRemainingTime', () => {
|
||||
const baseNow = new Date('2026-08-17T12:00:00Z')
|
||||
@@ -44,3 +44,32 @@ describe('formatRemainingTime', () => {
|
||||
expect(formatRemainingTime('2026-08-17T15:18:00Z', baseNow)).toBe('3h 18m')
|
||||
})
|
||||
})
|
||||
|
||||
describe('getTrashDaysLeft', () => {
|
||||
const now = new Date('2026-08-17T12:00:00Z')
|
||||
|
||||
it('returns the full retention window for a freshly deleted asset', () => {
|
||||
expect(getTrashDaysLeft('2026-08-17T12:00:00Z', now)).toBe(30)
|
||||
})
|
||||
|
||||
it('counts down as days pass', () => {
|
||||
expect(getTrashDaysLeft('2026-08-16T12:00:00Z', now)).toBe(29)
|
||||
expect(getTrashDaysLeft('2026-08-06T12:00:00Z', now)).toBe(19)
|
||||
})
|
||||
|
||||
it('rounds partial days up', () => {
|
||||
// Deleted 23h ago: 29d 1h remain, which rounds up to 30
|
||||
expect(getTrashDaysLeft('2026-08-16T13:00:00Z', now)).toBe(30)
|
||||
})
|
||||
|
||||
it('never returns less than zero once the retention window has elapsed', () => {
|
||||
expect(getTrashDaysLeft('2026-07-01T12:00:00Z', now)).toBe(0)
|
||||
})
|
||||
|
||||
it('returns null when there is no valid deletedAt', () => {
|
||||
expect(getTrashDaysLeft(null, now)).toBeNull()
|
||||
expect(getTrashDaysLeft(undefined, now)).toBeNull()
|
||||
expect(getTrashDaysLeft('', now)).toBeNull()
|
||||
expect(getTrashDaysLeft('not-a-date', now)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
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()
|
||||
@@ -71,3 +74,20 @@ export const formatRemainingTime = (targetDate: string | Date, now: Date = new D
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user