2025-09-20 22:43:31 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query"
|
2025-09-26 22:20:30 +00:00
|
|
|
import type { PrimitiveAtom } from "jotai"
|
2025-12-15 00:13:10 +00:00
|
|
|
import { useAtomValue, useSetAtom, useStore } from "jotai"
|
2025-09-20 22:43:31 +00:00
|
|
|
import { useState } from "react"
|
|
|
|
|
import { toast } from "sonner"
|
2025-12-15 00:13:10 +00:00
|
|
|
import {
|
|
|
|
|
type MoveDirectoryItemsResult,
|
|
|
|
|
moveDirectoryItemsMutationAtom,
|
|
|
|
|
} from "@/vfs/api"
|
|
|
|
|
import type { DirectoryInfo, DirectoryItem } from "@/vfs/vfs"
|
2025-09-20 22:43:31 +00:00
|
|
|
|
|
|
|
|
export interface FileDragInfo {
|
2025-12-15 00:13:10 +00:00
|
|
|
source: DirectoryItem
|
|
|
|
|
items: DirectoryItem[]
|
2025-09-20 22:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UseFileDropOptions {
|
2025-12-15 00:13:10 +00:00
|
|
|
destDir: DirectoryInfo | string
|
2025-09-26 22:20:30 +00:00
|
|
|
dragInfoAtom: PrimitiveAtom<FileDragInfo | null>
|
2025-09-20 22:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UseFileDropReturn {
|
|
|
|
|
isDraggedOver: boolean
|
|
|
|
|
dropHandlers: {
|
|
|
|
|
onDrop: (e: React.DragEvent) => void
|
|
|
|
|
onDragOver: (e: React.DragEvent) => void
|
|
|
|
|
onDragLeave: () => void
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useFileDrop({
|
2025-12-15 00:13:10 +00:00
|
|
|
destDir,
|
2025-09-20 22:43:31 +00:00
|
|
|
dragInfoAtom,
|
|
|
|
|
}: UseFileDropOptions): UseFileDropReturn {
|
|
|
|
|
const [isDraggedOver, setIsDraggedOver] = useState(false)
|
2025-09-26 22:20:30 +00:00
|
|
|
const setDragInfo = useSetAtom(dragInfoAtom)
|
2025-09-20 22:43:31 +00:00
|
|
|
const store = useStore()
|
|
|
|
|
|
2025-09-21 17:03:50 +00:00
|
|
|
const { mutate: moveDroppedItems } = useMutation({
|
2025-12-15 00:13:10 +00:00
|
|
|
...useAtomValue(moveDirectoryItemsMutationAtom),
|
|
|
|
|
onSuccess: (result: MoveDirectoryItemsResult) => {
|
|
|
|
|
const conflictCount = result.conflicts.length
|
2025-09-25 23:12:13 +00:00
|
|
|
if (conflictCount > 0) {
|
|
|
|
|
toast.warning(
|
2025-12-15 00:13:10 +00:00
|
|
|
`${result.moved.length} items moved${conflictCount > 0 ? `, ${conflictCount} conflicts` : ""}`,
|
2025-09-25 23:12:13 +00:00
|
|
|
)
|
|
|
|
|
} else {
|
2025-12-15 00:13:10 +00:00
|
|
|
toast.success(`${result.moved.length} items moved!`)
|
2025-09-25 23:12:13 +00:00
|
|
|
}
|
2025-09-20 22:43:31 +00:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2025-12-15 00:13:10 +00:00
|
|
|
const dirId = typeof destDir === "string" ? destDir : destDir.id
|
|
|
|
|
|
2025-09-20 22:43:31 +00:00
|
|
|
const handleDrop = (_e: React.DragEvent) => {
|
|
|
|
|
const dragInfo = store.get(dragInfoAtom)
|
2025-12-15 00:13:10 +00:00
|
|
|
if (dragInfo) {
|
|
|
|
|
const items = dragInfo.items.filter((item) => item.id !== dirId)
|
2025-09-26 22:20:30 +00:00
|
|
|
if (items.length > 0) {
|
|
|
|
|
moveDroppedItems({
|
2025-12-15 00:13:10 +00:00
|
|
|
targetDirectory: destDir,
|
2025-09-26 22:20:30 +00:00
|
|
|
items,
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-09-20 22:43:31 +00:00
|
|
|
}
|
|
|
|
|
setIsDraggedOver(false)
|
2025-09-26 22:20:30 +00:00
|
|
|
setDragInfo(null)
|
2025-09-20 22:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleDragOver = (e: React.DragEvent) => {
|
|
|
|
|
const dragInfo = store.get(dragInfoAtom)
|
2025-12-15 00:13:10 +00:00
|
|
|
if (dragInfo && destDir) {
|
2025-09-20 22:43:31 +00:00
|
|
|
e.preventDefault()
|
|
|
|
|
e.dataTransfer.dropEffect = "move"
|
|
|
|
|
setIsDraggedOver(true)
|
|
|
|
|
} else {
|
|
|
|
|
e.dataTransfer.dropEffect = "none"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleDragLeave = () => {
|
|
|
|
|
setIsDraggedOver(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
isDraggedOver,
|
|
|
|
|
dropHandlers: {
|
|
|
|
|
onDrop: handleDrop,
|
|
|
|
|
onDragOver: handleDragOver,
|
|
|
|
|
onDragLeave: handleDragLeave,
|
|
|
|
|
},
|
|
|
|
|
}
|
2025-09-20 23:54:27 +00:00
|
|
|
}
|