Merge pull request #266 from shumaiOne/fix/field-reorder-dnd-kit

fix(webui): persist field order on drag and drop
This commit is contained in:
Yiling-J
2026-08-05 16:01:55 +08:00
committed by GitHub
3 changed files with 68 additions and 49 deletions
+19 -16
View File
@@ -8,7 +8,7 @@ import { ulid } from 'ulid'
import { client } from '@/ui/api/client' import { client } from '@/ui/api/client'
import { useFieldStore } from '@/ui/stores/fields' import { useFieldStore } from '@/ui/stores/fields'
import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react' import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react'
import { useSortable } from '@dnd-kit/react/sortable' import { isSortable, useSortable } from '@dnd-kit/react/sortable'
import { useMutation, useQueryClient } from '@tanstack/react-query' import { useMutation, useQueryClient } from '@tanstack/react-query'
import { InferRequestType, InferResponseType } from 'hono/client' import { InferRequestType, InferResponseType } from 'hono/client'
import { m } from '@/ui/paraglide/messages.js' import { m } from '@/ui/paraglide/messages.js'
@@ -24,7 +24,7 @@ import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popo
import { Input } from '@/ui/components/ui/input' import { Input } from '@/ui/components/ui/input'
import { Label } from '@/ui/components/ui/label' import { Label } from '@/ui/components/ui/label'
import { Switch } from '@/ui/components/ui/switch' import { Switch } from '@/ui/components/ui/switch'
import { arrayMove } from '@/ui/lib/dnd-utils' import { reorderFieldSubset } from '@/ui/lib/dnd-utils'
import { PointerActivationConstraints } from '@dnd-kit/dom' import { PointerActivationConstraints } from '@dnd-kit/dom'
export const PREDEFINED_COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'gray'] export const PREDEFINED_COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'gray']
@@ -220,20 +220,23 @@ export function FieldsManager({ projectId, onManageFields, onSave }: FieldsManag
) )
const handleDragEnd = (event: DragEndEvent) => { const handleDragEnd = (event: DragEndEvent) => {
const { source, target } = event.operation if (event.canceled) return
if (source && target && source.id !== target.id) { const { source } = event.operation
const oldIndex = fields.findIndex((f) => f.id === source.id) if (!isSortable(source)) return
const newIndex = fields.findIndex((f) => f.id === target.id) const { initialIndex, index } = source
const newFields = arrayMove(fields, oldIndex, newIndex) if (initialIndex === index) return
updateFields(newFields)
updateFieldsOrder({ // Rows are rendered from the search-filtered subset, so indices are
param: { projectId: projectId }, // relative to `filteredFields`, not the full `fields` array.
json: newFields.map((f: MetadataFieldInfo) => ({ const newFields = reorderFieldSubset(fields, filteredFields, initialIndex, index)
fieldId: f.id!, updateFields(newFields)
visible: f.visible || false, updateFieldsOrder({
})), param: { projectId: projectId },
}) json: newFields.map((f: MetadataFieldInfo) => ({
} fieldId: f.id!,
visible: f.visible || false,
})),
})
} }
const handleVisibilityChange = (fieldId: string, visible: boolean) => { const handleVisibilityChange = (fieldId: string, visible: boolean) => {
@@ -1,18 +1,19 @@
import { client } from '@/ui/api/client'
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover'
import { reorderFieldSubset } from '@/ui/lib/dnd-utils'
import { m } from '@/ui/paraglide/messages.js'
import { useFieldStore } from '@/ui/stores/fields'
import { PointerActivationConstraints } from '@dnd-kit/dom'
import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react'
import { isSortable, useSortable } from '@dnd-kit/react/sortable'
import { import {
type FieldInfo as MetadataFieldInfo,
type FieldInfo,
FieldType, FieldType,
type FieldInfo,
type FieldInfo as MetadataFieldInfo,
type SelectOption, type SelectOption,
} from '@shumai/dtos' } from '@shumai/dtos'
import { ulid } from 'ulid'
import { client } from '@/ui/api/client'
import { useMutation, useQueryClient } from '@tanstack/react-query' import { useMutation, useQueryClient } from '@tanstack/react-query'
import { InferRequestType, InferResponseType } from 'hono/client' import { InferRequestType, InferResponseType } from 'hono/client'
import { useFieldStore } from '@/ui/stores/fields'
import { DragDropProvider, KeyboardSensor, PointerSensor, type DragEndEvent } from '@dnd-kit/react'
import { useSortable } from '@dnd-kit/react/sortable'
import { arrayMove } from '@/ui/lib/dnd-utils'
import { PointerActivationConstraints } from '@dnd-kit/dom'
import { import {
ChevronDown, ChevronDown,
FileText, FileText,
@@ -23,15 +24,14 @@ import {
Sparkles, Sparkles,
X, X,
} from 'lucide-react' } from 'lucide-react'
import { useMemo, useState, useEffect } from 'react' import { useEffect, useMemo, useState } from 'react'
import { toast } from 'sonner' import { toast } from 'sonner'
import { Popover, PopoverTrigger, PopoverContent } from '@/ui/components/ui/popover' import { ulid } from 'ulid'
import { m } from '@/ui/paraglide/messages.js'
import { import {
FIELD_TYPE_ICONS,
PREDEFINED_COLORS,
COLOR_MAP, COLOR_MAP,
FIELD_TYPE_ICONS,
getRandomUnusedColor, getRandomUnusedColor,
PREDEFINED_COLORS,
} from './fields-manager' } from './fields-manager'
import { Button } from '@/ui/components/ui/button' import { Button } from '@/ui/components/ui/button'
@@ -278,25 +278,24 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
} }
const handleDragEnd = (event: DragEndEvent) => { const handleDragEnd = (event: DragEndEvent) => {
const { source, target } = event.operation if (event.canceled) return
if (source && target && source.id !== target.id) { const { source } = event.operation
const oldIndex = fields.findIndex((f) => f.id === source.id) if (!isSortable(source)) return
const newIndex = fields.findIndex((f) => f.id === target.id) const { initialIndex, index } = source
const newFields = arrayMove(fields, oldIndex, newIndex) if (initialIndex === index) return
updateFields(newFields)
// Only persist order if we are in project scope (custom fields) // Sortable rows are rendered from the selected group's subset, so indices are
// or if the API supports ordering for other scopes. // relative to `filteredFields`, not the full `fields` array.
if (selectedGroup === SCOPE_GROUPS.PROJECT) { const newFields = reorderFieldSubset(fields, filteredFields, initialIndex, index)
updateFieldsOrder({ updateFields(newFields)
param: { projectId: projectId },
json: newFields.map((f: MetadataFieldInfo) => ({ updateFieldsOrder({
fieldId: f.id!, param: { projectId: projectId },
visible: f.visible || false, json: newFields.map((f: MetadataFieldInfo) => ({
})), fieldId: f.id!,
}) visible: f.visible || false,
} })),
} })
} }
const handleSaveCreation = () => { const handleSaveCreation = () => {
@@ -730,7 +729,7 @@ export function ManageFieldsDialog({ projectId, open, onOpenChange }: ManageFiel
index={index} index={index}
isSelected={selectedFieldId === field.id} isSelected={selectedFieldId === field.id}
onSelect={handleSelectField} onSelect={handleSelectField}
isSortable={selectedGroup === SCOPE_GROUPS.PROJECT} isSortable
/> />
))} ))}
</DragDropProvider> </DragDropProvider>
+17
View File
@@ -4,6 +4,23 @@ export function arrayMove<T>(array: T[], from: number, to: number): T[] {
return newArray return newArray
} }
/**
* Reorders a subset of fields (a grouped/filtered view) within the full list.
* `subset` must appear in `full` in the same relative order (filtering preserves order).
*/
export function reorderFieldSubset<T extends { id?: string | null }>(
full: T[],
subset: T[],
from: number,
to: number,
): T[] {
if (from === to) return full
const reordered = arrayMove(subset, from, to)
const subsetIds = new Set(subset.map((f) => f.id))
let i = 0
return full.map((f) => (subsetIds.has(f.id) ? reordered[i++] : f))
}
interface SafeFileSystemEntry { interface SafeFileSystemEntry {
name: string name: string
isFile: boolean isFile: boolean