Support new backend contracts: single-flight refresh, ad impression tokens, error handling
- apiFetch: proactive refresh, cross-tab single-flight via Web Locks, no logout on transient errors - AuthContext: save tokens returned on own login change, revoke refresh on logout, sync tabs - Ads: X-Session-Id, impressionToken in impression/click, IAB viewability, safe http(s) targetUrl - STOMP: refresh before CONNECT, stop reconnect loop on dead session, /user/queue/errors behind flag - Auth form: password 8 chars / 72 bytes; 429/413 messages via describeApiError - My posts with auth + moderation badges; post form step validation and field limits - Map: escape user text in Yandex hint/template Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,18 +1,33 @@
|
||||
// src/hooks/useChatSocket.js
|
||||
import { useEffect, useRef, useCallback } from "react";
|
||||
import { useStompClient, parseStompMessage } from "./useStompClient";
|
||||
import { WS_ERROR_QUEUE_ENABLED } from "../config/authConstants";
|
||||
|
||||
export function useChatSocket(chatId, onMessage) {
|
||||
/**
|
||||
* @param {string|number} chatId
|
||||
* @param {(message: object) => void} onMessage — новое сообщение чата
|
||||
* @param {(text: string) => void} [onError] — сервер отклонил отправку
|
||||
* (приходит в /user/queue/errors; подписка на /user/{id}/queue/... ничего
|
||||
* не получает)
|
||||
*/
|
||||
export function useChatSocket(chatId, onMessage, onError) {
|
||||
const onMessageRef = useRef(onMessage);
|
||||
const onErrorRef = useRef(onError);
|
||||
|
||||
useEffect(() => {
|
||||
onMessageRef.current = onMessage;
|
||||
}, [onMessage]);
|
||||
onErrorRef.current = onError;
|
||||
}, [onMessage, onError]);
|
||||
|
||||
const handleConnect = useCallback((client) => {
|
||||
client.subscribe(`/topic/chat/${chatId}`, (msg) => {
|
||||
parseStompMessage(msg, (body) => onMessageRef.current(body));
|
||||
});
|
||||
if (WS_ERROR_QUEUE_ENABLED) {
|
||||
client.subscribe("/user/queue/errors", (msg) => {
|
||||
onErrorRef.current?.(errorText(msg.body));
|
||||
});
|
||||
}
|
||||
}, [chatId]);
|
||||
|
||||
const { clientRef, connected } = useStompClient(!!chatId, handleConnect);
|
||||
@@ -28,4 +43,20 @@ export function useChatSocket(chatId, onMessage) {
|
||||
}, [chatId, clientRef]);
|
||||
|
||||
return { connected, sendMessage };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Бэкенд шлёт строку через JSON-конвертер ("\"Сообщение пустое\""), но на
|
||||
* всякий случай принимаем и голый текст, и объект {message}.
|
||||
*/
|
||||
function errorText(body) {
|
||||
if (typeof body !== "string" || !body) return "";
|
||||
try {
|
||||
const parsed = JSON.parse(body);
|
||||
if (typeof parsed === "string") return parsed;
|
||||
if (parsed && typeof parsed.message === "string") return parsed.message;
|
||||
} catch {
|
||||
// не JSON — это и есть текст
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user