CameraPage.tsx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. import { useState, useEffect, useRef, useCallback } from 'react';
  2. import { useParams } from 'react-router-dom';
  3. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  4. import { useTranslation } from 'react-i18next';
  5. import { RefreshCw, AlertTriangle, Camera, Maximize, Minimize, WifiOff, ZoomIn, ZoomOut } from 'lucide-react';
  6. import { api, getAuthToken } from '../api/client';
  7. import { useToast } from '../contexts/ToastContext';
  8. import { useAuth } from '../contexts/AuthContext';
  9. import { ChamberLight } from '../components/icons/ChamberLight';
  10. import { SkipObjectsModal, SkipObjectsIcon } from '../components/SkipObjectsModal';
  11. const MAX_RECONNECT_ATTEMPTS = 5;
  12. const INITIAL_RECONNECT_DELAY = 2000; // 2 seconds
  13. const MAX_RECONNECT_DELAY = 30000; // 30 seconds
  14. const STALL_CHECK_INTERVAL = 5000; // Check every 5 seconds
  15. export function CameraPage() {
  16. const { t } = useTranslation();
  17. const queryClient = useQueryClient();
  18. const { showToast } = useToast();
  19. const { hasPermission } = useAuth();
  20. const { printerId } = useParams<{ printerId: string }>();
  21. const id = parseInt(printerId || '0', 10);
  22. const [streamMode, setStreamMode] = useState<'stream' | 'snapshot'>('stream');
  23. const [showSkipObjectsModal, setShowSkipObjectsModal] = useState(false);
  24. const [streamError, setStreamError] = useState(false);
  25. const [streamLoading, setStreamLoading] = useState(true);
  26. const [imageKey, setImageKey] = useState(Date.now());
  27. const [transitioning, setTransitioning] = useState(false);
  28. const [isFullscreen, setIsFullscreen] = useState(false);
  29. const [reconnectAttempts, setReconnectAttempts] = useState(0);
  30. const [isReconnecting, setIsReconnecting] = useState(false);
  31. const [reconnectCountdown, setReconnectCountdown] = useState(0);
  32. const [zoomLevel, setZoomLevel] = useState(1);
  33. const [panOffset, setPanOffset] = useState({ x: 0, y: 0 });
  34. const [isPanning, setIsPanning] = useState(false);
  35. const [panStart, setPanStart] = useState({ x: 0, y: 0 });
  36. const [lastTouchDistance, setLastTouchDistance] = useState<number | null>(null);
  37. const [lastTouchCenter, setLastTouchCenter] = useState<{ x: number; y: number } | null>(null);
  38. const imgRef = useRef<HTMLImageElement>(null);
  39. const containerRef = useRef<HTMLDivElement>(null);
  40. const reconnectTimerRef = useRef<NodeJS.Timeout | null>(null);
  41. const countdownIntervalRef = useRef<NodeJS.Timeout | null>(null);
  42. const stallCheckIntervalRef = useRef<NodeJS.Timeout | null>(null);
  43. // Fetch printer info for the title
  44. const { data: printer } = useQuery({
  45. queryKey: ['printer', id],
  46. queryFn: () => api.getPrinter(id),
  47. enabled: id > 0,
  48. });
  49. // Fetch printer status for light toggle and skip objects
  50. const { data: status } = useQuery({
  51. queryKey: ['printerStatus', id],
  52. queryFn: () => api.getPrinterStatus(id),
  53. refetchInterval: 30000,
  54. enabled: id > 0,
  55. });
  56. // Chamber light mutation with optimistic update
  57. const chamberLightMutation = useMutation({
  58. mutationFn: (on: boolean) => api.setChamberLight(id, on),
  59. onMutate: async (on) => {
  60. await queryClient.cancelQueries({ queryKey: ['printerStatus', id] });
  61. const previousStatus = queryClient.getQueryData(['printerStatus', id]);
  62. queryClient.setQueryData(['printerStatus', id], (old: typeof status) => ({
  63. ...old,
  64. chamber_light: on,
  65. }));
  66. return { previousStatus };
  67. },
  68. onSuccess: (_, on) => {
  69. showToast(`Chamber light ${on ? 'on' : 'off'}`);
  70. },
  71. onError: (error: Error, _, context) => {
  72. if (context?.previousStatus) {
  73. queryClient.setQueryData(['printerStatus', id], context.previousStatus);
  74. }
  75. showToast(error.message || t('printers.toast.failedToControlChamberLight'), 'error');
  76. },
  77. });
  78. const isPrintingWithObjects = (status?.state === 'RUNNING' || status?.state === 'PAUSE') && (status?.printable_objects_count ?? 0) >= 2;
  79. // Update document title
  80. useEffect(() => {
  81. if (printer) {
  82. document.title = `${printer.name} - Camera`;
  83. }
  84. return () => {
  85. document.title = 'Bambuddy';
  86. };
  87. }, [printer]);
  88. // Cleanup on unmount - stop the camera stream
  89. // Track if we've already sent the stop signal to avoid duplicate calls
  90. const stopSentRef = useRef(false);
  91. useEffect(() => {
  92. const stopUrl = `/api/v1/printers/${id}/camera/stop`;
  93. stopSentRef.current = false;
  94. const sendStopOnce = () => {
  95. if (id > 0 && !stopSentRef.current) {
  96. stopSentRef.current = true;
  97. const headers: Record<string, string> = {};
  98. const token = getAuthToken();
  99. if (token) headers['Authorization'] = `Bearer ${token}`;
  100. fetch(stopUrl, { method: 'POST', keepalive: true, headers }).catch(() => {});
  101. }
  102. };
  103. // Handle page unload/close with keepalive fetch (more reliable than sendBeacon, supports auth)
  104. const handleBeforeUnload = () => {
  105. sendStopOnce();
  106. };
  107. window.addEventListener('beforeunload', handleBeforeUnload);
  108. // Store ref value for cleanup - ref may change by cleanup time
  109. const imgElement = imgRef.current;
  110. return () => {
  111. window.removeEventListener('beforeunload', handleBeforeUnload);
  112. // Clear the image source first to stop the stream
  113. if (imgElement) {
  114. imgElement.src = '';
  115. }
  116. // Send stop signal only once
  117. sendStopOnce();
  118. };
  119. }, [id]);
  120. // Auto-hide loading after timeout
  121. useEffect(() => {
  122. if (streamLoading && !transitioning) {
  123. const timeout = streamMode === 'stream' ? 3000 : 20000;
  124. const timer = setTimeout(() => {
  125. setStreamLoading(false);
  126. }, timeout);
  127. return () => clearTimeout(timer);
  128. }
  129. }, [streamMode, streamLoading, imageKey, transitioning]);
  130. // Fullscreen change listener - refresh stream after fullscreen transition
  131. useEffect(() => {
  132. const handleFullscreenChange = () => {
  133. const nowFullscreen = !!document.fullscreenElement;
  134. setIsFullscreen(nowFullscreen);
  135. // Reset zoom on fullscreen transition
  136. setZoomLevel(1);
  137. setPanOffset({ x: 0, y: 0 });
  138. // Refresh stream after fullscreen transition to prevent stall
  139. if (streamMode === 'stream' && !transitioning) {
  140. // Clear image src first, then set new key after delay
  141. if (imgRef.current) {
  142. imgRef.current.src = '';
  143. }
  144. setTimeout(() => {
  145. setStreamLoading(true);
  146. setImageKey(Date.now());
  147. }, 200);
  148. }
  149. };
  150. document.addEventListener('fullscreenchange', handleFullscreenChange);
  151. return () => document.removeEventListener('fullscreenchange', handleFullscreenChange);
  152. }, [streamMode, transitioning]);
  153. // Save window size and position when user resizes or moves
  154. // Works for both popup windows and standalone camera pages
  155. useEffect(() => {
  156. let saveTimeout: NodeJS.Timeout;
  157. const saveWindowState = () => {
  158. // Debounce to avoid saving during drag
  159. clearTimeout(saveTimeout);
  160. saveTimeout = setTimeout(() => {
  161. localStorage.setItem('cameraWindowState', JSON.stringify({
  162. width: window.outerWidth,
  163. height: window.outerHeight,
  164. left: window.screenX,
  165. top: window.screenY,
  166. }));
  167. }, 500);
  168. };
  169. window.addEventListener('resize', saveWindowState);
  170. return () => {
  171. clearTimeout(saveTimeout);
  172. window.removeEventListener('resize', saveWindowState);
  173. };
  174. }, []);
  175. // Clean up reconnect timers on unmount
  176. useEffect(() => {
  177. return () => {
  178. if (reconnectTimerRef.current) {
  179. clearTimeout(reconnectTimerRef.current);
  180. }
  181. if (countdownIntervalRef.current) {
  182. clearInterval(countdownIntervalRef.current);
  183. }
  184. if (stallCheckIntervalRef.current) {
  185. clearInterval(stallCheckIntervalRef.current);
  186. }
  187. };
  188. }, []);
  189. // Auto-reconnect logic
  190. const attemptReconnect = useCallback(() => {
  191. if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
  192. setIsReconnecting(false);
  193. setStreamError(true);
  194. return;
  195. }
  196. // Calculate delay with exponential backoff
  197. const delay = Math.min(
  198. INITIAL_RECONNECT_DELAY * Math.pow(2, reconnectAttempts),
  199. MAX_RECONNECT_DELAY
  200. );
  201. setIsReconnecting(true);
  202. setReconnectCountdown(Math.ceil(delay / 1000));
  203. // Countdown timer
  204. countdownIntervalRef.current = setInterval(() => {
  205. setReconnectCountdown((prev) => {
  206. if (prev <= 1) {
  207. if (countdownIntervalRef.current) {
  208. clearInterval(countdownIntervalRef.current);
  209. }
  210. return 0;
  211. }
  212. return prev - 1;
  213. });
  214. }, 1000);
  215. // Reconnect after delay
  216. reconnectTimerRef.current = setTimeout(() => {
  217. setReconnectAttempts((prev) => prev + 1);
  218. setIsReconnecting(false);
  219. setStreamLoading(true);
  220. setStreamError(false);
  221. if (imgRef.current) {
  222. imgRef.current.src = '';
  223. }
  224. setImageKey(Date.now());
  225. }, delay);
  226. }, [reconnectAttempts]);
  227. // Stall detection - periodically check if stream is still receiving frames
  228. useEffect(() => {
  229. // Only skip stall check during initial load, reconnecting, or transitioning
  230. // Continue checking even during streamError to detect recovery
  231. if (streamMode !== 'stream' || streamLoading || isReconnecting || transitioning) {
  232. if (stallCheckIntervalRef.current) {
  233. clearInterval(stallCheckIntervalRef.current);
  234. stallCheckIntervalRef.current = null;
  235. }
  236. return;
  237. }
  238. // Start stall detection after stream has loaded
  239. stallCheckIntervalRef.current = setInterval(async () => {
  240. try {
  241. const status = await api.getCameraStatus(id);
  242. // Trigger reconnect if:
  243. // 1. Backend reports stall (no frames for 10+ seconds)
  244. // 2. OR stream is not active anymore (process died)
  245. if (status.stalled || (!status.active && !streamError)) {
  246. console.log(`Stream issue detected: stalled=${status.stalled}, active=${status.active}, reconnecting...`);
  247. if (stallCheckIntervalRef.current) {
  248. clearInterval(stallCheckIntervalRef.current);
  249. stallCheckIntervalRef.current = null;
  250. }
  251. setStreamLoading(false);
  252. attemptReconnect();
  253. }
  254. } catch {
  255. // Ignore fetch errors - server might be temporarily unavailable
  256. }
  257. }, STALL_CHECK_INTERVAL);
  258. return () => {
  259. if (stallCheckIntervalRef.current) {
  260. clearInterval(stallCheckIntervalRef.current);
  261. stallCheckIntervalRef.current = null;
  262. }
  263. };
  264. }, [streamMode, streamLoading, streamError, isReconnecting, transitioning, id, attemptReconnect]);
  265. const handleStreamError = () => {
  266. setStreamLoading(false);
  267. // Only auto-reconnect for live stream mode
  268. if (streamMode === 'stream' && reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
  269. attemptReconnect();
  270. } else {
  271. setStreamError(true);
  272. }
  273. };
  274. const handleStreamLoad = () => {
  275. setStreamLoading(false);
  276. setStreamError(false);
  277. // Reset reconnect attempts on successful connection
  278. setReconnectAttempts(0);
  279. setIsReconnecting(false);
  280. if (reconnectTimerRef.current) {
  281. clearTimeout(reconnectTimerRef.current);
  282. }
  283. if (countdownIntervalRef.current) {
  284. clearInterval(countdownIntervalRef.current);
  285. }
  286. // Auto-resize window to fit video content (only if no saved preference)
  287. if (imgRef.current && !localStorage.getItem('cameraWindowState')) {
  288. const img = imgRef.current;
  289. const videoWidth = img.naturalWidth;
  290. const videoHeight = img.naturalHeight;
  291. if (videoWidth > 0 && videoHeight > 0) {
  292. // Add space for header bar (~45px) and some padding
  293. const headerHeight = 45;
  294. const padding = 16;
  295. // Calculate window size (outer size includes chrome)
  296. const chromeWidth = window.outerWidth - window.innerWidth;
  297. const chromeHeight = window.outerHeight - window.innerHeight;
  298. const targetWidth = videoWidth + padding + chromeWidth;
  299. const targetHeight = videoHeight + headerHeight + padding + chromeHeight;
  300. try {
  301. window.resizeTo(targetWidth, targetHeight);
  302. } catch {
  303. // resizeTo may not be allowed in all contexts
  304. }
  305. }
  306. }
  307. };
  308. const stopStream = () => {
  309. if (id > 0) {
  310. const headers: Record<string, string> = {};
  311. const token = getAuthToken();
  312. if (token) headers['Authorization'] = `Bearer ${token}`;
  313. fetch(`/api/v1/printers/${id}/camera/stop`, { method: 'POST', headers }).catch(() => {});
  314. }
  315. };
  316. const switchToMode = (newMode: 'stream' | 'snapshot') => {
  317. if (streamMode === newMode || transitioning) return;
  318. setTransitioning(true);
  319. setStreamLoading(true);
  320. setStreamError(false);
  321. // Reset reconnect state on mode switch
  322. setReconnectAttempts(0);
  323. setIsReconnecting(false);
  324. // Reset zoom on mode switch
  325. setZoomLevel(1);
  326. setPanOffset({ x: 0, y: 0 });
  327. if (reconnectTimerRef.current) {
  328. clearTimeout(reconnectTimerRef.current);
  329. }
  330. if (countdownIntervalRef.current) {
  331. clearInterval(countdownIntervalRef.current);
  332. }
  333. if (imgRef.current) {
  334. imgRef.current.src = '';
  335. }
  336. // Stop any active streams when switching modes
  337. if (streamMode === 'stream') {
  338. stopStream();
  339. }
  340. setTimeout(() => {
  341. setStreamMode(newMode);
  342. setImageKey(Date.now());
  343. setTransitioning(false);
  344. }, 100);
  345. };
  346. const refresh = () => {
  347. if (transitioning) return;
  348. setTransitioning(true);
  349. setStreamLoading(true);
  350. setStreamError(false);
  351. // Reset reconnect state on manual refresh
  352. setReconnectAttempts(0);
  353. setIsReconnecting(false);
  354. if (reconnectTimerRef.current) {
  355. clearTimeout(reconnectTimerRef.current);
  356. }
  357. if (countdownIntervalRef.current) {
  358. clearInterval(countdownIntervalRef.current);
  359. }
  360. if (imgRef.current) {
  361. imgRef.current.src = '';
  362. }
  363. // Stop any active streams before refresh
  364. if (streamMode === 'stream') {
  365. stopStream();
  366. }
  367. setTimeout(() => {
  368. setImageKey(Date.now());
  369. setTransitioning(false);
  370. }, 100);
  371. };
  372. const toggleFullscreen = () => {
  373. if (!containerRef.current) return;
  374. if (document.fullscreenElement) {
  375. document.exitFullscreen();
  376. } else {
  377. containerRef.current.requestFullscreen();
  378. }
  379. };
  380. const handleZoomIn = () => {
  381. setZoomLevel(prev => Math.min(prev + 0.5, 4));
  382. };
  383. const handleZoomOut = () => {
  384. setZoomLevel(prev => {
  385. const newZoom = Math.max(prev - 0.5, 1);
  386. if (newZoom === 1) setPanOffset({ x: 0, y: 0 });
  387. return newZoom;
  388. });
  389. };
  390. const handleWheel = (e: React.WheelEvent) => {
  391. e.preventDefault();
  392. if (e.deltaY < 0) {
  393. handleZoomIn();
  394. } else {
  395. handleZoomOut();
  396. }
  397. };
  398. const handleImageMouseDown = (e: React.MouseEvent) => {
  399. if (zoomLevel > 1) {
  400. e.preventDefault();
  401. setIsPanning(true);
  402. setPanStart({ x: e.clientX - panOffset.x, y: e.clientY - panOffset.y });
  403. }
  404. };
  405. // Calculate max pan based on container size and zoom level
  406. const getMaxPan = useCallback(() => {
  407. if (!containerRef.current) {
  408. return { x: 300, y: 200 };
  409. }
  410. const container = containerRef.current.getBoundingClientRect();
  411. // Allow panning up to half the zoomed overflow in each direction
  412. const maxX = (container.width * (zoomLevel - 1)) / 2;
  413. const maxY = (container.height * (zoomLevel - 1)) / 2;
  414. return { x: Math.max(50, maxX), y: Math.max(50, maxY) };
  415. }, [zoomLevel]);
  416. const handleImageMouseMove = (e: React.MouseEvent) => {
  417. if (isPanning && zoomLevel > 1) {
  418. const newX = e.clientX - panStart.x;
  419. const newY = e.clientY - panStart.y;
  420. const maxPan = getMaxPan();
  421. setPanOffset({
  422. x: Math.max(-maxPan.x, Math.min(maxPan.x, newX)),
  423. y: Math.max(-maxPan.y, Math.min(maxPan.y, newY)),
  424. });
  425. }
  426. };
  427. const handleImageMouseUp = () => {
  428. setIsPanning(false);
  429. };
  430. // Touch event handlers for mobile
  431. const getTouchDistance = (touches: React.TouchList) => {
  432. if (touches.length < 2) return 0;
  433. const dx = touches[0].clientX - touches[1].clientX;
  434. const dy = touches[0].clientY - touches[1].clientY;
  435. return Math.sqrt(dx * dx + dy * dy);
  436. };
  437. const getTouchCenter = (touches: React.TouchList) => {
  438. if (touches.length < 2) {
  439. return { x: touches[0].clientX, y: touches[0].clientY };
  440. }
  441. return {
  442. x: (touches[0].clientX + touches[1].clientX) / 2,
  443. y: (touches[0].clientY + touches[1].clientY) / 2,
  444. };
  445. };
  446. const handleTouchStart = (e: React.TouchEvent) => {
  447. if (e.touches.length === 2) {
  448. // Pinch gesture start
  449. e.preventDefault();
  450. setLastTouchDistance(getTouchDistance(e.touches));
  451. setLastTouchCenter(getTouchCenter(e.touches));
  452. } else if (e.touches.length === 1 && zoomLevel > 1) {
  453. // Single touch pan start
  454. e.preventDefault();
  455. setIsPanning(true);
  456. setPanStart({
  457. x: e.touches[0].clientX - panOffset.x,
  458. y: e.touches[0].clientY - panOffset.y,
  459. });
  460. }
  461. };
  462. const handleTouchMove = (e: React.TouchEvent) => {
  463. if (e.touches.length === 2 && lastTouchDistance !== null) {
  464. // Pinch gesture
  465. e.preventDefault();
  466. const newDistance = getTouchDistance(e.touches);
  467. const scale = newDistance / lastTouchDistance;
  468. setZoomLevel(prev => {
  469. const newZoom = Math.max(1, Math.min(4, prev * scale));
  470. if (newZoom === 1) {
  471. setPanOffset({ x: 0, y: 0 });
  472. }
  473. return newZoom;
  474. });
  475. setLastTouchDistance(newDistance);
  476. // Also handle pan during pinch
  477. const newCenter = getTouchCenter(e.touches);
  478. if (lastTouchCenter) {
  479. const maxPan = getMaxPan();
  480. setPanOffset(prev => ({
  481. x: Math.max(-maxPan.x, Math.min(maxPan.x, prev.x + (newCenter.x - lastTouchCenter.x))),
  482. y: Math.max(-maxPan.y, Math.min(maxPan.y, prev.y + (newCenter.y - lastTouchCenter.y))),
  483. }));
  484. }
  485. setLastTouchCenter(newCenter);
  486. } else if (e.touches.length === 1 && isPanning && zoomLevel > 1) {
  487. // Single touch pan
  488. e.preventDefault();
  489. const newX = e.touches[0].clientX - panStart.x;
  490. const newY = e.touches[0].clientY - panStart.y;
  491. const maxPan = getMaxPan();
  492. setPanOffset({
  493. x: Math.max(-maxPan.x, Math.min(maxPan.x, newX)),
  494. y: Math.max(-maxPan.y, Math.min(maxPan.y, newY)),
  495. });
  496. }
  497. };
  498. const handleTouchEnd = (e: React.TouchEvent) => {
  499. if (e.touches.length < 2) {
  500. setLastTouchDistance(null);
  501. setLastTouchCenter(null);
  502. }
  503. if (e.touches.length === 0) {
  504. setIsPanning(false);
  505. }
  506. };
  507. const resetZoom = () => {
  508. setZoomLevel(1);
  509. setPanOffset({ x: 0, y: 0 });
  510. };
  511. const currentUrl = transitioning
  512. ? ''
  513. : streamMode === 'stream'
  514. ? `/api/v1/printers/${id}/camera/stream?fps=15&t=${imageKey}`
  515. : `/api/v1/printers/${id}/camera/snapshot?t=${imageKey}`;
  516. const isDisabled = streamLoading || transitioning || isReconnecting;
  517. if (!id) {
  518. return (
  519. <div className="min-h-screen bg-black flex items-center justify-center">
  520. <p className="text-white">{t('camera.invalidPrinterId')}</p>
  521. </div>
  522. );
  523. }
  524. return (
  525. <div ref={containerRef} className="min-h-screen bg-black flex flex-col">
  526. {/* Header */}
  527. <div className="flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary">
  528. <h1 className="text-sm font-medium text-white flex items-center gap-2">
  529. <Camera className="w-4 h-4" />
  530. {printer?.name || `Printer ${id}`}
  531. </h1>
  532. <div className="flex items-center gap-2">
  533. {/* Mode toggle */}
  534. <div className="flex bg-bambu-dark rounded p-0.5">
  535. <button
  536. onClick={() => switchToMode('stream')}
  537. disabled={isDisabled}
  538. className={`px-3 py-1 text-xs rounded transition-colors ${
  539. streamMode === 'stream'
  540. ? 'bg-bambu-green text-white'
  541. : 'text-bambu-gray hover:text-white disabled:opacity-50'
  542. }`}
  543. >
  544. {t('camera.live')}
  545. </button>
  546. <button
  547. onClick={() => switchToMode('snapshot')}
  548. disabled={isDisabled}
  549. className={`px-3 py-1 text-xs rounded transition-colors ${
  550. streamMode === 'snapshot'
  551. ? 'bg-bambu-green text-white'
  552. : 'text-bambu-gray hover:text-white disabled:opacity-50'
  553. }`}
  554. >
  555. {t('camera.snapshot')}
  556. </button>
  557. </div>
  558. <button
  559. onClick={() => chamberLightMutation.mutate(!status?.chamber_light)}
  560. disabled={!status?.connected || chamberLightMutation.isPending || !hasPermission('printers:control')}
  561. className={`p-1.5 rounded disabled:opacity-50 ${status?.chamber_light ? 'bg-yellow-500/20 hover:bg-yellow-500/30' : 'hover:bg-bambu-dark-tertiary'}`}
  562. title={!hasPermission('printers:control') ? t('printers.permission.noControl') : t('camera.chamberLight')}
  563. >
  564. <ChamberLight on={status?.chamber_light ?? false} className="w-4 h-4" />
  565. </button>
  566. <button
  567. onClick={() => setShowSkipObjectsModal(true)}
  568. disabled={!isPrintingWithObjects || !hasPermission('printers:control')}
  569. className={`p-1.5 rounded disabled:opacity-50 ${isPrintingWithObjects && hasPermission('printers:control') ? 'hover:bg-bambu-dark-tertiary' : ''}`}
  570. title={
  571. !hasPermission('printers:control')
  572. ? t('printers.permission.noControl')
  573. : !isPrintingWithObjects
  574. ? t('printers.skipObjects.onlyWhilePrinting')
  575. : t('printers.skipObjects.tooltip')
  576. }
  577. >
  578. <SkipObjectsIcon className="w-4 h-4 text-bambu-gray" />
  579. </button>
  580. <button
  581. onClick={refresh}
  582. disabled={isDisabled}
  583. className="p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50"
  584. title={streamMode === 'stream' ? t('camera.restartStream') : t('camera.refreshSnapshot')}
  585. >
  586. <RefreshCw className={`w-4 h-4 text-bambu-gray ${isDisabled ? 'animate-spin' : ''}`} />
  587. </button>
  588. <button
  589. onClick={toggleFullscreen}
  590. className="p-1.5 hover:bg-bambu-dark-tertiary rounded"
  591. title={isFullscreen ? t('camera.exitFullscreen') : t('camera.fullscreen')}
  592. >
  593. {isFullscreen ? (
  594. <Minimize className="w-4 h-4 text-bambu-gray" />
  595. ) : (
  596. <Maximize className="w-4 h-4 text-bambu-gray" />
  597. )}
  598. </button>
  599. </div>
  600. </div>
  601. {/* Video area */}
  602. <div
  603. className="flex-1 flex items-center justify-center p-2 overflow-hidden"
  604. onWheel={handleWheel}
  605. onMouseMove={handleImageMouseMove}
  606. onMouseUp={handleImageMouseUp}
  607. onMouseLeave={handleImageMouseUp}
  608. onTouchStart={handleTouchStart}
  609. onTouchMove={handleTouchMove}
  610. onTouchEnd={handleTouchEnd}
  611. style={{ touchAction: 'none' }}
  612. >
  613. <div className="relative w-full h-full flex items-center justify-center">
  614. {(streamLoading || transitioning) && !isReconnecting && (
  615. <div className="absolute inset-0 flex items-center justify-center bg-black/50 z-10">
  616. <div className="text-center">
  617. <RefreshCw className="w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2" />
  618. <p className="text-sm text-bambu-gray">
  619. {streamMode === 'stream' ? t('camera.connectingToCamera') : t('camera.capturingSnapshot')}
  620. </p>
  621. </div>
  622. </div>
  623. )}
  624. {isReconnecting && (
  625. <div className="absolute inset-0 flex items-center justify-center bg-black/80 z-10">
  626. <div className="text-center p-4">
  627. <WifiOff className="w-10 h-10 text-orange-400 mx-auto mb-3" />
  628. <p className="text-white mb-2">{t('camera.connectionLost')}</p>
  629. <p className="text-sm text-bambu-gray mb-3">
  630. {t('camera.reconnecting', { countdown: reconnectCountdown, attempt: reconnectAttempts + 1, max: MAX_RECONNECT_ATTEMPTS })}
  631. </p>
  632. <button
  633. onClick={refresh}
  634. className="px-4 py-2 bg-bambu-green text-white text-sm rounded hover:bg-bambu-green/80 transition-colors"
  635. >
  636. {t('camera.reconnectNow')}
  637. </button>
  638. </div>
  639. </div>
  640. )}
  641. {streamError && !isReconnecting && (
  642. <div className="absolute inset-0 flex items-center justify-center bg-black z-10">
  643. <div className="text-center p-4">
  644. <AlertTriangle className="w-12 h-12 text-orange-400 mx-auto mb-3" />
  645. <p className="text-white mb-2">{t('camera.cameraUnavailable')}</p>
  646. <p className="text-xs text-bambu-gray mb-4 max-w-md">
  647. {t('camera.cameraUnavailableDesc')}
  648. </p>
  649. <button
  650. onClick={refresh}
  651. className="px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors"
  652. >
  653. {t('camera.retry')}
  654. </button>
  655. </div>
  656. </div>
  657. )}
  658. <img
  659. ref={imgRef}
  660. key={imageKey}
  661. src={currentUrl}
  662. alt={t('camera.cameraStream')}
  663. className="max-w-full max-h-full object-contain select-none"
  664. style={{
  665. transform: `scale(${zoomLevel}) translate(${panOffset.x / zoomLevel}px, ${panOffset.y / zoomLevel}px) rotate(${printer?.camera_rotation || 0}deg)`,
  666. ...(printer?.camera_rotation === 90 || printer?.camera_rotation === 270 ? { maxWidth: '100vh', maxHeight: '100vw' } : {}),
  667. cursor: zoomLevel > 1 ? (isPanning ? 'grabbing' : 'grab') : 'default',
  668. }}
  669. onError={currentUrl ? handleStreamError : undefined}
  670. onLoad={currentUrl ? handleStreamLoad : undefined}
  671. onMouseDown={handleImageMouseDown}
  672. draggable={false}
  673. />
  674. {/* Zoom controls */}
  675. <div className="absolute bottom-4 left-4 flex items-center gap-1.5 bg-black/60 rounded-lg px-2 py-1.5">
  676. <button
  677. onClick={handleZoomOut}
  678. disabled={zoomLevel <= 1}
  679. className="p-1.5 hover:bg-white/10 rounded disabled:opacity-30"
  680. title={t('camera.zoomOut')}
  681. >
  682. <ZoomOut className="w-4 h-4 text-white" />
  683. </button>
  684. <button
  685. onClick={resetZoom}
  686. className="px-2 py-1 text-sm text-white hover:bg-white/10 rounded min-w-[48px]"
  687. title={t('camera.resetZoom')}
  688. >
  689. {Math.round(zoomLevel * 100)}%
  690. </button>
  691. <button
  692. onClick={handleZoomIn}
  693. disabled={zoomLevel >= 4}
  694. className="p-1.5 hover:bg-white/10 rounded disabled:opacity-30"
  695. title={t('camera.zoomIn')}
  696. >
  697. <ZoomIn className="w-4 h-4 text-white" />
  698. </button>
  699. </div>
  700. </div>
  701. </div>
  702. {/* Skip Objects Modal */}
  703. <SkipObjectsModal
  704. printerId={id}
  705. isOpen={showSkipObjectsModal}
  706. onClose={() => setShowSkipObjectsModal(false)}
  707. />
  708. </div>
  709. );
  710. }