CameraPage.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { useState, useEffect, useRef } from 'react';
  2. import { useParams } from 'react-router-dom';
  3. import { useQuery } from '@tanstack/react-query';
  4. import { RefreshCw, AlertTriangle, Camera, Maximize, Minimize } from 'lucide-react';
  5. import { api } from '../api/client';
  6. export function CameraPage() {
  7. const { printerId } = useParams<{ printerId: string }>();
  8. const id = parseInt(printerId || '0', 10);
  9. const [streamMode, setStreamMode] = useState<'stream' | 'snapshot'>('stream');
  10. const [streamError, setStreamError] = useState(false);
  11. const [streamLoading, setStreamLoading] = useState(true);
  12. const [imageKey, setImageKey] = useState(Date.now());
  13. const [transitioning, setTransitioning] = useState(false);
  14. const [isFullscreen, setIsFullscreen] = useState(false);
  15. const imgRef = useRef<HTMLImageElement>(null);
  16. const containerRef = useRef<HTMLDivElement>(null);
  17. // Fetch printer info for the title
  18. const { data: printer } = useQuery({
  19. queryKey: ['printer', id],
  20. queryFn: () => api.getPrinter(id),
  21. enabled: id > 0,
  22. });
  23. // Update document title
  24. useEffect(() => {
  25. if (printer) {
  26. document.title = `${printer.name} - Camera`;
  27. }
  28. return () => {
  29. document.title = 'Bambuddy';
  30. };
  31. }, [printer]);
  32. // Cleanup on unmount - stop the camera stream
  33. useEffect(() => {
  34. const stopUrl = `/api/v1/printers/${id}/camera/stop`;
  35. // Handle page unload/close with sendBeacon (more reliable than fetch on unload)
  36. const handleBeforeUnload = () => {
  37. if (id > 0) {
  38. navigator.sendBeacon(stopUrl);
  39. }
  40. };
  41. // Handle visibility change (tab hidden/closed)
  42. const handleVisibilityChange = () => {
  43. if (document.visibilityState === 'hidden' && id > 0) {
  44. navigator.sendBeacon(stopUrl);
  45. }
  46. };
  47. window.addEventListener('beforeunload', handleBeforeUnload);
  48. document.addEventListener('visibilitychange', handleVisibilityChange);
  49. return () => {
  50. window.removeEventListener('beforeunload', handleBeforeUnload);
  51. document.removeEventListener('visibilitychange', handleVisibilityChange);
  52. // Clear the image source
  53. if (imgRef.current) {
  54. imgRef.current.src = '';
  55. }
  56. // Call the stop endpoint to terminate ffmpeg processes
  57. if (id > 0) {
  58. // Use sendBeacon for reliability during unmount
  59. navigator.sendBeacon(stopUrl);
  60. }
  61. };
  62. }, [id]);
  63. // Auto-hide loading after timeout
  64. useEffect(() => {
  65. if (streamLoading && !transitioning) {
  66. const timeout = streamMode === 'stream' ? 3000 : 20000;
  67. const timer = setTimeout(() => {
  68. setStreamLoading(false);
  69. }, timeout);
  70. return () => clearTimeout(timer);
  71. }
  72. }, [streamMode, streamLoading, imageKey, transitioning]);
  73. // Fullscreen change listener
  74. useEffect(() => {
  75. const handleFullscreenChange = () => {
  76. setIsFullscreen(!!document.fullscreenElement);
  77. };
  78. document.addEventListener('fullscreenchange', handleFullscreenChange);
  79. return () => document.removeEventListener('fullscreenchange', handleFullscreenChange);
  80. }, []);
  81. const handleStreamError = () => {
  82. setStreamError(true);
  83. setStreamLoading(false);
  84. };
  85. const handleStreamLoad = () => {
  86. setStreamLoading(false);
  87. setStreamError(false);
  88. };
  89. const stopStream = () => {
  90. if (id > 0) {
  91. fetch(`/api/v1/printers/${id}/camera/stop`).catch(() => {});
  92. }
  93. };
  94. const switchToMode = (newMode: 'stream' | 'snapshot') => {
  95. if (streamMode === newMode || transitioning) return;
  96. setTransitioning(true);
  97. setStreamLoading(true);
  98. setStreamError(false);
  99. if (imgRef.current) {
  100. imgRef.current.src = '';
  101. }
  102. // Stop any active streams when switching modes
  103. if (streamMode === 'stream') {
  104. stopStream();
  105. }
  106. setTimeout(() => {
  107. setStreamMode(newMode);
  108. setImageKey(Date.now());
  109. setTransitioning(false);
  110. }, 100);
  111. };
  112. const refresh = () => {
  113. if (transitioning) return;
  114. setTransitioning(true);
  115. setStreamLoading(true);
  116. setStreamError(false);
  117. if (imgRef.current) {
  118. imgRef.current.src = '';
  119. }
  120. // Stop any active streams before refresh
  121. if (streamMode === 'stream') {
  122. stopStream();
  123. }
  124. setTimeout(() => {
  125. setImageKey(Date.now());
  126. setTransitioning(false);
  127. }, 100);
  128. };
  129. const toggleFullscreen = () => {
  130. if (!containerRef.current) return;
  131. if (document.fullscreenElement) {
  132. document.exitFullscreen();
  133. } else {
  134. containerRef.current.requestFullscreen();
  135. }
  136. };
  137. const currentUrl = transitioning
  138. ? ''
  139. : streamMode === 'stream'
  140. ? `/api/v1/printers/${id}/camera/stream?fps=10&t=${imageKey}`
  141. : `/api/v1/printers/${id}/camera/snapshot?t=${imageKey}`;
  142. const isDisabled = streamLoading || transitioning;
  143. if (!id) {
  144. return (
  145. <div className="min-h-screen bg-black flex items-center justify-center">
  146. <p className="text-white">Invalid printer ID</p>
  147. </div>
  148. );
  149. }
  150. return (
  151. <div ref={containerRef} className="min-h-screen bg-black flex flex-col">
  152. {/* Header */}
  153. <div className="flex items-center justify-between px-4 py-2 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary">
  154. <h1 className="text-sm font-medium text-white flex items-center gap-2">
  155. <Camera className="w-4 h-4" />
  156. {printer?.name || `Printer ${id}`}
  157. </h1>
  158. <div className="flex items-center gap-2">
  159. {/* Mode toggle */}
  160. <div className="flex bg-bambu-dark rounded p-0.5">
  161. <button
  162. onClick={() => switchToMode('stream')}
  163. disabled={isDisabled}
  164. className={`px-3 py-1 text-xs rounded transition-colors ${
  165. streamMode === 'stream'
  166. ? 'bg-bambu-green text-white'
  167. : 'text-bambu-gray hover:text-white disabled:opacity-50'
  168. }`}
  169. >
  170. Live
  171. </button>
  172. <button
  173. onClick={() => switchToMode('snapshot')}
  174. disabled={isDisabled}
  175. className={`px-3 py-1 text-xs rounded transition-colors ${
  176. streamMode === 'snapshot'
  177. ? 'bg-bambu-green text-white'
  178. : 'text-bambu-gray hover:text-white disabled:opacity-50'
  179. }`}
  180. >
  181. Snapshot
  182. </button>
  183. </div>
  184. <button
  185. onClick={refresh}
  186. disabled={isDisabled}
  187. className="p-1.5 hover:bg-bambu-dark-tertiary rounded disabled:opacity-50"
  188. title={streamMode === 'stream' ? 'Restart stream' : 'Refresh snapshot'}
  189. >
  190. <RefreshCw className={`w-4 h-4 text-bambu-gray ${isDisabled ? 'animate-spin' : ''}`} />
  191. </button>
  192. <button
  193. onClick={toggleFullscreen}
  194. className="p-1.5 hover:bg-bambu-dark-tertiary rounded"
  195. title={isFullscreen ? 'Exit fullscreen' : 'Fullscreen'}
  196. >
  197. {isFullscreen ? (
  198. <Minimize className="w-4 h-4 text-bambu-gray" />
  199. ) : (
  200. <Maximize className="w-4 h-4 text-bambu-gray" />
  201. )}
  202. </button>
  203. </div>
  204. </div>
  205. {/* Video area */}
  206. <div className="flex-1 flex items-center justify-center p-2">
  207. <div className="relative w-full h-full flex items-center justify-center">
  208. {(streamLoading || transitioning) && (
  209. <div className="absolute inset-0 flex items-center justify-center bg-black/50 z-10">
  210. <div className="text-center">
  211. <RefreshCw className="w-8 h-8 text-bambu-gray animate-spin mx-auto mb-2" />
  212. <p className="text-sm text-bambu-gray">
  213. {streamMode === 'stream' ? 'Connecting to camera...' : 'Capturing snapshot...'}
  214. </p>
  215. </div>
  216. </div>
  217. )}
  218. {streamError && (
  219. <div className="absolute inset-0 flex items-center justify-center bg-black z-10">
  220. <div className="text-center p-4">
  221. <AlertTriangle className="w-12 h-12 text-orange-400 mx-auto mb-3" />
  222. <p className="text-white mb-2">Camera unavailable</p>
  223. <p className="text-xs text-bambu-gray mb-4 max-w-md">
  224. Make sure the printer is powered on and connected.
  225. </p>
  226. <button
  227. onClick={refresh}
  228. className="px-4 py-2 bg-bambu-green text-white rounded hover:bg-bambu-green/80 transition-colors"
  229. >
  230. Retry
  231. </button>
  232. </div>
  233. </div>
  234. )}
  235. <img
  236. ref={imgRef}
  237. key={imageKey}
  238. src={currentUrl}
  239. alt="Camera stream"
  240. className="max-w-full max-h-full object-contain"
  241. onError={currentUrl ? handleStreamError : undefined}
  242. onLoad={currentUrl ? handleStreamLoad : undefined}
  243. />
  244. </div>
  245. </div>
  246. </div>
  247. );
  248. }