mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 01:07:43 +00:00
Merge pull request #112 from shumaiOne/fix/optimize-reorder-query
fix(webui): avoid refetching all pages on file/folder reorder
This commit is contained in:
@@ -1152,6 +1152,134 @@ describe('AssetService', () => {
|
||||
expect(updatedA?.sortIndex && updatedA.sortIndex > 'a1').toBe(true)
|
||||
})
|
||||
|
||||
it('verifies that sortIndex uses C collation to prevent fractional indexing mismatch', async () => {
|
||||
const { user, project } = await setupBasicAssets()
|
||||
|
||||
// Create a parent folder to contain our assets
|
||||
const folder = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Parent Folder',
|
||||
type: AssetType.folder,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Asset A: In ASCII, "YjnsD0vZ" > "YjNuyGpV" because "n" (110) > "N" (78).
|
||||
// In default Postgres locale collation, "YjnsD0vZ" < "YjNuyGpV".
|
||||
const assetA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset A',
|
||||
type: AssetType.file,
|
||||
parentId: folder.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'YjnsD0vZ',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Asset B
|
||||
const assetB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset B',
|
||||
type: AssetType.file,
|
||||
parentId: folder.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'YjNuyGpV',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Create Asset C
|
||||
const assetC = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset C',
|
||||
type: AssetType.file,
|
||||
parentId: folder.id,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'Yjzaaa',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Verify ASCII ordering in JavaScript: assetA.sortIndex > assetB.sortIndex
|
||||
expect(assetA.sortIndex! > assetB.sortIndex!).toBe(true)
|
||||
|
||||
// Verify Postgres sorting behavior:
|
||||
// With C collation, it must follow ASCII ordering. Therefore, searching for
|
||||
// sortIndex < assetB.sortIndex must NOT find assetA.
|
||||
const prevAsset = await prisma.asset.findFirst({
|
||||
where: {
|
||||
parentId: folder.id,
|
||||
sortIndex: { lt: assetB.sortIndex! },
|
||||
id: { not: assetC.id },
|
||||
},
|
||||
orderBy: { sortIndex: 'desc' },
|
||||
})
|
||||
|
||||
// Assert that the database collation is correctly "C" (returns null for prevAsset)
|
||||
expect(prevAsset).toBeNull()
|
||||
|
||||
// Assert that updateAssetOrder succeeds without throwing a collation error
|
||||
const updatedC = await assetService.updateAssetOrder(assetC.id, {
|
||||
beforeIndex: assetB.sortIndex!,
|
||||
})
|
||||
expect(updatedC.sortIndex).toBeDefined()
|
||||
expect(updatedC.sortIndex! < assetB.sortIndex!).toBe(true)
|
||||
})
|
||||
|
||||
it('correctly bounds the new sortIndex when moving an asset after another asset in the middle of the list', async () => {
|
||||
const { user, project } = await setupBasicAssets()
|
||||
|
||||
// Create three assets: A, B, C with specific sort indexes
|
||||
const assetA = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset A',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'a0',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const assetB = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset B',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'a1',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
const assetC = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'Asset C',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
creatorId: user.id,
|
||||
sortIndex: 'a11',
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Move Asset A to be after Asset B (it should be placed between B ("a1") and C ("a11"))
|
||||
const updatedA = await assetService.updateAssetOrder(assetA.id, {
|
||||
afterIndex: assetB.sortIndex!,
|
||||
})
|
||||
|
||||
// Verify it is placed after B
|
||||
expect(updatedA.sortIndex! > assetB.sortIndex!).toBe(true)
|
||||
// Verify it is placed before C (this fails under the buggy code because C's bound is ignored, generating a key greater than "a11")
|
||||
expect(updatedA.sortIndex! < assetC.sortIndex!).toBe(true)
|
||||
})
|
||||
|
||||
describe('Symlinks', () => {
|
||||
it('toAssetInfo resolves symlink target metadata', async () => {
|
||||
const { project, user } = await setupBasicAssets()
|
||||
@@ -1266,6 +1394,7 @@ describe('AssetService', () => {
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'abc',
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1297,6 +1426,7 @@ describe('AssetService', () => {
|
||||
expect(res.data).toHaveLength(1)
|
||||
const stackAsset = res.data[0]
|
||||
expect(stackAsset.type).toBe(AssetType.version_stack)
|
||||
expect(stackAsset.sortIndex).toBe('abc')
|
||||
expect(stackAsset.versionStack).toBeDefined()
|
||||
expect(stackAsset.versionStack?.versions).toHaveLength(2)
|
||||
const versions = stackAsset.versionStack!.versions
|
||||
|
||||
@@ -533,6 +533,7 @@ export class AssetService {
|
||||
parentId: stackParentId,
|
||||
creatorId: creatorId,
|
||||
name: '',
|
||||
sortIndex: destFile.sortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -973,7 +974,15 @@ export class AssetService {
|
||||
afterSortIndex = prevAsset?.sortIndex || null
|
||||
} else if (req.afterIndex) {
|
||||
afterSortIndex = req.afterIndex
|
||||
beforeSortIndex = null
|
||||
const nextAsset = await this.prismaClient.asset.findFirst({
|
||||
where: {
|
||||
parentId: asset.parentId,
|
||||
sortIndex: { gt: req.afterIndex },
|
||||
id: { not: id },
|
||||
},
|
||||
orderBy: { sortIndex: 'asc' },
|
||||
})
|
||||
beforeSortIndex = nextAsset?.sortIndex || null
|
||||
}
|
||||
|
||||
const newSortIndex = generateKeyBetween(afterSortIndex, beforeSortIndex)
|
||||
|
||||
@@ -56,6 +56,7 @@ describe('VersionStackService', () => {
|
||||
creatorId: user.id,
|
||||
status: 'uploaded',
|
||||
sizeByte: 100,
|
||||
sortIndex: 'abc',
|
||||
},
|
||||
})
|
||||
|
||||
@@ -85,6 +86,7 @@ describe('VersionStackService', () => {
|
||||
expect(stack.fileCount).toBe(2)
|
||||
expect(stack.sizeByte).toBe(300)
|
||||
expect(stack.parentId).toBe(parent.id)
|
||||
expect(stack.sortIndex).toBe('abc')
|
||||
|
||||
// Verify Parent file count
|
||||
const updatedParent = await prisma.asset.findUnique({ where: { id: parent.id } })
|
||||
|
||||
@@ -58,6 +58,9 @@ export class VersionStackService {
|
||||
throw new Error('parent not found')
|
||||
}
|
||||
|
||||
const firstFile = files[0]
|
||||
const sortIndex = firstFile?.sortIndex || null
|
||||
|
||||
const stack = await tx.asset.create({
|
||||
data: {
|
||||
type: AssetType.version_stack,
|
||||
@@ -67,6 +70,7 @@ export class VersionStackService {
|
||||
projectId: projectId,
|
||||
creatorId: creatorId,
|
||||
parentId: parentId,
|
||||
sortIndex: sortIndex,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
-- AlterTable: Change sort_index column collation to "C"
|
||||
ALTER TABLE "assets" ALTER COLUMN "sort_index" TYPE TEXT COLLATE "C";
|
||||
|
||||
-- Recreate index on sort_index with "C" collation
|
||||
DROP INDEX IF EXISTS "assets_sort_index_idx";
|
||||
CREATE INDEX "assets_sort_index_idx" ON "assets" ("sort_index" COLLATE "C");
|
||||
@@ -853,6 +853,7 @@ export function FileBrowser({
|
||||
<div
|
||||
ref={scrollContainerRef}
|
||||
className="flex-1 overflow-y-auto min-h-0 relative flex flex-col"
|
||||
style={{ overflowAnchor: 'none' }}
|
||||
>
|
||||
{displayStyle === 'list' ? (
|
||||
<FileBrowserListView
|
||||
|
||||
@@ -12,7 +12,7 @@ import { useMemberStore } from '@/ui/stores/members'
|
||||
import { useTopNavStore } from '@/ui/stores/top-nav'
|
||||
import { useUiStore } from '@/ui/stores/ui'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
import { PointerActivationConstraints } from '@dnd-kit/dom'
|
||||
import { Feedback, PointerActivationConstraints } from '@dnd-kit/dom'
|
||||
import { DragDropProvider, DragOverlay, KeyboardSensor, PointerSensor } from '@dnd-kit/react'
|
||||
import { useInfiniteQuery, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useNavigate } from '@tanstack/react-router'
|
||||
@@ -400,6 +400,17 @@ export default function FileSystemManager({
|
||||
return (
|
||||
<DragDropProvider
|
||||
modifiers={[SnapToPointer.configure({ anchor: { x: 0, y: 0 } })]}
|
||||
plugins={(defaults) =>
|
||||
defaults.map((p) => {
|
||||
if (p === Feedback) {
|
||||
return Feedback.configure({ dropAnimation: null })
|
||||
}
|
||||
if (typeof p === 'object' && p !== null && 'plugin' in p && p.plugin === Feedback) {
|
||||
return Feedback.configure({ dropAnimation: null })
|
||||
}
|
||||
return p
|
||||
})
|
||||
}
|
||||
sensors={
|
||||
isRecentlyDeleted || !canEdit
|
||||
? []
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { InferRequestType, InferResponseType } from 'hono/client'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import type { AssetInfo, AssetInfoPaginatedList, SearchSort } from '@shumai/dtos'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import type { InfiniteData } from '@tanstack/react-query'
|
||||
import { useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import type { DragEndEvent, DragStartEvent } from '@dnd-kit/react'
|
||||
@@ -31,6 +32,71 @@ export function useFileSystemDnd({
|
||||
const queryClient = useQueryClient()
|
||||
const [dragState, setDragState] = useState<DragState | undefined>(undefined)
|
||||
|
||||
const updateLocalCache = (isFolder: boolean, updatedItem: AssetInfo) => {
|
||||
const assetType = isFolder ? 'folder' : 'file'
|
||||
const queryKeyPrefix = ['search', teamId, assetId, assetType]
|
||||
|
||||
const queries = queryClient.getQueriesData<InfiniteData<AssetInfoPaginatedList>>({
|
||||
queryKey: queryKeyPrefix,
|
||||
})
|
||||
|
||||
const isSearchSort = (val: unknown): val is SearchSort => {
|
||||
if (typeof val !== 'object' || val === null) return false
|
||||
const obj = val as Record<string, unknown>
|
||||
return (
|
||||
'field' in obj &&
|
||||
'order' in obj &&
|
||||
typeof obj.field === 'string' &&
|
||||
(obj.order === 'asc' || obj.order === 'desc')
|
||||
)
|
||||
}
|
||||
|
||||
for (const [queryKey, oldData] of queries) {
|
||||
if (!oldData || !Array.isArray(oldData.pages)) continue
|
||||
|
||||
const sort = queryKey.find(isSearchSort)
|
||||
|
||||
const allItems = oldData.pages.flatMap((page) => page.data ?? [])
|
||||
|
||||
const itemIndex = allItems.findIndex((item) => item.id === updatedItem.id)
|
||||
if (itemIndex === -1) {
|
||||
continue
|
||||
}
|
||||
|
||||
allItems[itemIndex] = { ...allItems[itemIndex], ...updatedItem }
|
||||
|
||||
if (sort?.field === 'custom') {
|
||||
const isDesc = sort.order === 'desc'
|
||||
allItems.sort((a, b) => {
|
||||
const indexA = a.sortIndex ?? ''
|
||||
const indexB = b.sortIndex ?? ''
|
||||
if (indexA === indexB) return 0
|
||||
if (isDesc) {
|
||||
return indexA < indexB ? 1 : -1
|
||||
} else {
|
||||
return indexA < indexB ? -1 : 1
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let pointer = 0
|
||||
const newPages = oldData.pages.map((page) => {
|
||||
const pageSize = page.data ? page.data.length : 0
|
||||
const pageData = allItems.slice(pointer, pointer + pageSize)
|
||||
pointer += pageSize
|
||||
return {
|
||||
...page,
|
||||
data: pageData,
|
||||
}
|
||||
})
|
||||
|
||||
queryClient.setQueryData<InfiniteData<AssetInfoPaginatedList>>(queryKey, {
|
||||
...oldData,
|
||||
pages: newPages,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const $reparent = client.api.projects[':projectId'].reparent.$post
|
||||
const { mutate: reparentAssets } = useMutation<
|
||||
InferResponseType<typeof $reparent, 204>,
|
||||
@@ -185,11 +251,9 @@ export function useFileSystemDnd({
|
||||
: { afterIndex: targetItem.sortIndex ?? undefined },
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
onSuccess: (updatedFolder) => {
|
||||
toast.success('Folder reordered')
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
updateLocalCache(true, updatedFolder)
|
||||
onClearSelection()
|
||||
},
|
||||
onError: (err) => {
|
||||
@@ -207,11 +271,9 @@ export function useFileSystemDnd({
|
||||
: { afterIndex: targetItem.sortIndex ?? undefined },
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
onSuccess: (updatedFile) => {
|
||||
toast.success('File reordered')
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
updateLocalCache(false, updatedFile)
|
||||
onClearSelection()
|
||||
},
|
||||
onError: (err) => {
|
||||
|
||||
Reference in New Issue
Block a user