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-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: ({
|
|
|
|
|
items,
|
|
|
|
|
targetDirectory,
|
|
|
|
|
}: {
|
2025-09-21 17:03:50 +00:00
|
|
|
items: FileSystemHandle[]
|
2025-09-20 22:43:31 +00:00
|
|
|
targetDirectory: Doc<"directories">
|
|
|
|
|
}) => {
|
|
|
|
|
toast.success(
|
2025-09-21 17:03:50 +00:00
|
|
|
`${items.length} items moved to ${targetDirectory.name}`,
|
2025-09-20 22:43:31 +00:00
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const handleDrop = (_e: React.DragEvent) => {
|
|
|
|
|
const dragInfo = store.get(dragInfoAtom)
|
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
|
|
|
}
|