AI Newsletter Digest improvements: fixed QP soft line break decoding, URL extraction, and content cleaning

This commit is contained in:
Krilly
2026-03-04 13:29:22 +00:00
parent 29a98137a7
commit 57dd294675
13706 changed files with 2114953 additions and 237629 deletions
@@ -0,0 +1,44 @@
import { useEffect, useRef, useCallback } from "react";
type MessageHandler = (data: { type: string; event: string; path: string }) => void;
/**
* Auto-reconnecting WebSocket hook for live file-change notifications.
* Supports dynamic URL for remote connections.
*/
export function useWebSocket(onMessage: MessageHandler, remoteBaseUrl = "") {
const wsRef = useRef<WebSocket | null>(null);
const cbRef = useRef(onMessage);
cbRef.current = onMessage;
const connect = useCallback(() => {
let wsUrl: string;
if (remoteBaseUrl) {
// Convert http(s)://host:port to ws(s)://host:port/ws
const url = new URL(remoteBaseUrl);
const proto = url.protocol === "https:" ? "wss:" : "ws:";
wsUrl = `${proto}//${url.host}/ws`;
} else {
const proto = location.protocol === "https:" ? "wss:" : "ws:";
wsUrl = `${proto}//${location.host}/ws`;
}
const ws = new WebSocket(wsUrl);
ws.onmessage = (e) => {
try {
cbRef.current(JSON.parse(e.data));
} catch { /* ignore bad json */ }
};
ws.onclose = () => {
setTimeout(connect, 3000);
};
wsRef.current = ws;
}, [remoteBaseUrl]);
useEffect(() => {
connect();
return () => wsRef.current?.close();
}, [connect]);
}