open a draft chat and create it on the first message
"Написать" now looks up an existing chat for the ad (GET, never creates).
If there is none it opens /chats/new as a draft with the ad in the header;
the first send calls POST /chats/with/{userId}/messages, which creates the
chat with that message, and the page switches to /chats/{id}. Reloading
/chats/new without draft data goes back to the chat list.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect, useCallback, useRef } from "react";
|
||||
import { useParams, useNavigate } from "react-router-dom";
|
||||
import { useParams, useNavigate, useLocation } from "react-router-dom";
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useChats } from "../hooks/useChats";
|
||||
import { useChatSocket } from "../hooks/useChatSocket";
|
||||
@@ -18,7 +18,11 @@ export default function ChatPage() {
|
||||
const { t } = useTranslation();
|
||||
const { chatId } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const { fetchMessages, markAsRead } = useChats();
|
||||
const location = useLocation();
|
||||
// Черновик (/chats/new): переписки ещё нет, чат создастся с первым сообщением
|
||||
const draft = chatId ? null : location.state;
|
||||
const [startingChat, setStartingChat] = useState(false);
|
||||
const { fetchMessages, markAsRead, startChat } = useChats();
|
||||
const { clearChatUnread } = useUnread();
|
||||
const { user } = useAuthContext();
|
||||
const [messages, setMessages] = useState([]);
|
||||
@@ -43,6 +47,11 @@ export default function ChatPage() {
|
||||
|
||||
const { sendMessage, connected } = useChatSocket(chatId, handleIncoming);
|
||||
|
||||
// /chats/new без данных черновика (например, после перезагрузки) — открывать нечего
|
||||
useEffect(() => {
|
||||
if (!chatId && !draft) navigate("/chats", { replace: true });
|
||||
}, [chatId, draft, navigate]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!chatId) return;
|
||||
|
||||
@@ -80,15 +89,28 @@ export default function ChatPage() {
|
||||
const trimmed = input.trim();
|
||||
if (!trimmed) return;
|
||||
|
||||
if (draft) {
|
||||
if (startingChat) return;
|
||||
setStartingChat(true);
|
||||
startChat(draft.otherUserId, draft.postId, trimmed, draft.post)
|
||||
.then((chat) => {
|
||||
setInput("");
|
||||
navigate(`/chats/${chat.id}`, { replace: true });
|
||||
})
|
||||
.catch(() => setSendError(t('chats.sendError')))
|
||||
.finally(() => setStartingChat(false));
|
||||
return;
|
||||
}
|
||||
|
||||
const sent = sendMessage(trimmed);
|
||||
if (!sent) {
|
||||
setSendError(t('chats.sendError'));
|
||||
return;
|
||||
}
|
||||
setInput("");
|
||||
}, [input, sendMessage, t]);
|
||||
}, [input, sendMessage, t, draft, startingChat, startChat, navigate]);
|
||||
|
||||
const post = getPostForChat(chatId);
|
||||
const post = draft ? draft.post : getPostForChat(chatId);
|
||||
const postImg = getImageUrl(post?.images?.[0]);
|
||||
|
||||
return (
|
||||
@@ -119,7 +141,7 @@ export default function ChatPage() {
|
||||
input={input}
|
||||
onInputChange={setInput}
|
||||
onSend={handleSend}
|
||||
connected={connected}
|
||||
connected={draft ? !startingChat : connected}
|
||||
placeholder={t('chat.messagePlaceholder')}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user