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

@@ -53,8 +53,12 @@ export function useImageUpload(existingUrls = []) {
/**
* Добавляет файлы к списку: проверка типа и размера, сжатие, превью.
* Общая точка для выбора через <input type="file"> и перетаскивания (drop).
*
* replace — заменить текущие фото вместо добавления (одиночный аватар).
* Старые фото снимаются только после успешной обработки новых: при ошибке
* пользователь остаётся с прежним фото.
*/
const addFiles = useCallback(async (fileList) => {
const addFiles = useCallback(async (fileList, { replace = false } = {}) => {
const files = Array.from(fileList || []);
if (!files.length) return;
setImgError("");
@@ -91,7 +95,13 @@ export function useImageUpload(existingUrls = []) {
isExisting: false,
serverUrl: null,
}));
setImages(prev => [...prev, ...newImages]);
setImages(prev => {
if (!replace) return [...prev, ...newImages];
prev.forEach(img => {
if (!img.isExisting && img.preview) URL.revokeObjectURL(img.preview);
});
return newImages;
});
} catch {
setImgError(t('imageUpload.processingFailed'));
} finally {