All checks were successful
CI / test (push) Successful in 19s
Web client: - Added React + TypeScript + Vite + Tailwind application in web/. - Implemented auth, chat list, chat messages, typing indicators, file uploads, and voice recording/playback. - Added typed API layer, Zustand stores, and realtime websocket hook integration. Containerization: - Added backend Dockerfile and project .dockerignore. - Added web multi-stage Dockerfile with nginx static hosting and API/WS reverse proxy. - Added full docker-compose stack with postgres, redis, minio, backend, worker, mailpit, and web. - Added MinIO bucket bootstrap init job and updated README with Docker quick-start.
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { useEffect, useMemo, useRef } from "react";
|
|
import { useAuthStore } from "../store/authStore";
|
|
import { useChatStore } from "../store/chatStore";
|
|
import type { Message } from "../chat/types";
|
|
import { buildWsUrl } from "../utils/ws";
|
|
|
|
interface RealtimeEnvelope {
|
|
event: string;
|
|
payload: Record<string, unknown>;
|
|
timestamp: string;
|
|
}
|
|
|
|
export function useRealtime() {
|
|
const accessToken = useAuthStore((s) => s.accessToken);
|
|
const me = useAuthStore((s) => s.me);
|
|
const prependMessage = useChatStore((s) => s.prependMessage);
|
|
const typingByChat = useRef<Record<number, Set<number>>>({});
|
|
|
|
const wsUrl = useMemo(() => {
|
|
return accessToken ? buildWsUrl(accessToken) : null;
|
|
}, [accessToken]);
|
|
|
|
useEffect(() => {
|
|
if (!wsUrl) {
|
|
return;
|
|
}
|
|
const ws = new WebSocket(wsUrl);
|
|
|
|
ws.onmessage = (messageEvent) => {
|
|
const event: RealtimeEnvelope = JSON.parse(messageEvent.data);
|
|
if (event.event === "receive_message") {
|
|
const chatId = Number(event.payload.chat_id);
|
|
const message = event.payload.message as Message;
|
|
prependMessage(chatId, message);
|
|
}
|
|
if (event.event === "typing_start") {
|
|
const chatId = Number(event.payload.chat_id);
|
|
const userId = Number(event.payload.user_id);
|
|
if (userId === me?.id) {
|
|
return;
|
|
}
|
|
if (!typingByChat.current[chatId]) {
|
|
typingByChat.current[chatId] = new Set<number>();
|
|
}
|
|
typingByChat.current[chatId].add(userId);
|
|
useChatStore.getState().setTypingUsers(chatId, [...typingByChat.current[chatId]]);
|
|
}
|
|
if (event.event === "typing_stop") {
|
|
const chatId = Number(event.payload.chat_id);
|
|
const userId = Number(event.payload.user_id);
|
|
typingByChat.current[chatId]?.delete(userId);
|
|
useChatStore.getState().setTypingUsers(chatId, [...(typingByChat.current[chatId] ?? [])]);
|
|
}
|
|
};
|
|
|
|
return () => ws.close();
|
|
}, [wsUrl, prependMessage, me?.id]);
|
|
|
|
return null;
|
|
}
|