mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 03:17:17 +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,8 +1,9 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
||||
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { FileBrowser } from './file-browser'
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
|
||||
@@ -126,6 +127,7 @@ describe('FileBrowser', () => {
|
||||
let queryClient: QueryClient
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
})
|
||||
@@ -312,4 +314,56 @@ describe('FileBrowser', () => {
|
||||
expect(screen.getByTestId('folder-preview-icon')).toBeDefined()
|
||||
expect(screen.getAllByText('Linked Folder').length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
const mockVersionStack: AssetInfo = {
|
||||
id: 'stack-1',
|
||||
name: 'stacked_clip.mp4',
|
||||
type: 'version_stack',
|
||||
sizeByte: 0,
|
||||
fileCount: 2,
|
||||
createdAt: '2026-01-01T00:00:00Z',
|
||||
updatedAt: '2026-01-01T00:00:00Z',
|
||||
status: 'processed',
|
||||
} as AssetInfo
|
||||
|
||||
it('deletes a version stack through the delete dialog', async () => {
|
||||
renderComponent({ folders: [], files: [mockVersionStack] })
|
||||
|
||||
const card = screen.getByTestId('file-card')
|
||||
fireEvent.keyDown(within(card).getByRole('button', { name: '' }), {
|
||||
key: 'ArrowDown',
|
||||
code: 'ArrowDown',
|
||||
})
|
||||
fireEvent.click(await screen.findByRole('menuitem', { name: /^Delete$|^删除$/ }))
|
||||
|
||||
const dialog = await screen.findByRole('alertdialog')
|
||||
fireEvent.click(within(dialog).getByRole('button', { name: /^Delete$|^删除$/ }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(vi.mocked(client.api.files.$delete)).toHaveBeenCalledWith({
|
||||
json: { ids: ['stack-1'] },
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('restores a version stack through the context action in recently deleted', async () => {
|
||||
renderComponent({
|
||||
folders: [],
|
||||
files: [mockVersionStack],
|
||||
isRecentlyDeleted: true,
|
||||
})
|
||||
|
||||
const card = screen.getByTestId('file-card')
|
||||
fireEvent.keyDown(within(card).getByRole('button', { name: '' }), {
|
||||
key: 'ArrowDown',
|
||||
code: 'ArrowDown',
|
||||
})
|
||||
fireEvent.click(await screen.findByRole('menuitem', { name: /^Restore$|^恢复$/ }))
|
||||
|
||||
await waitFor(() => {
|
||||
expect(vi.mocked(client.api.files.restore.$post)).toHaveBeenCalledWith({
|
||||
json: { ids: ['stack-1'] },
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -223,15 +223,16 @@ describe('FileCard', () => {
|
||||
|
||||
renderComponent({ item: itemWithComments })
|
||||
|
||||
const wrapper = screen.getByTestId('file-card-preview-badges')
|
||||
expect(wrapper.className).toContain('absolute')
|
||||
expect(wrapper.className).toContain('bottom-1')
|
||||
expect(wrapper.className).toContain('left-1')
|
||||
expect(wrapper.className).toContain('pointer-events-none')
|
||||
|
||||
const commentsBadge = screen.getByTestId('file-card-comments-count')
|
||||
expect(commentsBadge).toBeTruthy()
|
||||
expect(commentsBadge.textContent).toContain('5')
|
||||
expect(commentsBadge.className).toContain('absolute')
|
||||
expect(commentsBadge.className).toContain('bottom-1')
|
||||
expect(commentsBadge.className).toContain('left-1')
|
||||
expect(commentsBadge.className).toContain('bg-black/60')
|
||||
expect(commentsBadge.className).toContain('text-white')
|
||||
expect(commentsBadge.className).toContain('pointer-events-none')
|
||||
expect(commentsBadge.className).toContain('tabular-nums')
|
||||
})
|
||||
|
||||
@@ -253,4 +254,45 @@ describe('FileCard', () => {
|
||||
renderComponent({ item: itemNoComments })
|
||||
expect(screen.queryByTestId('file-card-comments-count')).toBeNull()
|
||||
})
|
||||
|
||||
it('renders the days-left badge in preview area for recently deleted items', () => {
|
||||
const deletedItem: AssetInfo = {
|
||||
...fileItem,
|
||||
deletedAt: new Date().toISOString(),
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: deletedItem, isRecentlyDeleted: true })
|
||||
|
||||
const badge = screen.getByTestId('file-card-days-left')
|
||||
expect(badge.textContent).toMatch(/30\s*(d|天)/)
|
||||
})
|
||||
|
||||
it('places the comments badge to the right of the days-left badge', () => {
|
||||
const deletedItem: AssetInfo = {
|
||||
...fileItem,
|
||||
deletedAt: new Date().toISOString(),
|
||||
commentsCount: 3,
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: deletedItem, isRecentlyDeleted: true })
|
||||
|
||||
const wrapper = screen.getByTestId('file-card-preview-badges')
|
||||
const order = Array.from(wrapper.children).map((child) => child.getAttribute('data-testid'))
|
||||
expect(order).toEqual(['file-card-days-left', 'file-card-comments-count'])
|
||||
})
|
||||
|
||||
it('does not render the days-left badge outside of recently deleted', () => {
|
||||
const deletedItem: AssetInfo = {
|
||||
...fileItem,
|
||||
deletedAt: new Date().toISOString(),
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: deletedItem, isRecentlyDeleted: false })
|
||||
expect(screen.queryByTestId('file-card-days-left')).toBeNull()
|
||||
})
|
||||
|
||||
it('does not render the days-left badge when deletedAt is missing', () => {
|
||||
renderComponent({ item: fileItem, isRecentlyDeleted: true })
|
||||
expect(screen.queryByTestId('file-card-days-left')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -22,12 +22,13 @@ import {
|
||||
} from '@/ui/components/ui/tooltip'
|
||||
import { ProgressCircle } from '@/ui/components/ui/progress-circle'
|
||||
import { Skeleton } from '@/ui/components/ui/skeleton'
|
||||
import { formatTimeAgo } from '@/ui/lib/time'
|
||||
import { formatTimeAgo, getTrashDaysLeft } from '@/ui/lib/time'
|
||||
import { selectFileNameWithoutExtension } from '@/ui/lib/rename-utils'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { useUploadStore } from '@/ui/stores/upload'
|
||||
import { useDraggable, useDroppable } from '@dnd-kit/react'
|
||||
import {
|
||||
Clock,
|
||||
Download,
|
||||
Edit,
|
||||
History,
|
||||
@@ -252,6 +253,14 @@ export function FileCard({
|
||||
})
|
||||
}, [displayItem.createdAt, displayItem.agent, displayItem.creator?.name])
|
||||
|
||||
const daysLeft = isRecentlyDeleted ? getTrashDaysLeft(displayItem.deletedAt) : null
|
||||
const daysLeftTooltip =
|
||||
daysLeft === null
|
||||
? ''
|
||||
: daysLeft === 1
|
||||
? m.n_days_left_singular({ count: daysLeft })
|
||||
: m.n_days_left_plural({ count: daysLeft })
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={setNodeRef}
|
||||
@@ -333,14 +342,32 @@ export function FileCard({
|
||||
) : (
|
||||
<>
|
||||
<FilePreview item={displayItem} showDuration />
|
||||
{typeof displayItem.commentsCount === 'number' && displayItem.commentsCount > 0 && (
|
||||
<span
|
||||
data-testid="file-card-comments-count"
|
||||
className="pointer-events-none absolute bottom-1 left-1 z-10 flex items-center gap-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white"
|
||||
{(daysLeft !== null ||
|
||||
(typeof displayItem.commentsCount === 'number' && displayItem.commentsCount > 0)) && (
|
||||
<div
|
||||
data-testid="file-card-preview-badges"
|
||||
className="pointer-events-none absolute bottom-1 left-1 z-10 flex items-center gap-1"
|
||||
>
|
||||
<MessageCircleMore className="h-3.5 w-3.5" />
|
||||
<span>{displayItem.commentsCount}</span>
|
||||
</span>
|
||||
{daysLeft !== null && (
|
||||
<span
|
||||
data-testid="file-card-days-left"
|
||||
title={daysLeftTooltip}
|
||||
className="flex items-center gap-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white"
|
||||
>
|
||||
<Clock className="h-3.5 w-3.5" />
|
||||
<span>{m.days_left_short({ count: daysLeft })}</span>
|
||||
</span>
|
||||
)}
|
||||
{typeof displayItem.commentsCount === 'number' && displayItem.commentsCount > 0 && (
|
||||
<span
|
||||
data-testid="file-card-comments-count"
|
||||
className="flex items-center gap-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white"
|
||||
>
|
||||
<MessageCircleMore className="h-3.5 w-3.5" />
|
||||
<span>{displayItem.commentsCount}</span>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, render, screen } from '@testing-library/react'
|
||||
import React from 'react'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { FolderCard } from './folder-card'
|
||||
|
||||
vi.mock('@dnd-kit/react', () => ({
|
||||
useDraggable: () => ({
|
||||
ref: vi.fn(),
|
||||
isDragging: false,
|
||||
}),
|
||||
useDroppable: () => ({
|
||||
ref: vi.fn(),
|
||||
isDropTarget: false,
|
||||
}),
|
||||
}))
|
||||
|
||||
describe('FolderCard', () => {
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
const folderItem: AssetInfo = {
|
||||
id: 'folder-123',
|
||||
name: 'regular_folder',
|
||||
type: 'folder',
|
||||
status: 'processed',
|
||||
fileCount: 2,
|
||||
createdAt: '2026-01-01T00:00:00Z',
|
||||
updatedAt: '2026-01-01T00:00:00Z',
|
||||
} as AssetInfo
|
||||
|
||||
const renderComponent = (props: Partial<React.ComponentProps<typeof FolderCard>> = {}) => {
|
||||
return render(
|
||||
<FolderCard
|
||||
item={folderItem}
|
||||
isSelected={false}
|
||||
isChecked={false}
|
||||
isEditing={false}
|
||||
onSelect={vi.fn()}
|
||||
onDoubleClick={vi.fn()}
|
||||
onContextMenu={vi.fn()}
|
||||
onDragStart={vi.fn()}
|
||||
onDrop={vi.fn()}
|
||||
onRename={vi.fn()}
|
||||
onFinishEditing={vi.fn()}
|
||||
{...props}
|
||||
/>,
|
||||
)
|
||||
}
|
||||
|
||||
it('renders previews for the children of a folder', () => {
|
||||
const itemWithChildren: AssetInfo = {
|
||||
...folderItem,
|
||||
latestChildren: [
|
||||
{ type: 'image', preview: { proxyType: 'image', thumbnailUrl: 'http://x/thumb-1.png' } },
|
||||
{ type: 'image', preview: { proxyType: 'image', thumbnailUrl: 'http://x/thumb-2.png' } },
|
||||
],
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: itemWithChildren })
|
||||
|
||||
const previews = screen.getAllByAltText('Preview')
|
||||
expect(previews).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('renders the days-left badge in the preview area for recently deleted folders', () => {
|
||||
const deletedItem: AssetInfo = {
|
||||
...folderItem,
|
||||
deletedAt: new Date().toISOString(),
|
||||
latestChildren: [
|
||||
{ type: 'image', preview: { proxyType: 'image', thumbnailUrl: 'http://x/thumb-1.png' } },
|
||||
],
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: deletedItem, isRecentlyDeleted: true })
|
||||
|
||||
const badge = screen.getByTestId('folder-card-days-left')
|
||||
expect(badge.textContent).toMatch(/30\s*(d|天)/)
|
||||
// The badge must not hide the preview of the deleted folder's content
|
||||
expect(screen.getAllByAltText('Preview')).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('does not render the days-left badge outside of recently deleted', () => {
|
||||
const deletedItem: AssetInfo = {
|
||||
...folderItem,
|
||||
deletedAt: new Date().toISOString(),
|
||||
} as AssetInfo
|
||||
|
||||
renderComponent({ item: deletedItem, isRecentlyDeleted: false })
|
||||
expect(screen.queryByTestId('folder-card-days-left')).toBeNull()
|
||||
})
|
||||
|
||||
it('does not render the days-left badge when deletedAt is missing', () => {
|
||||
renderComponent({ item: folderItem, isRecentlyDeleted: true })
|
||||
expect(screen.queryByTestId('folder-card-days-left')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -11,7 +11,8 @@ import { cn } from '@/ui/lib/utils'
|
||||
import { useDraggable, useDroppable } from '@dnd-kit/react'
|
||||
import type { AssetInfo, ChildPreview } from '@shumai/dtos'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { Download, Edit, History, MoreHorizontal, Trash2 } from 'lucide-react'
|
||||
import { getTrashDaysLeft } from '@/ui/lib/time'
|
||||
import { Clock, Download, Edit, History, MoreHorizontal, Trash2 } from 'lucide-react'
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import type { DragState } from '../dnd-types'
|
||||
import { FilePreview } from './file-preview'
|
||||
@@ -230,6 +231,14 @@ export function FolderCard({
|
||||
.trim()
|
||||
}, [])
|
||||
|
||||
const daysLeft = isRecentlyDeleted ? getTrashDaysLeft(item.deletedAt) : null
|
||||
const daysLeftTooltip =
|
||||
daysLeft === null
|
||||
? ''
|
||||
: daysLeft === 1
|
||||
? m.n_days_left_singular({ count: daysLeft })
|
||||
: m.n_days_left_plural({ count: daysLeft })
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={setNodeRef}
|
||||
@@ -333,7 +342,20 @@ export function FolderCard({
|
||||
</div>
|
||||
|
||||
{item.latestChildren && item.latestChildren.length > 0 && (
|
||||
<FolderPreviewGrid items={item.latestChildren} />
|
||||
<div data-testid="folder-preview-grid" className="h-full w-full">
|
||||
<FolderPreviewGrid items={item.latestChildren} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{daysLeft !== null && (
|
||||
<span
|
||||
data-testid="folder-card-days-left"
|
||||
title={daysLeftTooltip}
|
||||
className="pointer-events-none absolute bottom-1 left-1 z-10 flex items-center gap-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white"
|
||||
>
|
||||
<Clock className="h-3.5 w-3.5" />
|
||||
<span>{m.days_left_short({ count: daysLeft })}</span>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
@@ -141,7 +141,9 @@ export function useFileActions({
|
||||
}
|
||||
|
||||
const confirmDelete = () => {
|
||||
const fileIds = itemsToDelete.filter((i) => i.type === 'file').map((i) => i.id!)
|
||||
const fileIds = itemsToDelete
|
||||
.filter((i) => i.type === 'file' || i.type === 'version_stack')
|
||||
.map((i) => i.id!)
|
||||
const folderIds = itemsToDelete.filter((i) => i.type === 'folder').map((i) => i.id!)
|
||||
|
||||
if (fileIds.length > 0) {
|
||||
@@ -183,7 +185,9 @@ export function useFileActions({
|
||||
}
|
||||
|
||||
const handleRestore = (items: AssetInfo[]) => {
|
||||
const fileIds = items.filter((i) => i.type === 'file').map((i) => i.id!)
|
||||
const fileIds = items
|
||||
.filter((i) => i.type === 'file' || i.type === 'version_stack')
|
||||
.map((i) => i.id!)
|
||||
const folderIds = items.filter((i) => i.type === 'folder').map((i) => i.id!)
|
||||
|
||||
if (fileIds.length > 0) {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -1022,6 +1022,9 @@
|
||||
"n_tasks_plural": "{count} tasks",
|
||||
"n_days_ago_singular": "{count} day ago",
|
||||
"n_days_ago_plural": "{count} days ago",
|
||||
"days_left_short": "{count}d",
|
||||
"n_days_left_singular": "{count} day left before permanent deletion",
|
||||
"n_days_left_plural": "{count} days left before permanent deletion",
|
||||
"today_with_time": "Today {time}",
|
||||
"yesterday_with_time": "Yesterday {time}",
|
||||
"n_items_singular": "{count} Item",
|
||||
|
||||
@@ -1022,6 +1022,9 @@
|
||||
"n_tasks_plural": "{count} 个任务",
|
||||
"n_days_ago_singular": "{count} 天前",
|
||||
"n_days_ago_plural": "{count} 天前",
|
||||
"days_left_short": "{count}天",
|
||||
"n_days_left_singular": "永久删除前还剩 {count} 天",
|
||||
"n_days_left_plural": "永久删除前还剩 {count} 天",
|
||||
"today_with_time": "今天 {time}",
|
||||
"yesterday_with_time": "昨天 {time}",
|
||||
"n_items_singular": "{count} 个项目",
|
||||
|
||||
Reference in New Issue
Block a user