diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index 8a5738c9..840c3d15 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -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 diff --git a/packages/core/src/asset/asset.ts b/packages/core/src/asset/asset.ts index 0baf168a..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, }, }) @@ -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) 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, }, }) 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 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 ? [] diff --git a/packages/webui/components/use-file-system-dnd.ts b/packages/webui/components/use-file-system-dnd.ts index 0c7a2158..bf80c2fd 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,71 @@ 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, + }) + + 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') + ) + } + + 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>(queryKey, { + ...oldData, + pages: newPages, + }) + } + } + const $reparent = client.api.projects[':projectId'].reparent.$post const { mutate: reparentAssets } = useMutation< InferResponseType, @@ -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) => {