feat(search): render results in responsive card grid with sprite preview and new-tab navigation (#479)

* feat(search): render results in responsive card grid with sprite preview and new-tab navigation

* fix(search): align semantic matched duration badge style with standard duration badge

* fix(webui): use pointer cursor instead of crosshair cursor for sprite scrubber on hover
This commit is contained in:
Yiling-J
2026-10-16 10:42:32 +08:00
committed by GitHub
parent 1a46f63160
commit 0f318b4050
10 changed files with 606 additions and 213 deletions
@@ -0,0 +1,328 @@
// @vitest-environment happy-dom
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { AssetInfo } from '@shumai/dtos'
import { SearchFilterDialog } from './search-filter-dialog'
vi.mock('@dnd-kit/react', () => ({
useDraggable: () => ({
ref: vi.fn(),
isDragging: false,
}),
useDroppable: () => ({
ref: vi.fn(),
isDropTarget: false,
}),
}))
const mockSearchPost = vi.fn()
const mockSettingsGet = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ semanticSearchEnabled: true }),
})
vi.mock('@/ui/api/client', () => ({
client: {
api: {
teams: {
':teamId': {
settings: {
$get: (...args: unknown[]) => mockSettingsGet(...args),
},
},
},
folders: {
':folderId': {
search: {
$post: (...args: unknown[]) => mockSearchPost(...args),
},
},
},
files: {
':fileId': {
$get: vi.fn().mockResolvedValue({
ok: true,
json: async () => ({}),
}),
},
},
projects: {
':projectId': {
collections: {
$post: vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ id: 'col-1' }),
}),
},
},
},
},
},
}))
vi.mock('@tanstack/react-router', () => ({
useNavigate: () => vi.fn(),
useRouterState: () => '/projects/proj-1',
}))
describe('SearchFilterDialog', () => {
let queryClient: QueryClient
let windowOpenSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => {
queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
})
windowOpenSpy = vi.spyOn(window, 'open').mockImplementation(() => null)
})
afterEach(() => {
cleanup()
vi.clearAllMocks()
windowOpenSpy.mockRestore()
})
const defaultProps = {
open: true,
onOpenChange: vi.fn(),
teamId: 'team-1',
projectId: 'proj-1',
assetId: 'folder-root',
fields: [],
initialConditions: [],
onApply: vi.fn(),
}
it('renders initial ready to search state when open and no query', () => {
render(
<QueryClientProvider client={queryClient}>
<SearchFilterDialog {...defaultProps} />
</QueryClientProvider>,
)
expect(screen.getByText(/Ready to search|准备搜索/i)).toBeTruthy()
})
it('renders search results in a responsive grid using FileCard and FolderCard without checkboxes or action menus', async () => {
const mockFile: AssetInfo = {
id: 'file-101',
name: 'video_clip.mp4',
type: 'file',
sizeByte: 1048576,
status: 'processed',
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
preview: {
proxyType: 'video',
thumbnailUrl: 'https://example.com/clip.jpg',
duration: 120,
},
} as AssetInfo
const mockFolder: AssetInfo = {
id: 'folder-202',
name: 'B-Roll Folder',
type: 'folder',
fileCount: 4,
status: 'processed',
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
} as AssetInfo
mockSearchPost.mockResolvedValueOnce({
ok: true,
json: async () => ({
data: [mockFile, mockFolder],
pageInfo: { total: 2 },
}),
})
render(
<QueryClientProvider client={queryClient}>
<SearchFilterDialog
{...defaultProps}
initialConditions={[{ field: 'name', operator: 'contains', value: 'video' }]}
/>
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getByText('video_clip.mp4')).toBeTruthy()
expect(screen.getByText('B-Roll Folder')).toBeTruthy()
})
// Neither card renders checkboxes or 3-dots action menus in search dialog
expect(screen.queryByRole('checkbox')).toBeNull()
expect(screen.queryByRole('button', { name: /more/i })).toBeNull()
// Grid container is present
const fileCard = screen.getByText('video_clip.mp4').closest('[data-testid="file-card"]')
expect(fileCard).toBeTruthy()
const gridContainer = fileCard?.parentElement
expect(gridContainer?.className).toContain('grid')
})
it('single-clicking a file opens detail page in a new tab seeking to startTime without closing dialog', async () => {
const onOpenChange = vi.fn()
const mockSemanticFile: AssetInfo = {
id: 'file-sem-1',
name: 'interview.mp4',
type: 'file',
status: 'processed',
startTime: 45,
endTime: 90,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
preview: {
proxyType: 'video',
thumbnailUrl: 'https://example.com/interview.jpg',
duration: 300,
},
} as AssetInfo
mockSearchPost.mockResolvedValueOnce({
ok: true,
json: async () => ({
data: [mockSemanticFile],
pageInfo: { total: 1 },
}),
})
render(
<QueryClientProvider client={queryClient}>
<SearchFilterDialog
{...defaultProps}
onOpenChange={onOpenChange}
initialConditions={[{ field: 'name', operator: 'contains', value: 'interview' }]}
/>
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getByText('interview.mp4')).toBeTruthy()
})
// Matched chunk duration replaces total duration in preview
const matchedBadge = screen.getByTestId('file-preview-matched-duration')
expect(matchedBadge.textContent).toBe('00:45 - 01:30')
// Single click on file card
const card = screen.getByText('interview.mp4').closest('[data-testid="file-card"]')!
fireEvent.click(card)
expect(windowOpenSpy).toHaveBeenCalledWith(
'/projects/proj-1/files/file-sem-1?start=45',
'_blank',
'noopener,noreferrer',
)
// Dialog should NOT be closed
expect(onOpenChange).not.toHaveBeenCalled()
})
it('single-clicking a folder opens folder page in a new tab without closing dialog', async () => {
const onOpenChange = vi.fn()
const mockFolder: AssetInfo = {
id: 'folder-sub-1',
name: 'Subfolder Alpha',
type: 'folder',
status: 'processed',
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
} as AssetInfo
mockSearchPost.mockResolvedValueOnce({
ok: true,
json: async () => ({
data: [mockFolder],
pageInfo: { total: 1 },
}),
})
render(
<QueryClientProvider client={queryClient}>
<SearchFilterDialog
{...defaultProps}
onOpenChange={onOpenChange}
initialConditions={[{ field: 'name', operator: 'contains', value: 'Subfolder' }]}
/>
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getByText('Subfolder Alpha')).toBeTruthy()
})
const card = screen.getByText('Subfolder Alpha').closest('[data-testid="file-card"]')!
fireEvent.click(card)
expect(windowOpenSpy).toHaveBeenCalledWith(
'/projects/proj-1/folders/folder-sub-1',
'_blank',
'noopener,noreferrer',
)
expect(onOpenChange).not.toHaveBeenCalled()
})
it('renders multiple distinct chunk matches for the same video file with unique timestamps', async () => {
const chunk1: AssetInfo = {
id: 'video-same',
name: 'drone_shots.mp4',
type: 'file',
status: 'processed',
startTime: 10,
endTime: 30,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
preview: {
proxyType: 'video',
thumbnailUrl: 'https://example.com/drone.jpg',
duration: 200,
},
} as AssetInfo
const chunk2: AssetInfo = {
id: 'video-same',
name: 'drone_shots.mp4',
type: 'file',
status: 'processed',
startTime: 120,
endTime: 150,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
preview: {
proxyType: 'video',
thumbnailUrl: 'https://example.com/drone.jpg',
duration: 200,
},
} as AssetInfo
mockSearchPost.mockResolvedValueOnce({
ok: true,
json: async () => ({
data: [chunk1, chunk2],
pageInfo: { total: 2 },
}),
})
render(
<QueryClientProvider client={queryClient}>
<SearchFilterDialog
{...defaultProps}
initialConditions={[{ field: 'name', operator: 'contains', value: 'drone' }]}
/>
</QueryClientProvider>,
)
await waitFor(() => {
const titles = screen.getAllByText('drone_shots.mp4')
expect(titles).toHaveLength(2)
})
const badges = screen.getAllByTestId('file-preview-matched-duration')
expect(badges).toHaveLength(2)
expect(badges[0].textContent).toBe('00:10 - 00:30')
expect(badges[1].textContent).toBe('02:00 - 02:30')
})
})
@@ -8,23 +8,15 @@ import { ScrollArea } from '@/ui/components/ui/scroll-area'
import { Input } from '@/ui/components/ui/input'
import { Label } from '@/ui/components/ui/label'
import { Switch } from '@/ui/components/ui/switch'
import { formatSize } from '@/ui/lib/format'
import { cn } from '@/ui/lib/utils'
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useNavigate } from '@tanstack/react-router'
import {
AlertCircle,
ChevronDown,
ChevronUp,
FileIcon,
FolderIcon,
Loader2,
Search,
X,
} from 'lucide-react'
import { AlertCircle, ChevronDown, ChevronUp, Loader2, Search, X } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { useInView } from 'react-intersection-observer'
import { toast } from 'sonner'
import { FileCard } from '@/ui/components/file-browser/file-card'
import { FolderCard } from '@/ui/components/file-browser/folder-card'
import { FilterPanel } from './filter-panel'
import { m } from '@/ui/paraglide/messages.js'
@@ -225,36 +217,25 @@ export function SearchFilterDialog({
setConditions([])
}
const formatTimestamp = (seconds: number) => {
const m = Math.floor(seconds / 60)
const s = Math.floor(seconds % 60)
return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
}
const handleResultClick = (item: AssetInfo) => {
if (item.type === 'folder') {
navigate({
to: '/projects/$projectId/folders/$folderId',
params: { projectId, folderId: item.id! },
})
window.open(`/projects/${projectId}/folders/${item.id}`, '_blank', 'noopener,noreferrer')
} else {
navigate({
to: '/projects/$projectId/files/$fileId',
params: { projectId, fileId: item.versionStack ? item.versionStack.id : item.id! },
search: {
version: undefined,
start: item.startTime && item.startTime > 0 ? item.startTime : undefined,
},
})
const fileId = item.versionStack ? item.versionStack.id : item.id!
const searchParams = new URLSearchParams()
if (item.startTime !== undefined && item.startTime !== null && item.startTime > 0) {
searchParams.set('start', String(item.startTime))
}
const query = searchParams.toString() ? `?${searchParams.toString()}` : ''
window.open(`/projects/${projectId}/files/${fileId}${query}`, '_blank', 'noopener,noreferrer')
}
onOpenChange(false)
}
const activeFiltersCount = conditions.filter((c) => c.value !== '').length
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[900px] p-0 gap-0 overflow-hidden bg-background border-border rounded-2xl shadow-2xl flex flex-col h-[85vh]">
<DialogContent className="sm:max-w-4xl lg:max-w-5xl p-0 gap-0 overflow-hidden bg-background border-border rounded-2xl shadow-2xl flex flex-col h-[85vh]">
<DialogHeader className="px-6 pt-3 pb-0 text-left">
<DialogTitle className="text-lg font-bold">{m.search_dialog_title()}</DialogTitle>
</DialogHeader>
@@ -394,121 +375,94 @@ export function SearchFilterDialog({
)}
</div>
<div className="flex flex-col border border-border rounded-xl divide-y divide-border overflow-hidden min-w-0">
{!hasActiveCriteria ? (
<div className="py-20 text-center flex flex-col items-center justify-center bg-muted/20 px-4">
<Search className="w-10 h-10 text-muted-foreground/30 mb-2" />
<h3 className="text-sm font-semibold text-foreground/80">
{m.ready_to_search()}
</h3>
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
{m.type_query_instruction()}
{!hasActiveCriteria ? (
<div className="border border-border rounded-xl py-20 text-center flex flex-col items-center justify-center bg-muted/20 px-4">
<Search className="w-10 h-10 text-muted-foreground/30 mb-2" />
<h3 className="text-sm font-semibold text-foreground/80">{m.ready_to_search()}</h3>
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
{m.type_query_instruction()}
</p>
{isSemantic && (
<p className="text-xs text-muted-foreground/80 max-w-md mt-4 mx-auto border-t border-border/60 pt-3">
{m.semantic_search_note()}
</p>
{isSemantic && (
<p className="text-xs text-muted-foreground/80 max-w-md mt-4 mx-auto border-t border-border/60 pt-3">
{m.semantic_search_note()}
</p>
)}
</div>
) : searchError ? (
<div className="py-12 px-4 text-center flex flex-col items-center justify-center bg-muted/20">
<AlertCircle className="w-10 h-10 text-destructive/50 mb-2" />
<h3 className="text-sm font-semibold text-foreground/80">{m.search_failed()}</h3>
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
{searchError.message.includes('Embedding agent not configured')
? m.enable_embedding_agent_hint()
: searchError.message}
</p>
</div>
) : isLoading ? (
Array.from({ length: 3 }).map((_, idx) => (
)}
</div>
) : searchError ? (
<div className="border border-border rounded-xl py-12 px-4 text-center flex flex-col items-center justify-center bg-muted/20">
<AlertCircle className="w-10 h-10 text-destructive/50 mb-2" />
<h3 className="text-sm font-semibold text-foreground/80">{m.search_failed()}</h3>
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
{searchError.message.includes('Embedding agent not configured')
? m.enable_embedding_agent_hint()
: searchError.message}
</p>
</div>
) : isLoading ? (
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-4">
{Array.from({ length: 8 }).map((_, idx) => (
<div
key={idx}
className="p-3.5 flex items-center gap-3 animate-pulse bg-muted/10"
className="flex flex-col rounded-xl border border-border bg-card p-2 space-y-2 animate-pulse"
>
<div className="w-11 h-11 rounded-xl bg-muted shrink-0" />
<div className="flex-1 space-y-2">
<div className="h-4 bg-muted rounded w-1/3" />
<div className="h-3 bg-muted/50 rounded w-1/4" />
<div className="aspect-square w-full rounded-lg bg-muted/60" />
<div className="p-1 space-y-1.5">
<div className="h-4 bg-muted/60 rounded w-3/4" />
<div className="h-3 bg-muted/40 rounded w-1/2" />
</div>
</div>
))
) : flattenedResults.length === 0 ? (
<div className="py-12 px-4 text-center flex flex-col items-center justify-center bg-muted/20">
<AlertCircle className="w-10 h-10 text-muted-foreground/30 mb-2" />
<h3 className="text-sm font-semibold text-foreground/80">
{m.no_records_matched()}
</h3>
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
{m.try_adjusting_spelling()}
</p>
))}
</div>
) : flattenedResults.length === 0 ? (
<div className="border border-border rounded-xl py-12 px-4 text-center flex flex-col items-center justify-center bg-muted/20">
<AlertCircle className="w-10 h-10 text-muted-foreground/30 mb-2" />
<h3 className="text-sm font-semibold text-foreground/80">
{m.no_records_matched()}
</h3>
<p className="text-xs text-muted-foreground max-w-sm mt-1 mx-auto">
{m.try_adjusting_spelling()}
</p>
</div>
) : (
<>
<div className="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-4">
{flattenedResults.map((record) => {
const itemKey =
record.startTime != null && record.endTime != null
? `${record.id}-${record.startTime}-${record.endTime}`
: record.id
if (record.type === 'folder') {
return (
<FolderCard
key={itemKey}
item={record}
hideCheckbox
hideActions
disabled
onSelect={() => handleResultClick(record)}
/>
)
}
return (
<FileCard
key={itemKey}
teamId={teamId}
item={record}
hideCheckbox
hideActions
disabled
onSelect={() => handleResultClick(record)}
/>
)
})}
</div>
) : (
<>
{flattenedResults.map((record) => (
<div
key={record.id}
onClick={() => handleResultClick(record)}
className="p-3.5 flex items-center gap-3 hover:bg-muted/40 transition-colors group/record cursor-pointer min-w-0"
>
<div
className={cn(
'shrink-0 w-11 h-11 rounded-xl shadow-xs flex items-center justify-center text-white font-bold relative group-hover/record:scale-105 transition-transform bg-muted overflow-hidden',
record.type === 'folder' ? 'bg-primary/5' : '',
)}
>
{record.preview?.thumbnailUrl ? (
<img
src={record.preview.thumbnailUrl}
alt=""
className="w-full h-full object-cover rounded-xl"
/>
) : record.type === 'folder' ? (
<FolderIcon className="h-6 w-6 text-primary/70" />
) : (
<FileIcon className="h-6 w-6 text-muted-foreground" />
)}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center justify-between gap-3 min-w-0">
<div className="flex-1 min-w-0">
<span className="font-medium text-sm text-foreground group-hover/record:text-primary transition-colors block truncate">
{record.name}
</span>
<span className="text-xs text-muted-foreground block mt-0.5 truncate">
{record.type === 'folder'
? m.folder()
: `${m.size_prefix()} ${formatSize(record.sizeByte || 0)}`}
</span>
{record.startTime !== undefined &&
record.startTime !== null &&
record.endTime !== undefined &&
record.endTime !== null && (
<span className="text-[10px] font-semibold text-primary bg-primary/10 border border-primary/20 px-1.5 py-0.5 rounded mt-1 inline-block max-w-full truncate">
{m.match_found_at()} {formatTimestamp(record.startTime)} -{' '}
{formatTimestamp(record.endTime)}
</span>
)}
</div>
<span className="text-xs text-muted-foreground font-mono shrink-0">
{new Date(record.createdAt).toLocaleDateString(undefined, {
year: 'numeric',
month: 'short',
})}
</span>
</div>
</div>
</div>
))}
{(hasNextPage || isFetchingNextPage) && (
<div ref={ref} className="p-4 flex justify-center items-center">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
)}
</>
)}
</div>
{(hasNextPage || isFetchingNextPage) && (
<div ref={ref} className="p-4 flex justify-center items-center">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
)}
</>
)}
</div>
</ScrollArea>