mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 22:14:50 +00:00
initial commit
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
'use client'
|
||||
|
||||
import { client } from '@/ui/api/client'
|
||||
import type { InferRequestType, InferResponseType } from 'hono/client'
|
||||
import type { AssetInfo } from '@/dtos/asset'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
import type { DragEndEvent, DragStartEvent } from '@dnd-kit/react'
|
||||
import type { DragState } from './dnd-types'
|
||||
|
||||
interface UseFileSystemDndProps {
|
||||
teamId: string
|
||||
projectId: string
|
||||
assetId: string
|
||||
folders: AssetInfo[]
|
||||
files: AssetInfo[]
|
||||
selectedIds: Set<string>
|
||||
onClearSelection: () => void
|
||||
}
|
||||
|
||||
export function useFileSystemDnd({
|
||||
teamId,
|
||||
projectId,
|
||||
assetId,
|
||||
folders,
|
||||
files,
|
||||
selectedIds,
|
||||
onClearSelection,
|
||||
}: UseFileSystemDndProps) {
|
||||
const queryClient = useQueryClient()
|
||||
const [dragState, setDragState] = useState<DragState | undefined>(undefined)
|
||||
|
||||
const $reparent = client.api.projects[':projectId'].reparent.$post
|
||||
const { mutate: reparentAssets } = useMutation<
|
||||
InferResponseType<typeof $reparent, 204>,
|
||||
Error,
|
||||
InferRequestType<typeof $reparent>
|
||||
>({
|
||||
mutationFn: async (req) => {
|
||||
const res = await $reparent(req)
|
||||
if (!res.ok) throw new Error('Failed to move assets')
|
||||
return null as unknown as InferResponseType<typeof $reparent, 204>
|
||||
},
|
||||
})
|
||||
|
||||
const $reorderFolder = client.api.teams[':teamId'].folders[':folderId'].order.$patch
|
||||
const { mutate: reorderFolder } = useMutation<
|
||||
InferResponseType<typeof $reorderFolder, 200>,
|
||||
Error,
|
||||
InferRequestType<typeof $reorderFolder>
|
||||
>({
|
||||
mutationFn: async (req) => {
|
||||
const res = await $reorderFolder(req)
|
||||
if (!res.ok) throw new Error('Failed to reorder folder')
|
||||
return (await res.json()) as unknown as InferResponseType<typeof $reorderFolder, 200>
|
||||
},
|
||||
})
|
||||
|
||||
const $reorderFile = client.api.teams[':teamId'].files[':fileId'].order.$patch
|
||||
const { mutate: reorderFile } = useMutation<
|
||||
InferResponseType<typeof $reorderFile, 200>,
|
||||
Error,
|
||||
InferRequestType<typeof $reorderFile>
|
||||
>({
|
||||
mutationFn: async (req) => {
|
||||
const res = await $reorderFile(req)
|
||||
if (!res.ok) throw new Error('Failed to reorder file')
|
||||
return (await res.json()) as unknown as InferResponseType<typeof $reorderFile, 200>
|
||||
},
|
||||
})
|
||||
|
||||
const $addAssetToShare = client.api.teams[':teamId'].shares[':shareId'].assets.$post
|
||||
const { mutate: addAssetToShare } = useMutation<
|
||||
{ addedCount: number },
|
||||
Error,
|
||||
InferRequestType<typeof $addAssetToShare>
|
||||
>({
|
||||
mutationFn: async (req) => {
|
||||
const res = await $addAssetToShare(req)
|
||||
if (!res.ok) throw new Error('Failed to add asset to share')
|
||||
return (await res.json()) as { addedCount: number }
|
||||
},
|
||||
})
|
||||
|
||||
const handleDragStart = (event: DragStartEvent) => {
|
||||
const active = event.operation.source
|
||||
if (!active) return
|
||||
|
||||
const activeId = (active.id as string).split(':').pop()!
|
||||
const activeData = active.data as { type: string; item: AssetInfo } | undefined
|
||||
|
||||
if (!activeData) return
|
||||
|
||||
let draggedIds: Set<string>
|
||||
let draggedItems: AssetInfo[]
|
||||
|
||||
// If active item is in selection, we drag the whole selection
|
||||
if (selectedIds.has(activeId)) {
|
||||
draggedIds = new Set(selectedIds)
|
||||
draggedItems = [...folders, ...files].filter((item) => selectedIds.has(item.id!))
|
||||
} else {
|
||||
// Otherwise we drag only the active item
|
||||
draggedIds = new Set([activeId])
|
||||
draggedItems = [activeData.item]
|
||||
}
|
||||
|
||||
const hasFolders = draggedItems.some((item) => item.type === 'folder')
|
||||
|
||||
setDragState({
|
||||
isActive: true,
|
||||
draggedIds,
|
||||
hasFolders,
|
||||
itemCount: draggedIds.size,
|
||||
})
|
||||
}
|
||||
|
||||
const handleDragEnd = (event: DragEndEvent) => {
|
||||
const { target } = event.operation
|
||||
|
||||
if (target && dragState) {
|
||||
const targetId = (target.id as string).split(':').pop()!
|
||||
const targetData = target.data as { type: string; item: AssetInfo } | undefined
|
||||
|
||||
if (targetData?.type === 'share-link') {
|
||||
const shareId = targetId
|
||||
const assetIds = Array.from(dragState.draggedIds)
|
||||
|
||||
addAssetToShare(
|
||||
{
|
||||
param: { teamId, shareId },
|
||||
json: { assetIds },
|
||||
},
|
||||
{
|
||||
onSuccess: (data) => {
|
||||
if (data.addedCount === 0) {
|
||||
toast.info('Assets already exists in share')
|
||||
} else {
|
||||
toast.success(`Added ${data.addedCount} item(s) to share link`)
|
||||
}
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['shares', teamId, projectId],
|
||||
})
|
||||
onClearSelection()
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(`Failed to add to share: ${err.message}`)
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
setDragState(undefined)
|
||||
return
|
||||
}
|
||||
|
||||
let isValid = false
|
||||
|
||||
if (targetData && !dragState.draggedIds.has(targetId)) {
|
||||
if (targetData.type === 'folder' || targetData.type === 'reorder') {
|
||||
isValid = true
|
||||
} else if (targetData.type === 'file') {
|
||||
// Can only drop single file onto single file
|
||||
if (!dragState.hasFolders && dragState.itemCount === 1) {
|
||||
isValid = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetData?.type === 'reorder') {
|
||||
const { position, item: targetItem } = targetData as unknown as {
|
||||
position: 'before' | 'after'
|
||||
item: AssetInfo
|
||||
}
|
||||
|
||||
const isFolder = dragState.hasFolders
|
||||
const draggedId = Array.from(dragState.draggedIds)[0]
|
||||
|
||||
if (isFolder) {
|
||||
reorderFolder(
|
||||
{
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
param: { teamId: teamId, folderId: draggedId } as any,
|
||||
json:
|
||||
position === 'before'
|
||||
? { beforeIndex: targetItem.sortIndex ?? undefined }
|
||||
: { afterIndex: targetItem.sortIndex ?? undefined },
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success('Folder reordered')
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
onClearSelection()
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(`Failed to reorder: ${err.message}`)
|
||||
},
|
||||
},
|
||||
)
|
||||
} else {
|
||||
reorderFile(
|
||||
{
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
param: { teamId: teamId, fileId: draggedId } as any,
|
||||
json:
|
||||
position === 'before'
|
||||
? { beforeIndex: targetItem.sortIndex ?? undefined }
|
||||
: { afterIndex: targetItem.sortIndex ?? undefined },
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success('File reordered')
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
onClearSelection()
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(`Failed to reorder: ${err.message}`)
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
} else if (isValid) {
|
||||
// Execute API
|
||||
reparentAssets(
|
||||
{
|
||||
param: { projectId: projectId },
|
||||
json: {
|
||||
assetIds: Array.from(dragState.draggedIds),
|
||||
newParentId: targetId,
|
||||
},
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success('Assets moved successfully')
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['search', teamId, assetId],
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['folders', teamId],
|
||||
})
|
||||
// Clear selection after move
|
||||
onClearSelection()
|
||||
},
|
||||
onError: () => {
|
||||
toast.error('Failed to move assets')
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
setDragState(undefined)
|
||||
}
|
||||
|
||||
return {
|
||||
dragState,
|
||||
handleDragStart,
|
||||
handleDragEnd,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user