2025-10-18 19:32:05 +00:00
|
|
|
import { api } from "@fileone/convex/api"
|
|
|
|
|
import type { Doc, Id } from "@fileone/convex/dataModel"
|
2025-10-18 14:02:20 +00:00
|
|
|
import * as Err from "@fileone/convex/error"
|
2025-09-26 22:20:30 +00:00
|
|
|
import {
|
|
|
|
|
type DirectoryHandle,
|
|
|
|
|
type FileSystemHandle,
|
|
|
|
|
isSameHandle,
|
2025-10-18 14:02:20 +00:00
|
|
|
} from "@fileone/convex/filesystem"
|
2025-09-20 22:43:31 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query"
|
|
|
|
|
import { useMutation as useContextMutation } from "convex/react"
|
2025-09-26 22:20:30 +00:00
|
|
|
import type { PrimitiveAtom } from "jotai"
|
|
|
|
|
import { useSetAtom, useStore } from "jotai"
|
2025-09-20 22:43:31 +00:00
|
|
|
import { useState } from "react"
|
|
|
|
|
import { toast } from "sonner"
|
|
|
|
|
|
|
|
|
|
export interface FileDragInfo {
|
2025-09-20 23:54:27 +00:00
|
|
|
source: FileSystemHandle
|
2025-09-21 17:03:50 +00:00
|
|
|
items: FileSystemHandle[]
|
2025-09-20 22:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UseFileDropOptions {
|
2025-09-26 22:20:30 +00:00
|
|
|
destItem: DirectoryHandle | null
|
|
|
|
|
dragInfoAtom: PrimitiveAtom<FileDragInfo | null>
|
2025-09-20 23:54:27 +00:00
|
|
|
onDropSuccess?: (
|
|
|
|
|
items: Id<"files">[],
|
|
|
|
|
targetDirectory: Doc<"directories">,
|
|
|
|
|
) => void
|
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-09-26 22:20:30 +00:00
|
|
|
destItem,
|
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({
|
|
|
|
|
mutationFn: useContextMutation(api.filesystem.moveItems),
|
2025-09-20 22:43:31 +00:00
|
|
|
onSuccess: ({
|
2025-09-25 23:12:13 +00:00
|
|
|
moved,
|
|
|
|
|
errors,
|
2025-09-20 22:43:31 +00:00
|
|
|
}: {
|
2025-09-25 23:12:13 +00:00
|
|
|
moved: FileSystemHandle[]
|
|
|
|
|
errors: Err.ApplicationErrorData[]
|
2025-09-20 22:43:31 +00:00
|
|
|
}) => {
|
2025-09-25 23:12:13 +00:00
|
|
|
const conflictCount = errors.reduce((acc, error) => {
|
2025-11-08 18:03:15 +00:00
|
|
|
if (error.code === Err.ErrorCode.Conflict) {
|
2025-09-25 23:12:13 +00:00
|
|
|
return acc + 1
|
|
|
|
|
}
|
|
|
|
|
return acc
|
|
|
|
|
}, 0)
|
|
|
|
|
if (conflictCount > 0) {
|
|
|
|
|
toast.warning(
|
|
|
|
|
`${moved.length} items moved${conflictCount > 0 ? `, ${conflictCount} conflicts` : ""}`,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
toast.success(`${moved.length} items moved!`)
|
|
|
|
|
}
|
2025-09-20 22:43:31 +00:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const handleDrop = (_e: React.DragEvent) => {
|
|
|
|
|
const dragInfo = store.get(dragInfoAtom)
|
2025-09-26 22:20:30 +00:00
|
|
|
if (dragInfo && destItem) {
|
|
|
|
|
const items = dragInfo.items.filter(
|
|
|
|
|
(item) => !isSameHandle(item, destItem),
|
|
|
|
|
)
|
|
|
|
|
if (items.length > 0) {
|
|
|
|
|
moveDroppedItems({
|
|
|
|
|
targetDirectory: destItem,
|
|
|
|
|
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-09-26 22:20:30 +00:00
|
|
|
if (dragInfo && destItem) {
|
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
|
|
|
}
|