CameraPage.tsx 29 KB

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