Send and view photos in chats

- paperclip button and clipboard paste in the chat input, previews with
  remove, up to 10 photos per message (sent via REST multipart)
- photos in message bubbles with reserved size, fullscreen viewer
  (arrows, swipe, Esc), fresh link when a presigned URL expires
- chat list preview shows "Photo" for photo-only messages
- keep the feed pinned to the bottom when the preview strip appears

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SlimusMinus
2026-10-28 22:26:44 +03:00
parent 0380bc8985
commit b7b30575a2
12 changed files with 537 additions and 11 deletions

View File

@@ -98,7 +98,39 @@ export function useChats() {
}
}, [clearChatUnread]);
return { chats, loading, error, fetchChats, findChat, startChat, fetchMessages, markAsRead };
/**
* Сообщение с фото (и необязательным текстом) — через REST: файлы по
* WebSocket не передать. Возвращает сохранённое сообщение; собеседнику
* (и нам же) оно придёт и по сокету — дубль отсекается по id.
*/
const sendPhotoMessage = useCallback(async (chatId, content, files) => {
const data = new FormData();
if (content) data.append("content", content);
files.forEach((file) => data.append("files", file, file.name || "photo.jpg"));
const res = await apiFetch(`${API_URL}/api/v1/chats/${chatId}/messages`, {
method: "POST",
body: data,
});
if (!res.ok) {
const err = await readApiError(res);
console.warn("sendPhotoMessage failed:", err.status, err.serverMessage);
throw err;
}
return res.json();
}, []);
/** Свежая ссылка на фото, если выданная вместе с сообщением истекла. */
const refreshAttachmentUrl = useCallback(async (chatId, attachmentId) => {
const res = await apiFetch(`${API_URL}/api/v1/chats/${chatId}/attachments/${attachmentId}/url`);
if (!res.ok) throw new Error(`Error ${res.status}`);
const { url } = await res.json();
return url;
}, []);
return {
chats, loading, error, fetchChats, findChat, startChat, fetchMessages, markAsRead,
sendPhotoMessage, refreshAttachmentUrl,
};
}
function chatWithUrl(otherUserId, postId, suffix = "") {

View File

@@ -122,6 +122,17 @@ export function useImageUpload(existingUrls = []) {
});
}, []);
/** Убирает все выбранные фото (например, после отправки сообщения). */
const clearImages = useCallback(() => {
setImages(prev => {
prev.forEach(img => {
if (!img.isExisting && img.preview) URL.revokeObjectURL(img.preview);
});
return [];
});
setImgError("");
}, []);
const getFilesForUpload = useCallback(() => {
return images.filter(i => !i.isExisting).map(i => i.file);
}, [images]);
@@ -156,6 +167,7 @@ export function useImageUpload(existingUrls = []) {
handleFileChange,
addFiles,
removeImage,
clearImages,
getFilesForUpload,
getExistingUrlsToKeep,
};