Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions services/platform/app/hooks/use-page-file-drop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ describe('usePageFileDrop', () => {
expect(onFilesDropped.mock.calls[0][0]).toEqual([file]);
});

it('clears the overlay even when a region drop zone stops propagation', () => {
// Regression: dropping ONTO the chat composer (a FileUpload.DropZone that
// calls stopPropagation on drop) stopped the window bubble handler from
// running, so isDragOver never reset and the full-page overlay stuck until
// reload. The capture-phase reset must still clear it.
const onFilesDropped = vi.fn();
const { result } = renderHook(() => usePageFileDrop({ onFilesDropped }));

const region = document.createElement('div');
document.body.appendChild(region);
region.addEventListener('drop', (e) => e.stopPropagation());

act(() => {
fireEvent.dragEnter(window, { dataTransfer: fileTransfer([]) });
});
expect(result.current.isDragOver).toBe(true);

const file = new File(['hi'], 'note.txt', { type: 'text/plain' });
act(() => {
fireEvent.drop(region, { dataTransfer: fileTransfer([file]) });
});

// Overlay cleared despite the region zone swallowing the bubble...
expect(result.current.isDragOver).toBe(false);
// ...and the window handler did NOT also upload (the region zone owns it).
expect(onFilesDropped).not.toHaveBeenCalled();

document.body.removeChild(region);
});

it('ignores drags that carry no files (text/links/internal dnd)', () => {
const onFilesDropped = vi.fn();
const { result } = renderHook(() => usePageFileDrop({ onFilesDropped }));
Expand Down
20 changes: 17 additions & 3 deletions services/platform/app/hooks/use-page-file-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ interface UsePageFileDropOptions {
* cursor crosses child elements.
* - A region-scoped `FileUpload.DropZone` that calls `stopPropagation()` on a
* drop (e.g. the chat composer) still wins for drops landing on it; only
* drops elsewhere bubble to this window handler.
* drops elsewhere bubble to this window handler. The overlay reset runs in
* the CAPTURE phase so it still clears on those region-zone drops (a missing
* reset there left the full-page overlay stuck until reload).
* - Prevents the browser's default "navigate to the dropped file" while
* mounted, so a stray drop never blows away the app.
*/
Expand Down Expand Up @@ -71,23 +73,35 @@ export function usePageFileDrop(options: UsePageFileDropOptions): {
const onDrop = (e: DragEvent) => {
if (!carriesFiles(e)) return;
e.preventDefault();
dragDepth.current = 0;
setIsDragOver(false);
const all = Array.from(e.dataTransfer?.files ?? []);
const accept = acceptRef.current;
const files = accept ? all.filter(accept) : all;
if (files.length > 0) onFilesDroppedRef.current(files);
};
// Overlay reset runs in the CAPTURE phase: a drop ALWAYS ends the drag, so
// the overlay must clear even when a region-scoped DropZone (the chat
// composer) calls stopPropagation() on its drop — which would otherwise
// stop the bubble-phase `onDrop` above from ever running, leaving the
// overlay stuck until a page reload. Capture runs window->target, BEFORE
// the target's stopPropagation, so it always fires. Reset only here (no
// file handling) so a region-zone drop never double-uploads: the bubble
// `onDrop` still owns the upload for drops that DON'T hit a region zone.
const onDropResetCapture = () => {
dragDepth.current = 0;
setIsDragOver(false);
};

window.addEventListener('dragenter', onDragEnter);
window.addEventListener('dragover', onDragOver);
window.addEventListener('dragleave', onDragLeave);
window.addEventListener('drop', onDrop);
window.addEventListener('drop', onDropResetCapture, true);
return () => {
window.removeEventListener('dragenter', onDragEnter);
window.removeEventListener('dragover', onDragOver);
window.removeEventListener('dragleave', onDragLeave);
window.removeEventListener('drop', onDrop);
window.removeEventListener('drop', onDropResetCapture, true);
};
}, [disabled]);

Expand Down