fix(webui): improve virtualized file browser and total size querying

This commit is contained in:
yiling
2026-06-10 15:41:25 +08:00
parent 0d74579802
commit 412900bd84
8 changed files with 123 additions and 37 deletions
+1
View File
@@ -13,6 +13,7 @@ export interface PaginationParams {
export interface PageInfo {
total?: number
cursor?: string
totalSize?: number
}
export interface PaginatedData<T> {
@@ -224,8 +224,8 @@ describe('N-gram Search Integration', () => {
conditions: [{ field: 'name', operator: 'contains', value: 'rareterm' }],
})
// Verify probe and main query were executed
expect(queries).toHaveLength(2)
// Verify probe, main query, and sum query were executed
expect(queries).toHaveLength(3)
// Check that main query used GIN index
const mainQuery = queries.find((q) => q.includes('as "assetId"'))
expect(mainQuery).toBeDefined()
+55 -5
View File
@@ -186,7 +186,16 @@ export class SearchService {
}
const countRes = await this.prismaClient.$queryRaw<{ count: bigint }[]>(countBuilder.build())
pageInfo.total = Number(countRes[0]?.count || 0)
const totalCount = Number(countRes[0]?.count || 0)
pageInfo.total = totalCount
if (totalCount < 2000) {
countBuilder.select(Prisma.sql`SUM(COALESCE(a.size_byte, 0))::bigint as sum`)
const sumRes = await this.prismaClient.$queryRaw<{ sum: bigint | null }[]>(countBuilder.build())
pageInfo.totalSize = Number(sumRes[0]?.sum || 0)
} else {
pageInfo.totalSize = -1
}
if (hasNextPage) {
pageInfo.cursor = encodeCursor(offset + limit)
@@ -338,9 +347,8 @@ export class SearchService {
}
const pageInfo: PageInfo = {}
if (countOverride !== undefined) {
pageInfo.total = countOverride
} else {
let totalCount = countOverride
if (totalCount === undefined) {
const countBuilder = new SqlQueryBuilder()
.select(Prisma.sql`COUNT(*)`)
.from(Prisma.sql`assets a`)
@@ -367,7 +375,49 @@ export class SearchService {
}
const countRes = await this.prismaClient.$queryRaw<{ count: bigint }[]>(countBuilder.build())
pageInfo.total = Number(countRes[0]?.count || 0)
totalCount = Number(countRes[0]?.count || 0)
pageInfo.total = totalCount
if (totalCount < 2000) {
countBuilder.select(Prisma.sql`SUM(COALESCE(a.size_byte, 0))::bigint as sum`)
const sumRes = await this.prismaClient.$queryRaw<{ sum: bigint | null }[]>(countBuilder.build())
pageInfo.totalSize = Number(sumRes[0]?.sum || 0)
} else {
pageInfo.totalSize = -1
}
} else {
pageInfo.total = totalCount
if (totalCount < 2000) {
const countBuilder = new SqlQueryBuilder()
.select(Prisma.sql`SUM(COALESCE(a.size_byte, 0))::bigint as sum`)
.from(Prisma.sql`assets a`)
.addWhere(Prisma.sql`a.is_deleted = false`)
if (targetFolderIds.length > 0) {
countBuilder.addWhere(Prisma.sql`a.parent_id = ANY(${targetFolderIds})`)
}
if (req.showSymlink) {
countBuilder.addWhere(Prisma.sql`
(a.type = ANY(${targetTypes}::"AssetType"[]) OR (a.type = 'symlink' AND a.target_id IN (SELECT id FROM assets WHERE type = ANY(${targetTypes}::"AssetType"[]))))
`)
} else {
countBuilder.addWhere(Prisma.sql`a.type = ANY(${targetTypes}::"AssetType"[])`)
}
if (req.conditions && req.conditions.length > 0) {
countBuilder.addSearchConditions(req.operator, req.conditions, { skipNameContains: true })
}
if (nameCond) {
countBuilder.addWhere(Prisma.sql`a.name ILIKE ${'%' + valStr + '%'}`)
}
const sumRes = await this.prismaClient.$queryRaw<{ sum: bigint | null }[]>(countBuilder.build())
pageInfo.totalSize = Number(sumRes[0]?.sum || 0)
} else {
pageInfo.totalSize = -1
}
}
if (hasNextPage) {
+1
View File
@@ -3,6 +3,7 @@ import { z } from 'zod'
export const paginationPageInfoSchema = z.object({
cursor: z.string().optional(),
total: z.number().optional(),
totalSize: z.number().optional(),
})
export type PaginationPageInfo = z.infer<typeof paginationPageInfoSchema>
@@ -48,6 +48,8 @@ interface FileBrowserProps {
files: AssetInfo[]
totalFolders?: number
totalFiles?: number
totalFoldersSize?: number
totalFilesSize?: number
selectedItem: AssetInfo | null
selectedIds: Set<string>
onItemSelect: (item: AssetInfo, event: React.MouseEvent) => void
@@ -92,6 +94,8 @@ export function FileBrowser({
files,
totalFolders,
totalFiles,
totalFoldersSize,
totalFilesSize,
selectedItem,
selectedIds,
onItemSelect,
@@ -795,6 +799,8 @@ export function FileBrowser({
files={displayedFiles}
totalFolders={totalFolders}
totalFiles={totalFiles}
totalFoldersSize={totalFoldersSize}
totalFilesSize={totalFilesSize}
selectedItem={selectedItem}
selectedIds={selectedIds}
displayedFields={displayedFields}
@@ -826,6 +832,8 @@ export function FileBrowser({
files={displayedFiles}
totalFolders={totalFolders}
totalFiles={totalFiles}
totalFoldersSize={totalFoldersSize}
totalFilesSize={totalFilesSize}
renderItem={renderItem}
foldersExpanded={foldersExpanded}
setFoldersExpanded={setFoldersExpanded}
@@ -81,6 +81,8 @@ interface FileBrowserGridViewProps {
formatSize: (bytes: number) => string
foldersSize: number
filesSize: number
totalFoldersSize?: number
totalFilesSize?: number
handleEmptyAreaClick: (e: React.MouseEvent) => void
dragState?: DragState
sort?: SearchSort
@@ -105,8 +107,8 @@ export function FileBrowserGridView({
fetchNextFilesPage,
formatCount,
formatSize,
foldersSize,
filesSize,
totalFoldersSize,
totalFilesSize,
handleEmptyAreaClick,
dragState,
sort,
@@ -139,22 +141,26 @@ export function FileBrowserGridView({
)[] = []
// Folders
list.push({ type: 'header', kind: 'folder' })
if (foldersExpanded) {
const count = totalFolders ?? folders.length
const rowCount = Math.ceil(count / cols)
for (let i = 0; i < rowCount; i++) {
list.push({ type: 'row', kind: 'folder', rowIndex: i })
const foldersCount = totalFolders ?? folders.length
if (foldersCount > 0) {
list.push({ type: 'header', kind: 'folder' })
if (foldersExpanded) {
const rowCount = Math.ceil(foldersCount / cols)
for (let i = 0; i < rowCount; i++) {
list.push({ type: 'row', kind: 'folder', rowIndex: i })
}
}
}
// Files
list.push({ type: 'header', kind: 'file' })
if (filesExpanded) {
const count = totalFiles ?? files.length
const rowCount = Math.ceil(count / cols)
for (let i = 0; i < rowCount; i++) {
list.push({ type: 'row', kind: 'file', rowIndex: i })
const filesCount = totalFiles ?? files.length
if (filesCount > 0) {
list.push({ type: 'header', kind: 'file' })
if (filesExpanded) {
const rowCount = Math.ceil(filesCount / cols)
for (let i = 0; i < rowCount; i++) {
list.push({ type: 'row', kind: 'file', rowIndex: i })
}
}
}
@@ -168,6 +174,10 @@ export function FileBrowserGridView({
overscan: 5,
})
useEffect(() => {
rowVirtualizer.measure()
}, [folders.length, files.length, foldersExpanded, filesExpanded, cols, rowVirtualizer])
const virtualItems = rowVirtualizer.getVirtualItems()
useEffect(() => {
@@ -223,7 +233,8 @@ export function FileBrowserGridView({
if (row.type === 'header') {
const isFolder = row.kind === 'folder'
const count = isFolder ? (totalFolders ?? folders.length) : (totalFiles ?? files.length)
const size = isFolder ? foldersSize : filesSize
const size = isFolder ? (totalFoldersSize ?? -1) : (totalFilesSize ?? -1)
const showSize = size > -1
const expanded = isFolder ? foldersExpanded : filesExpanded
const setExpanded = isFolder ? setFoldersExpanded : setFilesExpanded
@@ -250,7 +261,7 @@ export function FileBrowserGridView({
<ChevronRight className="h-4 w-4" />
)}
<span>
{formatCount(count, !isFolder)} • {formatSize(size)}
{formatCount(count, !isFolder)}{showSize ? ` • ${formatSize(size)}` : ''}
</span>
</button>
</div>
@@ -139,6 +139,8 @@ interface FileBrowserListViewProps {
formatSize: (bytes: number) => string
foldersSize: number
filesSize: number
totalFoldersSize?: number
totalFilesSize?: number
handleEmptyAreaClick: (e: React.MouseEvent) => void
dragState?: DragState
sort?: SearchSort
@@ -166,8 +168,8 @@ export function FileBrowserListView({
fetchNextFilesPage,
formatCount,
formatSize,
foldersSize,
filesSize,
totalFoldersSize,
totalFilesSize,
handleEmptyAreaClick,
dragState,
sort,
@@ -227,20 +229,24 @@ export function FileBrowserListView({
)[] = []
// Folders section
list.push({ type: 'header', kind: 'folder' })
if (foldersExpanded) {
const count = totalFolders ?? folders.length
for (let i = 0; i < count; i++) {
list.push({ type: 'item', kind: 'folder', index: i })
const foldersCount = totalFolders ?? folders.length
if (foldersCount > 0) {
list.push({ type: 'header', kind: 'folder' })
if (foldersExpanded) {
for (let i = 0; i < foldersCount; i++) {
list.push({ type: 'item', kind: 'folder', index: i })
}
}
}
// Files section
list.push({ type: 'header', kind: 'file' })
if (filesExpanded) {
const count = totalFiles ?? files.length
for (let i = 0; i < count; i++) {
list.push({ type: 'item', kind: 'file', index: i })
const filesCount = totalFiles ?? files.length
if (filesCount > 0) {
list.push({ type: 'header', kind: 'file' })
if (filesExpanded) {
for (let i = 0; i < filesCount; i++) {
list.push({ type: 'item', kind: 'file', index: i })
}
}
}
@@ -254,6 +260,10 @@ export function FileBrowserListView({
overscan: 10,
})
useEffect(() => {
rowVirtualizer.measure()
}, [folders.length, files.length, foldersExpanded, filesExpanded, rowVirtualizer])
const virtualItems = rowVirtualizer.getVirtualItems()
useEffect(() => {
@@ -348,7 +358,8 @@ export function FileBrowserListView({
const count = isFolder
? (totalFolders ?? folders.length)
: (totalFiles ?? files.length)
const size = isFolder ? foldersSize : filesSize
const size = isFolder ? (totalFoldersSize ?? -1) : (totalFilesSize ?? -1)
const showSize = size > -1
const expanded = isFolder ? foldersExpanded : filesExpanded
const setExpanded = isFolder ? setFoldersExpanded : setFilesExpanded
const hasItems = isFolder ? folders.length > 0 : files.length > 0
@@ -395,7 +406,7 @@ export function FileBrowserListView({
)}
</div>
<span className="text-sm font-medium text-muted-foreground whitespace-nowrap">
{formatCount(count, !isFolder)} • {formatSize(size)}
{formatCount(count, !isFolder)}{showSize ? ` • ${formatSize(size)}` : ''}
</span>
</div>
<div className="flex-1 bg-card" />
@@ -274,6 +274,8 @@ export default function FileSystemManager({
const totalFolders = foldersData?.pages[0]?.pageInfo?.total
const totalFiles = filesData?.pages[0]?.pageInfo?.total
const totalFoldersSize = foldersData?.pages[0]?.pageInfo?.totalSize
const totalFilesSize = filesData?.pages[0]?.pageInfo?.totalSize
const handleClearSelection = () => {
setSelectedIds(new Set())
@@ -454,6 +456,8 @@ export default function FileSystemManager({
files={files}
totalFolders={totalFolders}
totalFiles={totalFiles}
totalFoldersSize={totalFoldersSize}
totalFilesSize={totalFilesSize}
selectedItem={selectedItem}
selectedIds={selectedIds}
onItemSelect={handleItemSelect}