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:
@@ -18,7 +18,7 @@ export function useContactSeller(post) {
|
||||
const { t } = useTranslation();
|
||||
const [error, setError] = useState("");
|
||||
const navigate = useNavigate();
|
||||
const { createOrGetChat } = useChats();
|
||||
const { findChat } = useChats();
|
||||
|
||||
/**
|
||||
* @param {function} [onBeforeNavigate] — вызывается перед переходом в чат
|
||||
@@ -27,19 +27,26 @@ export function useContactSeller(post) {
|
||||
const contactSeller = useCallback(async (onBeforeNavigate) => {
|
||||
if (!post) return;
|
||||
try {
|
||||
const chat = await createOrGetChat(post.userId, post.id, {
|
||||
const postInfo = {
|
||||
id: post.id,
|
||||
title: post.title,
|
||||
images: post.images,
|
||||
emoji: post.emoji,
|
||||
});
|
||||
};
|
||||
// Чат не создаём заранее: если переписки ещё нет, открываем черновик,
|
||||
// а чат появится на бэкенде вместе с первым сообщением
|
||||
const chat = await findChat(post.userId, post.id, postInfo);
|
||||
onBeforeNavigate?.();
|
||||
navigate(`/chats/${chat.id}`);
|
||||
if (chat) {
|
||||
navigate(`/chats/${chat.id}`);
|
||||
} else {
|
||||
navigate("/chats/new", { state: { otherUserId: post.userId, postId: post.id, post: postInfo } });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
setError(t('postCard.contactError'));
|
||||
}
|
||||
}, [post, createOrGetChat, navigate, t]);
|
||||
}, [post, findChat, navigate, t]);
|
||||
|
||||
const clearError = useCallback(() => setError(""), []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user