Added screenshots

This commit is contained in:
Martin Ziegler
2025-11-28 10:23:59 +01:00
parent 8a85147713
commit 09677861ba
94 changed files with 25918 additions and 0 deletions
+119
View File
@@ -0,0 +1,119 @@
import { useEffect, useRef, useCallback, useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
interface WebSocketMessage {
type: string;
printer_id?: number;
data?: Record<string, unknown>;
}
export function useWebSocket() {
const wsRef = useRef<WebSocket | null>(null);
const reconnectTimeoutRef = useRef<number | null>(null);
const queryClient = useQueryClient();
const [isConnected, setIsConnected] = useState(false);
const connect = useCallback(() => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
return;
}
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${window.location.host}/api/v1/ws`;
const ws = new WebSocket(wsUrl);
ws.onopen = () => {
setIsConnected(true);
// Start ping interval
const pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: 'ping' }));
}
}, 30000);
ws.onclose = () => {
clearInterval(pingInterval);
};
};
ws.onmessage = (event) => {
try {
const message: WebSocketMessage = JSON.parse(event.data);
handleMessage(message);
} catch {
// Ignore parse errors
}
};
ws.onclose = () => {
setIsConnected(false);
wsRef.current = null;
// Reconnect after 3 seconds
reconnectTimeoutRef.current = window.setTimeout(() => {
connect();
}, 3000);
};
ws.onerror = () => {
ws.close();
};
wsRef.current = ws;
}, []);
const handleMessage = useCallback((message: WebSocketMessage) => {
switch (message.type) {
case 'printer_status':
// Update the printer status in the query cache
if (message.printer_id !== undefined) {
queryClient.setQueryData(
['printerStatus', message.printer_id],
(old: Record<string, unknown> | undefined) => ({
...old,
...message.data,
})
);
}
break;
case 'print_complete':
// Invalidate archives to refresh the list
queryClient.invalidateQueries({ queryKey: ['archives'] });
queryClient.invalidateQueries({ queryKey: ['archiveStats'] });
break;
case 'archive_created':
// Invalidate archives to show new archive
queryClient.invalidateQueries({ queryKey: ['archives'] });
queryClient.invalidateQueries({ queryKey: ['archiveStats'] });
break;
case 'pong':
// Keepalive response, ignore
break;
}
}, [queryClient]);
useEffect(() => {
connect();
return () => {
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current);
}
if (wsRef.current) {
wsRef.current.close();
}
};
}, [connect]);
const sendMessage = useCallback((message: Record<string, unknown>) => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
wsRef.current.send(JSON.stringify(message));
}
}, []);
return { isConnected, sendMessage };
}