StreamOverlayPage.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { useEffect, useMemo, useState } from 'react';
  2. import { useParams, useSearchParams } from 'react-router-dom';
  3. import { useQuery, useQueryClient } from '@tanstack/react-query';
  4. import { useTranslation } from 'react-i18next';
  5. import { Layers, Clock, Timer, Printer } from 'lucide-react';
  6. import { api } from '../api/client';
  7. import type { PrinterStatus } from '../api/client';
  8. import { formatDuration, formatETA, type TimeFormat } from '../utils/date';
  9. type TFunction = (key: string, options?: Record<string, unknown>) => string;
  10. type OverlaySize = 'small' | 'medium' | 'large';
  11. interface OverlayConfig {
  12. size: OverlaySize;
  13. fps: number;
  14. showCamera: boolean;
  15. showProgress: boolean;
  16. showLayers: boolean;
  17. showEta: boolean;
  18. showFilename: boolean;
  19. showStatus: boolean;
  20. showPrinter: boolean;
  21. }
  22. function parseConfig(params: URLSearchParams): OverlayConfig {
  23. const show = params.get('show')?.split(',') || ['progress', 'layers', 'eta', 'filename', 'status'];
  24. // Parse FPS (default 15, max 30, min 1)
  25. const fpsParam = parseInt(params.get('fps') || '15', 10);
  26. const fps = Math.min(Math.max(isNaN(fpsParam) ? 15 : fpsParam, 1), 30);
  27. // Parse camera toggle (default true, set camera=false to hide)
  28. const cameraParam = params.get('camera');
  29. const showCamera = cameraParam !== 'false' && cameraParam !== '0';
  30. return {
  31. size: (params.get('size') as OverlaySize) || 'medium',
  32. fps,
  33. showCamera,
  34. showProgress: show.includes('progress'),
  35. showLayers: show.includes('layers'),
  36. showEta: show.includes('eta'),
  37. showFilename: show.includes('filename'),
  38. showStatus: show.includes('status'),
  39. showPrinter: show.includes('printer'),
  40. };
  41. }
  42. function getStatusText(status: PrinterStatus, t: TFunction): string {
  43. if (status.stg_cur_name) return status.stg_cur_name;
  44. switch (status.state) {
  45. case 'RUNNING': return t('streamOverlay.status.printing');
  46. case 'PAUSE': return t('streamOverlay.status.paused');
  47. case 'FINISH': return t('streamOverlay.status.finished');
  48. case 'FAILED': return t('streamOverlay.status.failed');
  49. case 'IDLE': return t('streamOverlay.status.idle');
  50. default: return status.state || t('streamOverlay.status.unknown');
  51. }
  52. }
  53. function getSizeClasses(size: OverlaySize) {
  54. switch (size) {
  55. case 'small':
  56. return {
  57. container: 'p-3',
  58. text: 'text-sm',
  59. textLarge: 'text-lg',
  60. progressHeight: 'h-2',
  61. icon: 'w-3 h-3',
  62. gap: 'gap-2',
  63. logoHeight: 'h-12',
  64. };
  65. case 'large':
  66. return {
  67. container: 'p-6',
  68. text: 'text-xl',
  69. textLarge: 'text-3xl',
  70. progressHeight: 'h-4',
  71. icon: 'w-6 h-6',
  72. gap: 'gap-4',
  73. logoHeight: 'h-24',
  74. };
  75. case 'medium':
  76. default:
  77. return {
  78. container: 'p-4',
  79. text: 'text-base',
  80. textLarge: 'text-xl',
  81. progressHeight: 'h-3',
  82. icon: 'w-4 h-4',
  83. gap: 'gap-3',
  84. logoHeight: 'h-16',
  85. };
  86. }
  87. }
  88. export function StreamOverlayPage() {
  89. const { printerId } = useParams<{ printerId: string }>();
  90. const [searchParams] = useSearchParams();
  91. const { t } = useTranslation();
  92. const queryClient = useQueryClient();
  93. const id = parseInt(printerId || '0', 10);
  94. const [imageKey, setImageKey] = useState(Date.now());
  95. const config = useMemo(() => parseConfig(searchParams), [searchParams]);
  96. const sizes = getSizeClasses(config.size);
  97. // Fetch printer info
  98. const { data: printer } = useQuery({
  99. queryKey: ['printer', id],
  100. queryFn: () => api.getPrinter(id),
  101. enabled: id > 0,
  102. });
  103. // Fetch printer status with polling
  104. const { data: status } = useQuery({
  105. queryKey: ['printerStatus', id],
  106. queryFn: () => api.getPrinterStatus(id),
  107. enabled: id > 0,
  108. refetchInterval: 2000,
  109. });
  110. // Fetch settings info
  111. const { data: settings } = useQuery({
  112. queryKey: ['settings'],
  113. queryFn: api.getSettings,
  114. });
  115. const timeFormat: TimeFormat = settings?.time_format || 'system';
  116. // WebSocket for real-time updates
  117. useEffect(() => {
  118. if (!id) return;
  119. const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
  120. const wsUrl = `${protocol}//${window.location.host}/api/v1/ws`;
  121. const ws = new WebSocket(wsUrl);
  122. ws.onmessage = (event) => {
  123. try {
  124. const data = JSON.parse(event.data);
  125. if (data.type === 'printer_status' && data.printer_id === id) {
  126. queryClient.setQueryData(['printerStatus', id], data.status);
  127. }
  128. } catch {
  129. // Ignore parse errors
  130. }
  131. };
  132. ws.onerror = () => {
  133. // WebSocket error - polling will continue as fallback
  134. };
  135. return () => {
  136. ws.close();
  137. };
  138. }, [id, queryClient]);
  139. // Update document title
  140. useEffect(() => {
  141. document.title = printer ? `${printer.name} - ${t('streamOverlay.title')}` : t('streamOverlay.title');
  142. return () => {
  143. document.title = 'Bambuddy';
  144. };
  145. }, [printer, t]);
  146. // Refresh stream on error
  147. const handleStreamError = () => {
  148. setTimeout(() => {
  149. setImageKey(Date.now());
  150. }, 3000);
  151. };
  152. if (!id) {
  153. return (
  154. <div className="min-h-screen bg-black flex items-center justify-center">
  155. <p className="text-white">{t('streamOverlay.invalidPrinterId')}</p>
  156. </div>
  157. );
  158. }
  159. if (!status) {
  160. return (
  161. <div className="min-h-screen bg-black flex items-center justify-center">
  162. <p className="text-gray-400">{t('common.loading')}</p>
  163. </div>
  164. );
  165. }
  166. const isPrinting = status.state === 'RUNNING' || status.state === 'PAUSE';
  167. const progress = status.progress || 0;
  168. const streamUrl = `/api/v1/printers/${id}/camera/stream?fps=${config.fps}&t=${imageKey}`;
  169. return (
  170. <div className="min-h-screen bg-black relative overflow-hidden">
  171. {/* Camera feed - fullscreen background (optional) */}
  172. {config.showCamera && (
  173. <img
  174. key={imageKey}
  175. src={streamUrl}
  176. alt={t('streamOverlay.cameraStream')}
  177. className="absolute inset-0 w-full h-full object-contain"
  178. onError={handleStreamError}
  179. />
  180. )}
  181. {/* Bambuddy logo - top right */}
  182. <a
  183. href="https://github.com/maziggy/bambuddy"
  184. target="_blank"
  185. rel="noopener noreferrer"
  186. className="absolute top-4 right-4 z-10"
  187. >
  188. <img
  189. src="/img/bambuddy_logo_dark_transparent.png"
  190. alt="Bambuddy"
  191. className={`${sizes.logoHeight} object-contain drop-shadow-lg hover:scale-105 transition-transform`}
  192. />
  193. </a>
  194. {/* Status overlay - bottom */}
  195. <div className="absolute bottom-0 left-0 right-0 z-10 bg-gradient-to-t from-black/80 via-black/60 to-transparent">
  196. <div className={`${sizes.container}`}>
  197. {/* Printer name */}
  198. {config.showPrinter && printer && (
  199. <div className={`flex items-center ${sizes.gap} mb-2`}>
  200. <Printer className={`${sizes.icon} text-white/70`} />
  201. <span className={`${sizes.text} text-white font-medium`}>{printer.name}</span>
  202. </div>
  203. )}
  204. {/* Filename */}
  205. {config.showFilename && status.current_print && (
  206. <div className={`${sizes.textLarge} text-white font-semibold mb-2 truncate drop-shadow-md`}>
  207. {status.current_print.replace(/\.gcode\.3mf$|\.3mf$|\.gcode$/i, '')}
  208. </div>
  209. )}
  210. {/* Status text */}
  211. {config.showStatus && (
  212. <div className={`${sizes.text} text-white/70 mb-2`}>
  213. {getStatusText(status, t)}
  214. </div>
  215. )}
  216. {/* Progress bar */}
  217. {config.showProgress && isPrinting && (
  218. <div className="mb-3">
  219. <div className={`flex items-center justify-between mb-1 ${sizes.text}`}>
  220. <span className="text-white/70">{t('streamOverlay.progress')}</span>
  221. <span className="text-white font-bold">{Math.round(progress)}%</span>
  222. </div>
  223. <div className={`w-full bg-white/20 rounded-full ${sizes.progressHeight}`}>
  224. <div
  225. className={`bg-bambu-green ${sizes.progressHeight} rounded-full transition-all duration-500`}
  226. style={{ width: `${progress}%` }}
  227. />
  228. </div>
  229. </div>
  230. )}
  231. {/* Stats row */}
  232. {isPrinting && (config.showLayers || config.showEta) && (
  233. <div className={`flex items-center ${sizes.gap} flex-wrap`}>
  234. {/* Layers */}
  235. {config.showLayers && status.layer_num != null && status.total_layers != null && status.total_layers > 0 && (
  236. <div className={`flex items-center ${sizes.gap} text-white/70`}>
  237. <Layers className={sizes.icon} />
  238. <span className={sizes.text}>
  239. <span className="text-white">{status.layer_num}</span>
  240. <span className="mx-1">/</span>
  241. <span>{status.total_layers}</span>
  242. </span>
  243. </div>
  244. )}
  245. {/* Remaining time */}
  246. {config.showEta && status.remaining_time != null && status.remaining_time > 0 && (
  247. <>
  248. <div className={`flex items-center ${sizes.gap} text-white/70`}>
  249. <Timer className={sizes.icon} />
  250. <span className={`${sizes.text} text-white`}>
  251. {formatDuration(status.remaining_time * 60)}
  252. </span>
  253. </div>
  254. <div className={`flex items-center ${sizes.gap} text-white/70`}>
  255. <Clock className={sizes.icon} />
  256. <span className={`${sizes.text} text-white`}>
  257. {t('streamOverlay.eta')} {formatETA(status.remaining_time, timeFormat, t)}
  258. </span>
  259. </div>
  260. </>
  261. )}
  262. </div>
  263. )}
  264. {/* Idle state */}
  265. {!isPrinting && (
  266. <div className={`${sizes.text} text-white/70 py-2`}>
  267. {status.connected ? t('streamOverlay.printerIdle') : t('streamOverlay.printerOffline')}
  268. </div>
  269. )}
  270. </div>
  271. </div>
  272. </div>
  273. );
  274. }