xonecas.io
8058
Ir

irvc / commit 9b469e1

Merge pull request #1 from sacenox/jm/dnd

drag & drop support for image uploads

xonecas2026-10-12 9b469e1c08c136f4e4bab8be2458b82f734680dc parent f1da788parent b79ad1f

1 file changed

web/src/components/ChatPane.tsx+26−0
@@ -151,6 +151,32 @@ export default function ChatPane({
151151 }
152152 }
153153
154+ // The whole window is a drop target for image files. Keep the latest attach()
155+ // in a ref so the listeners below are registered once.
156+ const attachRef = useRef(attach);
157+ attachRef.current = attach;
158+ const hasChannel = channel !== null;
159+ useEffect(() => {
160+ if (!hasChannel) return;
161+ const hasFiles = (e: DragEvent) => e.dataTransfer?.types.includes("Files") ?? false;
162+ // preventDefault on dragover is what makes the window a valid drop target;
163+ // on drop it stops the browser from navigating to the file.
164+ const onDragOver = (e: DragEvent) => {
165+ if (hasFiles(e)) e.preventDefault();
166+ };
167+ const onDrop = (e: DragEvent) => {
168+ if (!hasFiles(e)) return;
169+ e.preventDefault();
170+ attachRef.current([...(e.dataTransfer?.files ?? [])]);
171+ };
172+ window.addEventListener("dragover", onDragOver);
173+ window.addEventListener("drop", onDrop);
174+ return () => {
175+ window.removeEventListener("dragover", onDragOver);
176+ window.removeEventListener("drop", onDrop);
177+ };
178+ }, [hasChannel]);
179+
154180 function onPaste(e: ClipboardEvent<HTMLTextAreaElement>) {
155181 const images = [...e.clipboardData.files].filter((f) => f.type.startsWith("image/"));
156182 if (images.length === 0) return;