Desktop: drag and drop profile photo; share drop-zone hook with post form

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-26 23:27:40 +03:00
parent abd6f83535
commit 16b886081b
9 changed files with 209 additions and 45 deletions

View File

@@ -1,4 +1,4 @@
import { useCallback, useRef, useState } from "react";
import { useCallback, useState } from "react";
import CloseIcon from "../components/icons/CloseIcon";
import RulesModal from "./RulesModal";
import { EMPTY_FORM, CATEGORIES } from "../config/feedConstants";
@@ -11,6 +11,7 @@ import MapModal from "./MapModal";
import ClearableField from "../components/ClearableField";
import AddressAutocomplete from "../components/AddressAutocomplete";
import { useIsDesktop } from "../hooks/useIsDesktop";
import { useFileDropZone } from "../hooks/useFileDropZone";
import { useAuthContext } from "../context/AuthContext";
// Ограничения полей бэкенда (PostFieldLimits): длиннее — 400
@@ -71,46 +72,12 @@ export default function CreateUpdatePost({ onClose, onSuccess, initialData, admi
getExistingUrlsToKeep,
} = useImageUpload(isEdit ? initialData.images : []);
// Перетаскивание фото (drag & drop) — только на десктопе.
// dragenter/dragleave приходят и от дочерних элементов зоны, поэтому
// считаем глубину, иначе подсветка мигает при движении над превью.
const [dragOver, setDragOver] = useState(false);
const dragDepthRef = useRef(0);
const isFileDrag = e => Array.from(e.dataTransfer?.types || []).includes("Files");
const dropZoneHandlers = isDesktop ? {
onDragEnter: e => {
if (!isFileDrag(e)) return;
e.preventDefault();
dragDepthRef.current += 1;
setDragOver(true);
},
onDragOver: e => {
if (!isFileDrag(e)) return;
e.preventDefault();
e.dataTransfer.dropEffect = compressing ? "none" : "copy";
},
onDragLeave: e => {
if (!isFileDrag(e)) return;
dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
if (dragDepthRef.current === 0) setDragOver(false);
},
onDrop: e => {
if (!isFileDrag(e)) return;
e.preventDefault();
e.stopPropagation();
dragDepthRef.current = 0;
setDragOver(false);
if (!compressing) addFiles(e.dataTransfer.files);
},
} : {};
// Файл, отпущенный мимо зоны, браузер открыл бы вместо сайта — гасим это
// на всём окне формы, иначе введённые данные пропадут.
const blockStrayDrop = isDesktop ? {
onDragOver: e => { if (isFileDrag(e)) e.preventDefault(); },
onDrop: e => { if (isFileDrag(e)) e.preventDefault(); },
} : {};
// Перетаскивание фото (drag & drop) — только на десктопе
const { dragOver, zoneProps: dropZoneHandlers, strayDropProps: blockStrayDrop } = useFileDropZone({
enabled: isDesktop,
busy: compressing,
onFiles: addFiles,
});
const { save, loading: saving, error: apiError } = usePostMutation({ adminMode });