2025-09-20 22:43:31 +00:00
|
|
|
import { api } from "@fileone/convex/_generated/api"
|
|
|
|
|
import type { Doc, Id } from "@fileone/convex/_generated/dataModel"
|
2025-09-25 23:12:13 +00:00
|
|
|
import * as Err from "@fileone/convex/model/error"
|
2025-09-20 23:54:27 +00:00
|
|
|
import type {
|
|
|
|
|
DirectoryHandle,
|
|
|
|
|
FileSystemHandle,
|
|
|
|
|
} from "@fileone/convex/model/filesystem"
|
2025-09-20 22:43:31 +00:00
|
|
|
import { useMutation } from "@tanstack/react-query"
|
|
|
|
|
import { useMutation as useContextMutation } from "convex/react"
|
|
|
|
|
import type { Atom } from "jotai"
|
|
|
|
|
import { useStore } from "jotai"
|
|
|
|
|
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-20 23:54:27 +00:00
|
|
|
item: DirectoryHandle | null
|
2025-09-20 22:43:31 +00:00
|
|
|
dragInfoAtom: Atom<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({
|
|
|
|
|
item,
|
|
|
|
|
dragInfoAtom,
|
|
|
|
|
}: UseFileDropOptions): UseFileDropReturn {
|
|
|
|
|
const [isDraggedOver, setIsDraggedOver] = useState(false)
|
|
|
|
|
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) => {
|
|
|
|
|
if (error.code === Err.Code.Conflict) {
|
|
|
|
|
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-25 23:12:13 +00:00
|
|
|
console.log("dragInfo", dragInfo)
|
2025-09-20 23:54:27 +00:00
|
|
|
if (dragInfo && item) {
|
2025-09-21 17:03:50 +00:00
|
|
|
moveDroppedItems({
|
|
|
|
|
targetDirectory: item,
|
|
|
|
|
items: dragInfo.items,
|
2025-09-20 22:43:31 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
setIsDraggedOver(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleDragOver = (e: React.DragEvent) => {
|
|
|
|
|
const dragInfo = store.get(dragInfoAtom)
|
2025-09-20 23:54:27 +00:00
|
|
|
if (dragInfo && item) {
|
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
|
|
|
}
|