feat: migrate Leptos frontend to Next.js 16 (React 19)
Deploy to VPS / deploy (push) Failing after 42s
Deploy to VPS / deploy (push) Failing after 42s
Complete migration from services/frontend.old/ (Leptos 0.7 WASM + Rust) to services/frontend/ (Next.js 16 static export + TypeScript + Tailwind v4). Summary: - Port all shared types (message, guild, voice, media, dashboard, recording, ui) - Build fetch-based API client covering all 30+ backend endpoints - WebSocket client with auto-reconnect (exponential backoff, 20 attempts) - React context provider for WS with typed event subscription (22 event types) - Login page with localStorage auth + auto-redirect - Dashboard layout with sidebar, header (WS status + theme toggle) - Messages: feed, search, images tab, review tab, channel filter, detail modal - Live: voice connection, music player, recordings, mic transmit, active speakers - Dashboard: stats, user list, channel list, detail views - Mascot chatbot with history + clear - uiStateApi persistence for selected tab - Add static export config, update deploy scripts and CI
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import type { WsEvent, WsStatus } from "./types";
|
||||
|
||||
type WsEventCallback = (event: WsEvent) => void;
|
||||
|
||||
function getWsUrl(): string {
|
||||
if (typeof window === "undefined") return "ws://localhost:3001/ws";
|
||||
const protocol = window.location.protocol === "https:" ? "wss" : "ws";
|
||||
const host = window.location.host;
|
||||
return `${protocol}://${host}/ws`;
|
||||
}
|
||||
|
||||
export class WsConnection {
|
||||
private ws: WebSocket | null = null;
|
||||
private url: string;
|
||||
private reconnectAttempt = 0;
|
||||
private maxReconnectAttempts = 20;
|
||||
private reconnectTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
private _status: WsStatus = "disconnected";
|
||||
private statusListeners: Array<(status: WsStatus) => void> = [];
|
||||
private eventListeners: Array<WsEventCallback> = [];
|
||||
private destroyed = false;
|
||||
|
||||
constructor(url?: string) {
|
||||
this.url = url ?? getWsUrl();
|
||||
}
|
||||
|
||||
get status(): WsStatus {
|
||||
return this._status;
|
||||
}
|
||||
|
||||
onStatusChange(listener: (status: WsStatus) => void): () => void {
|
||||
this.statusListeners.push(listener);
|
||||
return () => {
|
||||
this.statusListeners = this.statusListeners.filter((l) => l !== listener);
|
||||
};
|
||||
}
|
||||
|
||||
onEvent(listener: WsEventCallback): () => void {
|
||||
this.eventListeners.push(listener);
|
||||
return () => {
|
||||
this.eventListeners = this.eventListeners.filter((l) => l !== listener);
|
||||
};
|
||||
}
|
||||
|
||||
connect(): void {
|
||||
if (this.destroyed) return;
|
||||
if (this._status === "connected" || this._status === "connecting") return;
|
||||
|
||||
this.setStatus("connecting");
|
||||
|
||||
try {
|
||||
this.ws = new WebSocket(this.url);
|
||||
} catch (_err) {
|
||||
this.setStatus("error");
|
||||
this.scheduleReconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
this.ws.onopen = () => {
|
||||
this.reconnectAttempt = 0;
|
||||
this.setStatus("connected");
|
||||
};
|
||||
|
||||
this.ws.onclose = () => {
|
||||
this.setStatus("disconnected");
|
||||
this.scheduleReconnect();
|
||||
};
|
||||
|
||||
this.ws.onerror = () => {
|
||||
this.setStatus("error");
|
||||
};
|
||||
|
||||
this.ws.onmessage = (msg: MessageEvent) => {
|
||||
if (typeof msg.data === "string") {
|
||||
this.dispatchEvent({ type: "text", data: msg.data });
|
||||
} else if (msg.data instanceof ArrayBuffer) {
|
||||
this.dispatchEvent({ type: "binary", data: msg.data });
|
||||
} else if (msg.data instanceof Blob) {
|
||||
// Blob — convert to ArrayBuffer
|
||||
msg.data.arrayBuffer().then((buffer) => {
|
||||
this.dispatchEvent({ type: "binary", data: buffer });
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
disconnect(): void {
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout);
|
||||
this.reconnectTimeout = null;
|
||||
}
|
||||
if (this.ws) {
|
||||
this.ws.onclose = null; // prevent reconnect
|
||||
this.ws.close();
|
||||
this.ws = null;
|
||||
}
|
||||
this.setStatus("disconnected");
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.destroyed = true;
|
||||
this.disconnect();
|
||||
this.statusListeners = [];
|
||||
this.eventListeners = [];
|
||||
}
|
||||
|
||||
sendText(text: string): void {
|
||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(text);
|
||||
}
|
||||
}
|
||||
|
||||
sendBinary(data: ArrayBufferLike): void {
|
||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(data);
|
||||
}
|
||||
}
|
||||
|
||||
private setStatus(status: WsStatus): void {
|
||||
if (this._status === status) return;
|
||||
this._status = status;
|
||||
this.statusListeners.forEach((l) => l(status));
|
||||
}
|
||||
|
||||
private dispatchEvent(event: WsEvent): void {
|
||||
this.eventListeners.forEach((l) => l(event));
|
||||
}
|
||||
|
||||
private scheduleReconnect(): void {
|
||||
if (this.destroyed || this.reconnectAttempt >= this.maxReconnectAttempts)
|
||||
return;
|
||||
|
||||
// Full-jitter exponential backoff: min(1000 * 2^attempt, 30000) * (0.5 + random * 0.5)
|
||||
const base = Math.min(1000 * 2 ** this.reconnectAttempt, 30000);
|
||||
const jitter = 0.5 + Math.random() * 0.5;
|
||||
const delay = Math.floor(base * jitter);
|
||||
|
||||
this.reconnectAttempt++;
|
||||
|
||||
this.reconnectTimeout = setTimeout(() => {
|
||||
this.connect();
|
||||
}, delay);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user