PrintersPage.tsx 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352
  1. import { useState, useEffect, useMemo, useRef } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTheme } from '../contexts/ThemeContext';
  4. import {
  5. Plus,
  6. Link,
  7. Unlink,
  8. Signal,
  9. Clock,
  10. MoreVertical,
  11. Trash2,
  12. RefreshCw,
  13. Box,
  14. HardDrive,
  15. AlertTriangle,
  16. AlertCircle,
  17. Terminal,
  18. Power,
  19. PowerOff,
  20. Zap,
  21. Wrench,
  22. ChevronDown,
  23. Pencil,
  24. ArrowUp,
  25. ArrowDown,
  26. LayoutGrid,
  27. LayoutList,
  28. Layers,
  29. Video,
  30. Search,
  31. Loader2,
  32. Square,
  33. Pause,
  34. Play,
  35. X,
  36. Monitor,
  37. } from 'lucide-react';
  38. // Custom Skip Objects icon - arrow jumping over boxes
  39. const SkipObjectsIcon = ({ className }: { className?: string }) => (
  40. <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}>
  41. {/* Three boxes at the bottom */}
  42. <rect x="2" y="15" width="5" height="5" rx="0.5" />
  43. <rect x="9.5" y="15" width="5" height="5" rx="0.5" fill="currentColor" opacity="0.3" />
  44. <rect x="17" y="15" width="5" height="5" rx="0.5" />
  45. {/* Curved arrow jumping over first box */}
  46. <path d="M4 12 C4 6, 14 6, 14 12" />
  47. <polyline points="12,10 14,12 12,14" />
  48. </svg>
  49. );
  50. import { useNavigate } from 'react-router-dom';
  51. import { api, discoveryApi } from '../api/client';
  52. import type { Printer, PrinterCreate, AMSUnit, DiscoveredPrinter } from '../api/client';
  53. import { Card, CardContent } from '../components/Card';
  54. import { Button } from '../components/Button';
  55. import { ConfirmModal } from '../components/ConfirmModal';
  56. import { FileManagerModal } from '../components/FileManagerModal';
  57. import { MQTTDebugModal } from '../components/MQTTDebugModal';
  58. import { HMSErrorModal, filterKnownHMSErrors } from '../components/HMSErrorModal';
  59. import { PrinterQueueWidget } from '../components/PrinterQueueWidget';
  60. import { AMSHistoryModal } from '../components/AMSHistoryModal';
  61. import { FilamentHoverCard, EmptySlotHoverCard } from '../components/FilamentHoverCard';
  62. import { useToast } from '../contexts/ToastContext';
  63. // Bambu Lab color code mapping (color suffix from tray_id_name -> color name)
  64. // tray_id_name format: "A00-Y2" where Y2 is the color code
  65. const BAMBU_COLOR_CODES: Record<string, string> = {
  66. // Yellows
  67. 'Y0': 'Yellow',
  68. 'Y1': 'Savana Yellow',
  69. 'Y2': 'Sunflower Yellow',
  70. 'Y3': 'Lemon Yellow',
  71. // Oranges
  72. 'O0': 'Orange',
  73. 'O1': 'Mandarin Orange',
  74. 'O2': 'Coral Orange',
  75. // Reds
  76. 'R0': 'Red',
  77. 'R1': 'Scarlet Red',
  78. 'R2': 'Magenta',
  79. 'R3': 'Sakura Pink',
  80. 'R4': 'Raspberry Red',
  81. // Pinks
  82. 'P0': 'Pink',
  83. 'P1': 'Sakura Pink',
  84. // Purples
  85. 'V0': 'Purple',
  86. 'V1': 'Violet',
  87. 'V2': 'Lilac Purple',
  88. // Blues
  89. 'B0': 'Blue',
  90. 'B1': 'Sky Blue',
  91. 'B2': 'Navy Blue',
  92. 'B3': 'Ice Blue',
  93. 'B4': 'Cyan',
  94. // Greens
  95. 'G0': 'Green',
  96. 'G1': 'Grass Green',
  97. 'G2': 'Lime Green',
  98. 'G3': 'Mint Green',
  99. 'G4': 'Olive Green',
  100. 'G5': 'Jungle Green',
  101. 'G6': 'Bambu Green',
  102. // Browns
  103. 'N0': 'Brown',
  104. 'N1': 'Peanut Brown',
  105. 'N2': 'Coffee Brown',
  106. 'N3': 'Caramel Brown',
  107. // Grays
  108. 'A0': 'Gray',
  109. 'A1': 'Charcoal Gray',
  110. 'A2': 'Silver Gray',
  111. 'A3': 'Titan Gray',
  112. // Blacks
  113. 'K0': 'Black',
  114. 'K1': 'Black',
  115. // Whites
  116. 'W0': 'White',
  117. 'W1': 'Jade White',
  118. 'W2': 'Ivory White',
  119. // Special
  120. 'T0': 'Transparent',
  121. 'C0': 'Marble',
  122. 'X0': 'Bronze',
  123. 'X1': 'Gold',
  124. 'X2': 'Silver',
  125. };
  126. // Get color name from Bambu Lab tray_id_name (e.g., "A00-Y2" -> "Sunflower Yellow")
  127. function getBambuColorName(trayIdName: string | null | undefined): string | null {
  128. if (!trayIdName) return null;
  129. // Extract color code after the dash (e.g., "A00-Y2" -> "Y2")
  130. const parts = trayIdName.split('-');
  131. if (parts.length < 2) return null;
  132. const colorCode = parts[1];
  133. return BAMBU_COLOR_CODES[colorCode] || null;
  134. }
  135. // Convert hex color to basic color name
  136. function hexToBasicColorName(hex: string | null | undefined): string {
  137. if (!hex || hex.length < 6) return 'Unknown';
  138. // Parse RGB from hex (format: RRGGBBAA or RRGGBB)
  139. const r = parseInt(hex.substring(0, 2), 16);
  140. const g = parseInt(hex.substring(2, 4), 16);
  141. const b = parseInt(hex.substring(4, 6), 16);
  142. // Calculate HSL for better color classification
  143. const max = Math.max(r, g, b) / 255;
  144. const min = Math.min(r, g, b) / 255;
  145. const l = (max + min) / 2;
  146. let h = 0;
  147. let s = 0;
  148. if (max !== min) {
  149. const d = max - min;
  150. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  151. const rNorm = r / 255;
  152. const gNorm = g / 255;
  153. const bNorm = b / 255;
  154. if (max === rNorm) {
  155. h = ((gNorm - bNorm) / d + (gNorm < bNorm ? 6 : 0)) / 6;
  156. } else if (max === gNorm) {
  157. h = ((bNorm - rNorm) / d + 2) / 6;
  158. } else {
  159. h = ((rNorm - gNorm) / d + 4) / 6;
  160. }
  161. }
  162. // Convert to degrees
  163. h = h * 360;
  164. // Classify by lightness first
  165. if (l < 0.15) return 'Black';
  166. if (l > 0.85) return 'White';
  167. // Low saturation = gray
  168. if (s < 0.15) {
  169. if (l < 0.4) return 'Dark Gray';
  170. if (l > 0.6) return 'Light Gray';
  171. return 'Gray';
  172. }
  173. // Classify by hue
  174. if (h < 15 || h >= 345) return 'Red';
  175. if (h < 45) return 'Orange';
  176. if (h < 70) return 'Yellow';
  177. if (h < 150) return 'Green';
  178. if (h < 200) return 'Cyan';
  179. if (h < 260) return 'Blue';
  180. if (h < 290) return 'Purple';
  181. if (h < 345) return 'Pink';
  182. return 'Unknown';
  183. }
  184. // Format K value with 3 decimal places, default to 0.020 if null
  185. function formatKValue(k: number | null | undefined): string {
  186. const value = k ?? 0.020;
  187. return value.toFixed(3);
  188. }
  189. // Nozzle side indicators (Bambu Lab style - square badge with L/R)
  190. function NozzleBadge({ side }: { side: 'L' | 'R' }) {
  191. const { mode } = useTheme();
  192. // Light mode: #e7f5e9 (light green), Dark mode: #1a4d2e (dark green)
  193. const bgColor = mode === 'dark' ? '#1a4d2e' : '#e7f5e9';
  194. return (
  195. <span
  196. className="inline-flex items-center justify-center w-4 h-4 text-[10px] font-bold rounded"
  197. style={{ backgroundColor: bgColor, color: '#00ae42' }}
  198. >
  199. {side}
  200. </span>
  201. );
  202. }
  203. // Water drop SVG - empty outline (Bambu Lab style from bambu-humidity)
  204. function WaterDropEmpty({ className }: { className?: string }) {
  205. return (
  206. <svg className={className} viewBox="0 0 36 54" fill="none" xmlns="http://www.w3.org/2000/svg">
  207. <path d="M17.8131 0.00538C18.4463 -0.15091 20.3648 3.14642 20.8264 3.84781C25.4187 10.816 35.3089 26.9368 35.9383 34.8694C37.4182 53.5822 11.882 61.3357 2.53721 45.3789C-1.73471 38.0791 0.016 32.2049 3.178 25.0232C6.99221 16.3662 12.6411 7.90372 17.8131 0.00538ZM18.3738 7.24807L17.5881 7.48441C14.4452 12.9431 10.917 18.2341 8.19369 23.9368C4.6808 31.29 1.18317 38.5479 7.69403 45.5657C17.3058 55.9228 34.9847 46.8808 31.4604 32.8681C29.2558 24.0969 22.4207 15.2913 18.3776 7.24807H18.3738Z" fill="#C3C2C1"/>
  208. </svg>
  209. );
  210. }
  211. // Water drop SVG - half filled with blue water (Bambu Lab style from bambu-humidity)
  212. function WaterDropHalf({ className }: { className?: string }) {
  213. return (
  214. <svg className={className} viewBox="0 0 35 53" fill="none" xmlns="http://www.w3.org/2000/svg">
  215. <path d="M17.3165 0.0038C17.932 -0.14959 19.7971 3.08645 20.2458 3.77481C24.7103 10.6135 34.3251 26.4346 34.937 34.2198C36.3757 52.5848 11.5505 60.1942 2.46584 44.534C-1.68714 37.3735 0.0148 31.6085 3.08879 24.5603C6.79681 16.0605 12.2884 7.75907 17.3165 0.0038ZM17.8615 7.11561L17.0977 7.34755C14.0423 12.7048 10.6124 17.8974 7.96483 23.4941C4.54975 30.7107 1.14949 37.8337 7.47908 44.721C16.8233 54.8856 34.01 46.0117 30.5838 32.2595C28.4405 23.6512 21.7957 15.0093 17.8652 7.11561H17.8615Z" fill="#C3C2C1"/>
  216. <path d="M5.03547 30.112C9.64453 30.4936 11.632 35.7985 16.4154 35.791C19.6339 35.7873 20.2161 33.2283 22.3853 31.6197C31.6776 24.7286 33.5835 37.4894 27.9881 44.4254C18.1878 56.5653 -1.16063 44.6013 5.03917 30.1158L5.03547 30.112Z" fill="#1F8FEB"/>
  217. </svg>
  218. );
  219. }
  220. // Water drop SVG - fully filled with blue water (Bambu Lab style from bambu-humidity)
  221. function WaterDropFull({ className }: { className?: string }) {
  222. return (
  223. <svg className={className} viewBox="0 0 36 54" fill="none" xmlns="http://www.w3.org/2000/svg">
  224. <path d="M17.9625 4.48059L4.77216 26.3154L2.08228 40.2175L10.0224 50.8414H23.1594L33.3246 42.1693V30.2455L17.9625 4.48059Z" fill="#1F8FEB"/>
  225. <path d="M17.7948 0.00538C18.4273 -0.15091 20.3438 3.14642 20.8048 3.84781C25.3921 10.816 35.2715 26.9368 35.9001 34.8694C37.3784 53.5822 11.8702 61.3357 2.53562 45.3789C-1.73163 38.0829 0.0134 32.2087 3.1757 25.027C6.98574 16.3662 12.6284 7.90372 17.7948 0.00538ZM18.3549 7.24807L17.57 7.48441C14.4306 12.9431 10.9063 18.2341 8.1859 23.9368C4.67686 31.29 1.18305 38.5479 7.68679 45.5657C17.2881 55.9228 34.9476 46.8808 31.4271 32.8681C29.2249 24.0969 22.3974 15.2913 18.3587 7.24807H18.3549Z" fill="#C3C2C1"/>
  226. </svg>
  227. );
  228. }
  229. // Thermometer SVG - empty outline
  230. function ThermometerEmpty({ className }: { className?: string }) {
  231. return (
  232. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  233. <path d="M6 0.5C4.6 0.5 3.5 1.6 3.5 3V12.1C2.6 12.8 2 13.9 2 15C2 17.2 3.8 19 6 19C8.2 19 10 17.2 10 15C10 13.9 9.4 12.8 8.5 12.1V3C8.5 1.6 7.4 0.5 6 0.5Z" stroke="#C3C2C1" strokeWidth="1" fill="none"/>
  234. <circle cx="6" cy="15" r="2.5" stroke="#C3C2C1" strokeWidth="1" fill="none"/>
  235. </svg>
  236. );
  237. }
  238. // Thermometer SVG - half filled (gold - same as humidity fair)
  239. function ThermometerHalf({ className }: { className?: string }) {
  240. return (
  241. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  242. <rect x="4.5" y="8" width="3" height="4.5" fill="#d4a017" rx="0.5"/>
  243. <circle cx="6" cy="15" r="2" fill="#d4a017"/>
  244. <path d="M6 0.5C4.6 0.5 3.5 1.6 3.5 3V12.1C2.6 12.8 2 13.9 2 15C2 17.2 3.8 19 6 19C8.2 19 10 17.2 10 15C10 13.9 9.4 12.8 8.5 12.1V3C8.5 1.6 7.4 0.5 6 0.5Z" stroke="#C3C2C1" strokeWidth="1" fill="none"/>
  245. </svg>
  246. );
  247. }
  248. // Thermometer SVG - fully filled (red - same as humidity bad)
  249. function ThermometerFull({ className }: { className?: string }) {
  250. return (
  251. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  252. <rect x="4.5" y="3" width="3" height="9.5" fill="#c62828" rx="0.5"/>
  253. <circle cx="6" cy="15" r="2" fill="#c62828"/>
  254. <path d="M6 0.5C4.6 0.5 3.5 1.6 3.5 3V12.1C2.6 12.8 2 13.9 2 15C2 17.2 3.8 19 6 19C8.2 19 10 17.2 10 15C10 13.9 9.4 12.8 8.5 12.1V3C8.5 1.6 7.4 0.5 6 0.5Z" stroke="#C3C2C1" strokeWidth="1" fill="none"/>
  255. </svg>
  256. );
  257. }
  258. // Heater thermometer icon - filled when heating, outline when off
  259. interface HeaterThermometerProps {
  260. className?: string;
  261. color: string; // The color class (e.g., "text-orange-400")
  262. isHeating: boolean;
  263. }
  264. function HeaterThermometer({ className, color, isHeating }: HeaterThermometerProps) {
  265. // Extract the actual color from Tailwind class for SVG fill
  266. const colorMap: Record<string, string> = {
  267. 'text-orange-400': '#fb923c',
  268. 'text-blue-400': '#60a5fa',
  269. 'text-green-400': '#4ade80',
  270. };
  271. const fillColor = colorMap[color] || '#888';
  272. // Glow style when heating
  273. const glowStyle = isHeating ? {
  274. filter: `drop-shadow(0 0 4px ${fillColor}) drop-shadow(0 0 8px ${fillColor})`,
  275. } : {};
  276. if (isHeating) {
  277. // Filled thermometer with glow - heater is ON
  278. return (
  279. <svg className={className} style={glowStyle} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  280. <rect x="4.5" y="3" width="3" height="9.5" fill={fillColor} rx="0.5"/>
  281. <circle cx="6" cy="15" r="2" fill={fillColor}/>
  282. <path d="M6 0.5C4.6 0.5 3.5 1.6 3.5 3V12.1C2.6 12.8 2 13.9 2 15C2 17.2 3.8 19 6 19C8.2 19 10 17.2 10 15C10 13.9 9.4 12.8 8.5 12.1V3C8.5 1.6 7.4 0.5 6 0.5Z" stroke={fillColor} strokeWidth="1" fill="none"/>
  283. </svg>
  284. );
  285. }
  286. // Empty thermometer - heater is OFF
  287. return (
  288. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  289. <path d="M6 0.5C4.6 0.5 3.5 1.6 3.5 3V12.1C2.6 12.8 2 13.9 2 15C2 17.2 3.8 19 6 19C8.2 19 10 17.2 10 15C10 13.9 9.4 12.8 8.5 12.1V3C8.5 1.6 7.4 0.5 6 0.5Z" stroke={fillColor} strokeWidth="1" fill="none"/>
  290. <circle cx="6" cy="15" r="2.5" stroke={fillColor} strokeWidth="1" fill="none"/>
  291. </svg>
  292. );
  293. }
  294. // Humidity indicator with water drop that fills based on level (Bambu Lab style)
  295. // Reference: https://github.com/theicedmango/bambu-humidity
  296. interface HumidityIndicatorProps {
  297. humidity: number | string;
  298. goodThreshold?: number; // <= this is green
  299. fairThreshold?: number; // <= this is orange, > is red
  300. onClick?: () => void;
  301. compact?: boolean; // Smaller version for grid layout
  302. }
  303. function HumidityIndicator({ humidity, goodThreshold = 40, fairThreshold = 60, onClick, compact }: HumidityIndicatorProps) {
  304. const humidityValue = typeof humidity === 'string' ? parseInt(humidity, 10) : humidity;
  305. const good = typeof goodThreshold === 'number' ? goodThreshold : 40;
  306. const fair = typeof fairThreshold === 'number' ? fairThreshold : 60;
  307. // Status thresholds (configurable via settings)
  308. // Good: ≤goodThreshold (green #22a352), Fair: ≤fairThreshold (gold #d4a017), Bad: >fairThreshold (red #c62828)
  309. let textColor: string;
  310. let statusText: string;
  311. if (isNaN(humidityValue)) {
  312. textColor = '#C3C2C1';
  313. statusText = 'Unknown';
  314. } else if (humidityValue <= good) {
  315. textColor = '#22a352'; // Green - Good
  316. statusText = 'Good';
  317. } else if (humidityValue <= fair) {
  318. textColor = '#d4a017'; // Gold - Fair
  319. statusText = 'Fair';
  320. } else {
  321. textColor = '#c62828'; // Red - Bad
  322. statusText = 'Bad';
  323. }
  324. // Fill level based on status: Good=Empty (dry), Fair=Half, Bad=Full (wet)
  325. let DropComponent: React.FC<{ className?: string }>;
  326. if (isNaN(humidityValue)) {
  327. DropComponent = WaterDropEmpty;
  328. } else if (humidityValue <= good) {
  329. DropComponent = WaterDropEmpty; // Good - empty drop (dry)
  330. } else if (humidityValue <= fair) {
  331. DropComponent = WaterDropHalf; // Fair - half filled
  332. } else {
  333. DropComponent = WaterDropFull; // Bad - full (too humid)
  334. }
  335. return (
  336. <button
  337. type="button"
  338. onClick={onClick}
  339. className={`flex items-center gap-1 ${onClick ? 'cursor-pointer hover:opacity-80 transition-opacity' : ''}`}
  340. title={`Humidity: ${humidityValue}% - ${statusText}${onClick ? ' (click for history)' : ''}`}
  341. >
  342. <DropComponent className={compact ? "w-2.5 h-3" : "w-3 h-4"} />
  343. <span className={`font-medium tabular-nums ${compact ? 'text-[10px]' : 'text-xs'}`} style={{ color: textColor }}>{humidityValue}%</span>
  344. </button>
  345. );
  346. }
  347. // Temperature indicator with dynamic icon and coloring
  348. interface TemperatureIndicatorProps {
  349. temp: number;
  350. goodThreshold?: number; // <= this is blue
  351. fairThreshold?: number; // <= this is orange, > is red
  352. onClick?: () => void;
  353. compact?: boolean; // Smaller version for grid layout
  354. }
  355. function TemperatureIndicator({ temp, goodThreshold = 28, fairThreshold = 35, onClick, compact }: TemperatureIndicatorProps) {
  356. // Ensure thresholds are numbers
  357. const good = typeof goodThreshold === 'number' ? goodThreshold : 28;
  358. const fair = typeof fairThreshold === 'number' ? fairThreshold : 35;
  359. let textColor: string;
  360. let statusText: string;
  361. let ThermoComponent: React.FC<{ className?: string }>;
  362. if (temp <= good) {
  363. textColor = '#22a352'; // Green - good (same as humidity)
  364. statusText = 'Good';
  365. ThermoComponent = ThermometerEmpty;
  366. } else if (temp <= fair) {
  367. textColor = '#d4a017'; // Gold - fair (same as humidity)
  368. statusText = 'Fair';
  369. ThermoComponent = ThermometerHalf;
  370. } else {
  371. textColor = '#c62828'; // Red - bad (same as humidity)
  372. statusText = 'Bad';
  373. ThermoComponent = ThermometerFull;
  374. }
  375. return (
  376. <button
  377. type="button"
  378. onClick={onClick}
  379. className={`flex items-center gap-1 ${onClick ? 'cursor-pointer hover:opacity-80 transition-opacity' : ''}`}
  380. title={`Temperature: ${temp}°C - ${statusText}${onClick ? ' (click for history)' : ''}`}
  381. >
  382. <ThermoComponent className={compact ? "w-2.5 h-3" : "w-3 h-4"} />
  383. <span className={`tabular-nums text-right ${compact ? 'text-[10px] w-8' : 'w-12'}`} style={{ color: textColor }}>{temp}°C</span>
  384. </button>
  385. );
  386. }
  387. // Get AMS label: AMS-A/B/C/D for regular AMS, HT-A/B for AMS-HT (single spool)
  388. // Always use tray count as the source of truth (1 tray = AMS-HT, 4 trays = regular AMS)
  389. // AMS-HT uses IDs 128+ while regular AMS uses 0-3
  390. function getAmsLabel(amsId: number | string, trayCount: number): string {
  391. // Ensure amsId is a number (backend might send string)
  392. const id = typeof amsId === 'string' ? parseInt(amsId, 10) : amsId;
  393. const safeId = isNaN(id) ? 0 : id;
  394. const isHt = trayCount === 1;
  395. // AMS-HT uses IDs starting at 128, regular AMS uses 0-3
  396. const normalizedId = safeId >= 128 ? safeId - 128 : safeId;
  397. const letter = String.fromCharCode(65 + normalizedId); // 0=A, 1=B, 2=C, 3=D
  398. return isHt ? `HT-${letter}` : `AMS-${letter}`;
  399. }
  400. // Get fill bar color based on spool fill level
  401. function getFillBarColor(fillLevel: number): string {
  402. if (fillLevel > 50) return '#00ae42'; // Green - good
  403. if (fillLevel >= 15) return '#f59e0b'; // Amber - warning (<= 50%)
  404. return '#ef4444'; // Red - critical (< 15%)
  405. }
  406. function formatTime(seconds: number): string {
  407. const hours = Math.floor(seconds / 3600);
  408. const minutes = Math.floor((seconds % 3600) / 60);
  409. return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
  410. }
  411. function formatETA(remainingMinutes: number): string {
  412. const now = new Date();
  413. const eta = new Date(now.getTime() + remainingMinutes * 60 * 1000);
  414. const today = new Date();
  415. today.setHours(0, 0, 0, 0);
  416. const etaDay = new Date(eta);
  417. etaDay.setHours(0, 0, 0, 0);
  418. const timeStr = eta.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  419. // Check if it's tomorrow or later
  420. const dayDiff = Math.floor((etaDay.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
  421. if (dayDiff === 0) {
  422. return timeStr;
  423. } else if (dayDiff === 1) {
  424. return `Tomorrow ${timeStr}`;
  425. } else {
  426. return eta.toLocaleDateString([], { weekday: 'short' }) + ' ' + timeStr;
  427. }
  428. }
  429. function getPrinterImage(model: string | null | undefined): string {
  430. if (!model) return '/img/printers/default.png';
  431. const modelLower = model.toLowerCase().replace(/\s+/g, '');
  432. // Map model names to image files
  433. if (modelLower.includes('x1e')) return '/img/printers/x1e.png';
  434. if (modelLower.includes('x1c') || modelLower.includes('x1carbon')) return '/img/printers/x1c.png';
  435. if (modelLower.includes('x1')) return '/img/printers/x1c.png';
  436. if (modelLower.includes('h2d')) return '/img/printers/h2d.png';
  437. if (modelLower.includes('h2c') || modelLower.includes('h2s')) return '/img/printers/h2d.png';
  438. if (modelLower.includes('p2s')) return '/img/printers/p1s.png';
  439. if (modelLower.includes('p1s')) return '/img/printers/p1s.png';
  440. if (modelLower.includes('p1p')) return '/img/printers/p1p.png';
  441. if (modelLower.includes('a1mini')) return '/img/printers/a1mini.png';
  442. if (modelLower.includes('a1')) return '/img/printers/a1.png';
  443. return '/img/printers/default.png';
  444. }
  445. function getWifiStrength(rssi: number | null | undefined): { label: string; color: string; bars: number } {
  446. if (rssi == null) return { label: '', color: 'text-bambu-gray', bars: 0 };
  447. if (rssi >= -50) return { label: 'Excellent', color: 'text-bambu-green', bars: 4 };
  448. if (rssi >= -60) return { label: 'Good', color: 'text-bambu-green', bars: 3 };
  449. if (rssi >= -70) return { label: 'Fair', color: 'text-yellow-400', bars: 2 };
  450. if (rssi >= -80) return { label: 'Weak', color: 'text-orange-400', bars: 1 };
  451. return { label: 'Very weak', color: 'text-red-400', bars: 1 };
  452. }
  453. function CoverImage({ url, printName }: { url: string | null; printName?: string }) {
  454. const [loaded, setLoaded] = useState(false);
  455. const [error, setError] = useState(false);
  456. const [showOverlay, setShowOverlay] = useState(false);
  457. return (
  458. <>
  459. <div
  460. className={`w-20 h-20 flex-shrink-0 rounded-lg overflow-hidden bg-bambu-dark-tertiary flex items-center justify-center ${url && loaded ? 'cursor-pointer' : ''}`}
  461. onClick={() => url && loaded && setShowOverlay(true)}
  462. >
  463. {url && !error ? (
  464. <>
  465. <img
  466. src={url}
  467. alt="Print preview"
  468. className={`w-full h-full object-cover ${loaded ? 'block' : 'hidden'}`}
  469. onLoad={() => setLoaded(true)}
  470. onError={() => setError(true)}
  471. />
  472. {!loaded && <Box className="w-8 h-8 text-bambu-gray" />}
  473. </>
  474. ) : (
  475. <Box className="w-8 h-8 text-bambu-gray" />
  476. )}
  477. </div>
  478. {/* Cover Image Overlay */}
  479. {showOverlay && url && (
  480. <div
  481. className="fixed inset-0 bg-black/80 flex items-center justify-center z-50 p-8"
  482. onClick={() => setShowOverlay(false)}
  483. >
  484. <div className="relative max-w-2xl max-h-full">
  485. <img
  486. src={url}
  487. alt="Print preview"
  488. className="max-w-full max-h-[80vh] rounded-lg shadow-2xl"
  489. />
  490. {printName && (
  491. <p className="text-white text-center mt-4 text-lg">{printName}</p>
  492. )}
  493. </div>
  494. </div>
  495. )}
  496. </>
  497. );
  498. }
  499. interface PrinterMaintenanceInfo {
  500. due_count: number;
  501. warning_count: number;
  502. total_print_hours: number;
  503. }
  504. // Status summary bar component - uses queryClient to read cached statuses
  505. function StatusSummaryBar({ printers }: { printers: Printer[] | undefined }) {
  506. const queryClient = useQueryClient();
  507. const counts = useMemo(() => {
  508. let printing = 0;
  509. let idle = 0;
  510. let offline = 0;
  511. let loading = 0;
  512. printers?.forEach((printer) => {
  513. const status = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', printer.id]);
  514. if (status === undefined) {
  515. // Status not yet loaded - don't count as offline yet
  516. loading++;
  517. } else if (!status.connected) {
  518. offline++;
  519. } else if (status.state === 'RUNNING') {
  520. printing++;
  521. } else {
  522. idle++;
  523. }
  524. });
  525. return { printing, idle, offline, loading, total: (printers?.length || 0) };
  526. }, [printers, queryClient]);
  527. // Subscribe to query cache changes to re-render when status updates
  528. // Throttled to prevent rapid re-renders from causing tab crashes
  529. const [, setTick] = useState(0);
  530. useEffect(() => {
  531. let pending = false;
  532. const unsubscribe = queryClient.getQueryCache().subscribe(() => {
  533. if (!pending) {
  534. pending = true;
  535. requestAnimationFrame(() => {
  536. setTick(t => t + 1);
  537. pending = false;
  538. });
  539. }
  540. });
  541. return () => unsubscribe();
  542. }, [queryClient]);
  543. if (!printers?.length) return null;
  544. return (
  545. <div className="flex items-center gap-4 text-sm">
  546. {counts.printing > 0 && (
  547. <div className="flex items-center gap-1.5">
  548. <div className="w-2 h-2 rounded-full bg-bambu-green animate-pulse" />
  549. <span className="text-bambu-gray">
  550. <span className="text-white font-medium">{counts.printing}</span> printing
  551. </span>
  552. </div>
  553. )}
  554. {counts.idle > 0 && (
  555. <div className="flex items-center gap-1.5">
  556. <div className="w-2 h-2 rounded-full bg-blue-400" />
  557. <span className="text-bambu-gray">
  558. <span className="text-white font-medium">{counts.idle}</span> idle
  559. </span>
  560. </div>
  561. )}
  562. {counts.offline > 0 && (
  563. <div className="flex items-center gap-1.5">
  564. <div className="w-2 h-2 rounded-full bg-gray-400" />
  565. <span className="text-bambu-gray">
  566. <span className="text-white font-medium">{counts.offline}</span> offline
  567. </span>
  568. </div>
  569. )}
  570. </div>
  571. );
  572. }
  573. type SortOption = 'name' | 'status' | 'model' | 'location';
  574. type ViewMode = 'expanded' | 'compact';
  575. /**
  576. * Get human-readable status display text for a printer.
  577. * Uses stg_cur_name for detailed calibration/preparation stages,
  578. * otherwise formats the gcode_state nicely.
  579. */
  580. function getStatusDisplay(state: string | null | undefined, stg_cur_name: string | null | undefined): string {
  581. // If we have a specific stage name (calibration, heating, etc.), use it
  582. if (stg_cur_name) {
  583. return stg_cur_name;
  584. }
  585. // Format the gcode_state nicely
  586. switch (state) {
  587. case 'RUNNING':
  588. return 'Printing';
  589. case 'PAUSE':
  590. return 'Paused';
  591. case 'FINISH':
  592. return 'Finished';
  593. case 'FAILED':
  594. return 'Failed';
  595. case 'IDLE':
  596. return 'Idle';
  597. default:
  598. return state ? state.charAt(0) + state.slice(1).toLowerCase() : 'Idle';
  599. }
  600. }
  601. function PrinterCard({
  602. printer,
  603. hideIfDisconnected,
  604. maintenanceInfo,
  605. viewMode = 'expanded',
  606. amsThresholds,
  607. }: {
  608. printer: Printer;
  609. hideIfDisconnected?: boolean;
  610. maintenanceInfo?: PrinterMaintenanceInfo;
  611. viewMode?: ViewMode;
  612. amsThresholds?: {
  613. humidityGood: number;
  614. humidityFair: number;
  615. tempGood: number;
  616. tempFair: number;
  617. };
  618. }) {
  619. const queryClient = useQueryClient();
  620. const navigate = useNavigate();
  621. const { showToast } = useToast();
  622. const [showMenu, setShowMenu] = useState(false);
  623. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  624. const [deleteArchives, setDeleteArchives] = useState(true);
  625. const [showEditModal, setShowEditModal] = useState(false);
  626. const [showFileManager, setShowFileManager] = useState(false);
  627. const [showMQTTDebug, setShowMQTTDebug] = useState(false);
  628. const [showPowerOnConfirm, setShowPowerOnConfirm] = useState(false);
  629. const [showPowerOffConfirm, setShowPowerOffConfirm] = useState(false);
  630. const [showHMSModal, setShowHMSModal] = useState(false);
  631. const [showStopConfirm, setShowStopConfirm] = useState(false);
  632. const [showPauseConfirm, setShowPauseConfirm] = useState(false);
  633. const [showResumeConfirm, setShowResumeConfirm] = useState(false);
  634. const [showSkipObjectsModal, setShowSkipObjectsModal] = useState(false);
  635. const [amsHistoryModal, setAmsHistoryModal] = useState<{
  636. amsId: number;
  637. amsLabel: string;
  638. mode: 'humidity' | 'temperature';
  639. } | null>(null);
  640. const { data: status } = useQuery({
  641. queryKey: ['printerStatus', printer.id],
  642. queryFn: () => api.getPrinterStatus(printer.id),
  643. refetchInterval: 30000, // Fallback polling, WebSocket handles real-time
  644. });
  645. // Collect unique tray_info_idx values for cloud filament info lookup
  646. const trayInfoIds = useMemo(() => {
  647. const ids = new Set<string>();
  648. if (status?.ams) {
  649. for (const ams of status.ams) {
  650. for (const tray of ams.tray || []) {
  651. if (tray.tray_info_idx) {
  652. ids.add(tray.tray_info_idx);
  653. }
  654. }
  655. }
  656. }
  657. if (status?.vt_tray?.tray_info_idx) {
  658. ids.add(status.vt_tray.tray_info_idx);
  659. }
  660. return Array.from(ids);
  661. }, [status?.ams, status?.vt_tray]);
  662. // Fetch cloud filament info for tooltips (name includes color, also has K value)
  663. const { data: filamentInfo } = useQuery({
  664. queryKey: ['filamentInfo', trayInfoIds],
  665. queryFn: () => api.getFilamentInfo(trayInfoIds),
  666. enabled: trayInfoIds.length > 0,
  667. staleTime: 5 * 60 * 1000, // 5 minutes
  668. });
  669. // Cache WiFi signal to prevent it disappearing on updates
  670. const [cachedWifiSignal, setCachedWifiSignal] = useState<number | null>(null);
  671. useEffect(() => {
  672. if (status?.wifi_signal != null) {
  673. setCachedWifiSignal(status.wifi_signal);
  674. }
  675. }, [status?.wifi_signal]);
  676. const wifiSignal = status?.wifi_signal ?? cachedWifiSignal;
  677. // Cache connected state to prevent flicker when status briefly becomes undefined
  678. const cachedConnected = useRef<boolean | undefined>(undefined);
  679. useEffect(() => {
  680. if (status?.connected !== undefined) {
  681. cachedConnected.current = status.connected;
  682. }
  683. }, [status?.connected]);
  684. const isConnected = status?.connected ?? cachedConnected.current;
  685. // Cache ams_extruder_map to prevent L/R indicators bouncing on updates
  686. const cachedAmsExtruderMap = useRef<Record<string, number>>({});
  687. useEffect(() => {
  688. if (status?.ams_extruder_map && Object.keys(status.ams_extruder_map).length > 0) {
  689. cachedAmsExtruderMap.current = status.ams_extruder_map;
  690. }
  691. }, [status?.ams_extruder_map]);
  692. const amsExtruderMap = (status?.ams_extruder_map && Object.keys(status.ams_extruder_map).length > 0)
  693. ? status.ams_extruder_map
  694. : cachedAmsExtruderMap.current;
  695. // Cache AMS data to prevent it disappearing on idle/offline printers
  696. const cachedAmsData = useRef<AMSUnit[]>([]);
  697. useEffect(() => {
  698. if (status?.ams && status.ams.length > 0) {
  699. cachedAmsData.current = status.ams;
  700. }
  701. }, [status?.ams]);
  702. const amsData = (status?.ams && status.ams.length > 0) ? status.ams : cachedAmsData.current;
  703. // Cache tray_now to prevent flickering when 255 (unloaded) or undefined values come in
  704. // Only update cache when we get a valid tray ID (0-253 or 254 for external)
  705. const cachedTrayNow = useRef<number>(255);
  706. const currentTrayNow = status?.tray_now;
  707. // Update cache synchronously during render if we have a valid value
  708. if (currentTrayNow !== undefined && currentTrayNow !== 255) {
  709. cachedTrayNow.current = currentTrayNow;
  710. }
  711. // Use cached value if current is 255/undefined but we had a valid value before
  712. const effectiveTrayNow = (currentTrayNow === undefined || currentTrayNow === 255)
  713. ? cachedTrayNow.current
  714. : currentTrayNow;
  715. // Fetch smart plug for this printer
  716. const { data: smartPlug } = useQuery({
  717. queryKey: ['smartPlugByPrinter', printer.id],
  718. queryFn: () => api.getSmartPlugByPrinter(printer.id),
  719. });
  720. // Fetch smart plug status if plug exists (faster refresh for energy monitoring)
  721. const { data: plugStatus } = useQuery({
  722. queryKey: ['smartPlugStatus', smartPlug?.id],
  723. queryFn: () => smartPlug ? api.getSmartPlugStatus(smartPlug.id) : null,
  724. enabled: !!smartPlug,
  725. refetchInterval: 10000, // 10 seconds for real-time power display
  726. });
  727. // Fetch queue count for this printer
  728. const { data: queueItems } = useQuery({
  729. queryKey: ['queue', printer.id, 'pending'],
  730. queryFn: () => api.getQueue(printer.id, 'pending'),
  731. });
  732. const queueCount = queueItems?.length || 0;
  733. // Fetch last completed print for this printer
  734. const { data: lastPrints } = useQuery({
  735. queryKey: ['archives', printer.id, 'last'],
  736. queryFn: () => api.getArchives(printer.id, 1, 0),
  737. enabled: status?.connected && status?.state !== 'RUNNING',
  738. });
  739. const lastPrint = lastPrints?.[0];
  740. // Determine if this card should be hidden (use cached connected state to prevent flicker)
  741. const shouldHide = hideIfDisconnected && isConnected === false;
  742. const deleteMutation = useMutation({
  743. mutationFn: (options: { deleteArchives: boolean }) =>
  744. api.deletePrinter(printer.id, options.deleteArchives),
  745. onSuccess: () => {
  746. queryClient.invalidateQueries({ queryKey: ['printers'] });
  747. queryClient.invalidateQueries({ queryKey: ['archives'] });
  748. },
  749. });
  750. const connectMutation = useMutation({
  751. mutationFn: () => api.connectPrinter(printer.id),
  752. onSuccess: () => {
  753. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  754. },
  755. });
  756. // Smart plug control mutations
  757. const powerControlMutation = useMutation({
  758. mutationFn: (action: 'on' | 'off') =>
  759. smartPlug ? api.controlSmartPlug(smartPlug.id, action) : Promise.reject('No plug'),
  760. onSuccess: () => {
  761. queryClient.invalidateQueries({ queryKey: ['smartPlugStatus', smartPlug?.id] });
  762. },
  763. });
  764. const toggleAutoOffMutation = useMutation({
  765. mutationFn: (enabled: boolean) =>
  766. smartPlug ? api.updateSmartPlug(smartPlug.id, { auto_off: enabled }) : Promise.reject('No plug'),
  767. onSuccess: () => {
  768. queryClient.invalidateQueries({ queryKey: ['smartPlugByPrinter', printer.id] });
  769. // Also invalidate the smart-plugs list to keep Settings page in sync
  770. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  771. },
  772. });
  773. // Print control mutations
  774. const stopPrintMutation = useMutation({
  775. mutationFn: () => api.stopPrint(printer.id),
  776. onSuccess: () => {
  777. showToast('Print stopped');
  778. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  779. },
  780. onError: (error: Error) => showToast(error.message || 'Failed to stop print', 'error'),
  781. });
  782. const pausePrintMutation = useMutation({
  783. mutationFn: () => api.pausePrint(printer.id),
  784. onSuccess: () => {
  785. showToast('Print paused');
  786. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  787. },
  788. onError: (error: Error) => showToast(error.message || 'Failed to pause print', 'error'),
  789. });
  790. const resumePrintMutation = useMutation({
  791. mutationFn: () => api.resumePrint(printer.id),
  792. onSuccess: () => {
  793. showToast('Print resumed');
  794. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  795. },
  796. onError: (error: Error) => showToast(error.message || 'Failed to resume print', 'error'),
  797. });
  798. // Query for printable objects (for skip functionality)
  799. // Fetch when printing with 2+ objects OR when modal is open
  800. const isPrintingWithObjects = (status?.state === 'RUNNING' || status?.state === 'PAUSE' || status?.state === 'PAUSED') && (status?.printable_objects_count ?? 0) >= 2;
  801. const { data: objectsData, refetch: refetchObjects } = useQuery({
  802. queryKey: ['printableObjects', printer.id],
  803. queryFn: () => api.getPrintableObjects(printer.id),
  804. enabled: showSkipObjectsModal || isPrintingWithObjects,
  805. refetchInterval: showSkipObjectsModal ? 5000 : (isPrintingWithObjects ? 30000 : false), // 5s when modal open, 30s otherwise
  806. });
  807. // Skip objects mutation
  808. const skipObjectsMutation = useMutation({
  809. mutationFn: (objectIds: number[]) => api.skipObjects(printer.id, objectIds),
  810. onSuccess: (data) => {
  811. showToast(data.message || 'Objects skipped');
  812. refetchObjects();
  813. },
  814. onError: (error: Error) => showToast(error.message || 'Failed to skip objects', 'error'),
  815. });
  816. // State for tracking which AMS slot is being refreshed
  817. const [refreshingSlot, setRefreshingSlot] = useState<{ amsId: number; slotId: number } | null>(null);
  818. // Track if we've seen the printer enter "busy" state (ams_status_main !== 0)
  819. const seenBusyStateRef = useRef<boolean>(false);
  820. // Fallback timeout ref
  821. const refreshTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  822. // Minimum display time passed
  823. const minTimePassedRef = useRef<boolean>(false);
  824. // AMS slot refresh mutation
  825. const refreshAmsSlotMutation = useMutation({
  826. mutationFn: ({ amsId, slotId }: { amsId: number; slotId: number }) =>
  827. api.refreshAmsSlot(printer.id, amsId, slotId),
  828. onMutate: ({ amsId, slotId }) => {
  829. // Clear any existing timeout
  830. if (refreshTimeoutRef.current) {
  831. clearTimeout(refreshTimeoutRef.current);
  832. }
  833. // Reset state
  834. seenBusyStateRef.current = false;
  835. minTimePassedRef.current = false;
  836. setRefreshingSlot({ amsId, slotId });
  837. // Minimum display time (2 seconds)
  838. setTimeout(() => {
  839. minTimePassedRef.current = true;
  840. }, 2000);
  841. // Fallback timeout (30 seconds max)
  842. refreshTimeoutRef.current = setTimeout(() => {
  843. setRefreshingSlot(null);
  844. }, 30000);
  845. },
  846. onSuccess: (data) => {
  847. showToast(data.message || 'RFID re-read initiated');
  848. },
  849. onError: (error: Error) => {
  850. showToast(error.message || 'Failed to re-read RFID', 'error');
  851. if (refreshTimeoutRef.current) {
  852. clearTimeout(refreshTimeoutRef.current);
  853. }
  854. setRefreshingSlot(null);
  855. },
  856. });
  857. // Watch ams_status_main to detect when RFID read completes
  858. // ams_status_main: 0=idle, 2=rfid_identifying
  859. const deferredClearRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  860. useEffect(() => {
  861. if (!refreshingSlot) return;
  862. const amsStatus = status?.ams_status_main ?? 0;
  863. // Track when we see non-idle state (printer is working)
  864. if (amsStatus !== 0) {
  865. seenBusyStateRef.current = true;
  866. // Cancel any deferred clear since we're back to busy
  867. if (deferredClearRef.current) {
  868. clearTimeout(deferredClearRef.current);
  869. deferredClearRef.current = null;
  870. }
  871. }
  872. // When we've seen busy and now idle, clear (with min time check)
  873. if (seenBusyStateRef.current && amsStatus === 0) {
  874. if (minTimePassedRef.current) {
  875. // Min time passed - clear now
  876. if (refreshTimeoutRef.current) {
  877. clearTimeout(refreshTimeoutRef.current);
  878. }
  879. setRefreshingSlot(null);
  880. } else {
  881. // Schedule clear after min time (2 seconds from start)
  882. if (!deferredClearRef.current) {
  883. deferredClearRef.current = setTimeout(() => {
  884. if (refreshTimeoutRef.current) {
  885. clearTimeout(refreshTimeoutRef.current);
  886. }
  887. setRefreshingSlot(null);
  888. }, 2000);
  889. }
  890. }
  891. }
  892. return () => {
  893. if (deferredClearRef.current) {
  894. clearTimeout(deferredClearRef.current);
  895. }
  896. };
  897. }, [status?.ams_status_main, refreshingSlot]);
  898. // State for AMS slot menu
  899. const [amsSlotMenu, setAmsSlotMenu] = useState<{ amsId: number; slotId: number } | null>(null);
  900. if (shouldHide) {
  901. return null;
  902. }
  903. return (
  904. <Card className="relative">
  905. <CardContent>
  906. {/* Header */}
  907. <div className={viewMode === 'compact' ? 'mb-2' : 'mb-4'}>
  908. {/* Top row: Image, Name, Menu */}
  909. <div className="flex items-start justify-between gap-2">
  910. <div className="flex items-center gap-3 min-w-0 flex-1">
  911. {/* Printer Model Image */}
  912. <img
  913. src={getPrinterImage(printer.model)}
  914. alt={printer.model || 'Printer'}
  915. className={`object-contain rounded-lg bg-bambu-dark flex-shrink-0 ${viewMode === 'compact' ? 'w-10 h-10' : 'w-14 h-14'}`}
  916. />
  917. <div className="min-w-0 flex-1">
  918. <div className="flex items-center gap-2">
  919. <h3 className={`font-semibold text-white ${viewMode === 'compact' ? 'text-base truncate' : 'text-lg'}`}>{printer.name}</h3>
  920. {/* Connection indicator dot for compact mode */}
  921. {viewMode === 'compact' && (
  922. <div
  923. className={`w-2 h-2 rounded-full flex-shrink-0 ${
  924. status?.connected ? 'bg-bambu-green' : 'bg-red-500'
  925. }`}
  926. title={status?.connected ? 'Connected' : 'Offline'}
  927. />
  928. )}
  929. </div>
  930. <p className="text-sm text-bambu-gray">
  931. {printer.model || 'Unknown Model'}
  932. {/* Nozzle Info - only in expanded */}
  933. {viewMode === 'expanded' && status?.nozzles && status.nozzles[0]?.nozzle_diameter && (
  934. <span className="ml-1.5 text-bambu-gray" title={status.nozzles[0].nozzle_type || 'Nozzle'}>
  935. • {status.nozzles[0].nozzle_diameter}mm
  936. </span>
  937. )}
  938. {viewMode === 'expanded' && maintenanceInfo && maintenanceInfo.total_print_hours > 0 && (
  939. <span className="ml-2 text-bambu-gray">
  940. <Clock className="w-3 h-3 inline-block mr-1" />
  941. {Math.round(maintenanceInfo.total_print_hours)}h
  942. </span>
  943. )}
  944. </p>
  945. </div>
  946. </div>
  947. {/* Menu button */}
  948. <div className="relative flex-shrink-0">
  949. <Button
  950. variant="ghost"
  951. size="sm"
  952. onClick={() => setShowMenu(!showMenu)}
  953. >
  954. <MoreVertical className="w-4 h-4" />
  955. </Button>
  956. {showMenu && (
  957. <div className="absolute right-0 mt-2 w-48 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg z-10">
  958. <button
  959. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  960. onClick={() => {
  961. setShowEditModal(true);
  962. setShowMenu(false);
  963. }}
  964. >
  965. <Pencil className="w-4 h-4" />
  966. Edit
  967. </button>
  968. <button
  969. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  970. onClick={() => {
  971. connectMutation.mutate();
  972. setShowMenu(false);
  973. }}
  974. >
  975. <RefreshCw className="w-4 h-4" />
  976. Reconnect
  977. </button>
  978. <button
  979. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  980. onClick={() => {
  981. setShowMQTTDebug(true);
  982. setShowMenu(false);
  983. }}
  984. >
  985. <Terminal className="w-4 h-4" />
  986. MQTT Debug
  987. </button>
  988. <button
  989. className="w-full px-4 py-2 text-left text-sm text-red-400 hover:bg-bambu-dark-tertiary flex items-center gap-2"
  990. onClick={() => {
  991. setShowDeleteConfirm(true);
  992. setShowMenu(false);
  993. }}
  994. >
  995. <Trash2 className="w-4 h-4" />
  996. Delete
  997. </button>
  998. </div>
  999. )}
  1000. </div>
  1001. </div>
  1002. {/* Badges row - only in expanded mode */}
  1003. {viewMode === 'expanded' && (
  1004. <div className="flex flex-wrap items-center gap-2 mt-2">
  1005. {/* Connection status badge */}
  1006. <span
  1007. className={`flex items-center gap-1.5 px-2 py-1 rounded-full text-xs ${
  1008. status?.connected
  1009. ? 'bg-bambu-green/20 text-bambu-green'
  1010. : 'bg-red-500/20 text-red-400'
  1011. }`}
  1012. >
  1013. {status?.connected ? (
  1014. <Link className="w-3 h-3" />
  1015. ) : (
  1016. <Unlink className="w-3 h-3" />
  1017. )}
  1018. {status?.connected ? 'Connected' : 'Offline'}
  1019. </span>
  1020. {/* WiFi signal strength indicator */}
  1021. {status?.connected && wifiSignal != null && (
  1022. <span
  1023. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs ${
  1024. wifiSignal >= -50
  1025. ? 'bg-bambu-green/20 text-bambu-green'
  1026. : wifiSignal >= -60
  1027. ? 'bg-bambu-green/20 text-bambu-green'
  1028. : wifiSignal >= -70
  1029. ? 'bg-amber-500/20 text-amber-600'
  1030. : wifiSignal >= -80
  1031. ? 'bg-orange-500/20 text-orange-600'
  1032. : 'bg-red-500/20 text-red-600'
  1033. }`}
  1034. title={`WiFi: ${wifiSignal} dBm - ${getWifiStrength(wifiSignal).label}`}
  1035. >
  1036. <Signal className="w-3 h-3" />
  1037. {wifiSignal}dBm
  1038. </span>
  1039. )}
  1040. {/* HMS Status Indicator */}
  1041. {status?.connected && (() => {
  1042. const knownErrors = status.hms_errors ? filterKnownHMSErrors(status.hms_errors) : [];
  1043. return (
  1044. <button
  1045. onClick={() => setShowHMSModal(true)}
  1046. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs cursor-pointer hover:opacity-80 transition-opacity ${
  1047. knownErrors.length > 0
  1048. ? knownErrors.some(e => e.severity <= 2)
  1049. ? 'bg-red-500/20 text-red-400'
  1050. : 'bg-orange-500/20 text-orange-400'
  1051. : 'bg-bambu-green/20 text-bambu-green'
  1052. }`}
  1053. title="Click to view HMS errors"
  1054. >
  1055. <AlertTriangle className="w-3 h-3" />
  1056. {knownErrors.length > 0 ? knownErrors.length : 'OK'}
  1057. </button>
  1058. );
  1059. })()}
  1060. {/* Maintenance Status Indicator */}
  1061. {maintenanceInfo && (
  1062. <button
  1063. onClick={() => navigate('/maintenance')}
  1064. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs cursor-pointer hover:opacity-80 transition-opacity ${
  1065. maintenanceInfo.due_count > 0
  1066. ? 'bg-red-500/20 text-red-400'
  1067. : maintenanceInfo.warning_count > 0
  1068. ? 'bg-orange-500/20 text-orange-400'
  1069. : 'bg-bambu-green/20 text-bambu-green'
  1070. }`}
  1071. title={
  1072. maintenanceInfo.due_count > 0 || maintenanceInfo.warning_count > 0
  1073. ? `${maintenanceInfo.due_count > 0 ? `${maintenanceInfo.due_count} maintenance due` : ''}${maintenanceInfo.due_count > 0 && maintenanceInfo.warning_count > 0 ? ', ' : ''}${maintenanceInfo.warning_count > 0 ? `${maintenanceInfo.warning_count} due soon` : ''} - Click to view`
  1074. : 'All maintenance up to date - Click to view'
  1075. }
  1076. >
  1077. <Wrench className="w-3 h-3" />
  1078. {maintenanceInfo.due_count > 0 || maintenanceInfo.warning_count > 0
  1079. ? maintenanceInfo.due_count + maintenanceInfo.warning_count
  1080. : 'OK'}
  1081. </button>
  1082. )}
  1083. {/* Queue Count Badge */}
  1084. {queueCount > 0 && (
  1085. <button
  1086. onClick={() => navigate('/queue')}
  1087. className="flex items-center gap-1 px-2 py-1 rounded-full text-xs bg-purple-500/20 text-purple-400 hover:opacity-80 transition-opacity"
  1088. title={`${queueCount} print${queueCount > 1 ? 's' : ''} in queue`}
  1089. >
  1090. <Layers className="w-3 h-3" />
  1091. {queueCount}
  1092. </button>
  1093. )}
  1094. </div>
  1095. )}
  1096. </div>
  1097. {/* Delete Confirmation */}
  1098. {showDeleteConfirm && (
  1099. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
  1100. <Card className="w-full max-w-md mx-4">
  1101. <CardContent>
  1102. <div className="flex items-start gap-3 mb-4">
  1103. <div className="p-2 rounded-full bg-red-500/20">
  1104. <AlertTriangle className="w-5 h-5 text-red-400" />
  1105. </div>
  1106. <div>
  1107. <h3 className="text-lg font-semibold text-white">Delete Printer</h3>
  1108. <p className="text-sm text-bambu-gray mt-1">
  1109. Are you sure you want to delete "{printer.name}"? This will remove all connection settings.
  1110. </p>
  1111. </div>
  1112. </div>
  1113. <div className="bg-bambu-dark rounded-lg p-3 mb-4">
  1114. <label className="flex items-start gap-3 cursor-pointer">
  1115. <input
  1116. type="checkbox"
  1117. checked={deleteArchives}
  1118. onChange={(e) => setDeleteArchives(e.target.checked)}
  1119. className="mt-0.5 w-4 h-4 rounded border-bambu-gray bg-bambu-dark-secondary text-bambu-green focus:ring-bambu-green focus:ring-offset-0"
  1120. />
  1121. <div>
  1122. <span className="text-sm text-white">Delete print archives</span>
  1123. <p className="text-xs text-bambu-gray mt-0.5">
  1124. {deleteArchives
  1125. ? 'All print history for this printer will be permanently deleted.'
  1126. : 'Print history will be kept but no longer associated with this printer.'}
  1127. </p>
  1128. </div>
  1129. </label>
  1130. </div>
  1131. <div className="flex justify-end gap-2">
  1132. <Button
  1133. variant="secondary"
  1134. onClick={() => {
  1135. setShowDeleteConfirm(false);
  1136. setDeleteArchives(true);
  1137. }}
  1138. >
  1139. Cancel
  1140. </Button>
  1141. <Button
  1142. variant="danger"
  1143. onClick={() => {
  1144. deleteMutation.mutate({ deleteArchives });
  1145. setShowDeleteConfirm(false);
  1146. setDeleteArchives(true);
  1147. }}
  1148. >
  1149. Delete
  1150. </Button>
  1151. </div>
  1152. </CardContent>
  1153. </Card>
  1154. </div>
  1155. )}
  1156. {/* Status */}
  1157. {status?.connected && (
  1158. <>
  1159. {/* Compact: Simple status bar */}
  1160. {viewMode === 'compact' ? (
  1161. <div className="mt-2">
  1162. {status.state === 'RUNNING' ? (
  1163. <div className="flex items-center gap-2">
  1164. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-1.5">
  1165. <div
  1166. className="bg-bambu-green h-1.5 rounded-full transition-all"
  1167. style={{ width: `${status.progress || 0}%` }}
  1168. />
  1169. </div>
  1170. <span className="text-xs text-white">{Math.round(status.progress || 0)}%</span>
  1171. </div>
  1172. ) : (
  1173. <p className="text-xs text-bambu-gray">{getStatusDisplay(status.state, status.stg_cur_name)}</p>
  1174. )}
  1175. </div>
  1176. ) : (
  1177. /* Expanded: Full status section */
  1178. <>
  1179. {/* Current Print or Idle Placeholder */}
  1180. <div className="mb-4 p-3 bg-bambu-dark rounded-lg relative">
  1181. {/* Skip Objects button - top right corner, always visible */}
  1182. <button
  1183. onClick={() => setShowSkipObjectsModal(true)}
  1184. disabled={!(status.state === 'RUNNING' || status.state === 'PAUSE' || status.state === 'PAUSED') || (status.printable_objects_count ?? 0) < 2}
  1185. className={`absolute top-2 right-2 p-1.5 rounded transition-colors z-10 ${
  1186. (status.state === 'RUNNING' || status.state === 'PAUSE' || status.state === 'PAUSED') && (status.printable_objects_count ?? 0) >= 2
  1187. ? 'text-bambu-gray hover:text-white hover:bg-white/10'
  1188. : 'text-bambu-gray/30 cursor-not-allowed'
  1189. }`}
  1190. title={
  1191. !(status.state === 'RUNNING' || status.state === 'PAUSE' || status.state === 'PAUSED')
  1192. ? "Skip objects (only while printing)"
  1193. : (status.printable_objects_count ?? 0) >= 2
  1194. ? "Skip objects"
  1195. : "Skip objects (requires 2+ objects)"
  1196. }
  1197. >
  1198. <SkipObjectsIcon className="w-4 h-4" />
  1199. {/* Badge showing skipped count */}
  1200. {objectsData && objectsData.skipped_count > 0 && (
  1201. <span className="absolute -top-1 -right-1 min-w-[16px] h-4 px-1 flex items-center justify-center text-[10px] font-bold bg-red-500 text-white rounded-full">
  1202. {objectsData.skipped_count}
  1203. </span>
  1204. )}
  1205. </button>
  1206. <div className="flex gap-3">
  1207. {/* Cover Image */}
  1208. <CoverImage
  1209. url={status.state === 'RUNNING' ? status.cover_url : null}
  1210. printName={status.state === 'RUNNING' ? (status.subtask_name || status.current_print || undefined) : undefined}
  1211. />
  1212. {/* Print Info */}
  1213. <div className="flex-1 min-w-0">
  1214. {status.current_print && status.state === 'RUNNING' ? (
  1215. <>
  1216. <p className="text-sm text-bambu-gray mb-1">{status.stg_cur_name || 'Printing'}</p>
  1217. <p className="text-white text-sm mb-2 truncate">
  1218. {status.subtask_name || status.current_print}
  1219. </p>
  1220. <div className="flex items-center justify-between text-sm">
  1221. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-2 mr-3">
  1222. <div
  1223. className="bg-bambu-green h-2 rounded-full transition-all"
  1224. style={{ width: `${status.progress || 0}%` }}
  1225. />
  1226. </div>
  1227. <span className="text-white">{Math.round(status.progress || 0)}%</span>
  1228. </div>
  1229. <div className="flex items-center gap-3 mt-2 text-xs text-bambu-gray">
  1230. {status.remaining_time != null && status.remaining_time > 0 && (
  1231. <>
  1232. <span className="flex items-center gap-1">
  1233. <Clock className="w-3 h-3" />
  1234. {formatTime(status.remaining_time * 60)}
  1235. </span>
  1236. <span className="text-bambu-green font-medium" title="Estimated completion time">
  1237. ETA {formatETA(status.remaining_time)}
  1238. </span>
  1239. </>
  1240. )}
  1241. {status.layer_num != null && status.total_layers != null && status.total_layers > 0 && (
  1242. <span className="flex items-center gap-1">
  1243. <Layers className="w-3 h-3" />
  1244. {status.layer_num}/{status.total_layers}
  1245. </span>
  1246. )}
  1247. </div>
  1248. </>
  1249. ) : (
  1250. <>
  1251. <p className="text-sm text-bambu-gray mb-1">Status</p>
  1252. <p className="text-white text-sm mb-2">
  1253. {getStatusDisplay(status.state, status.stg_cur_name)}
  1254. </p>
  1255. <div className="flex items-center justify-between text-sm">
  1256. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-2 mr-3">
  1257. <div className="bg-bambu-dark-tertiary h-2 rounded-full" />
  1258. </div>
  1259. <span className="text-bambu-gray">—</span>
  1260. </div>
  1261. {lastPrint ? (
  1262. <p className="text-xs text-bambu-gray mt-2 truncate" title={lastPrint.print_name || lastPrint.filename}>
  1263. Last: {lastPrint.print_name || lastPrint.filename}
  1264. {lastPrint.completed_at && (
  1265. <span className="ml-1 text-bambu-gray/60">
  1266. • {new Date(lastPrint.completed_at).toLocaleDateString([], { month: 'short', day: 'numeric' })}
  1267. </span>
  1268. )}
  1269. </p>
  1270. ) : (
  1271. <p className="text-xs text-bambu-gray mt-2">Ready to print</p>
  1272. )}
  1273. </>
  1274. )}
  1275. </div>
  1276. </div>
  1277. </div>
  1278. {/* Queue Widget - shows next scheduled print */}
  1279. {status.state !== 'RUNNING' && (
  1280. <PrinterQueueWidget printerId={printer.id} />
  1281. )}
  1282. </>
  1283. )}
  1284. {/* Temperatures + Print Controls */}
  1285. {status.temperatures && viewMode === 'expanded' && (() => {
  1286. // Use actual heater states from MQTT stream
  1287. const nozzleHeating = status.temperatures.nozzle_heating || status.temperatures.nozzle_2_heating || false;
  1288. const bedHeating = status.temperatures.bed_heating || false;
  1289. const chamberHeating = status.temperatures.chamber_heating || false;
  1290. // Determine print state for control buttons
  1291. const isRunning = status.state === 'RUNNING';
  1292. const isPaused = status.state === 'PAUSED' || status.state === 'PAUSE';
  1293. const isPrinting = isRunning || isPaused;
  1294. const isControlBusy = stopPrintMutation.isPending || pausePrintMutation.isPending || resumePrintMutation.isPending;
  1295. return (
  1296. <div className="flex gap-3">
  1297. {/* Temperature cards */}
  1298. <div className="flex-1 grid grid-cols-3 gap-2">
  1299. {/* Nozzle temp - combined for dual nozzle */}
  1300. <div className="text-center p-2 bg-bambu-dark rounded-lg">
  1301. <HeaterThermometer className="w-4 h-4 mx-auto mb-1" color="text-orange-400" isHeating={nozzleHeating} />
  1302. {status.temperatures.nozzle_2 !== undefined ? (
  1303. <>
  1304. <p className="text-[10px] text-bambu-gray">L / R</p>
  1305. <p className="text-xs text-white">
  1306. {Math.round(status.temperatures.nozzle || 0)}° / {Math.round(status.temperatures.nozzle_2 || 0)}°
  1307. </p>
  1308. </>
  1309. ) : (
  1310. <>
  1311. <p className="text-[10px] text-bambu-gray">Nozzle</p>
  1312. <p className="text-xs text-white">
  1313. {Math.round(status.temperatures.nozzle || 0)}°C
  1314. </p>
  1315. </>
  1316. )}
  1317. </div>
  1318. <div className="text-center p-2 bg-bambu-dark rounded-lg">
  1319. <HeaterThermometer className="w-4 h-4 mx-auto mb-1" color="text-blue-400" isHeating={bedHeating} />
  1320. <p className="text-[10px] text-bambu-gray">Bed</p>
  1321. <p className="text-xs text-white">
  1322. {Math.round(status.temperatures.bed || 0)}°C
  1323. </p>
  1324. </div>
  1325. {status.temperatures.chamber !== undefined ? (
  1326. <div className="text-center p-2 bg-bambu-dark rounded-lg">
  1327. <HeaterThermometer className="w-4 h-4 mx-auto mb-1" color="text-green-400" isHeating={chamberHeating} />
  1328. <p className="text-[10px] text-bambu-gray">Chamber</p>
  1329. <p className="text-xs text-white">
  1330. {Math.round(status.temperatures.chamber || 0)}°C
  1331. </p>
  1332. </div>
  1333. ) : (
  1334. <div /> /* Empty placeholder to maintain grid */
  1335. )}
  1336. </div>
  1337. {/* Print control buttons */}
  1338. <div className="flex flex-col justify-center gap-1.5 w-20">
  1339. {/* Stop button - visible when printing/paused */}
  1340. <button
  1341. onClick={() => setShowStopConfirm(true)}
  1342. disabled={!isPrinting || isControlBusy}
  1343. className={`
  1344. flex items-center justify-center gap-1 px-2 py-1.5 rounded-lg text-xs font-medium
  1345. transition-colors
  1346. ${isPrinting
  1347. ? 'bg-red-500/20 text-red-400 hover:bg-red-500/30'
  1348. : 'bg-bambu-dark text-bambu-gray/50 cursor-not-allowed'
  1349. }
  1350. `}
  1351. title="Stop print"
  1352. >
  1353. <Square className="w-3 h-3" />
  1354. Stop
  1355. </button>
  1356. {/* Pause/Resume button */}
  1357. <button
  1358. onClick={() => isPaused ? setShowResumeConfirm(true) : setShowPauseConfirm(true)}
  1359. disabled={!isPrinting || isControlBusy}
  1360. className={`
  1361. flex items-center justify-center gap-1 px-2 py-1.5 rounded-lg text-xs font-medium
  1362. transition-colors
  1363. ${isPrinting
  1364. ? isPaused
  1365. ? 'bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30'
  1366. : 'bg-yellow-500/20 text-yellow-400 hover:bg-yellow-500/30'
  1367. : 'bg-bambu-dark text-bambu-gray/50 cursor-not-allowed'
  1368. }
  1369. `}
  1370. title={isPaused ? 'Resume print' : 'Pause print'}
  1371. >
  1372. {isPaused ? <Play className="w-3 h-3" /> : <Pause className="w-3 h-3" />}
  1373. {isPaused ? 'Resume' : 'Pause'}
  1374. </button>
  1375. </div>
  1376. </div>
  1377. );
  1378. })()}
  1379. {/* AMS Units - 2-Column Grid Layout */}
  1380. {amsData && amsData.length > 0 && viewMode === 'expanded' && (() => {
  1381. // Separate regular AMS (4-tray) from HT AMS (1-tray)
  1382. const regularAms = amsData.filter(ams => ams.tray.length > 1);
  1383. const htAms = amsData.filter(ams => ams.tray.length === 1);
  1384. const isDualNozzle = printer.nozzle_count === 2 || status?.temperatures?.nozzle_2 !== undefined;
  1385. return (
  1386. <div className="mt-4 pt-3 border-t border-bambu-dark-tertiary/50">
  1387. {/* Section Header */}
  1388. <div className="flex items-center gap-2 mb-3">
  1389. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  1390. Filaments
  1391. </span>
  1392. <div className="flex-1 h-px bg-bambu-dark-tertiary/30" />
  1393. </div>
  1394. {/* AMS Content */}
  1395. <div className="space-y-3">
  1396. {/* Row 1-2: Regular AMS (4-tray) in 2-column grid */}
  1397. {regularAms.length > 0 && (
  1398. <div className="grid grid-cols-2 gap-3">
  1399. {regularAms.map((ams) => {
  1400. const mappedExtruderId = amsExtruderMap[String(ams.id)];
  1401. const normalizedId = ams.id >= 128 ? ams.id - 128 : ams.id;
  1402. const extruderId = mappedExtruderId !== undefined ? mappedExtruderId : normalizedId;
  1403. const isLeftNozzle = extruderId === 1;
  1404. const isRightNozzle = extruderId === 0;
  1405. return (
  1406. <div key={ams.id} className="p-2.5 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary/30">
  1407. {/* Header: Label + Stats (no icon) */}
  1408. <div className="flex items-center justify-between mb-2">
  1409. <div className="flex items-center gap-1.5">
  1410. <span className="text-[10px] text-white font-medium">
  1411. {getAmsLabel(ams.id, ams.tray.length)}
  1412. </span>
  1413. {isDualNozzle && (isLeftNozzle || isRightNozzle) && (
  1414. <NozzleBadge side={isLeftNozzle ? 'L' : 'R'} />
  1415. )}
  1416. </div>
  1417. {(ams.humidity != null || ams.temp != null) && (
  1418. <div className="flex items-center gap-1.5">
  1419. {ams.humidity != null && (
  1420. <HumidityIndicator
  1421. humidity={ams.humidity}
  1422. goodThreshold={amsThresholds?.humidityGood}
  1423. fairThreshold={amsThresholds?.humidityFair}
  1424. onClick={() => setAmsHistoryModal({
  1425. amsId: ams.id,
  1426. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1427. mode: 'humidity',
  1428. })}
  1429. compact
  1430. />
  1431. )}
  1432. {ams.temp != null && (
  1433. <TemperatureIndicator
  1434. temp={ams.temp}
  1435. goodThreshold={amsThresholds?.tempGood}
  1436. fairThreshold={amsThresholds?.tempFair}
  1437. onClick={() => setAmsHistoryModal({
  1438. amsId: ams.id,
  1439. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1440. mode: 'temperature',
  1441. })}
  1442. compact
  1443. />
  1444. )}
  1445. </div>
  1446. )}
  1447. </div>
  1448. {/* Slots grid: 4 columns - always render 4 slots */}
  1449. <div className="grid grid-cols-4 gap-1.5">
  1450. {[0, 1, 2, 3].map((slotIdx) => {
  1451. // Find tray data for this slot (may be undefined if data incomplete)
  1452. // Use array index if available, as tray.id may not always be set
  1453. const tray = ams.tray[slotIdx] || ams.tray.find(t => t.id === slotIdx);
  1454. const hasFillLevel = tray?.tray_type && tray.remain >= 0;
  1455. const isEmpty = !tray?.tray_type;
  1456. // Check if this is the currently loaded tray
  1457. // Global tray ID = ams.id * 4 + slot index (for standard AMS)
  1458. const globalTrayId = ams.id * 4 + slotIdx;
  1459. const isActive = effectiveTrayNow === globalTrayId;
  1460. // Get cloud preset info if available
  1461. const cloudInfo = tray?.tray_info_idx ? filamentInfo?.[tray.tray_info_idx] : null;
  1462. // Build filament data for hover card
  1463. const filamentData = tray?.tray_type ? {
  1464. vendor: (tray.tray_uuid ? 'Bambu Lab' : 'Generic') as 'Bambu Lab' | 'Generic',
  1465. profile: cloudInfo?.name || tray.tray_sub_brands || tray.tray_type,
  1466. colorName: getBambuColorName(tray.tray_id_name) || hexToBasicColorName(tray.tray_color),
  1467. colorHex: tray.tray_color || null,
  1468. kFactor: formatKValue(tray.k),
  1469. fillLevel: hasFillLevel ? tray.remain : null,
  1470. } : null;
  1471. // Check if this specific slot is being refreshed
  1472. const isRefreshing = refreshingSlot?.amsId === ams.id &&
  1473. refreshingSlot?.slotId === slotIdx;
  1474. // Slot visual content (goes inside hover card)
  1475. const slotVisual = (
  1476. <div
  1477. className={`bg-bambu-dark-tertiary rounded p-1 text-center ${isEmpty ? 'opacity-50' : ''} ${isActive ? 'ring-2 ring-bambu-green ring-offset-1 ring-offset-bambu-dark' : ''}`}
  1478. >
  1479. <div
  1480. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2"
  1481. style={{
  1482. backgroundColor: tray?.tray_color ? `#${tray.tray_color}` : (tray?.tray_type ? '#333' : 'transparent'),
  1483. borderColor: isEmpty ? '#666' : 'rgba(255,255,255,0.1)',
  1484. borderStyle: isEmpty ? 'dashed' : 'solid',
  1485. }}
  1486. />
  1487. <div className="text-[9px] text-white font-bold truncate">
  1488. {tray?.tray_type || '—'}
  1489. </div>
  1490. {/* Fill bar */}
  1491. <div className="mt-1 h-1.5 bg-black/30 rounded-full overflow-hidden">
  1492. {hasFillLevel && tray ? (
  1493. <div
  1494. className="h-full rounded-full transition-all"
  1495. style={{
  1496. width: `${tray.remain}%`,
  1497. backgroundColor: getFillBarColor(tray.remain),
  1498. }}
  1499. />
  1500. ) : tray?.tray_type ? (
  1501. <div className="h-full w-full rounded-full bg-white/50 dark:bg-gray-500/40" />
  1502. ) : null}
  1503. </div>
  1504. </div>
  1505. );
  1506. // Wrapper with menu button, dropdown, and loading overlay (outside hover card)
  1507. return (
  1508. <div key={slotIdx} className="relative group">
  1509. {/* Loading overlay during RFID re-read */}
  1510. {isRefreshing && (
  1511. <div className="absolute inset-0 bg-bambu-dark-tertiary/80 rounded flex items-center justify-center z-20">
  1512. <RefreshCw className="w-4 h-4 text-bambu-green animate-spin" />
  1513. </div>
  1514. )}
  1515. {/* Menu button - appears on hover, hidden when printer busy */}
  1516. {status?.state !== 'RUNNING' && (
  1517. <button
  1518. onClick={(e) => {
  1519. e.stopPropagation();
  1520. setAmsSlotMenu(
  1521. amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === slotIdx
  1522. ? null
  1523. : { amsId: ams.id, slotId: slotIdx }
  1524. );
  1525. }}
  1526. className="absolute -top-1 -right-1 w-4 h-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity z-10 hover:bg-bambu-dark-tertiary"
  1527. title="Slot options"
  1528. >
  1529. <MoreVertical className="w-2.5 h-2.5 text-bambu-gray" />
  1530. </button>
  1531. )}
  1532. {/* Dropdown menu */}
  1533. {status?.state !== 'RUNNING' && amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === slotIdx && (
  1534. <div className="absolute top-full left-0 mt-1 z-50 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]">
  1535. <button
  1536. className="w-full px-3 py-1.5 text-left text-xs text-white hover:bg-bambu-dark-tertiary flex items-center gap-2"
  1537. onClick={(e) => {
  1538. e.stopPropagation();
  1539. refreshAmsSlotMutation.mutate({ amsId: ams.id, slotId: slotIdx });
  1540. setAmsSlotMenu(null);
  1541. }}
  1542. disabled={isRefreshing}
  1543. >
  1544. <RefreshCw className={`w-3 h-3 ${isRefreshing ? 'animate-spin' : ''}`} />
  1545. Re-read RFID
  1546. </button>
  1547. </div>
  1548. )}
  1549. {/* Hover card wraps only the visual content */}
  1550. {filamentData ? (
  1551. <FilamentHoverCard data={filamentData}>
  1552. {slotVisual}
  1553. </FilamentHoverCard>
  1554. ) : (
  1555. <EmptySlotHoverCard>
  1556. {slotVisual}
  1557. </EmptySlotHoverCard>
  1558. )}
  1559. </div>
  1560. );
  1561. })}
  1562. </div>
  1563. </div>
  1564. );
  1565. })}
  1566. </div>
  1567. )}
  1568. {/* Row 3: HT AMS + External spools (same style as regular AMS, 4 across) */}
  1569. {(htAms.length > 0 || (status.vt_tray && status.vt_tray.tray_type)) && (
  1570. <div className="grid grid-cols-4 gap-3">
  1571. {/* HT AMS units - name/badge top, slot left, stats right */}
  1572. {htAms.map((ams) => {
  1573. const mappedExtruderId = amsExtruderMap[String(ams.id)];
  1574. const normalizedId = ams.id >= 128 ? ams.id - 128 : ams.id;
  1575. const extruderId = mappedExtruderId !== undefined ? mappedExtruderId : normalizedId;
  1576. const isLeftNozzle = extruderId === 1;
  1577. const isRightNozzle = extruderId === 0;
  1578. const tray = ams.tray[0];
  1579. const hasFillLevel = tray?.tray_type && tray.remain >= 0;
  1580. const isEmpty = !tray?.tray_type;
  1581. // Check if this is the currently loaded tray
  1582. // Global tray ID = ams.id * 4 + tray.id
  1583. const globalTrayId = ams.id * 4 + (tray?.id ?? 0);
  1584. const isActive = effectiveTrayNow === globalTrayId;
  1585. // Get cloud preset info if available
  1586. const cloudInfo = tray?.tray_info_idx ? filamentInfo?.[tray.tray_info_idx] : null;
  1587. // Build filament data for hover card
  1588. const filamentData = tray?.tray_type ? {
  1589. vendor: (tray.tray_uuid ? 'Bambu Lab' : 'Generic') as 'Bambu Lab' | 'Generic',
  1590. profile: cloudInfo?.name || tray.tray_sub_brands || tray.tray_type,
  1591. colorName: getBambuColorName(tray.tray_id_name) || hexToBasicColorName(tray.tray_color),
  1592. colorHex: tray.tray_color || null,
  1593. kFactor: formatKValue(tray.k),
  1594. fillLevel: hasFillLevel ? tray.remain : null,
  1595. } : null;
  1596. const htSlotId = tray?.id ?? 0;
  1597. // Check if this specific slot is being refreshed
  1598. const isHtRefreshing = refreshingSlot?.amsId === ams.id &&
  1599. refreshingSlot?.slotId === htSlotId;
  1600. // Slot visual content (goes inside hover card)
  1601. const slotVisual = (
  1602. <div
  1603. className={`bg-bambu-dark-tertiary rounded p-1 text-center ${isEmpty ? 'opacity-50' : ''} ${isActive ? 'ring-2 ring-bambu-green ring-offset-1 ring-offset-bambu-dark' : ''}`}
  1604. >
  1605. <div
  1606. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2"
  1607. style={{
  1608. backgroundColor: tray?.tray_color ? `#${tray.tray_color}` : (tray?.tray_type ? '#333' : 'transparent'),
  1609. borderColor: isEmpty ? '#666' : 'rgba(255,255,255,0.1)',
  1610. borderStyle: isEmpty ? 'dashed' : 'solid',
  1611. }}
  1612. />
  1613. <div className="text-[9px] text-white font-bold truncate">
  1614. {tray?.tray_type || '—'}
  1615. </div>
  1616. {/* Fill bar */}
  1617. <div className="mt-1 h-1.5 bg-black/30 rounded-full overflow-hidden">
  1618. {hasFillLevel ? (
  1619. <div
  1620. className="h-full rounded-full transition-all"
  1621. style={{
  1622. width: `${tray.remain}%`,
  1623. backgroundColor: getFillBarColor(tray.remain),
  1624. }}
  1625. />
  1626. ) : tray?.tray_type ? (
  1627. <div className="h-full w-full rounded-full bg-white/50 dark:bg-gray-500/40" />
  1628. ) : null}
  1629. </div>
  1630. </div>
  1631. );
  1632. return (
  1633. <div key={ams.id} className="p-2.5 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary/30">
  1634. {/* Row 1: Label + Nozzle */}
  1635. <div className="flex items-center gap-1 mb-2">
  1636. <span className="text-[10px] text-white font-medium">
  1637. {getAmsLabel(ams.id, ams.tray.length)}
  1638. </span>
  1639. {isDualNozzle && (isLeftNozzle || isRightNozzle) && (
  1640. <NozzleBadge side={isLeftNozzle ? 'L' : 'R'} />
  1641. )}
  1642. </div>
  1643. {/* Row 2: Slot (left) + Stats (right stacked) */}
  1644. <div className="flex gap-1.5">
  1645. {/* Slot wrapper with menu button, dropdown, and loading overlay */}
  1646. <div className="relative group flex-1">
  1647. {/* Loading overlay during RFID re-read */}
  1648. {isHtRefreshing && (
  1649. <div className="absolute inset-0 bg-bambu-dark-tertiary/80 rounded flex items-center justify-center z-20">
  1650. <RefreshCw className="w-4 h-4 text-bambu-green animate-spin" />
  1651. </div>
  1652. )}
  1653. {/* Menu button - appears on hover, hidden when printer busy */}
  1654. {status?.state !== 'RUNNING' && (
  1655. <button
  1656. onClick={(e) => {
  1657. e.stopPropagation();
  1658. setAmsSlotMenu(
  1659. amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === htSlotId
  1660. ? null
  1661. : { amsId: ams.id, slotId: htSlotId }
  1662. );
  1663. }}
  1664. className="absolute -top-1 -right-1 w-4 h-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity z-10 hover:bg-bambu-dark-tertiary"
  1665. title="Slot options"
  1666. >
  1667. <MoreVertical className="w-2.5 h-2.5 text-bambu-gray" />
  1668. </button>
  1669. )}
  1670. {/* Dropdown menu */}
  1671. {status?.state !== 'RUNNING' && amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === htSlotId && (
  1672. <div className="absolute top-full left-0 mt-1 z-50 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]">
  1673. <button
  1674. className="w-full px-3 py-1.5 text-left text-xs text-white hover:bg-bambu-dark-tertiary flex items-center gap-2"
  1675. onClick={(e) => {
  1676. e.stopPropagation();
  1677. refreshAmsSlotMutation.mutate({ amsId: ams.id, slotId: htSlotId });
  1678. setAmsSlotMenu(null);
  1679. }}
  1680. disabled={isHtRefreshing}
  1681. >
  1682. <RefreshCw className={`w-3 h-3 ${isHtRefreshing ? 'animate-spin' : ''}`} />
  1683. Re-read RFID
  1684. </button>
  1685. </div>
  1686. )}
  1687. {/* Hover card wraps only the visual content */}
  1688. {filamentData ? (
  1689. <FilamentHoverCard data={filamentData}>
  1690. {slotVisual}
  1691. </FilamentHoverCard>
  1692. ) : (
  1693. <EmptySlotHoverCard>
  1694. {slotVisual}
  1695. </EmptySlotHoverCard>
  1696. )}
  1697. </div>
  1698. {/* Stats stacked vertically: Temp on top, Humidity below */}
  1699. {(ams.humidity != null || ams.temp != null) && (
  1700. <div className="flex flex-col justify-center gap-1 shrink-0">
  1701. {ams.temp != null && (
  1702. <TemperatureIndicator
  1703. temp={ams.temp}
  1704. goodThreshold={amsThresholds?.tempGood}
  1705. fairThreshold={amsThresholds?.tempFair}
  1706. onClick={() => setAmsHistoryModal({
  1707. amsId: ams.id,
  1708. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1709. mode: 'temperature',
  1710. })}
  1711. compact
  1712. />
  1713. )}
  1714. {ams.humidity != null && (
  1715. <HumidityIndicator
  1716. humidity={ams.humidity}
  1717. goodThreshold={amsThresholds?.humidityGood}
  1718. fairThreshold={amsThresholds?.humidityFair}
  1719. onClick={() => setAmsHistoryModal({
  1720. amsId: ams.id,
  1721. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1722. mode: 'humidity',
  1723. })}
  1724. compact
  1725. />
  1726. )}
  1727. </div>
  1728. )}
  1729. </div>
  1730. </div>
  1731. );
  1732. })}
  1733. {/* External spool - name top, slot below (no stats) */}
  1734. {status.vt_tray && status.vt_tray.tray_type && (() => {
  1735. const extTray = status.vt_tray;
  1736. // Check if external spool is active (tray_now = 254)
  1737. const isExtActive = effectiveTrayNow === 254;
  1738. // Get cloud preset info if available
  1739. const extCloudInfo = extTray.tray_info_idx ? filamentInfo?.[extTray.tray_info_idx] : null;
  1740. // Build filament data for hover card
  1741. const extFilamentData = {
  1742. vendor: (extTray.tray_uuid ? 'Bambu Lab' : 'Generic') as 'Bambu Lab' | 'Generic',
  1743. profile: extCloudInfo?.name || extTray.tray_sub_brands || extTray.tray_type || 'Unknown',
  1744. colorName: getBambuColorName(extTray.tray_id_name) || hexToBasicColorName(extTray.tray_color),
  1745. colorHex: extTray.tray_color || null,
  1746. kFactor: formatKValue(extTray.k),
  1747. fillLevel: null, // External spool has unknown fill level
  1748. };
  1749. const extSlotContent = (
  1750. <div className={`bg-bambu-dark-tertiary rounded p-1 text-center cursor-default ${isExtActive ? 'ring-2 ring-bambu-green ring-offset-1 ring-offset-bambu-dark' : ''}`}>
  1751. <div
  1752. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2"
  1753. style={{
  1754. backgroundColor: extTray.tray_color ? `#${extTray.tray_color}` : '#333',
  1755. borderColor: isExtActive ? 'var(--accent)' : 'rgba(255,255,255,0.1)',
  1756. }}
  1757. />
  1758. <div className="text-[9px] text-white font-bold truncate">
  1759. {extTray.tray_type || 'Spool'}
  1760. </div>
  1761. {/* Unknown fill level - subtle bar */}
  1762. <div className="mt-1 h-1.5 bg-black/30 rounded-full overflow-hidden">
  1763. <div className="h-full w-full rounded-full bg-white/50 dark:bg-gray-500/40" />
  1764. </div>
  1765. </div>
  1766. );
  1767. return (
  1768. <div className="p-2.5 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary/30">
  1769. {/* Row 1: Label */}
  1770. <div className="flex items-center gap-1 mb-2">
  1771. <span className="text-[10px] text-white font-medium">External</span>
  1772. </div>
  1773. {/* Row 2: Slot (full width since no stats) */}
  1774. <FilamentHoverCard data={extFilamentData}>
  1775. {extSlotContent}
  1776. </FilamentHoverCard>
  1777. </div>
  1778. );
  1779. })()}
  1780. </div>
  1781. )}
  1782. </div>
  1783. </div>
  1784. );
  1785. })()}
  1786. </>
  1787. )}
  1788. {/* Smart Plug Controls - hidden in compact mode */}
  1789. {smartPlug && viewMode === 'expanded' && (
  1790. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary">
  1791. <div className="flex items-center gap-3">
  1792. {/* Plug name and status */}
  1793. <div className="flex items-center gap-2 min-w-0">
  1794. <Zap className="w-4 h-4 text-bambu-gray flex-shrink-0" />
  1795. <span className="text-sm text-white truncate">{smartPlug.name}</span>
  1796. {plugStatus && (
  1797. <span
  1798. className={`text-xs px-1.5 py-0.5 rounded flex-shrink-0 ${
  1799. plugStatus.state === 'ON'
  1800. ? 'bg-bambu-green/20 text-bambu-green'
  1801. : plugStatus.state === 'OFF'
  1802. ? 'bg-red-500/20 text-red-400'
  1803. : 'bg-bambu-gray/20 text-bambu-gray'
  1804. }`}
  1805. >
  1806. {plugStatus.state || '?'}
  1807. </span>
  1808. )}
  1809. {/* Power consumption display */}
  1810. {plugStatus?.energy?.power != null && plugStatus.state === 'ON' && (
  1811. <span className="text-xs text-yellow-400 font-medium flex-shrink-0">
  1812. {plugStatus.energy.power}W
  1813. </span>
  1814. )}
  1815. </div>
  1816. {/* Spacer */}
  1817. <div className="flex-1" />
  1818. {/* Power buttons */}
  1819. <div className="flex items-center gap-1">
  1820. <button
  1821. onClick={() => setShowPowerOnConfirm(true)}
  1822. disabled={powerControlMutation.isPending || plugStatus?.state === 'ON'}
  1823. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  1824. plugStatus?.state === 'ON'
  1825. ? 'bg-bambu-green text-white'
  1826. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  1827. }`}
  1828. >
  1829. <Power className="w-3 h-3" />
  1830. On
  1831. </button>
  1832. <button
  1833. onClick={() => setShowPowerOffConfirm(true)}
  1834. disabled={powerControlMutation.isPending || plugStatus?.state === 'OFF'}
  1835. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  1836. plugStatus?.state === 'OFF'
  1837. ? 'bg-red-500/30 text-red-400'
  1838. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  1839. }`}
  1840. >
  1841. <PowerOff className="w-3 h-3" />
  1842. Off
  1843. </button>
  1844. </div>
  1845. {/* Auto-off toggle */}
  1846. <div className="flex items-center gap-2 flex-shrink-0">
  1847. <span className={`text-xs hidden sm:inline ${smartPlug.auto_off_executed ? 'text-bambu-green' : 'text-bambu-gray'}`}>
  1848. {smartPlug.auto_off_executed ? 'Auto-off done' : 'Auto-off'}
  1849. </span>
  1850. <button
  1851. onClick={() => toggleAutoOffMutation.mutate(!smartPlug.auto_off)}
  1852. disabled={toggleAutoOffMutation.isPending || smartPlug.auto_off_executed}
  1853. title={smartPlug.auto_off_executed ? 'Auto-off was executed - turn printer on to reset' : 'Auto power-off after print'}
  1854. className={`relative w-9 h-5 rounded-full transition-colors flex-shrink-0 ${
  1855. smartPlug.auto_off_executed
  1856. ? 'bg-bambu-green/50 cursor-not-allowed'
  1857. : smartPlug.auto_off ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  1858. }`}
  1859. >
  1860. <span
  1861. className={`absolute top-[2px] left-[2px] w-4 h-4 bg-white rounded-full transition-transform ${
  1862. smartPlug.auto_off || smartPlug.auto_off_executed ? 'translate-x-4' : 'translate-x-0'
  1863. }`}
  1864. />
  1865. </button>
  1866. </div>
  1867. </div>
  1868. </div>
  1869. )}
  1870. {/* Connection Info & Actions - hidden in compact mode */}
  1871. {viewMode === 'expanded' && (
  1872. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary flex items-center justify-between">
  1873. <div className="text-xs text-bambu-gray">
  1874. <p>{printer.ip_address}</p>
  1875. <p className="truncate">{printer.serial_number}</p>
  1876. </div>
  1877. <div className="flex items-center gap-2">
  1878. <Button
  1879. variant="secondary"
  1880. size="sm"
  1881. onClick={() => {
  1882. // Use saved window state or defaults
  1883. const saved = localStorage.getItem('cameraWindowState');
  1884. const state = saved ? JSON.parse(saved) : { width: 640, height: 400 };
  1885. const features = [
  1886. `width=${state.width}`,
  1887. `height=${state.height}`,
  1888. state.left !== undefined ? `left=${state.left}` : '',
  1889. state.top !== undefined ? `top=${state.top}` : '',
  1890. 'menubar=no,toolbar=no,location=no,status=no',
  1891. ].filter(Boolean).join(',');
  1892. window.open(`/camera/${printer.id}`, `camera-${printer.id}`, features);
  1893. }}
  1894. disabled={!status?.connected}
  1895. title="Open camera in new window"
  1896. >
  1897. <Video className="w-4 h-4" />
  1898. </Button>
  1899. <Button
  1900. variant="secondary"
  1901. size="sm"
  1902. onClick={() => setShowFileManager(true)}
  1903. title="Browse printer files"
  1904. >
  1905. <HardDrive className="w-4 h-4" />
  1906. Files
  1907. </Button>
  1908. </div>
  1909. </div>
  1910. )}
  1911. </CardContent>
  1912. {/* File Manager Modal */}
  1913. {showFileManager && (
  1914. <FileManagerModal
  1915. printerId={printer.id}
  1916. printerName={printer.name}
  1917. onClose={() => setShowFileManager(false)}
  1918. />
  1919. )}
  1920. {/* MQTT Debug Modal */}
  1921. {showMQTTDebug && (
  1922. <MQTTDebugModal
  1923. printerId={printer.id}
  1924. printerName={printer.name}
  1925. onClose={() => setShowMQTTDebug(false)}
  1926. />
  1927. )}
  1928. {/* Power On Confirmation */}
  1929. {showPowerOnConfirm && smartPlug && (
  1930. <ConfirmModal
  1931. title="Power On Printer"
  1932. message={`Are you sure you want to turn ON the power for "${printer.name}"?`}
  1933. confirmText="Power On"
  1934. variant="default"
  1935. onConfirm={() => {
  1936. powerControlMutation.mutate('on');
  1937. setShowPowerOnConfirm(false);
  1938. }}
  1939. onCancel={() => setShowPowerOnConfirm(false)}
  1940. />
  1941. )}
  1942. {/* Power Off Confirmation */}
  1943. {showPowerOffConfirm && smartPlug && (
  1944. <ConfirmModal
  1945. title="Power Off Printer"
  1946. message={
  1947. status?.state === 'RUNNING'
  1948. ? `WARNING: "${printer.name}" is currently printing! Are you sure you want to turn OFF the power? This will interrupt the print and may damage the printer.`
  1949. : `Are you sure you want to turn OFF the power for "${printer.name}"?`
  1950. }
  1951. confirmText="Power Off"
  1952. variant="danger"
  1953. onConfirm={() => {
  1954. powerControlMutation.mutate('off');
  1955. setShowPowerOffConfirm(false);
  1956. }}
  1957. onCancel={() => setShowPowerOffConfirm(false)}
  1958. />
  1959. )}
  1960. {/* Stop Print Confirmation */}
  1961. {showStopConfirm && (
  1962. <ConfirmModal
  1963. title="Stop Print"
  1964. message={`Are you sure you want to stop the current print on "${printer.name}"? This will cancel the print job.`}
  1965. confirmText="Stop Print"
  1966. variant="danger"
  1967. onConfirm={() => {
  1968. stopPrintMutation.mutate();
  1969. setShowStopConfirm(false);
  1970. }}
  1971. onCancel={() => setShowStopConfirm(false)}
  1972. />
  1973. )}
  1974. {/* Pause Print Confirmation */}
  1975. {showPauseConfirm && (
  1976. <ConfirmModal
  1977. title="Pause Print"
  1978. message={`Are you sure you want to pause the current print on "${printer.name}"?`}
  1979. confirmText="Pause Print"
  1980. variant="default"
  1981. onConfirm={() => {
  1982. pausePrintMutation.mutate();
  1983. setShowPauseConfirm(false);
  1984. }}
  1985. onCancel={() => setShowPauseConfirm(false)}
  1986. />
  1987. )}
  1988. {/* Resume Print Confirmation */}
  1989. {showResumeConfirm && (
  1990. <ConfirmModal
  1991. title="Resume Print"
  1992. message={`Are you sure you want to resume the print on "${printer.name}"?`}
  1993. confirmText="Resume Print"
  1994. variant="default"
  1995. onConfirm={() => {
  1996. resumePrintMutation.mutate();
  1997. setShowResumeConfirm(false);
  1998. }}
  1999. onCancel={() => setShowResumeConfirm(false)}
  2000. />
  2001. )}
  2002. {/* Skip Objects Popup */}
  2003. {showSkipObjectsModal && (
  2004. <div
  2005. className="fixed inset-0 z-50 flex items-center justify-center"
  2006. onClick={() => setShowSkipObjectsModal(false)}
  2007. onKeyDown={(e) => e.key === 'Escape' && setShowSkipObjectsModal(false)}
  2008. tabIndex={-1}
  2009. ref={(el) => el?.focus()}
  2010. >
  2011. {/* Backdrop */}
  2012. <div className="absolute inset-0 bg-black/50 z-0" />
  2013. {/* Modal */}
  2014. <div
  2015. className="relative z-10 bg-white dark:bg-bambu-dark border border-gray-200 dark:border-bambu-dark-tertiary rounded-xl shadow-2xl w-[560px] overflow-hidden"
  2016. onClick={(e) => e.stopPropagation()}
  2017. >
  2018. {/* Header */}
  2019. <div className="flex items-center justify-between px-4 py-3 border-b border-gray-200 dark:border-bambu-dark-tertiary bg-gray-50 dark:bg-bambu-dark">
  2020. <div className="flex items-center gap-2">
  2021. <SkipObjectsIcon className="w-4 h-4 text-bambu-green" />
  2022. <span className="text-sm font-medium text-gray-900 dark:text-white">Skip Objects</span>
  2023. </div>
  2024. <button
  2025. onClick={() => setShowSkipObjectsModal(false)}
  2026. className="p-1 text-gray-500 dark:text-bambu-gray hover:text-gray-900 dark:hover:text-white rounded transition-colors"
  2027. >
  2028. <X className="w-4 h-4" />
  2029. </button>
  2030. </div>
  2031. {!objectsData ? (
  2032. <div className="flex items-center justify-center py-12">
  2033. <Loader2 className="w-5 h-5 animate-spin text-bambu-gray" />
  2034. </div>
  2035. ) : objectsData.objects.length === 0 ? (
  2036. <div className="text-center py-8 px-4 text-bambu-gray">
  2037. <p className="text-sm">No objects found</p>
  2038. <p className="text-xs mt-1 opacity-70">Objects are loaded when a print starts</p>
  2039. </div>
  2040. ) : (
  2041. <div className="flex flex-col">
  2042. {/* Info Banner */}
  2043. <div className="flex items-center gap-3 px-4 py-2.5 bg-blue-50 dark:bg-blue-500/10 border-b border-gray-200 dark:border-bambu-dark-tertiary">
  2044. <div className="flex-shrink-0 w-8 h-8 rounded-lg bg-blue-100 dark:bg-blue-500/20 flex items-center justify-center">
  2045. <Monitor className="w-4 h-4 text-blue-500 dark:text-blue-400" />
  2046. </div>
  2047. <div className="flex-1 min-w-0">
  2048. <p className="text-xs text-blue-600 dark:text-blue-300">Match IDs with your printer display</p>
  2049. <p className="text-[10px] text-blue-500/70 dark:text-blue-300/60">The printer screen shows object IDs on the build plate</p>
  2050. </div>
  2051. <div className="flex-shrink-0 text-xs text-gray-500 dark:text-bambu-gray">
  2052. {objectsData.skipped_count}/{objectsData.total} skipped
  2053. </div>
  2054. </div>
  2055. {/* Layer Warning */}
  2056. {(status?.layer_num ?? 0) <= 1 && (
  2057. <div className="flex items-center gap-2 px-4 py-2 bg-amber-50 dark:bg-amber-500/10 border-b border-gray-200 dark:border-bambu-dark-tertiary">
  2058. <AlertCircle className="w-4 h-4 text-amber-500 dark:text-amber-400 flex-shrink-0" />
  2059. <p className="text-xs text-amber-600 dark:text-amber-400">
  2060. Wait for layer 2+ to skip objects (currently layer {status?.layer_num ?? 0})
  2061. </p>
  2062. </div>
  2063. )}
  2064. {/* Content: Image + List side by side */}
  2065. <div className="flex">
  2066. {/* Left: Preview Image with object markers */}
  2067. <div className="w-52 flex-shrink-0 p-4 border-r border-gray-200 dark:border-bambu-dark-tertiary bg-gray-50 dark:bg-bambu-dark-secondary">
  2068. <div className="relative">
  2069. {status?.cover_url ? (
  2070. <img
  2071. src={status.cover_url}
  2072. alt="Print preview"
  2073. className="w-full aspect-square object-contain rounded-lg bg-gray-100 dark:bg-bambu-dark"
  2074. />
  2075. ) : (
  2076. <div className="w-full aspect-square rounded-lg bg-gray-100 dark:bg-bambu-dark flex items-center justify-center">
  2077. <Box className="w-8 h-8 text-gray-300 dark:text-bambu-gray/30" />
  2078. </div>
  2079. )}
  2080. {/* Object ID markers overlay - positioned based on object data */}
  2081. {objectsData.objects.length > 0 && (
  2082. <div className="absolute inset-0 pointer-events-none">
  2083. {objectsData.objects.map((obj, idx) => {
  2084. // Build plate is typically 256x256mm for X1C
  2085. const buildPlateSize = 256;
  2086. let x: number, y: number;
  2087. // Use position data if available, otherwise fall back to grid
  2088. if (obj.x != null && obj.y != null) {
  2089. // Convert mm position to percentage (0-100)
  2090. // Clamp to valid range and add padding
  2091. x = Math.max(10, Math.min(90, (obj.x / buildPlateSize) * 100));
  2092. y = Math.max(10, Math.min(90, (obj.y / buildPlateSize) * 100));
  2093. } else {
  2094. // Fallback: arrange in a grid pattern over the build plate area
  2095. const cols = Math.ceil(Math.sqrt(objectsData.objects.length));
  2096. const row = Math.floor(idx / cols);
  2097. const col = idx % cols;
  2098. const rows = Math.ceil(objectsData.objects.length / cols);
  2099. x = 15 + (col * (70 / cols)) + (35 / cols);
  2100. y = 15 + (row * (70 / rows)) + (35 / rows);
  2101. }
  2102. return (
  2103. <div
  2104. key={obj.id}
  2105. className={`absolute flex items-center justify-center w-6 h-6 rounded-full text-[10px] font-bold shadow-lg ${
  2106. obj.skipped
  2107. ? 'bg-red-500 text-white line-through'
  2108. : 'bg-bambu-green text-black'
  2109. }`}
  2110. style={{
  2111. left: `${x}%`,
  2112. top: `${y}%`,
  2113. transform: 'translate(-50%, -50%)'
  2114. }}
  2115. title={obj.name}
  2116. >
  2117. {obj.id}
  2118. </div>
  2119. );
  2120. })}
  2121. </div>
  2122. )}
  2123. {/* Object count overlay */}
  2124. <div className="absolute bottom-2 right-2 px-2 py-1 bg-white/90 dark:bg-black/80 rounded text-[10px] text-gray-700 dark:text-white shadow-sm">
  2125. {objectsData.objects.filter(o => !o.skipped).length} active
  2126. </div>
  2127. </div>
  2128. </div>
  2129. {/* Right: Object List with prominent IDs */}
  2130. <div className="flex-1 min-w-0">
  2131. {objectsData.objects.map((obj) => (
  2132. <div
  2133. key={obj.id}
  2134. className={`
  2135. flex items-center gap-3 px-4 py-3 border-b border-gray-200 dark:border-bambu-dark-tertiary/50 last:border-0
  2136. ${obj.skipped ? 'bg-red-50 dark:bg-red-500/10' : 'hover:bg-gray-50 dark:hover:bg-bambu-dark/50'}
  2137. `}
  2138. >
  2139. {/* Large prominent ID badge */}
  2140. <div className={`
  2141. w-12 h-12 flex-shrink-0 rounded-lg flex flex-col items-center justify-center
  2142. ${obj.skipped
  2143. ? 'bg-red-100 dark:bg-red-500/20 border border-red-300 dark:border-red-500/40'
  2144. : 'bg-green-100 dark:bg-bambu-green/20 border border-green-300 dark:border-bambu-green/40'}
  2145. `}>
  2146. <span className={`text-lg font-mono font-bold ${obj.skipped ? 'text-red-500 dark:text-red-400' : 'text-green-600 dark:text-bambu-green'}`}>
  2147. {obj.id}
  2148. </span>
  2149. <span className={`text-[8px] uppercase tracking-wider ${obj.skipped ? 'text-red-400/60' : 'text-green-500/60 dark:text-bambu-green/60'}`}>
  2150. ID
  2151. </span>
  2152. </div>
  2153. {/* Object name and status */}
  2154. <div className="flex-1 min-w-0">
  2155. <span className={`block text-sm truncate ${obj.skipped ? 'text-red-500 dark:text-red-400 line-through' : 'text-gray-900 dark:text-white'}`}>
  2156. {obj.name}
  2157. </span>
  2158. {obj.skipped && (
  2159. <span className="text-[10px] text-red-400/60">Will be skipped</span>
  2160. )}
  2161. </div>
  2162. {/* Skip button */}
  2163. {!obj.skipped ? (
  2164. <button
  2165. onClick={() => skipObjectsMutation.mutate([obj.id])}
  2166. disabled={skipObjectsMutation.isPending || (status?.layer_num ?? 0) <= 1}
  2167. className={`px-4 py-2 text-xs font-medium rounded-lg transition-colors ${
  2168. (status?.layer_num ?? 0) <= 1
  2169. ? 'bg-gray-100 dark:bg-bambu-dark text-gray-400 dark:text-bambu-gray/50 cursor-not-allowed'
  2170. : 'bg-red-100 dark:bg-red-500/20 text-red-600 dark:text-red-400 hover:bg-red-200 dark:hover:bg-red-500/30 border border-red-300 dark:border-red-500/30'
  2171. }`}
  2172. title={(status?.layer_num ?? 0) <= 1 ? 'Wait for layer 2+' : 'Skip this object'}
  2173. >
  2174. Skip
  2175. </button>
  2176. ) : (
  2177. <span className="px-4 py-2 text-xs text-red-500 dark:text-red-400/70 bg-red-100 dark:bg-red-500/10 rounded-lg">
  2178. Skipped
  2179. </span>
  2180. )}
  2181. </div>
  2182. ))}
  2183. </div>
  2184. </div>
  2185. </div>
  2186. )}
  2187. </div>
  2188. </div>
  2189. )}
  2190. {/* HMS Error Modal */}
  2191. {showHMSModal && (
  2192. <HMSErrorModal
  2193. printerName={printer.name}
  2194. errors={status?.hms_errors || []}
  2195. onClose={() => setShowHMSModal(false)}
  2196. />
  2197. )}
  2198. {/* AMS History Modal */}
  2199. {amsHistoryModal && (
  2200. <AMSHistoryModal
  2201. isOpen={!!amsHistoryModal}
  2202. onClose={() => setAmsHistoryModal(null)}
  2203. printerId={printer.id}
  2204. printerName={printer.name}
  2205. amsId={amsHistoryModal.amsId}
  2206. amsLabel={amsHistoryModal.amsLabel}
  2207. initialMode={amsHistoryModal.mode}
  2208. thresholds={amsThresholds}
  2209. />
  2210. )}
  2211. {/* Edit Printer Modal */}
  2212. {showEditModal && (
  2213. <EditPrinterModal
  2214. printer={printer}
  2215. onClose={() => setShowEditModal(false)}
  2216. />
  2217. )}
  2218. {/* AMS Slot Menu Backdrop - closes menu when clicking outside */}
  2219. {amsSlotMenu && (
  2220. <div
  2221. className="fixed inset-0 z-40"
  2222. onClick={() => setAmsSlotMenu(null)}
  2223. />
  2224. )}
  2225. </Card>
  2226. );
  2227. }
  2228. function AddPrinterModal({
  2229. onClose,
  2230. onAdd,
  2231. existingSerials,
  2232. }: {
  2233. onClose: () => void;
  2234. onAdd: (data: PrinterCreate) => void;
  2235. existingSerials: string[];
  2236. }) {
  2237. const [form, setForm] = useState<PrinterCreate>({
  2238. name: '',
  2239. serial_number: '',
  2240. ip_address: '',
  2241. access_code: '',
  2242. model: '',
  2243. auto_archive: true,
  2244. });
  2245. // Discovery state
  2246. const [discovering, setDiscovering] = useState(false);
  2247. const [discovered, setDiscovered] = useState<DiscoveredPrinter[]>([]);
  2248. const [discoveryError, setDiscoveryError] = useState('');
  2249. const [hasScanned, setHasScanned] = useState(false);
  2250. const [isDocker, setIsDocker] = useState(false);
  2251. const [subnet, setSubnet] = useState('192.168.1.0/24');
  2252. const [scanProgress, setScanProgress] = useState({ scanned: 0, total: 0 });
  2253. // Fetch discovery info on mount
  2254. useEffect(() => {
  2255. discoveryApi.getInfo().then(info => {
  2256. setIsDocker(info.is_docker);
  2257. }).catch(() => {
  2258. // Ignore errors, assume not Docker
  2259. });
  2260. }, []);
  2261. // Filter out already-added printers
  2262. const newPrinters = discovered.filter(p => !existingSerials.includes(p.serial));
  2263. const startDiscovery = async () => {
  2264. setDiscoveryError('');
  2265. setDiscovered([]);
  2266. setDiscovering(true);
  2267. setHasScanned(false);
  2268. setScanProgress({ scanned: 0, total: 0 });
  2269. try {
  2270. if (isDocker) {
  2271. // Use subnet scanning for Docker
  2272. await discoveryApi.startSubnetScan(subnet);
  2273. // Poll for scan status and results
  2274. const pollInterval = setInterval(async () => {
  2275. try {
  2276. const status = await discoveryApi.getScanStatus();
  2277. setScanProgress({ scanned: status.scanned, total: status.total });
  2278. const printers = await discoveryApi.getDiscoveredPrinters();
  2279. setDiscovered(printers);
  2280. if (!status.running) {
  2281. clearInterval(pollInterval);
  2282. setDiscovering(false);
  2283. setHasScanned(true);
  2284. }
  2285. } catch (e) {
  2286. console.error('Failed to get scan status:', e);
  2287. }
  2288. }, 500);
  2289. } else {
  2290. // Use SSDP discovery for native installs
  2291. await discoveryApi.startDiscovery(10);
  2292. // Poll for discovered printers every second
  2293. const pollInterval = setInterval(async () => {
  2294. try {
  2295. const printers = await discoveryApi.getDiscoveredPrinters();
  2296. setDiscovered(printers);
  2297. } catch (e) {
  2298. console.error('Failed to get discovered printers:', e);
  2299. }
  2300. }, 1000);
  2301. // Stop after 10 seconds
  2302. setTimeout(async () => {
  2303. clearInterval(pollInterval);
  2304. try {
  2305. await discoveryApi.stopDiscovery();
  2306. } catch (e) {
  2307. // Ignore stop errors
  2308. }
  2309. setDiscovering(false);
  2310. setHasScanned(true);
  2311. // Final fetch
  2312. try {
  2313. const printers = await discoveryApi.getDiscoveredPrinters();
  2314. setDiscovered(printers);
  2315. } catch (e) {
  2316. console.error('Failed to get final discovered printers:', e);
  2317. }
  2318. }, 10000);
  2319. }
  2320. } catch (e) {
  2321. console.error('Failed to start discovery:', e);
  2322. setDiscoveryError(e instanceof Error ? e.message : 'Failed to start discovery');
  2323. setDiscovering(false);
  2324. setHasScanned(true);
  2325. }
  2326. };
  2327. // Map SSDP model codes to dropdown values
  2328. const mapModelCode = (ssdpModel: string | null): string => {
  2329. if (!ssdpModel) return '';
  2330. const modelMap: Record<string, string> = {
  2331. // H2 Series
  2332. 'O1D': 'H2D',
  2333. 'O1C': 'H2C',
  2334. 'O1S': 'H2S',
  2335. // X1 Series
  2336. 'BL-P001': 'X1C',
  2337. 'BL-P002': 'X1',
  2338. 'BL-P003': 'X1E',
  2339. // P Series
  2340. 'C11': 'P1S',
  2341. 'C12': 'P1P',
  2342. 'C13': 'P2S',
  2343. // A1 Series
  2344. 'N2S': 'A1',
  2345. 'N1': 'A1 Mini',
  2346. // Direct matches
  2347. 'X1C': 'X1C',
  2348. 'X1': 'X1',
  2349. 'X1E': 'X1E',
  2350. 'P1S': 'P1S',
  2351. 'P1P': 'P1P',
  2352. 'P2S': 'P2S',
  2353. 'A1': 'A1',
  2354. 'A1 Mini': 'A1 Mini',
  2355. 'H2D': 'H2D',
  2356. 'H2C': 'H2C',
  2357. 'H2S': 'H2S',
  2358. };
  2359. return modelMap[ssdpModel] || ssdpModel;
  2360. };
  2361. const selectPrinter = (printer: DiscoveredPrinter) => {
  2362. setForm({
  2363. ...form,
  2364. name: printer.name || '',
  2365. serial_number: printer.serial,
  2366. ip_address: printer.ip_address,
  2367. model: mapModelCode(printer.model),
  2368. });
  2369. // Clear discovery results after selection
  2370. setDiscovered([]);
  2371. };
  2372. // Cleanup discovery on unmount
  2373. useEffect(() => {
  2374. return () => {
  2375. discoveryApi.stopDiscovery().catch(() => {});
  2376. discoveryApi.stopSubnetScan().catch(() => {});
  2377. };
  2378. }, []);
  2379. // Close on Escape key
  2380. useEffect(() => {
  2381. const handleKeyDown = (e: KeyboardEvent) => {
  2382. if (e.key === 'Escape') onClose();
  2383. };
  2384. window.addEventListener('keydown', handleKeyDown);
  2385. return () => window.removeEventListener('keydown', handleKeyDown);
  2386. }, [onClose]);
  2387. return (
  2388. <div
  2389. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"
  2390. onClick={onClose}
  2391. >
  2392. <Card className="w-full max-w-md" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
  2393. <CardContent>
  2394. <h2 className="text-xl font-semibold mb-4">Add Printer</h2>
  2395. {/* Discovery Section */}
  2396. <div className="mb-4 pb-4 border-b border-bambu-dark-tertiary">
  2397. {isDocker && (
  2398. <div className="mb-3">
  2399. <label className="block text-sm text-bambu-gray mb-1">
  2400. Subnet to scan
  2401. </label>
  2402. <input
  2403. type="text"
  2404. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none text-sm"
  2405. value={subnet}
  2406. onChange={(e) => setSubnet(e.target.value)}
  2407. placeholder="192.168.1.0/24"
  2408. disabled={discovering}
  2409. />
  2410. <p className="mt-1 text-xs text-bambu-gray">
  2411. Docker detected. Enter your printer's subnet in CIDR notation.
  2412. Requires <code className="text-bambu-green">network_mode: host</code> in docker-compose.yml.
  2413. </p>
  2414. </div>
  2415. )}
  2416. <Button
  2417. type="button"
  2418. variant="secondary"
  2419. onClick={startDiscovery}
  2420. disabled={discovering}
  2421. className="w-full"
  2422. >
  2423. {discovering ? (
  2424. <>
  2425. <Loader2 className="w-4 h-4 animate-spin" />
  2426. {isDocker && scanProgress.total > 0
  2427. ? `Scanning... ${scanProgress.scanned}/${scanProgress.total}`
  2428. : 'Scanning...'}
  2429. </>
  2430. ) : (
  2431. <>
  2432. <Search className="w-4 h-4" />
  2433. {isDocker ? 'Scan Subnet for Printers' : 'Discover Printers on Network'}
  2434. </>
  2435. )}
  2436. </Button>
  2437. {discoveryError && (
  2438. <div className="mt-2 text-sm text-red-400">{discoveryError}</div>
  2439. )}
  2440. {newPrinters.length > 0 && (
  2441. <div className="mt-3 space-y-2 max-h-40 overflow-y-auto">
  2442. {newPrinters.map((printer) => (
  2443. <div
  2444. key={printer.serial}
  2445. className="flex items-center justify-between p-2 bg-bambu-dark rounded-lg hover:bg-bambu-dark-secondary cursor-pointer transition-colors"
  2446. onClick={() => selectPrinter(printer)}
  2447. >
  2448. <div className="min-w-0 flex-1">
  2449. <p className="font-medium text-white text-sm truncate">
  2450. {printer.name || printer.serial}
  2451. </p>
  2452. <p className="text-xs text-bambu-gray truncate">
  2453. {mapModelCode(printer.model) || 'Unknown'} • {printer.ip_address}
  2454. </p>
  2455. </div>
  2456. <ChevronDown className="w-4 h-4 text-bambu-gray -rotate-90 flex-shrink-0 ml-2" />
  2457. </div>
  2458. ))}
  2459. </div>
  2460. )}
  2461. {discovering && (
  2462. <p className="mt-2 text-sm text-bambu-gray text-center">
  2463. {isDocker ? 'Scanning subnet for Bambu printers...' : 'Scanning network...'}
  2464. </p>
  2465. )}
  2466. {hasScanned && !discovering && discovered.length === 0 && (
  2467. <p className="mt-2 text-sm text-bambu-gray text-center">
  2468. No printers found{isDocker ? ' in the specified subnet' : ' on the network'}.
  2469. </p>
  2470. )}
  2471. {hasScanned && !discovering && discovered.length > 0 && newPrinters.length === 0 && (
  2472. <p className="mt-2 text-sm text-bambu-gray text-center">
  2473. All discovered printers are already configured.
  2474. </p>
  2475. )}
  2476. </div>
  2477. <form
  2478. onSubmit={(e) => {
  2479. e.preventDefault();
  2480. onAdd(form);
  2481. }}
  2482. className="space-y-4"
  2483. >
  2484. <div>
  2485. <label className="block text-sm text-bambu-gray mb-1">Name</label>
  2486. <input
  2487. type="text"
  2488. required
  2489. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2490. value={form.name}
  2491. onChange={(e) => setForm({ ...form, name: e.target.value })}
  2492. placeholder="My Printer"
  2493. />
  2494. </div>
  2495. <div>
  2496. <label className="block text-sm text-bambu-gray mb-1">IP Address</label>
  2497. <input
  2498. type="text"
  2499. required
  2500. pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
  2501. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2502. value={form.ip_address}
  2503. onChange={(e) => setForm({ ...form, ip_address: e.target.value })}
  2504. placeholder="192.168.1.100"
  2505. />
  2506. </div>
  2507. <div>
  2508. <label className="block text-sm text-bambu-gray mb-1">Serial Number</label>
  2509. <input
  2510. type="text"
  2511. required
  2512. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2513. value={form.serial_number}
  2514. onChange={(e) => setForm({ ...form, serial_number: e.target.value })}
  2515. placeholder="01P00A000000000"
  2516. />
  2517. </div>
  2518. <div>
  2519. <label className="block text-sm text-bambu-gray mb-1">Access Code</label>
  2520. <input
  2521. type="password"
  2522. required
  2523. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2524. value={form.access_code}
  2525. onChange={(e) => setForm({ ...form, access_code: e.target.value })}
  2526. placeholder="From printer settings"
  2527. />
  2528. </div>
  2529. <div>
  2530. <label className="block text-sm text-bambu-gray mb-1">Model (optional)</label>
  2531. <select
  2532. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2533. value={form.model || ''}
  2534. onChange={(e) => setForm({ ...form, model: e.target.value })}
  2535. >
  2536. <option value="">Select model...</option>
  2537. <optgroup label="H2 Series">
  2538. <option value="H2C">H2C</option>
  2539. <option value="H2D">H2D</option>
  2540. <option value="H2S">H2S</option>
  2541. </optgroup>
  2542. <optgroup label="X1 Series">
  2543. <option value="X1E">X1E</option>
  2544. <option value="X1C">X1 Carbon</option>
  2545. <option value="X1">X1</option>
  2546. </optgroup>
  2547. <optgroup label="P Series">
  2548. <option value="P2S">P2S</option>
  2549. <option value="P1S">P1S</option>
  2550. <option value="P1P">P1P</option>
  2551. </optgroup>
  2552. <optgroup label="A1 Series">
  2553. <option value="A1">A1</option>
  2554. <option value="A1 Mini">A1 Mini</option>
  2555. </optgroup>
  2556. </select>
  2557. </div>
  2558. <div className="flex items-center gap-2">
  2559. <input
  2560. type="checkbox"
  2561. id="auto_archive"
  2562. checked={form.auto_archive}
  2563. onChange={(e) => setForm({ ...form, auto_archive: e.target.checked })}
  2564. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  2565. />
  2566. <label htmlFor="auto_archive" className="text-sm text-bambu-gray">
  2567. Auto-archive completed prints
  2568. </label>
  2569. </div>
  2570. <div className="flex gap-3 pt-4">
  2571. <Button type="button" variant="secondary" onClick={onClose} className="flex-1">
  2572. Cancel
  2573. </Button>
  2574. <Button type="submit" className="flex-1">
  2575. Add Printer
  2576. </Button>
  2577. </div>
  2578. </form>
  2579. </CardContent>
  2580. </Card>
  2581. </div>
  2582. );
  2583. }
  2584. function EditPrinterModal({
  2585. printer,
  2586. onClose,
  2587. }: {
  2588. printer: Printer;
  2589. onClose: () => void;
  2590. }) {
  2591. const queryClient = useQueryClient();
  2592. const [form, setForm] = useState({
  2593. name: printer.name,
  2594. ip_address: printer.ip_address,
  2595. access_code: '',
  2596. model: printer.model || '',
  2597. location: printer.location || '',
  2598. auto_archive: printer.auto_archive,
  2599. });
  2600. const updateMutation = useMutation({
  2601. mutationFn: (data: Partial<PrinterCreate>) => api.updatePrinter(printer.id, data),
  2602. onSuccess: () => {
  2603. queryClient.invalidateQueries({ queryKey: ['printers'] });
  2604. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  2605. onClose();
  2606. },
  2607. });
  2608. // Close on Escape key
  2609. useEffect(() => {
  2610. const handleKeyDown = (e: KeyboardEvent) => {
  2611. if (e.key === 'Escape') onClose();
  2612. };
  2613. window.addEventListener('keydown', handleKeyDown);
  2614. return () => window.removeEventListener('keydown', handleKeyDown);
  2615. }, [onClose]);
  2616. const handleSubmit = (e: React.FormEvent) => {
  2617. e.preventDefault();
  2618. const data: Partial<PrinterCreate> = {
  2619. name: form.name,
  2620. ip_address: form.ip_address,
  2621. model: form.model || undefined,
  2622. location: form.location || undefined,
  2623. auto_archive: form.auto_archive,
  2624. };
  2625. // Only include access_code if it was changed
  2626. if (form.access_code) {
  2627. data.access_code = form.access_code;
  2628. }
  2629. updateMutation.mutate(data);
  2630. };
  2631. return (
  2632. <div
  2633. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"
  2634. onClick={onClose}
  2635. >
  2636. <Card className="w-full max-w-md" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
  2637. <CardContent>
  2638. <h2 className="text-xl font-semibold mb-4">Edit Printer</h2>
  2639. <form onSubmit={handleSubmit} className="space-y-4">
  2640. <div>
  2641. <label className="block text-sm text-bambu-gray mb-1">Name</label>
  2642. <input
  2643. type="text"
  2644. required
  2645. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2646. value={form.name}
  2647. onChange={(e) => setForm({ ...form, name: e.target.value })}
  2648. placeholder="My Printer"
  2649. />
  2650. </div>
  2651. <div>
  2652. <label className="block text-sm text-bambu-gray mb-1">IP Address</label>
  2653. <input
  2654. type="text"
  2655. required
  2656. pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
  2657. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2658. value={form.ip_address}
  2659. onChange={(e) => setForm({ ...form, ip_address: e.target.value })}
  2660. placeholder="192.168.1.100"
  2661. />
  2662. </div>
  2663. <div>
  2664. <label className="block text-sm text-bambu-gray mb-1">Serial Number</label>
  2665. <input
  2666. type="text"
  2667. disabled
  2668. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray cursor-not-allowed"
  2669. value={printer.serial_number}
  2670. />
  2671. <p className="text-xs text-bambu-gray mt-1">Serial number cannot be changed</p>
  2672. </div>
  2673. <div>
  2674. <label className="block text-sm text-bambu-gray mb-1">Access Code</label>
  2675. <input
  2676. type="password"
  2677. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2678. value={form.access_code}
  2679. onChange={(e) => setForm({ ...form, access_code: e.target.value })}
  2680. placeholder="Leave empty to keep current"
  2681. />
  2682. </div>
  2683. <div>
  2684. <label className="block text-sm text-bambu-gray mb-1">Model</label>
  2685. <select
  2686. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2687. value={form.model}
  2688. onChange={(e) => setForm({ ...form, model: e.target.value })}
  2689. >
  2690. <option value="">Select model...</option>
  2691. <optgroup label="H2 Series">
  2692. <option value="H2C">H2C</option>
  2693. <option value="H2D">H2D</option>
  2694. <option value="H2S">H2S</option>
  2695. </optgroup>
  2696. <optgroup label="X1 Series">
  2697. <option value="X1E">X1E</option>
  2698. <option value="X1C">X1 Carbon</option>
  2699. <option value="X1">X1</option>
  2700. </optgroup>
  2701. <optgroup label="P Series">
  2702. <option value="P2S">P2S</option>
  2703. <option value="P1S">P1S</option>
  2704. <option value="P1P">P1P</option>
  2705. </optgroup>
  2706. <optgroup label="A1 Series">
  2707. <option value="A1">A1</option>
  2708. <option value="A1 Mini">A1 Mini</option>
  2709. </optgroup>
  2710. </select>
  2711. </div>
  2712. <div>
  2713. <label className="block text-sm text-bambu-gray mb-1">Location / Group</label>
  2714. <input
  2715. type="text"
  2716. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none"
  2717. value={form.location}
  2718. onChange={(e) => setForm({ ...form, location: e.target.value })}
  2719. placeholder="e.g., Workshop, Office, Basement"
  2720. />
  2721. <p className="text-xs text-bambu-gray mt-1">Used to group printers on the dashboard</p>
  2722. </div>
  2723. <div className="flex items-center gap-2">
  2724. <input
  2725. type="checkbox"
  2726. id="edit_auto_archive"
  2727. checked={form.auto_archive}
  2728. onChange={(e) => setForm({ ...form, auto_archive: e.target.checked })}
  2729. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  2730. />
  2731. <label htmlFor="edit_auto_archive" className="text-sm text-bambu-gray">
  2732. Auto-archive completed prints
  2733. </label>
  2734. </div>
  2735. <div className="flex gap-3 pt-4">
  2736. <Button type="button" variant="secondary" onClick={onClose} className="flex-1">
  2737. Cancel
  2738. </Button>
  2739. <Button type="submit" className="flex-1" disabled={updateMutation.isPending}>
  2740. {updateMutation.isPending ? 'Saving...' : 'Save Changes'}
  2741. </Button>
  2742. </div>
  2743. </form>
  2744. </CardContent>
  2745. </Card>
  2746. </div>
  2747. );
  2748. }
  2749. // Component to check if a printer is offline (for power dropdown)
  2750. function usePrinterOfflineStatus(printerId: number) {
  2751. const { data: status } = useQuery({
  2752. queryKey: ['printerStatus', printerId],
  2753. queryFn: () => api.getPrinterStatus(printerId),
  2754. refetchInterval: 30000,
  2755. });
  2756. return !status?.connected;
  2757. }
  2758. // Power dropdown item for an offline printer
  2759. function PowerDropdownItem({
  2760. printer,
  2761. plug,
  2762. onPowerOn,
  2763. isPowering,
  2764. }: {
  2765. printer: Printer;
  2766. plug: { id: number; name: string };
  2767. onPowerOn: (plugId: number) => void;
  2768. isPowering: boolean;
  2769. }) {
  2770. const isOffline = usePrinterOfflineStatus(printer.id);
  2771. // Fetch plug status
  2772. const { data: plugStatus } = useQuery({
  2773. queryKey: ['smartPlugStatus', plug.id],
  2774. queryFn: () => api.getSmartPlugStatus(plug.id),
  2775. refetchInterval: 10000,
  2776. });
  2777. // Only show if printer is offline
  2778. if (!isOffline) {
  2779. return null;
  2780. }
  2781. return (
  2782. <div className="flex items-center justify-between px-3 py-2 hover:bg-gray-100 dark:hover:bg-bambu-dark-tertiary">
  2783. <div className="flex items-center gap-2 min-w-0">
  2784. <span className="text-sm text-gray-900 dark:text-white truncate">{printer.name}</span>
  2785. {plugStatus && (
  2786. <span
  2787. className={`text-xs px-1.5 py-0.5 rounded ${
  2788. plugStatus.state === 'ON'
  2789. ? 'bg-bambu-green/20 text-bambu-green'
  2790. : 'bg-red-500/20 text-red-400'
  2791. }`}
  2792. >
  2793. {plugStatus.state || '?'}
  2794. </span>
  2795. )}
  2796. </div>
  2797. <button
  2798. onClick={() => onPowerOn(plug.id)}
  2799. disabled={isPowering || plugStatus?.state === 'ON'}
  2800. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  2801. plugStatus?.state === 'ON'
  2802. ? 'bg-bambu-green/20 text-bambu-green cursor-default'
  2803. : 'bg-bambu-green/20 text-bambu-green hover:bg-bambu-green hover:text-white'
  2804. }`}
  2805. >
  2806. <Power className="w-3 h-3" />
  2807. {isPowering ? '...' : 'On'}
  2808. </button>
  2809. </div>
  2810. );
  2811. }
  2812. export function PrintersPage() {
  2813. const [showAddModal, setShowAddModal] = useState(false);
  2814. const [hideDisconnected, setHideDisconnected] = useState(() => {
  2815. return localStorage.getItem('hideDisconnectedPrinters') === 'true';
  2816. });
  2817. const [showPowerDropdown, setShowPowerDropdown] = useState(false);
  2818. const [poweringOn, setPoweringOn] = useState<number | null>(null);
  2819. const [sortBy, setSortBy] = useState<SortOption>(() => {
  2820. return (localStorage.getItem('printerSortBy') as SortOption) || 'name';
  2821. });
  2822. const [sortAsc, setSortAsc] = useState<boolean>(() => {
  2823. return localStorage.getItem('printerSortAsc') !== 'false';
  2824. });
  2825. const [viewMode, setViewMode] = useState<ViewMode>(() => {
  2826. return (localStorage.getItem('printerViewMode') as ViewMode) || 'expanded';
  2827. });
  2828. const queryClient = useQueryClient();
  2829. const { data: printers, isLoading } = useQuery({
  2830. queryKey: ['printers'],
  2831. queryFn: api.getPrinters,
  2832. });
  2833. // Fetch app settings for AMS thresholds
  2834. const { data: settings } = useQuery({
  2835. queryKey: ['settings'],
  2836. queryFn: api.getSettings,
  2837. });
  2838. // Fetch all smart plugs to know which printers have them
  2839. const { data: smartPlugs } = useQuery({
  2840. queryKey: ['smart-plugs'],
  2841. queryFn: api.getSmartPlugs,
  2842. });
  2843. // Fetch maintenance overview for all printers to show badges
  2844. const { data: maintenanceOverview } = useQuery({
  2845. queryKey: ['maintenanceOverview'],
  2846. queryFn: api.getMaintenanceOverview,
  2847. staleTime: 60 * 1000, // 1 minute
  2848. });
  2849. // Create a map of printer_id -> maintenance info for quick lookup
  2850. const maintenanceByPrinter = maintenanceOverview?.reduce(
  2851. (acc, overview) => {
  2852. acc[overview.printer_id] = {
  2853. due_count: overview.due_count,
  2854. warning_count: overview.warning_count,
  2855. total_print_hours: overview.total_print_hours,
  2856. };
  2857. return acc;
  2858. },
  2859. {} as Record<number, PrinterMaintenanceInfo>
  2860. ) || {};
  2861. // Create a map of printer_id -> smart plug
  2862. const smartPlugByPrinter = smartPlugs?.reduce(
  2863. (acc, plug) => {
  2864. if (plug.printer_id) {
  2865. acc[plug.printer_id] = plug;
  2866. }
  2867. return acc;
  2868. },
  2869. {} as Record<number, typeof smartPlugs[0]>
  2870. ) || {};
  2871. const addMutation = useMutation({
  2872. mutationFn: api.createPrinter,
  2873. onSuccess: () => {
  2874. queryClient.invalidateQueries({ queryKey: ['printers'] });
  2875. setShowAddModal(false);
  2876. },
  2877. });
  2878. const powerOnMutation = useMutation({
  2879. mutationFn: (plugId: number) => api.controlSmartPlug(plugId, 'on'),
  2880. onSuccess: () => {
  2881. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  2882. setPoweringOn(null);
  2883. },
  2884. onError: () => {
  2885. setPoweringOn(null);
  2886. },
  2887. });
  2888. const toggleHideDisconnected = () => {
  2889. const newValue = !hideDisconnected;
  2890. setHideDisconnected(newValue);
  2891. localStorage.setItem('hideDisconnectedPrinters', String(newValue));
  2892. };
  2893. const handleSortChange = (newSort: SortOption) => {
  2894. setSortBy(newSort);
  2895. localStorage.setItem('printerSortBy', newSort);
  2896. };
  2897. const toggleSortDirection = () => {
  2898. const newAsc = !sortAsc;
  2899. setSortAsc(newAsc);
  2900. localStorage.setItem('printerSortAsc', String(newAsc));
  2901. };
  2902. const toggleViewMode = () => {
  2903. const newMode = viewMode === 'expanded' ? 'compact' : 'expanded';
  2904. setViewMode(newMode);
  2905. localStorage.setItem('printerViewMode', newMode);
  2906. };
  2907. // Sort printers based on selected option
  2908. const sortedPrinters = useMemo(() => {
  2909. if (!printers) return [];
  2910. const sorted = [...printers];
  2911. switch (sortBy) {
  2912. case 'name':
  2913. sorted.sort((a, b) => a.name.localeCompare(b.name));
  2914. break;
  2915. case 'model':
  2916. sorted.sort((a, b) => (a.model || '').localeCompare(b.model || ''));
  2917. break;
  2918. case 'location':
  2919. // Sort by location, with ungrouped printers last
  2920. sorted.sort((a, b) => {
  2921. const locA = a.location || '';
  2922. const locB = b.location || '';
  2923. if (!locA && locB) return 1;
  2924. if (locA && !locB) return -1;
  2925. return locA.localeCompare(locB) || a.name.localeCompare(b.name);
  2926. });
  2927. break;
  2928. case 'status':
  2929. // Sort by status: printing > idle > offline
  2930. sorted.sort((a, b) => {
  2931. const statusA = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', a.id]);
  2932. const statusB = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', b.id]);
  2933. const getPriority = (s: typeof statusA) => {
  2934. if (!s?.connected) return 2; // offline
  2935. if (s.state === 'RUNNING') return 0; // printing
  2936. return 1; // idle
  2937. };
  2938. return getPriority(statusA) - getPriority(statusB);
  2939. });
  2940. break;
  2941. }
  2942. // Apply ascending/descending
  2943. if (!sortAsc) {
  2944. sorted.reverse();
  2945. }
  2946. return sorted;
  2947. }, [printers, sortBy, sortAsc, queryClient]);
  2948. // Group printers by location when sorted by location
  2949. const groupedPrinters = useMemo(() => {
  2950. if (sortBy !== 'location') return null;
  2951. const groups: Record<string, typeof sortedPrinters> = {};
  2952. sortedPrinters.forEach(printer => {
  2953. const location = printer.location || 'Ungrouped';
  2954. if (!groups[location]) groups[location] = [];
  2955. groups[location].push(printer);
  2956. });
  2957. return groups;
  2958. }, [sortBy, sortedPrinters]);
  2959. return (
  2960. <div className="p-4 md:p-8">
  2961. <div className="flex items-center justify-between mb-6">
  2962. <div>
  2963. <h1 className="text-2xl font-bold text-white">Printers</h1>
  2964. <StatusSummaryBar printers={printers} />
  2965. </div>
  2966. <div className="flex items-center gap-3">
  2967. {/* Sort dropdown */}
  2968. <div className="flex items-center gap-1">
  2969. <select
  2970. value={sortBy}
  2971. onChange={(e) => handleSortChange(e.target.value as SortOption)}
  2972. className="text-sm bg-bambu-dark border border-bambu-dark-tertiary rounded-lg px-2 py-1.5 text-white focus:border-bambu-green focus:outline-none"
  2973. >
  2974. <option value="name">Name</option>
  2975. <option value="status">Status</option>
  2976. <option value="model">Model</option>
  2977. <option value="location">Location</option>
  2978. </select>
  2979. <button
  2980. onClick={toggleSortDirection}
  2981. className="p-1.5 rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  2982. title={sortAsc ? 'Sort descending' : 'Sort ascending'}
  2983. >
  2984. {sortAsc ? (
  2985. <ArrowUp className="w-4 h-4 text-bambu-gray" />
  2986. ) : (
  2987. <ArrowDown className="w-4 h-4 text-bambu-gray" />
  2988. )}
  2989. </button>
  2990. </div>
  2991. {/* View mode toggle */}
  2992. <button
  2993. onClick={toggleViewMode}
  2994. className="p-1.5 rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  2995. title={viewMode === 'expanded' ? 'Switch to compact view' : 'Switch to expanded view'}
  2996. >
  2997. {viewMode === 'expanded' ? (
  2998. <LayoutList className="w-5 h-5 text-bambu-gray" />
  2999. ) : (
  3000. <LayoutGrid className="w-5 h-5 text-bambu-gray" />
  3001. )}
  3002. </button>
  3003. <div className="w-px h-6 bg-bambu-dark-tertiary" />
  3004. <label className="flex items-center gap-2 text-sm text-bambu-gray cursor-pointer">
  3005. <input
  3006. type="checkbox"
  3007. checked={hideDisconnected}
  3008. onChange={toggleHideDisconnected}
  3009. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  3010. />
  3011. Hide offline
  3012. </label>
  3013. {/* Power dropdown for offline printers with smart plugs */}
  3014. {hideDisconnected && Object.keys(smartPlugByPrinter).length > 0 && (
  3015. <div className="relative">
  3016. <button
  3017. onClick={() => setShowPowerDropdown(!showPowerDropdown)}
  3018. className="flex items-center gap-1.5 px-3 py-1.5 text-sm bg-white dark:bg-bambu-dark-secondary border border-gray-200 dark:border-bambu-dark-tertiary rounded-lg text-gray-600 dark:text-bambu-gray hover:text-gray-900 dark:hover:text-white hover:border-bambu-green transition-colors"
  3019. >
  3020. <Power className="w-4 h-4" />
  3021. Power On
  3022. <ChevronDown className={`w-3 h-3 transition-transform ${showPowerDropdown ? 'rotate-180' : ''}`} />
  3023. </button>
  3024. {showPowerDropdown && (
  3025. <>
  3026. {/* Backdrop to close dropdown */}
  3027. <div
  3028. className="fixed inset-0 z-10"
  3029. onClick={() => setShowPowerDropdown(false)}
  3030. />
  3031. <div className="absolute right-0 mt-2 w-56 bg-white dark:bg-bambu-dark-secondary border border-gray-200 dark:border-bambu-dark-tertiary rounded-lg shadow-lg z-20 py-1">
  3032. <div className="px-3 py-2 text-xs text-gray-500 dark:text-bambu-gray border-b border-gray-200 dark:border-bambu-dark-tertiary">
  3033. Offline printers with smart plugs
  3034. </div>
  3035. {printers?.filter(p => smartPlugByPrinter[p.id]).map(printer => (
  3036. <PowerDropdownItem
  3037. key={printer.id}
  3038. printer={printer}
  3039. plug={smartPlugByPrinter[printer.id]}
  3040. onPowerOn={(plugId) => {
  3041. setPoweringOn(plugId);
  3042. powerOnMutation.mutate(plugId);
  3043. }}
  3044. isPowering={poweringOn === smartPlugByPrinter[printer.id]?.id}
  3045. />
  3046. ))}
  3047. {printers?.filter(p => smartPlugByPrinter[p.id]).length === 0 && (
  3048. <div className="px-3 py-2 text-sm text-bambu-gray">
  3049. No printers with smart plugs
  3050. </div>
  3051. )}
  3052. </div>
  3053. </>
  3054. )}
  3055. </div>
  3056. )}
  3057. <Button onClick={() => setShowAddModal(true)}>
  3058. <Plus className="w-4 h-4" />
  3059. Add Printer
  3060. </Button>
  3061. </div>
  3062. </div>
  3063. {isLoading ? (
  3064. <div className="text-center py-12 text-bambu-gray">Loading printers...</div>
  3065. ) : printers?.length === 0 ? (
  3066. <Card>
  3067. <CardContent className="text-center py-12">
  3068. <p className="text-bambu-gray mb-4">No printers configured yet</p>
  3069. <Button onClick={() => setShowAddModal(true)}>
  3070. <Plus className="w-4 h-4" />
  3071. Add Your First Printer
  3072. </Button>
  3073. </CardContent>
  3074. </Card>
  3075. ) : groupedPrinters ? (
  3076. /* Grouped by location view */
  3077. <div className="space-y-6">
  3078. {Object.entries(groupedPrinters).map(([location, locationPrinters]) => (
  3079. <div key={location}>
  3080. <h2 className="text-lg font-semibold text-white mb-3 flex items-center gap-2">
  3081. <span className="w-2 h-2 rounded-full bg-bambu-green" />
  3082. {location}
  3083. <span className="text-sm font-normal text-bambu-gray">({locationPrinters.length})</span>
  3084. </h2>
  3085. <div className={`grid gap-4 ${
  3086. viewMode === 'compact'
  3087. ? 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4'
  3088. : 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6'
  3089. }`}>
  3090. {locationPrinters.map((printer) => (
  3091. <PrinterCard
  3092. key={printer.id}
  3093. printer={printer}
  3094. hideIfDisconnected={hideDisconnected}
  3095. maintenanceInfo={maintenanceByPrinter[printer.id]}
  3096. viewMode={viewMode}
  3097. amsThresholds={settings ? {
  3098. humidityGood: Number(settings.ams_humidity_good) || 40,
  3099. humidityFair: Number(settings.ams_humidity_fair) || 60,
  3100. tempGood: Number(settings.ams_temp_good) || 28,
  3101. tempFair: Number(settings.ams_temp_fair) || 35,
  3102. } : undefined}
  3103. />
  3104. ))}
  3105. </div>
  3106. </div>
  3107. ))}
  3108. </div>
  3109. ) : (
  3110. /* Regular grid view */
  3111. <div className={`grid gap-4 ${
  3112. viewMode === 'compact'
  3113. ? 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4'
  3114. : 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6'
  3115. }`}>
  3116. {sortedPrinters.map((printer) => (
  3117. <PrinterCard
  3118. key={printer.id}
  3119. printer={printer}
  3120. hideIfDisconnected={hideDisconnected}
  3121. maintenanceInfo={maintenanceByPrinter[printer.id]}
  3122. viewMode={viewMode}
  3123. amsThresholds={settings ? {
  3124. humidityGood: Number(settings.ams_humidity_good) || 40,
  3125. humidityFair: Number(settings.ams_humidity_fair) || 60,
  3126. tempGood: Number(settings.ams_temp_good) || 28,
  3127. tempFair: Number(settings.ams_temp_fair) || 35,
  3128. } : undefined}
  3129. />
  3130. ))}
  3131. </div>
  3132. )}
  3133. {showAddModal && (
  3134. <AddPrinterModal
  3135. onClose={() => setShowAddModal(false)}
  3136. onAdd={(data) => addMutation.mutate(data)}
  3137. existingSerials={printers?.map(p => p.serial_number) || []}
  3138. />
  3139. )}
  3140. </div>
  3141. );
  3142. }