"use client" import { useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react" import { createPortal } from "react-dom" import { Mail } from "lucide-react" import { EMAIL_DRAG_RETURN_MS, useEmailDrag } from "@/lib/drag-context" import { getDragPointerSnapshot, subscribeDragPointer, } from "@/lib/drag-pointer-store" import { useIsXs } from "@/hooks/use-xs" const DRAG_POINTER_SERVER_SNAPSHOT = { x: 0, y: 0 } as const export function MoveDragIndicator() { const isXs = useIsXs() const { state } = useEmailDrag() const [mounted, setMounted] = useState(false) const elRef = useRef(null) const livePointer = useSyncExternalStore( subscribeDragPointer, getDragPointerSnapshot, () => DRAG_POINTER_SERVER_SNAPSHOT ) useEffect(() => { setMounted(true) }, []) useLayoutEffect(() => { if (!state) return if (state.phase !== "returning") return const el = elRef.current if (!el) return const dx = state.originX - state.pointerX const dy = state.originY - state.pointerY const animation = el.animate( [ { translate: "0px 0px", opacity: 1 }, { translate: `${dx}px ${dy}px`, opacity: 0 }, ], { duration: EMAIL_DRAG_RETURN_MS, easing: "cubic-bezier(0.22, 0.61, 0.36, 1)", fill: "forwards", } ) return () => { animation.cancel() } }, [state?.phase, state?.originX, state?.originY, state?.pointerX, state?.pointerY]) if (isXs || !mounted || !state) return null const count = state.ids.length const label = count > 1 ? `Déplacer ${count} conversations` : "Déplacer 1 conversation" const x = state.phase === "returning" ? state.pointerX : livePointer.x const y = state.phase === "returning" ? state.pointerY : livePointer.y return createPortal(
{label}
, document.body ) }