From 3b4751bcb2490ee731f97e66b26f9bca29270497 Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 15 Jun 2026 19:20:45 +0800 Subject: [PATCH 1/7] fix(webui): avoid refetching all pages on file/folder reorder --- .../webui/components/use-file-system-dnd.ts | 64 ++++++++++++++++--- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/packages/webui/components/use-file-system-dnd.ts b/packages/webui/components/use-file-system-dnd.ts index 0c7a2158..170d098c 100644 --- a/packages/webui/components/use-file-system-dnd.ts +++ b/packages/webui/components/use-file-system-dnd.ts @@ -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,55 @@ export function useFileSystemDnd({ const queryClient = useQueryClient() const [dragState, setDragState] = useState(undefined) + const updateLocalCache = (isFolder: boolean, updatedItem: AssetInfo) => { + const assetType = isFolder ? 'folder' : 'file' + const queryKeyPrefix = ['search', teamId, assetId, assetType] + + const queries = queryClient.getQueriesData>({ + queryKey: queryKeyPrefix, + }) + + for (const [queryKey, oldData] of queries) { + if (!oldData) continue + + const sort = queryKey[5] as SearchSort | undefined + const isDesc = sort?.order === 'desc' + + 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 } + + allItems.sort((a, b) => { + const indexA = a.sortIndex ?? '' + const indexB = b.sortIndex ?? '' + if (indexA === indexB) return 0 + const comp = indexA.localeCompare(indexB) + return isDesc ? -comp : comp + }) + + 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>(queryKey, { + ...oldData, + pages: newPages, + }) + } + } + const $reparent = client.api.projects[':projectId'].reparent.$post const { mutate: reparentAssets } = useMutation< InferResponseType, @@ -185,11 +235,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 +255,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) => { From 1f640ede19ab9591d60e036e9636afba71b546eb Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 15 Jun 2026 19:33:51 +0800 Subject: [PATCH 2/7] test(core): add test reproducing fractional indexing collation mismatch --- packages/core/src/asset/asset.test.ts | 84 +++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index 8a5738c9..c687798d 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -1152,6 +1152,90 @@ describe('AssetService', () => { expect(updatedA?.sortIndex && updatedA.sortIndex > 'a1').toBe(true) }) + it('reproduces collation mismatch error when reordering assets with mixed-case sort indexes', 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: + // If the database collation is locale-aware (case-insensitive or groups n/N), + // it will consider assetA.sortIndex < assetB.sortIndex. + // So if we find the first asset with sortIndex < assetB.sortIndex, + // a locale-aware collation will find assetA, while ASCII ordering would NOT find assetA. + const prevAsset = await prisma.asset.findFirst({ + where: { + parentId: folder.id, + sortIndex: { lt: assetB.sortIndex! }, + id: { not: assetC.id }, + }, + orderBy: { sortIndex: 'desc' }, + }) + + console.log('Postgres found prevAsset with sortIndex:', prevAsset?.sortIndex) + + // If it finds assetA, it confirms that Postgres is using a different collation order than ASCII. + // Then calling updateAssetOrder will call generateKeyBetween("YjnsD0vZ", "YjNuyGpV"), which throws. + if (prevAsset?.sortIndex === 'YjnsD0vZ') { + await expect( + assetService.updateAssetOrder(assetC.id, { + beforeIndex: assetB.sortIndex!, + }) + ).rejects.toThrow() + } + }) + describe('Symlinks', () => { it('toAssetInfo resolves symlink target metadata', async () => { const { project, user } = await setupBasicAssets() From cf2546c86117220e1485892baccb4bb1a8a1ccf9 Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 15 Jun 2026 19:51:59 +0800 Subject: [PATCH 3/7] migration(db): add C collation to sort_index column --- packages/core/src/asset/asset.test.ts | 26 ++++++++----------- .../migration.sql | 6 +++++ 2 files changed, 17 insertions(+), 15 deletions(-) create mode 100644 packages/db/prisma/migrations/20260615115103_add_c_collation_to_sort_index/migration.sql diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index c687798d..82e81768 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -1152,7 +1152,7 @@ describe('AssetService', () => { expect(updatedA?.sortIndex && updatedA.sortIndex > 'a1').toBe(true) }) - it('reproduces collation mismatch error when reordering assets with mixed-case sort indexes', async () => { + 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 @@ -1210,10 +1210,8 @@ describe('AssetService', () => { expect(assetA.sortIndex! > assetB.sortIndex!).toBe(true) // Verify Postgres sorting behavior: - // If the database collation is locale-aware (case-insensitive or groups n/N), - // it will consider assetA.sortIndex < assetB.sortIndex. - // So if we find the first asset with sortIndex < assetB.sortIndex, - // a locale-aware collation will find assetA, while ASCII ordering would NOT find assetA. + // 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, @@ -1223,17 +1221,15 @@ describe('AssetService', () => { orderBy: { sortIndex: 'desc' }, }) - console.log('Postgres found prevAsset with sortIndex:', prevAsset?.sortIndex) + // Assert that the database collation is correctly "C" (returns null for prevAsset) + expect(prevAsset).toBeNull() - // If it finds assetA, it confirms that Postgres is using a different collation order than ASCII. - // Then calling updateAssetOrder will call generateKeyBetween("YjnsD0vZ", "YjNuyGpV"), which throws. - if (prevAsset?.sortIndex === 'YjnsD0vZ') { - await expect( - assetService.updateAssetOrder(assetC.id, { - beforeIndex: assetB.sortIndex!, - }) - ).rejects.toThrow() - } + // 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) }) describe('Symlinks', () => { diff --git a/packages/db/prisma/migrations/20260615115103_add_c_collation_to_sort_index/migration.sql b/packages/db/prisma/migrations/20260615115103_add_c_collation_to_sort_index/migration.sql new file mode 100644 index 00000000..38c3aaa5 --- /dev/null +++ b/packages/db/prisma/migrations/20260615115103_add_c_collation_to_sort_index/migration.sql @@ -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"); \ No newline at end of file From e8e04edfeef1e46578ca4119c8d132e74416bae3 Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 15 Jun 2026 19:57:02 +0800 Subject: [PATCH 4/7] fix(webui): address code review feedback for updateLocalCache --- .../webui/components/use-file-system-dnd.ts | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/webui/components/use-file-system-dnd.ts b/packages/webui/components/use-file-system-dnd.ts index 170d098c..bf80c2fd 100644 --- a/packages/webui/components/use-file-system-dnd.ts +++ b/packages/webui/components/use-file-system-dnd.ts @@ -40,11 +40,21 @@ export function useFileSystemDnd({ queryKey: queryKeyPrefix, }) - for (const [queryKey, oldData] of queries) { - if (!oldData) continue + const isSearchSort = (val: unknown): val is SearchSort => { + if (typeof val !== 'object' || val === null) return false + const obj = val as Record + return ( + 'field' in obj && + 'order' in obj && + typeof obj.field === 'string' && + (obj.order === 'asc' || obj.order === 'desc') + ) + } - const sort = queryKey[5] as SearchSort | undefined - const isDesc = sort?.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 ?? []) @@ -55,13 +65,19 @@ export function useFileSystemDnd({ allItems[itemIndex] = { ...allItems[itemIndex], ...updatedItem } - allItems.sort((a, b) => { - const indexA = a.sortIndex ?? '' - const indexB = b.sortIndex ?? '' - if (indexA === indexB) return 0 - const comp = indexA.localeCompare(indexB) - return isDesc ? -comp : comp - }) + 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) => { From 45318d1eec63bffaa6b83fd5ecac652c7589b3d1 Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 15 Jun 2026 20:25:49 +0800 Subject: [PATCH 5/7] fix(core): correctly query nextAsset for afterIndex reordering --- packages/core/src/asset/asset.test.ts | 48 +++++++++++++++++++++++++++ packages/core/src/asset/asset.ts | 10 +++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index 82e81768..424f1756 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -1232,6 +1232,54 @@ describe('AssetService', () => { 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() diff --git a/packages/core/src/asset/asset.ts b/packages/core/src/asset/asset.ts index 0baf168a..0ac43229 100644 --- a/packages/core/src/asset/asset.ts +++ b/packages/core/src/asset/asset.ts @@ -973,7 +973,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) From 2858274e52e5fdd6b2eed7d773261e3a60f81187 Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 15 Jun 2026 20:38:46 +0800 Subject: [PATCH 6/7] fix(core): inherit sortIndex when creating version stack container --- packages/core/src/asset/asset.test.ts | 2 ++ packages/core/src/asset/asset.ts | 1 + packages/core/src/versionStack/versionStack.test.ts | 2 ++ packages/core/src/versionStack/versionStack.ts | 4 ++++ 4 files changed, 9 insertions(+) diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index 424f1756..840c3d15 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -1394,6 +1394,7 @@ describe('AssetService', () => { creatorId: user.id, status: 'uploaded', sizeByte: 100, + sortIndex: 'abc', }, }) @@ -1425,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 diff --git a/packages/core/src/asset/asset.ts b/packages/core/src/asset/asset.ts index 0ac43229..afae531f 100644 --- a/packages/core/src/asset/asset.ts +++ b/packages/core/src/asset/asset.ts @@ -533,6 +533,7 @@ export class AssetService { parentId: stackParentId, creatorId: creatorId, name: '', + sortIndex: destFile.sortIndex, }, }) diff --git a/packages/core/src/versionStack/versionStack.test.ts b/packages/core/src/versionStack/versionStack.test.ts index 9a74b833..ec748b3f 100644 --- a/packages/core/src/versionStack/versionStack.test.ts +++ b/packages/core/src/versionStack/versionStack.test.ts @@ -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 } }) diff --git a/packages/core/src/versionStack/versionStack.ts b/packages/core/src/versionStack/versionStack.ts index e8d1b3f0..ab5fb746 100644 --- a/packages/core/src/versionStack/versionStack.ts +++ b/packages/core/src/versionStack/versionStack.ts @@ -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, }, }) From 68d0830d052274d1625ae549c88bc028f3a61141 Mon Sep 17 00:00:00 2001 From: yiling Date: Mon, 15 Jun 2026 22:13:46 +0800 Subject: [PATCH 7/7] fix(webui): resolve drag-and-drop cross-row reordering layout shift --- .../webui/components/file-browser/file-browser.tsx | 1 + packages/webui/components/file-system-manager.tsx | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/webui/components/file-browser/file-browser.tsx b/packages/webui/components/file-browser/file-browser.tsx index e174d3c1..1cd12260 100644 --- a/packages/webui/components/file-browser/file-browser.tsx +++ b/packages/webui/components/file-browser/file-browser.tsx @@ -853,6 +853,7 @@ export function FileBrowser({
{displayStyle === 'list' ? ( + 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 ? []