added timezone and fix chat
This commit is contained in:
@@ -8,16 +8,21 @@ import { useUnread } from "../context/UnreadContext";
|
||||
export function useChats() {
|
||||
const { t } = useTranslation();
|
||||
const [chats, setChats] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
// Стартуем в состоянии загрузки: иначе самый первый рендер успевает показать
|
||||
// "Нет чатов" до того, как эффект выставит loading — список "моргает".
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
const { updateFromChats, clearChatUnread } = useUnread();
|
||||
// fetchChats дёргается и при монтировании, и на каждое изменение totalUnread —
|
||||
// без guard'а медленный предыдущий ответ воскрешал бы прочитанные счётчики.
|
||||
const requestIdRef = useRef(0);
|
||||
|
||||
const fetchChats = useCallback(async () => {
|
||||
// silent=true — фоновое обновление списка (новое сообщение по WS, удаление
|
||||
// чата и т.п.): не трогаем loading, иначе текущий список на миг подменяется
|
||||
// экраном загрузки и страница "моргает".
|
||||
const fetchChats = useCallback(async ({ silent = false } = {}) => {
|
||||
const requestId = ++requestIdRef.current;
|
||||
setLoading(true);
|
||||
if (!silent) setLoading(true);
|
||||
setError("");
|
||||
try {
|
||||
const res = await apiFetch(`${API_URL}/api/v1/chats`);
|
||||
@@ -33,7 +38,7 @@ export function useChats() {
|
||||
if (requestId !== requestIdRef.current) return;
|
||||
setError(e.message || t('chats.fetchError'));
|
||||
} finally {
|
||||
if (requestId === requestIdRef.current) setLoading(false);
|
||||
if (requestId === requestIdRef.current && !silent) setLoading(false);
|
||||
}
|
||||
}, [updateFromChats, t]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user