PrintersPage.tsx 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  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. Terminal,
  17. Power,
  18. PowerOff,
  19. Zap,
  20. Wrench,
  21. ChevronDown,
  22. Pencil,
  23. ArrowUp,
  24. ArrowDown,
  25. LayoutGrid,
  26. LayoutList,
  27. Layers,
  28. Video,
  29. Search,
  30. Loader2,
  31. } from 'lucide-react';
  32. import { useNavigate } from 'react-router-dom';
  33. import { api, discoveryApi } from '../api/client';
  34. import type { Printer, PrinterCreate, AMSUnit, DiscoveredPrinter } from '../api/client';
  35. import { Card, CardContent } from '../components/Card';
  36. import { Button } from '../components/Button';
  37. import { ConfirmModal } from '../components/ConfirmModal';
  38. import { FileManagerModal } from '../components/FileManagerModal';
  39. import { MQTTDebugModal } from '../components/MQTTDebugModal';
  40. import { HMSErrorModal, filterKnownHMSErrors } from '../components/HMSErrorModal';
  41. import { PrinterQueueWidget } from '../components/PrinterQueueWidget';
  42. import { AMSHistoryModal } from '../components/AMSHistoryModal';
  43. // Nozzle side indicators (Bambu Lab style - square badge with L/R)
  44. function NozzleBadge({ side }: { side: 'L' | 'R' }) {
  45. const { mode } = useTheme();
  46. // Light mode: #e7f5e9 (light green), Dark mode: #1a4d2e (dark green)
  47. const bgColor = mode === 'dark' ? '#1a4d2e' : '#e7f5e9';
  48. return (
  49. <span
  50. className="inline-flex items-center justify-center w-4 h-4 text-[10px] font-bold rounded"
  51. style={{ backgroundColor: bgColor, color: '#00ae42' }}
  52. >
  53. {side}
  54. </span>
  55. );
  56. }
  57. // AMS 4-tray device icon with fillable colored spool slots (Bambu Studio style)
  58. interface AMS4TrayIconProps {
  59. colors: (string | null)[]; // Array of 4 colors (hex) or null for empty
  60. className?: string;
  61. }
  62. function AMS4TrayIcon({ colors, className }: AMS4TrayIconProps) {
  63. // Spool positions: x start, centered at 12.5, 21.5, 30.5, 39.5
  64. // Each spool slot is 6 units wide (from 9.5-15.5, 18.5-24.5, etc.)
  65. const spoolSlots = [
  66. { x: 9.5, cx: 12.5 },
  67. { x: 18.5, cx: 21.5 },
  68. { x: 27.5, cx: 30.5 },
  69. { x: 36.5, cx: 39.5 },
  70. ];
  71. return (
  72. <svg className={className} width="56" height="34" viewBox="0 0 52 32" fill="none" xmlns="http://www.w3.org/2000/svg">
  73. {/* Outer casing with window */}
  74. <path
  75. fillRule="evenodd"
  76. clipRule="evenodd"
  77. d="M4 0C1.79086 0 0 1.79086 0 4V28C0 30.2091 1.79086 32 4 32H48C50.2091 32 52 30.2091 52 28V4C52 1.79086 50.2091 0 48 0H4ZM44 8H8V24H44V8Z"
  78. fill="#2F2E33"
  79. />
  80. {/* Spool color fills - rectangles that fill the visible window area */}
  81. {spoolSlots.map((slot, i) => (
  82. colors[i] ? (
  83. <rect key={i} x={slot.x} y="8" width="6" height="16" fill={colors[i]!} />
  84. ) : (
  85. <g key={i}>
  86. <rect x={slot.x} y="8" width="6" height="16" fill="#ffffff" />
  87. <line x1={slot.x} y1="8" x2={slot.x + 6} y2="24" stroke="#555555" strokeWidth="1.5" />
  88. </g>
  89. )
  90. ))}
  91. {/* Bottom half overlay (spool holders - creates rounded bottom edges) */}
  92. <path
  93. fillRule="evenodd"
  94. clipRule="evenodd"
  95. d="M36.5 16H33.5V18.2617C33.5 19.9186 32.1569 21.2617 30.5 21.2617C28.8431 21.2617 27.5 19.9186 27.5 18.2617V16H24.5V18.2617C24.5 19.9186 23.1569 21.2617 21.5 21.2617C19.8431 21.2617 18.5 19.9186 18.5 18.2617V16H15.5V18.2617C15.5 19.9186 14.1569 21.2617 12.5 21.2617C10.8432 21.2617 9.5 19.9186 9.5 18.2617V16H4V28H48V16H42.5V18.2617C42.5 19.9186 41.1569 21.2617 39.5 21.2617C37.8431 21.2617 36.5 19.9186 36.5 18.2617V16Z"
  96. fill="#767676"
  97. />
  98. {/* Top half overlay (spool tops - creates rounded top edges) */}
  99. <path
  100. fillRule="evenodd"
  101. clipRule="evenodd"
  102. d="M6 9.18382C6 6.32088 8.32088 4 11.1838 4H40.8162C43.6791 4 46 6.32088 46 9.18382V16H42.5V12.2617C42.5 10.6049 41.1569 9.26172 39.5 9.26172C37.8431 9.26172 36.5 10.6049 36.5 12.2617V16H33.5V12.2617C33.5 10.6049 32.1569 9.26172 30.5 9.26172C28.8431 9.26172 27.5 10.6049 27.5 12.2617V16H24.5V12.2617C24.5 10.6049 23.1569 9.26172 21.5 9.26172C19.8431 9.26172 18.5 10.6049 18.5 12.2617V16H15.5V12.2617C15.5 10.6049 14.1569 9.26172 12.5 9.26172C10.8432 9.26172 9.5 10.6049 9.5 12.2617V16H6V9.18382Z"
  103. fill="#BFBFBF"
  104. />
  105. </svg>
  106. );
  107. }
  108. // AMS 1-tray device icon (AMS-HT) with fillable colored slot (Bambu Studio style)
  109. interface AMS1TrayIconProps {
  110. color: string | null; // Hex color or null for empty
  111. className?: string;
  112. }
  113. function AMS1TrayIcon({ color, className }: AMS1TrayIconProps) {
  114. return (
  115. <svg className={className} width="56" height="56" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg">
  116. {/* Filament color fill */}
  117. {color ? (
  118. <rect x="8.3" y="5.2" width="3.8" height="5.1" fill={color} rx="0.3"/>
  119. ) : (
  120. <g>
  121. <rect x="8.3" y="5.2" width="3.8" height="5.1" fill="#ffffff" rx="0.3"/>
  122. <line x1="8.3" y1="5.2" x2="12.1" y2="10.3" stroke="#555555" strokeWidth="0.8" />
  123. </g>
  124. )}
  125. {/* Device outline - top housing */}
  126. <path d="M5.88312 4.68555C5.88312 4.13326 6.33083 3.68555 6.88312 3.68555H13.5059C14.0582 3.68555 14.5059 4.13326 14.5059 4.68555V10.3887H5.88312V4.68555Z" stroke="#6B6B6B"/>
  127. {/* Bottom base */}
  128. <rect x="3.8725" y="10.3887" width="12.7037" height="7.55371" rx="1.2" stroke="#6B6B6B"/>
  129. {/* Inner tray outline */}
  130. <path d="M8.21991 5.65234C8.21991 5.3762 8.44377 5.15234 8.71991 5.15234H11.7288C12.005 5.15234 12.2288 5.3762 12.2288 5.65234V10.3887H8.21991V5.65234Z" stroke="#6B6B6B"/>
  131. </svg>
  132. );
  133. }
  134. // Water drop SVG - empty outline (Bambu Lab style from bambu-humidity)
  135. function WaterDropEmpty({ className }: { className?: string }) {
  136. return (
  137. <svg className={className} viewBox="0 0 36 54" fill="none" xmlns="http://www.w3.org/2000/svg">
  138. <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"/>
  139. </svg>
  140. );
  141. }
  142. // Water drop SVG - half filled with blue water (Bambu Lab style from bambu-humidity)
  143. function WaterDropHalf({ className }: { className?: string }) {
  144. return (
  145. <svg className={className} viewBox="0 0 35 53" fill="none" xmlns="http://www.w3.org/2000/svg">
  146. <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"/>
  147. <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"/>
  148. </svg>
  149. );
  150. }
  151. // Water drop SVG - fully filled with blue water (Bambu Lab style from bambu-humidity)
  152. function WaterDropFull({ className }: { className?: string }) {
  153. return (
  154. <svg className={className} viewBox="0 0 36 54" fill="none" xmlns="http://www.w3.org/2000/svg">
  155. <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"/>
  156. <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"/>
  157. </svg>
  158. );
  159. }
  160. // Thermometer SVG - empty outline
  161. function ThermometerEmpty({ className }: { className?: string }) {
  162. return (
  163. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  164. <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"/>
  165. <circle cx="6" cy="15" r="2.5" stroke="#C3C2C1" strokeWidth="1" fill="none"/>
  166. </svg>
  167. );
  168. }
  169. // Thermometer SVG - half filled (gold - same as humidity fair)
  170. function ThermometerHalf({ className }: { className?: string }) {
  171. return (
  172. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  173. <rect x="4.5" y="8" width="3" height="4.5" fill="#d4a017" rx="0.5"/>
  174. <circle cx="6" cy="15" r="2" fill="#d4a017"/>
  175. <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"/>
  176. </svg>
  177. );
  178. }
  179. // Thermometer SVG - fully filled (red - same as humidity bad)
  180. function ThermometerFull({ className }: { className?: string }) {
  181. return (
  182. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  183. <rect x="4.5" y="3" width="3" height="9.5" fill="#c62828" rx="0.5"/>
  184. <circle cx="6" cy="15" r="2" fill="#c62828"/>
  185. <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"/>
  186. </svg>
  187. );
  188. }
  189. // Heater thermometer icon - filled when heating, outline when off
  190. interface HeaterThermometerProps {
  191. className?: string;
  192. color: string; // The color class (e.g., "text-orange-400")
  193. isHeating: boolean;
  194. }
  195. function HeaterThermometer({ className, color, isHeating }: HeaterThermometerProps) {
  196. // Extract the actual color from Tailwind class for SVG fill
  197. const colorMap: Record<string, string> = {
  198. 'text-orange-400': '#fb923c',
  199. 'text-blue-400': '#60a5fa',
  200. 'text-green-400': '#4ade80',
  201. };
  202. const fillColor = colorMap[color] || '#888';
  203. // Glow style when heating
  204. const glowStyle = isHeating ? {
  205. filter: `drop-shadow(0 0 4px ${fillColor}) drop-shadow(0 0 8px ${fillColor})`,
  206. } : {};
  207. if (isHeating) {
  208. // Filled thermometer with glow - heater is ON
  209. return (
  210. <svg className={className} style={glowStyle} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  211. <rect x="4.5" y="3" width="3" height="9.5" fill={fillColor} rx="0.5"/>
  212. <circle cx="6" cy="15" r="2" fill={fillColor}/>
  213. <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"/>
  214. </svg>
  215. );
  216. }
  217. // Empty thermometer - heater is OFF
  218. return (
  219. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  220. <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"/>
  221. <circle cx="6" cy="15" r="2.5" stroke={fillColor} strokeWidth="1" fill="none"/>
  222. </svg>
  223. );
  224. }
  225. // Humidity indicator with water drop that fills based on level (Bambu Lab style)
  226. // Reference: https://github.com/theicedmango/bambu-humidity
  227. interface HumidityIndicatorProps {
  228. humidity: number | string;
  229. goodThreshold?: number; // <= this is green
  230. fairThreshold?: number; // <= this is orange, > is red
  231. onClick?: () => void;
  232. }
  233. function HumidityIndicator({ humidity, goodThreshold = 40, fairThreshold = 60, onClick }: HumidityIndicatorProps) {
  234. const humidityValue = typeof humidity === 'string' ? parseInt(humidity, 10) : humidity;
  235. const good = typeof goodThreshold === 'number' ? goodThreshold : 40;
  236. const fair = typeof fairThreshold === 'number' ? fairThreshold : 60;
  237. // Status thresholds (configurable via settings)
  238. // Good: ≤goodThreshold (green #22a352), Fair: ≤fairThreshold (gold #d4a017), Bad: >fairThreshold (red #c62828)
  239. let textColor: string;
  240. let statusText: string;
  241. if (isNaN(humidityValue)) {
  242. textColor = '#C3C2C1';
  243. statusText = 'Unknown';
  244. } else if (humidityValue <= good) {
  245. textColor = '#22a352'; // Green - Good
  246. statusText = 'Good';
  247. } else if (humidityValue <= fair) {
  248. textColor = '#d4a017'; // Gold - Fair
  249. statusText = 'Fair';
  250. } else {
  251. textColor = '#c62828'; // Red - Bad
  252. statusText = 'Bad';
  253. }
  254. // Fill level based on status: Good=Empty (dry), Fair=Half, Bad=Full (wet)
  255. let DropComponent: React.FC<{ className?: string }>;
  256. if (isNaN(humidityValue)) {
  257. DropComponent = WaterDropEmpty;
  258. } else if (humidityValue <= good) {
  259. DropComponent = WaterDropEmpty; // Good - empty drop (dry)
  260. } else if (humidityValue <= fair) {
  261. DropComponent = WaterDropHalf; // Fair - half filled
  262. } else {
  263. DropComponent = WaterDropFull; // Bad - full (too humid)
  264. }
  265. return (
  266. <button
  267. type="button"
  268. onClick={onClick}
  269. className={`flex items-center justify-end gap-1 ${onClick ? 'cursor-pointer hover:opacity-80 transition-opacity' : ''}`}
  270. title={`Humidity: ${humidityValue}% - ${statusText}${onClick ? ' (click for history)' : ''}`}
  271. >
  272. <DropComponent className="w-3 h-4" />
  273. <span className="text-xs font-medium tabular-nums w-8 text-right" style={{ color: textColor }}>{humidityValue}%</span>
  274. </button>
  275. );
  276. }
  277. // Temperature indicator with dynamic icon and coloring
  278. interface TemperatureIndicatorProps {
  279. temp: number;
  280. goodThreshold?: number; // <= this is blue
  281. fairThreshold?: number; // <= this is orange, > is red
  282. onClick?: () => void;
  283. }
  284. function TemperatureIndicator({ temp, goodThreshold = 28, fairThreshold = 35, onClick }: TemperatureIndicatorProps) {
  285. // Ensure thresholds are numbers
  286. const good = typeof goodThreshold === 'number' ? goodThreshold : 28;
  287. const fair = typeof fairThreshold === 'number' ? fairThreshold : 35;
  288. let textColor: string;
  289. let statusText: string;
  290. let ThermoComponent: React.FC<{ className?: string }>;
  291. if (temp <= good) {
  292. textColor = '#22a352'; // Green - good (same as humidity)
  293. statusText = 'Good';
  294. ThermoComponent = ThermometerEmpty;
  295. } else if (temp <= fair) {
  296. textColor = '#d4a017'; // Gold - fair (same as humidity)
  297. statusText = 'Fair';
  298. ThermoComponent = ThermometerHalf;
  299. } else {
  300. textColor = '#c62828'; // Red - bad (same as humidity)
  301. statusText = 'Bad';
  302. ThermoComponent = ThermometerFull;
  303. }
  304. return (
  305. <button
  306. type="button"
  307. onClick={onClick}
  308. className={`flex items-center gap-1 ${onClick ? 'cursor-pointer hover:opacity-80 transition-opacity' : ''}`}
  309. title={`Temperature: ${temp}°C - ${statusText}${onClick ? ' (click for history)' : ''}`}
  310. >
  311. <ThermoComponent className="w-3 h-4" />
  312. <span className="tabular-nums w-12 text-right" style={{ color: textColor }}>{temp}°C</span>
  313. </button>
  314. );
  315. }
  316. // Get AMS label: AMS-A/B/C/D for regular AMS, HT-A/B for AMS-HT (single spool)
  317. // Always use tray count as the source of truth (1 tray = AMS-HT, 4 trays = regular AMS)
  318. // AMS-HT uses IDs 128+ while regular AMS uses 0-3
  319. function getAmsLabel(amsId: number | string, trayCount: number): string {
  320. // Ensure amsId is a number (backend might send string)
  321. const id = typeof amsId === 'string' ? parseInt(amsId, 10) : amsId;
  322. const safeId = isNaN(id) ? 0 : id;
  323. const isHt = trayCount === 1;
  324. // AMS-HT uses IDs starting at 128, regular AMS uses 0-3
  325. const normalizedId = safeId >= 128 ? safeId - 128 : safeId;
  326. const letter = String.fromCharCode(65 + normalizedId); // 0=A, 1=B, 2=C, 3=D
  327. return isHt ? `HT-${letter}` : `AMS-${letter}`;
  328. }
  329. function formatTime(seconds: number): string {
  330. const hours = Math.floor(seconds / 3600);
  331. const minutes = Math.floor((seconds % 3600) / 60);
  332. return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
  333. }
  334. function formatETA(remainingMinutes: number): string {
  335. const now = new Date();
  336. const eta = new Date(now.getTime() + remainingMinutes * 60 * 1000);
  337. const today = new Date();
  338. today.setHours(0, 0, 0, 0);
  339. const etaDay = new Date(eta);
  340. etaDay.setHours(0, 0, 0, 0);
  341. const timeStr = eta.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  342. // Check if it's tomorrow or later
  343. const dayDiff = Math.floor((etaDay.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
  344. if (dayDiff === 0) {
  345. return timeStr;
  346. } else if (dayDiff === 1) {
  347. return `Tomorrow ${timeStr}`;
  348. } else {
  349. return eta.toLocaleDateString([], { weekday: 'short' }) + ' ' + timeStr;
  350. }
  351. }
  352. function getPrinterImage(model: string | null | undefined): string {
  353. if (!model) return '/img/printers/default.png';
  354. const modelLower = model.toLowerCase().replace(/\s+/g, '');
  355. // Map model names to image files
  356. if (modelLower.includes('x1e')) return '/img/printers/x1e.png';
  357. if (modelLower.includes('x1c') || modelLower.includes('x1carbon')) return '/img/printers/x1c.png';
  358. if (modelLower.includes('x1')) return '/img/printers/x1c.png';
  359. if (modelLower.includes('h2d')) return '/img/printers/h2d.png';
  360. if (modelLower.includes('h2c') || modelLower.includes('h2s')) return '/img/printers/h2d.png';
  361. if (modelLower.includes('p2s')) return '/img/printers/p1s.png';
  362. if (modelLower.includes('p1s')) return '/img/printers/p1s.png';
  363. if (modelLower.includes('p1p')) return '/img/printers/p1p.png';
  364. if (modelLower.includes('a1mini')) return '/img/printers/a1mini.png';
  365. if (modelLower.includes('a1')) return '/img/printers/a1.png';
  366. return '/img/printers/default.png';
  367. }
  368. function getWifiStrength(rssi: number | null | undefined): { label: string; color: string; bars: number } {
  369. if (rssi == null) return { label: '', color: 'text-bambu-gray', bars: 0 };
  370. if (rssi >= -50) return { label: 'Excellent', color: 'text-bambu-green', bars: 4 };
  371. if (rssi >= -60) return { label: 'Good', color: 'text-bambu-green', bars: 3 };
  372. if (rssi >= -70) return { label: 'Fair', color: 'text-yellow-400', bars: 2 };
  373. if (rssi >= -80) return { label: 'Weak', color: 'text-orange-400', bars: 1 };
  374. return { label: 'Very weak', color: 'text-red-400', bars: 1 };
  375. }
  376. function CoverImage({ url, printName }: { url: string | null; printName?: string }) {
  377. const [loaded, setLoaded] = useState(false);
  378. const [error, setError] = useState(false);
  379. const [showOverlay, setShowOverlay] = useState(false);
  380. return (
  381. <>
  382. <div
  383. 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' : ''}`}
  384. onClick={() => url && loaded && setShowOverlay(true)}
  385. >
  386. {url && !error ? (
  387. <>
  388. <img
  389. src={url}
  390. alt="Print preview"
  391. className={`w-full h-full object-cover ${loaded ? 'block' : 'hidden'}`}
  392. onLoad={() => setLoaded(true)}
  393. onError={() => setError(true)}
  394. />
  395. {!loaded && <Box className="w-8 h-8 text-bambu-gray" />}
  396. </>
  397. ) : (
  398. <Box className="w-8 h-8 text-bambu-gray" />
  399. )}
  400. </div>
  401. {/* Cover Image Overlay */}
  402. {showOverlay && url && (
  403. <div
  404. className="fixed inset-0 bg-black/80 flex items-center justify-center z-50 p-8"
  405. onClick={() => setShowOverlay(false)}
  406. >
  407. <div className="relative max-w-2xl max-h-full">
  408. <img
  409. src={url}
  410. alt="Print preview"
  411. className="max-w-full max-h-[80vh] rounded-lg shadow-2xl"
  412. />
  413. {printName && (
  414. <p className="text-white text-center mt-4 text-lg">{printName}</p>
  415. )}
  416. </div>
  417. </div>
  418. )}
  419. </>
  420. );
  421. }
  422. interface PrinterMaintenanceInfo {
  423. due_count: number;
  424. warning_count: number;
  425. total_print_hours: number;
  426. }
  427. // Status summary bar component - uses queryClient to read cached statuses
  428. function StatusSummaryBar({ printers }: { printers: Printer[] | undefined }) {
  429. const queryClient = useQueryClient();
  430. const counts = useMemo(() => {
  431. let printing = 0;
  432. let idle = 0;
  433. let offline = 0;
  434. let loading = 0;
  435. printers?.forEach((printer) => {
  436. const status = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', printer.id]);
  437. if (status === undefined) {
  438. // Status not yet loaded - don't count as offline yet
  439. loading++;
  440. } else if (!status.connected) {
  441. offline++;
  442. } else if (status.state === 'RUNNING') {
  443. printing++;
  444. } else {
  445. idle++;
  446. }
  447. });
  448. return { printing, idle, offline, loading, total: (printers?.length || 0) };
  449. }, [printers, queryClient]);
  450. // Subscribe to query cache changes to re-render when status updates
  451. // Throttled to prevent rapid re-renders from causing tab crashes
  452. const [, setTick] = useState(0);
  453. useEffect(() => {
  454. let pending = false;
  455. const unsubscribe = queryClient.getQueryCache().subscribe(() => {
  456. if (!pending) {
  457. pending = true;
  458. requestAnimationFrame(() => {
  459. setTick(t => t + 1);
  460. pending = false;
  461. });
  462. }
  463. });
  464. return () => unsubscribe();
  465. }, [queryClient]);
  466. if (!printers?.length) return null;
  467. return (
  468. <div className="flex items-center gap-4 text-sm">
  469. {counts.printing > 0 && (
  470. <div className="flex items-center gap-1.5">
  471. <div className="w-2 h-2 rounded-full bg-bambu-green animate-pulse" />
  472. <span className="text-bambu-gray">
  473. <span className="text-white font-medium">{counts.printing}</span> printing
  474. </span>
  475. </div>
  476. )}
  477. {counts.idle > 0 && (
  478. <div className="flex items-center gap-1.5">
  479. <div className="w-2 h-2 rounded-full bg-blue-400" />
  480. <span className="text-bambu-gray">
  481. <span className="text-white font-medium">{counts.idle}</span> idle
  482. </span>
  483. </div>
  484. )}
  485. {counts.offline > 0 && (
  486. <div className="flex items-center gap-1.5">
  487. <div className="w-2 h-2 rounded-full bg-gray-400" />
  488. <span className="text-bambu-gray">
  489. <span className="text-white font-medium">{counts.offline}</span> offline
  490. </span>
  491. </div>
  492. )}
  493. </div>
  494. );
  495. }
  496. type SortOption = 'name' | 'status' | 'model' | 'location';
  497. type ViewMode = 'expanded' | 'compact';
  498. /**
  499. * Get human-readable status display text for a printer.
  500. * Uses stg_cur_name for detailed calibration/preparation stages,
  501. * otherwise formats the gcode_state nicely.
  502. */
  503. function getStatusDisplay(state: string | null | undefined, stg_cur_name: string | null | undefined): string {
  504. // If we have a specific stage name (calibration, heating, etc.), use it
  505. if (stg_cur_name) {
  506. return stg_cur_name;
  507. }
  508. // Format the gcode_state nicely
  509. switch (state) {
  510. case 'RUNNING':
  511. return 'Printing';
  512. case 'PAUSE':
  513. return 'Paused';
  514. case 'FINISH':
  515. return 'Finished';
  516. case 'FAILED':
  517. return 'Failed';
  518. case 'IDLE':
  519. return 'Idle';
  520. default:
  521. return state ? state.charAt(0) + state.slice(1).toLowerCase() : 'Idle';
  522. }
  523. }
  524. function PrinterCard({
  525. printer,
  526. hideIfDisconnected,
  527. maintenanceInfo,
  528. viewMode = 'expanded',
  529. amsThresholds,
  530. }: {
  531. printer: Printer;
  532. hideIfDisconnected?: boolean;
  533. maintenanceInfo?: PrinterMaintenanceInfo;
  534. viewMode?: ViewMode;
  535. amsThresholds?: {
  536. humidityGood: number;
  537. humidityFair: number;
  538. tempGood: number;
  539. tempFair: number;
  540. };
  541. }) {
  542. const queryClient = useQueryClient();
  543. const navigate = useNavigate();
  544. const [showMenu, setShowMenu] = useState(false);
  545. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  546. const [deleteArchives, setDeleteArchives] = useState(true);
  547. const [showEditModal, setShowEditModal] = useState(false);
  548. const [showFileManager, setShowFileManager] = useState(false);
  549. const [showMQTTDebug, setShowMQTTDebug] = useState(false);
  550. const [showPowerOnConfirm, setShowPowerOnConfirm] = useState(false);
  551. const [showPowerOffConfirm, setShowPowerOffConfirm] = useState(false);
  552. const [showHMSModal, setShowHMSModal] = useState(false);
  553. const [amsHistoryModal, setAmsHistoryModal] = useState<{
  554. amsId: number;
  555. amsLabel: string;
  556. mode: 'humidity' | 'temperature';
  557. } | null>(null);
  558. const { data: status } = useQuery({
  559. queryKey: ['printerStatus', printer.id],
  560. queryFn: () => api.getPrinterStatus(printer.id),
  561. refetchInterval: 30000, // Fallback polling, WebSocket handles real-time
  562. });
  563. // Cache WiFi signal to prevent it disappearing on updates
  564. const [cachedWifiSignal, setCachedWifiSignal] = useState<number | null>(null);
  565. useEffect(() => {
  566. if (status?.wifi_signal != null) {
  567. setCachedWifiSignal(status.wifi_signal);
  568. }
  569. }, [status?.wifi_signal]);
  570. const wifiSignal = status?.wifi_signal ?? cachedWifiSignal;
  571. // Cache connected state to prevent flicker when status briefly becomes undefined
  572. const cachedConnected = useRef<boolean | undefined>(undefined);
  573. useEffect(() => {
  574. if (status?.connected !== undefined) {
  575. cachedConnected.current = status.connected;
  576. }
  577. }, [status?.connected]);
  578. const isConnected = status?.connected ?? cachedConnected.current;
  579. // Cache ams_extruder_map to prevent L/R indicators bouncing on updates
  580. const cachedAmsExtruderMap = useRef<Record<string, number>>({});
  581. useEffect(() => {
  582. if (status?.ams_extruder_map && Object.keys(status.ams_extruder_map).length > 0) {
  583. cachedAmsExtruderMap.current = status.ams_extruder_map;
  584. }
  585. }, [status?.ams_extruder_map]);
  586. const amsExtruderMap = (status?.ams_extruder_map && Object.keys(status.ams_extruder_map).length > 0)
  587. ? status.ams_extruder_map
  588. : cachedAmsExtruderMap.current;
  589. // Cache AMS data to prevent it disappearing on idle/offline printers
  590. const cachedAmsData = useRef<AMSUnit[]>([]);
  591. useEffect(() => {
  592. if (status?.ams && status.ams.length > 0) {
  593. cachedAmsData.current = status.ams;
  594. }
  595. }, [status?.ams]);
  596. const amsData = (status?.ams && status.ams.length > 0) ? status.ams : cachedAmsData.current;
  597. // Fetch smart plug for this printer
  598. const { data: smartPlug } = useQuery({
  599. queryKey: ['smartPlugByPrinter', printer.id],
  600. queryFn: () => api.getSmartPlugByPrinter(printer.id),
  601. });
  602. // Fetch smart plug status if plug exists (faster refresh for energy monitoring)
  603. const { data: plugStatus } = useQuery({
  604. queryKey: ['smartPlugStatus', smartPlug?.id],
  605. queryFn: () => smartPlug ? api.getSmartPlugStatus(smartPlug.id) : null,
  606. enabled: !!smartPlug,
  607. refetchInterval: 10000, // 10 seconds for real-time power display
  608. });
  609. // Fetch queue count for this printer
  610. const { data: queueItems } = useQuery({
  611. queryKey: ['queue', printer.id, 'pending'],
  612. queryFn: () => api.getQueue(printer.id, 'pending'),
  613. });
  614. const queueCount = queueItems?.length || 0;
  615. // Fetch last completed print for this printer
  616. const { data: lastPrints } = useQuery({
  617. queryKey: ['archives', printer.id, 'last'],
  618. queryFn: () => api.getArchives(printer.id, 1, 0),
  619. enabled: status?.connected && status?.state !== 'RUNNING',
  620. });
  621. const lastPrint = lastPrints?.[0];
  622. // Determine if this card should be hidden (use cached connected state to prevent flicker)
  623. const shouldHide = hideIfDisconnected && isConnected === false;
  624. const deleteMutation = useMutation({
  625. mutationFn: (options: { deleteArchives: boolean }) =>
  626. api.deletePrinter(printer.id, options.deleteArchives),
  627. onSuccess: () => {
  628. queryClient.invalidateQueries({ queryKey: ['printers'] });
  629. queryClient.invalidateQueries({ queryKey: ['archives'] });
  630. },
  631. });
  632. const connectMutation = useMutation({
  633. mutationFn: () => api.connectPrinter(printer.id),
  634. onSuccess: () => {
  635. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  636. },
  637. });
  638. // Smart plug control mutations
  639. const powerControlMutation = useMutation({
  640. mutationFn: (action: 'on' | 'off') =>
  641. smartPlug ? api.controlSmartPlug(smartPlug.id, action) : Promise.reject('No plug'),
  642. onSuccess: () => {
  643. queryClient.invalidateQueries({ queryKey: ['smartPlugStatus', smartPlug?.id] });
  644. },
  645. });
  646. const toggleAutoOffMutation = useMutation({
  647. mutationFn: (enabled: boolean) =>
  648. smartPlug ? api.updateSmartPlug(smartPlug.id, { auto_off: enabled }) : Promise.reject('No plug'),
  649. onSuccess: () => {
  650. queryClient.invalidateQueries({ queryKey: ['smartPlugByPrinter', printer.id] });
  651. // Also invalidate the smart-plugs list to keep Settings page in sync
  652. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  653. },
  654. });
  655. if (shouldHide) {
  656. return null;
  657. }
  658. return (
  659. <Card className="relative">
  660. <CardContent>
  661. {/* Header */}
  662. <div className={viewMode === 'compact' ? 'mb-2' : 'mb-4'}>
  663. {/* Top row: Image, Name, Menu */}
  664. <div className="flex items-start justify-between gap-2">
  665. <div className="flex items-center gap-3 min-w-0 flex-1">
  666. {/* Printer Model Image */}
  667. <img
  668. src={getPrinterImage(printer.model)}
  669. alt={printer.model || 'Printer'}
  670. className={`object-contain rounded-lg bg-bambu-dark flex-shrink-0 ${viewMode === 'compact' ? 'w-10 h-10' : 'w-14 h-14'}`}
  671. />
  672. <div className="min-w-0 flex-1">
  673. <div className="flex items-center gap-2">
  674. <h3 className={`font-semibold text-white ${viewMode === 'compact' ? 'text-base truncate' : 'text-lg'}`}>{printer.name}</h3>
  675. {/* Connection indicator dot for compact mode */}
  676. {viewMode === 'compact' && (
  677. <div
  678. className={`w-2 h-2 rounded-full flex-shrink-0 ${
  679. status?.connected ? 'bg-bambu-green' : 'bg-red-500'
  680. }`}
  681. title={status?.connected ? 'Connected' : 'Offline'}
  682. />
  683. )}
  684. </div>
  685. <p className="text-sm text-bambu-gray">
  686. {printer.model || 'Unknown Model'}
  687. {/* Nozzle Info - only in expanded */}
  688. {viewMode === 'expanded' && status?.nozzles && status.nozzles[0]?.nozzle_diameter && (
  689. <span className="ml-1.5 text-bambu-gray" title={status.nozzles[0].nozzle_type || 'Nozzle'}>
  690. • {status.nozzles[0].nozzle_diameter}mm
  691. </span>
  692. )}
  693. {viewMode === 'expanded' && maintenanceInfo && maintenanceInfo.total_print_hours > 0 && (
  694. <span className="ml-2 text-bambu-gray">
  695. <Clock className="w-3 h-3 inline-block mr-1" />
  696. {Math.round(maintenanceInfo.total_print_hours)}h
  697. </span>
  698. )}
  699. </p>
  700. </div>
  701. </div>
  702. {/* Menu button */}
  703. <div className="relative flex-shrink-0">
  704. <Button
  705. variant="ghost"
  706. size="sm"
  707. onClick={() => setShowMenu(!showMenu)}
  708. >
  709. <MoreVertical className="w-4 h-4" />
  710. </Button>
  711. {showMenu && (
  712. <div className="absolute right-0 mt-2 w-48 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg z-10">
  713. <button
  714. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  715. onClick={() => {
  716. setShowEditModal(true);
  717. setShowMenu(false);
  718. }}
  719. >
  720. <Pencil className="w-4 h-4" />
  721. Edit
  722. </button>
  723. <button
  724. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  725. onClick={() => {
  726. connectMutation.mutate();
  727. setShowMenu(false);
  728. }}
  729. >
  730. <RefreshCw className="w-4 h-4" />
  731. Reconnect
  732. </button>
  733. <button
  734. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  735. onClick={() => {
  736. setShowMQTTDebug(true);
  737. setShowMenu(false);
  738. }}
  739. >
  740. <Terminal className="w-4 h-4" />
  741. MQTT Debug
  742. </button>
  743. <button
  744. className="w-full px-4 py-2 text-left text-sm text-red-400 hover:bg-bambu-dark-tertiary flex items-center gap-2"
  745. onClick={() => {
  746. setShowDeleteConfirm(true);
  747. setShowMenu(false);
  748. }}
  749. >
  750. <Trash2 className="w-4 h-4" />
  751. Delete
  752. </button>
  753. </div>
  754. )}
  755. </div>
  756. </div>
  757. {/* Badges row - only in expanded mode */}
  758. {viewMode === 'expanded' && (
  759. <div className="flex flex-wrap items-center gap-2 mt-2">
  760. {/* Connection status badge */}
  761. <span
  762. className={`flex items-center gap-1.5 px-2 py-1 rounded-full text-xs ${
  763. status?.connected
  764. ? 'bg-bambu-green/20 text-bambu-green'
  765. : 'bg-red-500/20 text-red-400'
  766. }`}
  767. >
  768. {status?.connected ? (
  769. <Link className="w-3 h-3" />
  770. ) : (
  771. <Unlink className="w-3 h-3" />
  772. )}
  773. {status?.connected ? 'Connected' : 'Offline'}
  774. </span>
  775. {/* WiFi signal strength indicator */}
  776. {status?.connected && wifiSignal != null && (
  777. <span
  778. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs ${
  779. wifiSignal >= -50
  780. ? 'bg-bambu-green/20 text-bambu-green'
  781. : wifiSignal >= -60
  782. ? 'bg-bambu-green/20 text-bambu-green'
  783. : wifiSignal >= -70
  784. ? 'bg-amber-500/20 text-amber-600'
  785. : wifiSignal >= -80
  786. ? 'bg-orange-500/20 text-orange-600'
  787. : 'bg-red-500/20 text-red-600'
  788. }`}
  789. title={`WiFi: ${wifiSignal} dBm - ${getWifiStrength(wifiSignal).label}`}
  790. >
  791. <Signal className="w-3 h-3" />
  792. {wifiSignal}dBm
  793. </span>
  794. )}
  795. {/* HMS Status Indicator */}
  796. {status?.connected && (() => {
  797. const knownErrors = status.hms_errors ? filterKnownHMSErrors(status.hms_errors) : [];
  798. return (
  799. <button
  800. onClick={() => setShowHMSModal(true)}
  801. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs cursor-pointer hover:opacity-80 transition-opacity ${
  802. knownErrors.length > 0
  803. ? knownErrors.some(e => e.severity <= 2)
  804. ? 'bg-red-500/20 text-red-400'
  805. : 'bg-orange-500/20 text-orange-400'
  806. : 'bg-bambu-green/20 text-bambu-green'
  807. }`}
  808. title="Click to view HMS errors"
  809. >
  810. <AlertTriangle className="w-3 h-3" />
  811. {knownErrors.length > 0 ? knownErrors.length : 'OK'}
  812. </button>
  813. );
  814. })()}
  815. {/* Maintenance Status Indicator */}
  816. {maintenanceInfo && (
  817. <button
  818. onClick={() => navigate('/maintenance')}
  819. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs cursor-pointer hover:opacity-80 transition-opacity ${
  820. maintenanceInfo.due_count > 0
  821. ? 'bg-red-500/20 text-red-400'
  822. : maintenanceInfo.warning_count > 0
  823. ? 'bg-orange-500/20 text-orange-400'
  824. : 'bg-bambu-green/20 text-bambu-green'
  825. }`}
  826. title={
  827. maintenanceInfo.due_count > 0 || maintenanceInfo.warning_count > 0
  828. ? `${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`
  829. : 'All maintenance up to date - Click to view'
  830. }
  831. >
  832. <Wrench className="w-3 h-3" />
  833. {maintenanceInfo.due_count > 0 || maintenanceInfo.warning_count > 0
  834. ? maintenanceInfo.due_count + maintenanceInfo.warning_count
  835. : 'OK'}
  836. </button>
  837. )}
  838. {/* Queue Count Badge */}
  839. {queueCount > 0 && (
  840. <button
  841. onClick={() => navigate('/queue')}
  842. 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"
  843. title={`${queueCount} print${queueCount > 1 ? 's' : ''} in queue`}
  844. >
  845. <Layers className="w-3 h-3" />
  846. {queueCount}
  847. </button>
  848. )}
  849. </div>
  850. )}
  851. </div>
  852. {/* Delete Confirmation */}
  853. {showDeleteConfirm && (
  854. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
  855. <Card className="w-full max-w-md mx-4">
  856. <CardContent>
  857. <div className="flex items-start gap-3 mb-4">
  858. <div className="p-2 rounded-full bg-red-500/20">
  859. <AlertTriangle className="w-5 h-5 text-red-400" />
  860. </div>
  861. <div>
  862. <h3 className="text-lg font-semibold text-white">Delete Printer</h3>
  863. <p className="text-sm text-bambu-gray mt-1">
  864. Are you sure you want to delete "{printer.name}"? This will remove all connection settings.
  865. </p>
  866. </div>
  867. </div>
  868. <div className="bg-bambu-dark rounded-lg p-3 mb-4">
  869. <label className="flex items-start gap-3 cursor-pointer">
  870. <input
  871. type="checkbox"
  872. checked={deleteArchives}
  873. onChange={(e) => setDeleteArchives(e.target.checked)}
  874. 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"
  875. />
  876. <div>
  877. <span className="text-sm text-white">Delete print archives</span>
  878. <p className="text-xs text-bambu-gray mt-0.5">
  879. {deleteArchives
  880. ? 'All print history for this printer will be permanently deleted.'
  881. : 'Print history will be kept but no longer associated with this printer.'}
  882. </p>
  883. </div>
  884. </label>
  885. </div>
  886. <div className="flex justify-end gap-2">
  887. <Button
  888. variant="secondary"
  889. onClick={() => {
  890. setShowDeleteConfirm(false);
  891. setDeleteArchives(true);
  892. }}
  893. >
  894. Cancel
  895. </Button>
  896. <Button
  897. variant="danger"
  898. onClick={() => {
  899. deleteMutation.mutate({ deleteArchives });
  900. setShowDeleteConfirm(false);
  901. setDeleteArchives(true);
  902. }}
  903. >
  904. Delete
  905. </Button>
  906. </div>
  907. </CardContent>
  908. </Card>
  909. </div>
  910. )}
  911. {/* Status */}
  912. {status?.connected && (
  913. <>
  914. {/* Compact: Simple status bar */}
  915. {viewMode === 'compact' ? (
  916. <div className="mt-2">
  917. {status.state === 'RUNNING' ? (
  918. <div className="flex items-center gap-2">
  919. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-1.5">
  920. <div
  921. className="bg-bambu-green h-1.5 rounded-full transition-all"
  922. style={{ width: `${status.progress || 0}%` }}
  923. />
  924. </div>
  925. <span className="text-xs text-white">{Math.round(status.progress || 0)}%</span>
  926. </div>
  927. ) : (
  928. <p className="text-xs text-bambu-gray">{getStatusDisplay(status.state, status.stg_cur_name)}</p>
  929. )}
  930. </div>
  931. ) : (
  932. /* Expanded: Full status section */
  933. <>
  934. {/* Current Print or Idle Placeholder */}
  935. <div className="mb-4 p-3 bg-bambu-dark rounded-lg">
  936. <div className="flex gap-3">
  937. {/* Cover Image */}
  938. <CoverImage
  939. url={status.state === 'RUNNING' ? status.cover_url : null}
  940. printName={status.state === 'RUNNING' ? (status.subtask_name || status.current_print || undefined) : undefined}
  941. />
  942. {/* Print Info */}
  943. <div className="flex-1 min-w-0">
  944. {status.current_print && status.state === 'RUNNING' ? (
  945. <>
  946. <p className="text-sm text-bambu-gray mb-1">{status.stg_cur_name || 'Printing'}</p>
  947. <p className="text-white text-sm mb-2 truncate">
  948. {status.subtask_name || status.current_print}
  949. </p>
  950. <div className="flex items-center justify-between text-sm">
  951. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-2 mr-3">
  952. <div
  953. className="bg-bambu-green h-2 rounded-full transition-all"
  954. style={{ width: `${status.progress || 0}%` }}
  955. />
  956. </div>
  957. <span className="text-white">{Math.round(status.progress || 0)}%</span>
  958. </div>
  959. <div className="flex items-center gap-3 mt-2 text-xs text-bambu-gray">
  960. {status.remaining_time != null && status.remaining_time > 0 && (
  961. <>
  962. <span className="flex items-center gap-1">
  963. <Clock className="w-3 h-3" />
  964. {formatTime(status.remaining_time * 60)}
  965. </span>
  966. <span className="text-bambu-green font-medium" title="Estimated completion time">
  967. ETA {formatETA(status.remaining_time)}
  968. </span>
  969. </>
  970. )}
  971. {status.layer_num != null && status.total_layers != null && status.total_layers > 0 && (
  972. <span className="flex items-center gap-1">
  973. <Layers className="w-3 h-3" />
  974. {status.layer_num}/{status.total_layers}
  975. </span>
  976. )}
  977. </div>
  978. </>
  979. ) : (
  980. <>
  981. <p className="text-sm text-bambu-gray mb-1">Status</p>
  982. <p className="text-white text-sm mb-2">
  983. {getStatusDisplay(status.state, status.stg_cur_name)}
  984. </p>
  985. <div className="flex items-center justify-between text-sm">
  986. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-2 mr-3">
  987. <div className="bg-bambu-dark-tertiary h-2 rounded-full" />
  988. </div>
  989. <span className="text-bambu-gray">—</span>
  990. </div>
  991. {lastPrint ? (
  992. <p className="text-xs text-bambu-gray mt-2 truncate" title={lastPrint.print_name || lastPrint.filename}>
  993. Last: {lastPrint.print_name || lastPrint.filename}
  994. {lastPrint.completed_at && (
  995. <span className="ml-1 text-bambu-gray/60">
  996. • {new Date(lastPrint.completed_at).toLocaleDateString([], { month: 'short', day: 'numeric' })}
  997. </span>
  998. )}
  999. </p>
  1000. ) : (
  1001. <p className="text-xs text-bambu-gray mt-2">Ready to print</p>
  1002. )}
  1003. </>
  1004. )}
  1005. </div>
  1006. </div>
  1007. </div>
  1008. {/* Queue Widget - shows next scheduled print */}
  1009. {status.state !== 'RUNNING' && (
  1010. <PrinterQueueWidget printerId={printer.id} />
  1011. )}
  1012. </>
  1013. )}
  1014. {/* Temperatures */}
  1015. {status.temperatures && viewMode === 'expanded' && (() => {
  1016. // Use actual heater states from MQTT stream
  1017. const nozzleHeating = status.temperatures.nozzle_heating || status.temperatures.nozzle_2_heating || false;
  1018. const bedHeating = status.temperatures.bed_heating || false;
  1019. const chamberHeating = status.temperatures.chamber_heating || false;
  1020. return (
  1021. <div className="grid grid-cols-3 gap-3">
  1022. {/* Nozzle temp - combined for dual nozzle */}
  1023. <div className="text-center p-2 bg-bambu-dark rounded-lg">
  1024. <HeaterThermometer className="w-4 h-4 mx-auto mb-1" color="text-orange-400" isHeating={nozzleHeating} />
  1025. {status.temperatures.nozzle_2 !== undefined ? (
  1026. <>
  1027. <p className="text-xs text-bambu-gray">Left / Right</p>
  1028. <p className="text-sm text-white">
  1029. {Math.round(status.temperatures.nozzle || 0)}°C / {Math.round(status.temperatures.nozzle_2 || 0)}°C
  1030. </p>
  1031. </>
  1032. ) : (
  1033. <>
  1034. <p className="text-xs text-bambu-gray">Nozzle</p>
  1035. <p className="text-sm text-white">
  1036. {Math.round(status.temperatures.nozzle || 0)}°C
  1037. </p>
  1038. </>
  1039. )}
  1040. </div>
  1041. <div className="text-center p-2 bg-bambu-dark rounded-lg">
  1042. <HeaterThermometer className="w-4 h-4 mx-auto mb-1" color="text-blue-400" isHeating={bedHeating} />
  1043. <p className="text-xs text-bambu-gray">Bed</p>
  1044. <p className="text-sm text-white">
  1045. {Math.round(status.temperatures.bed || 0)}°C
  1046. </p>
  1047. </div>
  1048. {status.temperatures.chamber !== undefined && (
  1049. <div className="text-center p-2 bg-bambu-dark rounded-lg">
  1050. <HeaterThermometer className="w-4 h-4 mx-auto mb-1" color="text-green-400" isHeating={chamberHeating} />
  1051. <p className="text-xs text-bambu-gray">Chamber</p>
  1052. <p className="text-sm text-white">
  1053. {Math.round(status.temperatures.chamber || 0)}°C
  1054. </p>
  1055. </div>
  1056. )}
  1057. </div>
  1058. );
  1059. })()}
  1060. {/* AMS Units with Device Icons, Humidity & Temperature */}
  1061. {amsData && amsData.length > 0 && viewMode === 'expanded' && (
  1062. <div className="mt-3 space-y-2">
  1063. {amsData.map((ams) => {
  1064. // For dual nozzle printers, determine which nozzle this AMS is connected to
  1065. // Use actual ams.id for map lookup (map uses real IDs: 0-3 for AMS, 128+ for AMS-HT)
  1066. const mappedExtruderId = amsExtruderMap[String(ams.id)];
  1067. // Fallback: normalize ID for conventional mapping (0=R, 1=L)
  1068. const normalizedId = ams.id >= 128 ? ams.id - 128 : ams.id;
  1069. const extruderId = mappedExtruderId !== undefined
  1070. ? mappedExtruderId
  1071. : normalizedId; // Fallback: AMS 0 → extruder 0 (R), AMS 1 → extruder 1 (L)
  1072. // Use printer.nozzle_count as primary source (stable), fallback to nozzle_2 temp
  1073. const isDualNozzle = printer.nozzle_count === 2 || status?.temperatures?.nozzle_2 !== undefined;
  1074. // extruder 0 = Right, extruder 1 = Left
  1075. const isLeftNozzle = extruderId === 1;
  1076. const isRightNozzle = extruderId === 0;
  1077. // Get colors for the AMS icon (null for empty slots)
  1078. const slotColors = ams.tray.map(tray =>
  1079. tray.tray_color ? `#${tray.tray_color}` : (tray.tray_type ? '#333' : null)
  1080. );
  1081. const isHtAms = ams.tray.length === 1;
  1082. return (
  1083. <div key={ams.id} className="p-2 bg-bambu-dark rounded-lg">
  1084. <div className="flex flex-wrap items-center gap-2 sm:gap-3">
  1085. {/* Nozzle badge + AMS device icon */}
  1086. <div className="flex items-center gap-1 flex-shrink-0">
  1087. {isDualNozzle && (isLeftNozzle || isRightNozzle) && (
  1088. <NozzleBadge side={isLeftNozzle ? 'L' : 'R'} />
  1089. )}
  1090. {isHtAms ? (
  1091. <AMS1TrayIcon
  1092. color={slotColors[0]}
  1093. className="flex-shrink-0"
  1094. />
  1095. ) : (
  1096. <AMS4TrayIcon
  1097. colors={slotColors as (string | null)[]}
  1098. className="flex-shrink-0"
  1099. />
  1100. )}
  1101. </div>
  1102. {/* Label and filament info */}
  1103. <div className="flex-1 min-w-0">
  1104. <span className="text-xs text-bambu-gray font-medium">
  1105. {getAmsLabel(ams.id, ams.tray.length)}
  1106. </span>
  1107. {/* Filament types and fill levels */}
  1108. <div className="mt-0.5 text-[10px] flex items-start">
  1109. {ams.tray.map((tray, i) => (
  1110. <div key={i} className="flex items-start">
  1111. <div className="flex flex-col">
  1112. <span className="text-bambu-gray/70 truncate max-w-[60px] sm:max-w-none">
  1113. {tray.tray_type ? (tray.tray_sub_brands || tray.tray_type) : '—'}
  1114. </span>
  1115. <span className="text-bambu-gray/50 truncate">
  1116. {tray.tray_type && tray.remain >= 0 ? `${tray.remain}%` : '—'}
  1117. </span>
  1118. </div>
  1119. {i < ams.tray.length - 1 && (
  1120. <span className="text-bambu-gray/50 mx-1 flex flex-col">
  1121. <span>·</span>
  1122. <span>·</span>
  1123. </span>
  1124. )}
  1125. </div>
  1126. ))}
  1127. </div>
  1128. </div>
  1129. {/* Humidity/temp - responsive positioning */}
  1130. {(ams.humidity != null || ams.temp != null) && (
  1131. <div className="flex items-center gap-2 text-xs flex-shrink-0 ml-auto">
  1132. {ams.humidity != null && (
  1133. <HumidityIndicator
  1134. humidity={ams.humidity}
  1135. goodThreshold={amsThresholds?.humidityGood}
  1136. fairThreshold={amsThresholds?.humidityFair}
  1137. onClick={() => setAmsHistoryModal({
  1138. amsId: ams.id,
  1139. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1140. mode: 'humidity',
  1141. })}
  1142. />
  1143. )}
  1144. {ams.temp != null && (
  1145. <TemperatureIndicator
  1146. temp={ams.temp}
  1147. goodThreshold={amsThresholds?.tempGood}
  1148. fairThreshold={amsThresholds?.tempFair}
  1149. onClick={() => setAmsHistoryModal({
  1150. amsId: ams.id,
  1151. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1152. mode: 'temperature',
  1153. })}
  1154. />
  1155. )}
  1156. </div>
  1157. )}
  1158. </div>
  1159. </div>
  1160. );
  1161. })}
  1162. {/* External spool indicator */}
  1163. {status.vt_tray && status.vt_tray.tray_type && (
  1164. <div className="p-2 bg-bambu-dark rounded-lg">
  1165. <div className="flex items-center gap-3">
  1166. <div
  1167. className="w-10 h-10 rounded-full border-2 border-white/20 flex-shrink-0"
  1168. style={{
  1169. backgroundColor: status.vt_tray.tray_color ? `#${status.vt_tray.tray_color}` : '#333',
  1170. }}
  1171. />
  1172. <div>
  1173. <span className="text-xs text-bambu-gray font-medium">External</span>
  1174. <p className="text-[10px] text-bambu-gray/70">
  1175. {status.vt_tray.tray_sub_brands || status.vt_tray.tray_type || 'Spool'}
  1176. </p>
  1177. </div>
  1178. </div>
  1179. </div>
  1180. )}
  1181. </div>
  1182. )}
  1183. </>
  1184. )}
  1185. {/* Smart Plug Controls - hidden in compact mode */}
  1186. {smartPlug && viewMode === 'expanded' && (
  1187. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary">
  1188. <div className="flex items-center gap-3">
  1189. {/* Plug name and status */}
  1190. <div className="flex items-center gap-2 min-w-0">
  1191. <Zap className="w-4 h-4 text-bambu-gray flex-shrink-0" />
  1192. <span className="text-sm text-white truncate">{smartPlug.name}</span>
  1193. {plugStatus && (
  1194. <span
  1195. className={`text-xs px-1.5 py-0.5 rounded flex-shrink-0 ${
  1196. plugStatus.state === 'ON'
  1197. ? 'bg-bambu-green/20 text-bambu-green'
  1198. : plugStatus.state === 'OFF'
  1199. ? 'bg-red-500/20 text-red-400'
  1200. : 'bg-bambu-gray/20 text-bambu-gray'
  1201. }`}
  1202. >
  1203. {plugStatus.state || '?'}
  1204. </span>
  1205. )}
  1206. {/* Power consumption display */}
  1207. {plugStatus?.energy?.power != null && plugStatus.state === 'ON' && (
  1208. <span className="text-xs text-yellow-400 font-medium flex-shrink-0">
  1209. {plugStatus.energy.power}W
  1210. </span>
  1211. )}
  1212. </div>
  1213. {/* Spacer */}
  1214. <div className="flex-1" />
  1215. {/* Power buttons */}
  1216. <div className="flex items-center gap-1">
  1217. <button
  1218. onClick={() => setShowPowerOnConfirm(true)}
  1219. disabled={powerControlMutation.isPending || plugStatus?.state === 'ON'}
  1220. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  1221. plugStatus?.state === 'ON'
  1222. ? 'bg-bambu-green text-white'
  1223. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  1224. }`}
  1225. >
  1226. <Power className="w-3 h-3" />
  1227. On
  1228. </button>
  1229. <button
  1230. onClick={() => setShowPowerOffConfirm(true)}
  1231. disabled={powerControlMutation.isPending || plugStatus?.state === 'OFF'}
  1232. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  1233. plugStatus?.state === 'OFF'
  1234. ? 'bg-red-500/30 text-red-400'
  1235. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  1236. }`}
  1237. >
  1238. <PowerOff className="w-3 h-3" />
  1239. Off
  1240. </button>
  1241. </div>
  1242. {/* Auto-off toggle */}
  1243. <div className="flex items-center gap-2 flex-shrink-0">
  1244. <span className={`text-xs hidden sm:inline ${smartPlug.auto_off_executed ? 'text-bambu-green' : 'text-bambu-gray'}`}>
  1245. {smartPlug.auto_off_executed ? 'Auto-off done' : 'Auto-off'}
  1246. </span>
  1247. <button
  1248. onClick={() => toggleAutoOffMutation.mutate(!smartPlug.auto_off)}
  1249. disabled={toggleAutoOffMutation.isPending || smartPlug.auto_off_executed}
  1250. title={smartPlug.auto_off_executed ? 'Auto-off was executed - turn printer on to reset' : 'Auto power-off after print'}
  1251. className={`relative w-9 h-5 rounded-full transition-colors flex-shrink-0 ${
  1252. smartPlug.auto_off_executed
  1253. ? 'bg-bambu-green/50 cursor-not-allowed'
  1254. : smartPlug.auto_off ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  1255. }`}
  1256. >
  1257. <span
  1258. className={`absolute top-[2px] left-[2px] w-4 h-4 bg-white rounded-full transition-transform ${
  1259. smartPlug.auto_off || smartPlug.auto_off_executed ? 'translate-x-4' : 'translate-x-0'
  1260. }`}
  1261. />
  1262. </button>
  1263. </div>
  1264. </div>
  1265. </div>
  1266. )}
  1267. {/* Connection Info & Actions - hidden in compact mode */}
  1268. {viewMode === 'expanded' && (
  1269. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary flex items-center justify-between">
  1270. <div className="text-xs text-bambu-gray">
  1271. <p>{printer.ip_address}</p>
  1272. <p className="truncate">{printer.serial_number}</p>
  1273. </div>
  1274. <div className="flex items-center gap-2">
  1275. <Button
  1276. variant="secondary"
  1277. size="sm"
  1278. onClick={() => {
  1279. window.open(
  1280. `/camera/${printer.id}`,
  1281. `camera-${printer.id}`,
  1282. 'width=640,height=400,menubar=no,toolbar=no,location=no,status=no'
  1283. );
  1284. }}
  1285. disabled={!status?.connected}
  1286. title="Open camera in new window"
  1287. >
  1288. <Video className="w-4 h-4" />
  1289. </Button>
  1290. <Button
  1291. variant="secondary"
  1292. size="sm"
  1293. onClick={() => setShowFileManager(true)}
  1294. title="Browse printer files"
  1295. >
  1296. <HardDrive className="w-4 h-4" />
  1297. Files
  1298. </Button>
  1299. </div>
  1300. </div>
  1301. )}
  1302. </CardContent>
  1303. {/* File Manager Modal */}
  1304. {showFileManager && (
  1305. <FileManagerModal
  1306. printerId={printer.id}
  1307. printerName={printer.name}
  1308. onClose={() => setShowFileManager(false)}
  1309. />
  1310. )}
  1311. {/* MQTT Debug Modal */}
  1312. {showMQTTDebug && (
  1313. <MQTTDebugModal
  1314. printerId={printer.id}
  1315. printerName={printer.name}
  1316. onClose={() => setShowMQTTDebug(false)}
  1317. />
  1318. )}
  1319. {/* Power On Confirmation */}
  1320. {showPowerOnConfirm && smartPlug && (
  1321. <ConfirmModal
  1322. title="Power On Printer"
  1323. message={`Are you sure you want to turn ON the power for "${printer.name}"?`}
  1324. confirmText="Power On"
  1325. variant="default"
  1326. onConfirm={() => {
  1327. powerControlMutation.mutate('on');
  1328. setShowPowerOnConfirm(false);
  1329. }}
  1330. onCancel={() => setShowPowerOnConfirm(false)}
  1331. />
  1332. )}
  1333. {/* Power Off Confirmation */}
  1334. {showPowerOffConfirm && smartPlug && (
  1335. <ConfirmModal
  1336. title="Power Off Printer"
  1337. message={
  1338. status?.state === 'RUNNING'
  1339. ? `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.`
  1340. : `Are you sure you want to turn OFF the power for "${printer.name}"?`
  1341. }
  1342. confirmText="Power Off"
  1343. variant="danger"
  1344. onConfirm={() => {
  1345. powerControlMutation.mutate('off');
  1346. setShowPowerOffConfirm(false);
  1347. }}
  1348. onCancel={() => setShowPowerOffConfirm(false)}
  1349. />
  1350. )}
  1351. {/* HMS Error Modal */}
  1352. {showHMSModal && (
  1353. <HMSErrorModal
  1354. printerName={printer.name}
  1355. errors={status?.hms_errors || []}
  1356. onClose={() => setShowHMSModal(false)}
  1357. />
  1358. )}
  1359. {/* AMS History Modal */}
  1360. {amsHistoryModal && (
  1361. <AMSHistoryModal
  1362. isOpen={!!amsHistoryModal}
  1363. onClose={() => setAmsHistoryModal(null)}
  1364. printerId={printer.id}
  1365. printerName={printer.name}
  1366. amsId={amsHistoryModal.amsId}
  1367. amsLabel={amsHistoryModal.amsLabel}
  1368. initialMode={amsHistoryModal.mode}
  1369. thresholds={amsThresholds}
  1370. />
  1371. )}
  1372. {/* Edit Printer Modal */}
  1373. {showEditModal && (
  1374. <EditPrinterModal
  1375. printer={printer}
  1376. onClose={() => setShowEditModal(false)}
  1377. />
  1378. )}
  1379. </Card>
  1380. );
  1381. }
  1382. function AddPrinterModal({
  1383. onClose,
  1384. onAdd,
  1385. existingSerials,
  1386. }: {
  1387. onClose: () => void;
  1388. onAdd: (data: PrinterCreate) => void;
  1389. existingSerials: string[];
  1390. }) {
  1391. const [form, setForm] = useState<PrinterCreate>({
  1392. name: '',
  1393. serial_number: '',
  1394. ip_address: '',
  1395. access_code: '',
  1396. model: '',
  1397. auto_archive: true,
  1398. });
  1399. // Discovery state
  1400. const [discovering, setDiscovering] = useState(false);
  1401. const [discovered, setDiscovered] = useState<DiscoveredPrinter[]>([]);
  1402. const [discoveryError, setDiscoveryError] = useState('');
  1403. const [hasScanned, setHasScanned] = useState(false);
  1404. const [isDocker, setIsDocker] = useState(false);
  1405. const [subnet, setSubnet] = useState('192.168.1.0/24');
  1406. const [scanProgress, setScanProgress] = useState({ scanned: 0, total: 0 });
  1407. // Fetch discovery info on mount
  1408. useEffect(() => {
  1409. discoveryApi.getInfo().then(info => {
  1410. setIsDocker(info.is_docker);
  1411. }).catch(() => {
  1412. // Ignore errors, assume not Docker
  1413. });
  1414. }, []);
  1415. // Filter out already-added printers
  1416. const newPrinters = discovered.filter(p => !existingSerials.includes(p.serial));
  1417. const startDiscovery = async () => {
  1418. setDiscoveryError('');
  1419. setDiscovered([]);
  1420. setDiscovering(true);
  1421. setHasScanned(false);
  1422. setScanProgress({ scanned: 0, total: 0 });
  1423. try {
  1424. if (isDocker) {
  1425. // Use subnet scanning for Docker
  1426. await discoveryApi.startSubnetScan(subnet);
  1427. // Poll for scan status and results
  1428. const pollInterval = setInterval(async () => {
  1429. try {
  1430. const status = await discoveryApi.getScanStatus();
  1431. setScanProgress({ scanned: status.scanned, total: status.total });
  1432. const printers = await discoveryApi.getDiscoveredPrinters();
  1433. setDiscovered(printers);
  1434. if (!status.running) {
  1435. clearInterval(pollInterval);
  1436. setDiscovering(false);
  1437. setHasScanned(true);
  1438. }
  1439. } catch (e) {
  1440. console.error('Failed to get scan status:', e);
  1441. }
  1442. }, 500);
  1443. } else {
  1444. // Use SSDP discovery for native installs
  1445. await discoveryApi.startDiscovery(10);
  1446. // Poll for discovered printers every second
  1447. const pollInterval = setInterval(async () => {
  1448. try {
  1449. const printers = await discoveryApi.getDiscoveredPrinters();
  1450. setDiscovered(printers);
  1451. } catch (e) {
  1452. console.error('Failed to get discovered printers:', e);
  1453. }
  1454. }, 1000);
  1455. // Stop after 10 seconds
  1456. setTimeout(async () => {
  1457. clearInterval(pollInterval);
  1458. try {
  1459. await discoveryApi.stopDiscovery();
  1460. } catch (e) {
  1461. // Ignore stop errors
  1462. }
  1463. setDiscovering(false);
  1464. setHasScanned(true);
  1465. // Final fetch
  1466. try {
  1467. const printers = await discoveryApi.getDiscoveredPrinters();
  1468. setDiscovered(printers);
  1469. } catch (e) {
  1470. console.error('Failed to get final discovered printers:', e);
  1471. }
  1472. }, 10000);
  1473. }
  1474. } catch (e) {
  1475. console.error('Failed to start discovery:', e);
  1476. setDiscoveryError(e instanceof Error ? e.message : 'Failed to start discovery');
  1477. setDiscovering(false);
  1478. setHasScanned(true);
  1479. }
  1480. };
  1481. // Map SSDP model codes to dropdown values
  1482. const mapModelCode = (ssdpModel: string | null): string => {
  1483. if (!ssdpModel) return '';
  1484. const modelMap: Record<string, string> = {
  1485. // H2 Series
  1486. 'O1D': 'H2D',
  1487. 'O1C': 'H2C',
  1488. 'O1S': 'H2S',
  1489. // X1 Series
  1490. 'BL-P001': 'X1C',
  1491. 'BL-P002': 'X1',
  1492. 'BL-P003': 'X1E',
  1493. // P Series
  1494. 'C11': 'P1S',
  1495. 'C12': 'P1P',
  1496. 'C13': 'P2S',
  1497. // A1 Series
  1498. 'N2S': 'A1',
  1499. 'N1': 'A1 Mini',
  1500. // Direct matches
  1501. 'X1C': 'X1C',
  1502. 'X1': 'X1',
  1503. 'X1E': 'X1E',
  1504. 'P1S': 'P1S',
  1505. 'P1P': 'P1P',
  1506. 'P2S': 'P2S',
  1507. 'A1': 'A1',
  1508. 'A1 Mini': 'A1 Mini',
  1509. 'H2D': 'H2D',
  1510. 'H2C': 'H2C',
  1511. 'H2S': 'H2S',
  1512. };
  1513. return modelMap[ssdpModel] || ssdpModel;
  1514. };
  1515. const selectPrinter = (printer: DiscoveredPrinter) => {
  1516. setForm({
  1517. ...form,
  1518. name: printer.name || '',
  1519. serial_number: printer.serial,
  1520. ip_address: printer.ip_address,
  1521. model: mapModelCode(printer.model),
  1522. });
  1523. // Clear discovery results after selection
  1524. setDiscovered([]);
  1525. };
  1526. // Cleanup discovery on unmount
  1527. useEffect(() => {
  1528. return () => {
  1529. discoveryApi.stopDiscovery().catch(() => {});
  1530. discoveryApi.stopSubnetScan().catch(() => {});
  1531. };
  1532. }, []);
  1533. // Close on Escape key
  1534. useEffect(() => {
  1535. const handleKeyDown = (e: KeyboardEvent) => {
  1536. if (e.key === 'Escape') onClose();
  1537. };
  1538. window.addEventListener('keydown', handleKeyDown);
  1539. return () => window.removeEventListener('keydown', handleKeyDown);
  1540. }, [onClose]);
  1541. return (
  1542. <div
  1543. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"
  1544. onClick={onClose}
  1545. >
  1546. <Card className="w-full max-w-md" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
  1547. <CardContent>
  1548. <h2 className="text-xl font-semibold mb-4">Add Printer</h2>
  1549. {/* Discovery Section */}
  1550. <div className="mb-4 pb-4 border-b border-bambu-dark-tertiary">
  1551. {isDocker && (
  1552. <div className="mb-3">
  1553. <label className="block text-sm text-bambu-gray mb-1">
  1554. Subnet to scan
  1555. </label>
  1556. <input
  1557. type="text"
  1558. 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"
  1559. value={subnet}
  1560. onChange={(e) => setSubnet(e.target.value)}
  1561. placeholder="192.168.1.0/24"
  1562. disabled={discovering}
  1563. />
  1564. <p className="mt-1 text-xs text-bambu-gray">
  1565. Docker detected. Enter your printer's subnet in CIDR notation.
  1566. Requires <code className="text-bambu-green">network_mode: host</code> in docker-compose.yml.
  1567. </p>
  1568. </div>
  1569. )}
  1570. <Button
  1571. type="button"
  1572. variant="secondary"
  1573. onClick={startDiscovery}
  1574. disabled={discovering}
  1575. className="w-full"
  1576. >
  1577. {discovering ? (
  1578. <>
  1579. <Loader2 className="w-4 h-4 animate-spin" />
  1580. {isDocker && scanProgress.total > 0
  1581. ? `Scanning... ${scanProgress.scanned}/${scanProgress.total}`
  1582. : 'Scanning...'}
  1583. </>
  1584. ) : (
  1585. <>
  1586. <Search className="w-4 h-4" />
  1587. {isDocker ? 'Scan Subnet for Printers' : 'Discover Printers on Network'}
  1588. </>
  1589. )}
  1590. </Button>
  1591. {discoveryError && (
  1592. <div className="mt-2 text-sm text-red-400">{discoveryError}</div>
  1593. )}
  1594. {newPrinters.length > 0 && (
  1595. <div className="mt-3 space-y-2 max-h-40 overflow-y-auto">
  1596. {newPrinters.map((printer) => (
  1597. <div
  1598. key={printer.serial}
  1599. className="flex items-center justify-between p-2 bg-bambu-dark rounded-lg hover:bg-bambu-dark-secondary cursor-pointer transition-colors"
  1600. onClick={() => selectPrinter(printer)}
  1601. >
  1602. <div className="min-w-0 flex-1">
  1603. <p className="font-medium text-white text-sm truncate">
  1604. {printer.name || printer.serial}
  1605. </p>
  1606. <p className="text-xs text-bambu-gray truncate">
  1607. {mapModelCode(printer.model) || 'Unknown'} • {printer.ip_address}
  1608. </p>
  1609. </div>
  1610. <ChevronDown className="w-4 h-4 text-bambu-gray -rotate-90 flex-shrink-0 ml-2" />
  1611. </div>
  1612. ))}
  1613. </div>
  1614. )}
  1615. {discovering && (
  1616. <p className="mt-2 text-sm text-bambu-gray text-center">
  1617. {isDocker ? 'Scanning subnet for Bambu printers...' : 'Scanning network...'}
  1618. </p>
  1619. )}
  1620. {hasScanned && !discovering && discovered.length === 0 && (
  1621. <p className="mt-2 text-sm text-bambu-gray text-center">
  1622. No printers found{isDocker ? ' in the specified subnet' : ' on the network'}.
  1623. </p>
  1624. )}
  1625. {hasScanned && !discovering && discovered.length > 0 && newPrinters.length === 0 && (
  1626. <p className="mt-2 text-sm text-bambu-gray text-center">
  1627. All discovered printers are already configured.
  1628. </p>
  1629. )}
  1630. </div>
  1631. <form
  1632. onSubmit={(e) => {
  1633. e.preventDefault();
  1634. onAdd(form);
  1635. }}
  1636. className="space-y-4"
  1637. >
  1638. <div>
  1639. <label className="block text-sm text-bambu-gray mb-1">Name</label>
  1640. <input
  1641. type="text"
  1642. required
  1643. 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"
  1644. value={form.name}
  1645. onChange={(e) => setForm({ ...form, name: e.target.value })}
  1646. placeholder="My Printer"
  1647. />
  1648. </div>
  1649. <div>
  1650. <label className="block text-sm text-bambu-gray mb-1">IP Address</label>
  1651. <input
  1652. type="text"
  1653. required
  1654. pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
  1655. 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"
  1656. value={form.ip_address}
  1657. onChange={(e) => setForm({ ...form, ip_address: e.target.value })}
  1658. placeholder="192.168.1.100"
  1659. />
  1660. </div>
  1661. <div>
  1662. <label className="block text-sm text-bambu-gray mb-1">Serial Number</label>
  1663. <input
  1664. type="text"
  1665. required
  1666. 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"
  1667. value={form.serial_number}
  1668. onChange={(e) => setForm({ ...form, serial_number: e.target.value })}
  1669. placeholder="01P00A000000000"
  1670. />
  1671. </div>
  1672. <div>
  1673. <label className="block text-sm text-bambu-gray mb-1">Access Code</label>
  1674. <input
  1675. type="password"
  1676. required
  1677. 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"
  1678. value={form.access_code}
  1679. onChange={(e) => setForm({ ...form, access_code: e.target.value })}
  1680. placeholder="From printer settings"
  1681. />
  1682. </div>
  1683. <div>
  1684. <label className="block text-sm text-bambu-gray mb-1">Model (optional)</label>
  1685. <select
  1686. 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"
  1687. value={form.model || ''}
  1688. onChange={(e) => setForm({ ...form, model: e.target.value })}
  1689. >
  1690. <option value="">Select model...</option>
  1691. <optgroup label="H2 Series">
  1692. <option value="H2C">H2C</option>
  1693. <option value="H2D">H2D</option>
  1694. <option value="H2S">H2S</option>
  1695. </optgroup>
  1696. <optgroup label="X1 Series">
  1697. <option value="X1E">X1E</option>
  1698. <option value="X1C">X1 Carbon</option>
  1699. <option value="X1">X1</option>
  1700. </optgroup>
  1701. <optgroup label="P Series">
  1702. <option value="P2S">P2S</option>
  1703. <option value="P1S">P1S</option>
  1704. <option value="P1P">P1P</option>
  1705. </optgroup>
  1706. <optgroup label="A1 Series">
  1707. <option value="A1">A1</option>
  1708. <option value="A1 Mini">A1 Mini</option>
  1709. </optgroup>
  1710. </select>
  1711. </div>
  1712. <div className="flex items-center gap-2">
  1713. <input
  1714. type="checkbox"
  1715. id="auto_archive"
  1716. checked={form.auto_archive}
  1717. onChange={(e) => setForm({ ...form, auto_archive: e.target.checked })}
  1718. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  1719. />
  1720. <label htmlFor="auto_archive" className="text-sm text-bambu-gray">
  1721. Auto-archive completed prints
  1722. </label>
  1723. </div>
  1724. <div className="flex gap-3 pt-4">
  1725. <Button type="button" variant="secondary" onClick={onClose} className="flex-1">
  1726. Cancel
  1727. </Button>
  1728. <Button type="submit" className="flex-1">
  1729. Add Printer
  1730. </Button>
  1731. </div>
  1732. </form>
  1733. </CardContent>
  1734. </Card>
  1735. </div>
  1736. );
  1737. }
  1738. function EditPrinterModal({
  1739. printer,
  1740. onClose,
  1741. }: {
  1742. printer: Printer;
  1743. onClose: () => void;
  1744. }) {
  1745. const queryClient = useQueryClient();
  1746. const [form, setForm] = useState({
  1747. name: printer.name,
  1748. ip_address: printer.ip_address,
  1749. access_code: '',
  1750. model: printer.model || '',
  1751. location: printer.location || '',
  1752. auto_archive: printer.auto_archive,
  1753. });
  1754. const updateMutation = useMutation({
  1755. mutationFn: (data: Partial<PrinterCreate>) => api.updatePrinter(printer.id, data),
  1756. onSuccess: () => {
  1757. queryClient.invalidateQueries({ queryKey: ['printers'] });
  1758. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  1759. onClose();
  1760. },
  1761. });
  1762. // Close on Escape key
  1763. useEffect(() => {
  1764. const handleKeyDown = (e: KeyboardEvent) => {
  1765. if (e.key === 'Escape') onClose();
  1766. };
  1767. window.addEventListener('keydown', handleKeyDown);
  1768. return () => window.removeEventListener('keydown', handleKeyDown);
  1769. }, [onClose]);
  1770. const handleSubmit = (e: React.FormEvent) => {
  1771. e.preventDefault();
  1772. const data: Partial<PrinterCreate> = {
  1773. name: form.name,
  1774. ip_address: form.ip_address,
  1775. model: form.model || undefined,
  1776. location: form.location || undefined,
  1777. auto_archive: form.auto_archive,
  1778. };
  1779. // Only include access_code if it was changed
  1780. if (form.access_code) {
  1781. data.access_code = form.access_code;
  1782. }
  1783. updateMutation.mutate(data);
  1784. };
  1785. return (
  1786. <div
  1787. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"
  1788. onClick={onClose}
  1789. >
  1790. <Card className="w-full max-w-md" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
  1791. <CardContent>
  1792. <h2 className="text-xl font-semibold mb-4">Edit Printer</h2>
  1793. <form onSubmit={handleSubmit} className="space-y-4">
  1794. <div>
  1795. <label className="block text-sm text-bambu-gray mb-1">Name</label>
  1796. <input
  1797. type="text"
  1798. required
  1799. 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"
  1800. value={form.name}
  1801. onChange={(e) => setForm({ ...form, name: e.target.value })}
  1802. placeholder="My Printer"
  1803. />
  1804. </div>
  1805. <div>
  1806. <label className="block text-sm text-bambu-gray mb-1">IP Address</label>
  1807. <input
  1808. type="text"
  1809. required
  1810. pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
  1811. 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"
  1812. value={form.ip_address}
  1813. onChange={(e) => setForm({ ...form, ip_address: e.target.value })}
  1814. placeholder="192.168.1.100"
  1815. />
  1816. </div>
  1817. <div>
  1818. <label className="block text-sm text-bambu-gray mb-1">Serial Number</label>
  1819. <input
  1820. type="text"
  1821. disabled
  1822. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray cursor-not-allowed"
  1823. value={printer.serial_number}
  1824. />
  1825. <p className="text-xs text-bambu-gray mt-1">Serial number cannot be changed</p>
  1826. </div>
  1827. <div>
  1828. <label className="block text-sm text-bambu-gray mb-1">Access Code</label>
  1829. <input
  1830. type="password"
  1831. 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"
  1832. value={form.access_code}
  1833. onChange={(e) => setForm({ ...form, access_code: e.target.value })}
  1834. placeholder="Leave empty to keep current"
  1835. />
  1836. </div>
  1837. <div>
  1838. <label className="block text-sm text-bambu-gray mb-1">Model</label>
  1839. <select
  1840. 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"
  1841. value={form.model}
  1842. onChange={(e) => setForm({ ...form, model: e.target.value })}
  1843. >
  1844. <option value="">Select model...</option>
  1845. <optgroup label="H2 Series">
  1846. <option value="H2C">H2C</option>
  1847. <option value="H2D">H2D</option>
  1848. <option value="H2S">H2S</option>
  1849. </optgroup>
  1850. <optgroup label="X1 Series">
  1851. <option value="X1E">X1E</option>
  1852. <option value="X1C">X1 Carbon</option>
  1853. <option value="X1">X1</option>
  1854. </optgroup>
  1855. <optgroup label="P Series">
  1856. <option value="P2S">P2S</option>
  1857. <option value="P1S">P1S</option>
  1858. <option value="P1P">P1P</option>
  1859. </optgroup>
  1860. <optgroup label="A1 Series">
  1861. <option value="A1">A1</option>
  1862. <option value="A1 Mini">A1 Mini</option>
  1863. </optgroup>
  1864. </select>
  1865. </div>
  1866. <div>
  1867. <label className="block text-sm text-bambu-gray mb-1">Location / Group</label>
  1868. <input
  1869. type="text"
  1870. 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"
  1871. value={form.location}
  1872. onChange={(e) => setForm({ ...form, location: e.target.value })}
  1873. placeholder="e.g., Workshop, Office, Basement"
  1874. />
  1875. <p className="text-xs text-bambu-gray mt-1">Used to group printers on the dashboard</p>
  1876. </div>
  1877. <div className="flex items-center gap-2">
  1878. <input
  1879. type="checkbox"
  1880. id="edit_auto_archive"
  1881. checked={form.auto_archive}
  1882. onChange={(e) => setForm({ ...form, auto_archive: e.target.checked })}
  1883. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  1884. />
  1885. <label htmlFor="edit_auto_archive" className="text-sm text-bambu-gray">
  1886. Auto-archive completed prints
  1887. </label>
  1888. </div>
  1889. <div className="flex gap-3 pt-4">
  1890. <Button type="button" variant="secondary" onClick={onClose} className="flex-1">
  1891. Cancel
  1892. </Button>
  1893. <Button type="submit" className="flex-1" disabled={updateMutation.isPending}>
  1894. {updateMutation.isPending ? 'Saving...' : 'Save Changes'}
  1895. </Button>
  1896. </div>
  1897. </form>
  1898. </CardContent>
  1899. </Card>
  1900. </div>
  1901. );
  1902. }
  1903. // Component to check if a printer is offline (for power dropdown)
  1904. function usePrinterOfflineStatus(printerId: number) {
  1905. const { data: status } = useQuery({
  1906. queryKey: ['printerStatus', printerId],
  1907. queryFn: () => api.getPrinterStatus(printerId),
  1908. refetchInterval: 30000,
  1909. });
  1910. return !status?.connected;
  1911. }
  1912. // Power dropdown item for an offline printer
  1913. function PowerDropdownItem({
  1914. printer,
  1915. plug,
  1916. onPowerOn,
  1917. isPowering,
  1918. }: {
  1919. printer: Printer;
  1920. plug: { id: number; name: string };
  1921. onPowerOn: (plugId: number) => void;
  1922. isPowering: boolean;
  1923. }) {
  1924. const isOffline = usePrinterOfflineStatus(printer.id);
  1925. // Fetch plug status
  1926. const { data: plugStatus } = useQuery({
  1927. queryKey: ['smartPlugStatus', plug.id],
  1928. queryFn: () => api.getSmartPlugStatus(plug.id),
  1929. refetchInterval: 10000,
  1930. });
  1931. // Only show if printer is offline
  1932. if (!isOffline) {
  1933. return null;
  1934. }
  1935. return (
  1936. <div className="flex items-center justify-between px-3 py-2 hover:bg-gray-100 dark:hover:bg-bambu-dark-tertiary">
  1937. <div className="flex items-center gap-2 min-w-0">
  1938. <span className="text-sm text-gray-900 dark:text-white truncate">{printer.name}</span>
  1939. {plugStatus && (
  1940. <span
  1941. className={`text-xs px-1.5 py-0.5 rounded ${
  1942. plugStatus.state === 'ON'
  1943. ? 'bg-bambu-green/20 text-bambu-green'
  1944. : 'bg-red-500/20 text-red-400'
  1945. }`}
  1946. >
  1947. {plugStatus.state || '?'}
  1948. </span>
  1949. )}
  1950. </div>
  1951. <button
  1952. onClick={() => onPowerOn(plug.id)}
  1953. disabled={isPowering || plugStatus?.state === 'ON'}
  1954. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  1955. plugStatus?.state === 'ON'
  1956. ? 'bg-bambu-green/20 text-bambu-green cursor-default'
  1957. : 'bg-bambu-green/20 text-bambu-green hover:bg-bambu-green hover:text-white'
  1958. }`}
  1959. >
  1960. <Power className="w-3 h-3" />
  1961. {isPowering ? '...' : 'On'}
  1962. </button>
  1963. </div>
  1964. );
  1965. }
  1966. export function PrintersPage() {
  1967. const [showAddModal, setShowAddModal] = useState(false);
  1968. const [hideDisconnected, setHideDisconnected] = useState(() => {
  1969. return localStorage.getItem('hideDisconnectedPrinters') === 'true';
  1970. });
  1971. const [showPowerDropdown, setShowPowerDropdown] = useState(false);
  1972. const [poweringOn, setPoweringOn] = useState<number | null>(null);
  1973. const [sortBy, setSortBy] = useState<SortOption>(() => {
  1974. return (localStorage.getItem('printerSortBy') as SortOption) || 'name';
  1975. });
  1976. const [sortAsc, setSortAsc] = useState<boolean>(() => {
  1977. return localStorage.getItem('printerSortAsc') !== 'false';
  1978. });
  1979. const [viewMode, setViewMode] = useState<ViewMode>(() => {
  1980. return (localStorage.getItem('printerViewMode') as ViewMode) || 'expanded';
  1981. });
  1982. const queryClient = useQueryClient();
  1983. const { data: printers, isLoading } = useQuery({
  1984. queryKey: ['printers'],
  1985. queryFn: api.getPrinters,
  1986. });
  1987. // Fetch app settings for AMS thresholds
  1988. const { data: settings } = useQuery({
  1989. queryKey: ['settings'],
  1990. queryFn: api.getSettings,
  1991. });
  1992. // Fetch all smart plugs to know which printers have them
  1993. const { data: smartPlugs } = useQuery({
  1994. queryKey: ['smart-plugs'],
  1995. queryFn: api.getSmartPlugs,
  1996. });
  1997. // Fetch maintenance overview for all printers to show badges
  1998. const { data: maintenanceOverview } = useQuery({
  1999. queryKey: ['maintenanceOverview'],
  2000. queryFn: api.getMaintenanceOverview,
  2001. staleTime: 60 * 1000, // 1 minute
  2002. });
  2003. // Create a map of printer_id -> maintenance info for quick lookup
  2004. const maintenanceByPrinter = maintenanceOverview?.reduce(
  2005. (acc, overview) => {
  2006. acc[overview.printer_id] = {
  2007. due_count: overview.due_count,
  2008. warning_count: overview.warning_count,
  2009. total_print_hours: overview.total_print_hours,
  2010. };
  2011. return acc;
  2012. },
  2013. {} as Record<number, PrinterMaintenanceInfo>
  2014. ) || {};
  2015. // Create a map of printer_id -> smart plug
  2016. const smartPlugByPrinter = smartPlugs?.reduce(
  2017. (acc, plug) => {
  2018. if (plug.printer_id) {
  2019. acc[plug.printer_id] = plug;
  2020. }
  2021. return acc;
  2022. },
  2023. {} as Record<number, typeof smartPlugs[0]>
  2024. ) || {};
  2025. const addMutation = useMutation({
  2026. mutationFn: api.createPrinter,
  2027. onSuccess: () => {
  2028. queryClient.invalidateQueries({ queryKey: ['printers'] });
  2029. setShowAddModal(false);
  2030. },
  2031. });
  2032. const powerOnMutation = useMutation({
  2033. mutationFn: (plugId: number) => api.controlSmartPlug(plugId, 'on'),
  2034. onSuccess: () => {
  2035. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  2036. setPoweringOn(null);
  2037. },
  2038. onError: () => {
  2039. setPoweringOn(null);
  2040. },
  2041. });
  2042. const toggleHideDisconnected = () => {
  2043. const newValue = !hideDisconnected;
  2044. setHideDisconnected(newValue);
  2045. localStorage.setItem('hideDisconnectedPrinters', String(newValue));
  2046. };
  2047. const handleSortChange = (newSort: SortOption) => {
  2048. setSortBy(newSort);
  2049. localStorage.setItem('printerSortBy', newSort);
  2050. };
  2051. const toggleSortDirection = () => {
  2052. const newAsc = !sortAsc;
  2053. setSortAsc(newAsc);
  2054. localStorage.setItem('printerSortAsc', String(newAsc));
  2055. };
  2056. const toggleViewMode = () => {
  2057. const newMode = viewMode === 'expanded' ? 'compact' : 'expanded';
  2058. setViewMode(newMode);
  2059. localStorage.setItem('printerViewMode', newMode);
  2060. };
  2061. // Sort printers based on selected option
  2062. const sortedPrinters = useMemo(() => {
  2063. if (!printers) return [];
  2064. const sorted = [...printers];
  2065. switch (sortBy) {
  2066. case 'name':
  2067. sorted.sort((a, b) => a.name.localeCompare(b.name));
  2068. break;
  2069. case 'model':
  2070. sorted.sort((a, b) => (a.model || '').localeCompare(b.model || ''));
  2071. break;
  2072. case 'location':
  2073. // Sort by location, with ungrouped printers last
  2074. sorted.sort((a, b) => {
  2075. const locA = a.location || '';
  2076. const locB = b.location || '';
  2077. if (!locA && locB) return 1;
  2078. if (locA && !locB) return -1;
  2079. return locA.localeCompare(locB) || a.name.localeCompare(b.name);
  2080. });
  2081. break;
  2082. case 'status':
  2083. // Sort by status: printing > idle > offline
  2084. sorted.sort((a, b) => {
  2085. const statusA = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', a.id]);
  2086. const statusB = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', b.id]);
  2087. const getPriority = (s: typeof statusA) => {
  2088. if (!s?.connected) return 2; // offline
  2089. if (s.state === 'RUNNING') return 0; // printing
  2090. return 1; // idle
  2091. };
  2092. return getPriority(statusA) - getPriority(statusB);
  2093. });
  2094. break;
  2095. }
  2096. // Apply ascending/descending
  2097. if (!sortAsc) {
  2098. sorted.reverse();
  2099. }
  2100. return sorted;
  2101. }, [printers, sortBy, sortAsc, queryClient]);
  2102. // Group printers by location when sorted by location
  2103. const groupedPrinters = useMemo(() => {
  2104. if (sortBy !== 'location') return null;
  2105. const groups: Record<string, typeof sortedPrinters> = {};
  2106. sortedPrinters.forEach(printer => {
  2107. const location = printer.location || 'Ungrouped';
  2108. if (!groups[location]) groups[location] = [];
  2109. groups[location].push(printer);
  2110. });
  2111. return groups;
  2112. }, [sortBy, sortedPrinters]);
  2113. return (
  2114. <div className="p-4 md:p-8">
  2115. <div className="flex items-center justify-between mb-6">
  2116. <div>
  2117. <h1 className="text-2xl font-bold text-white">Printers</h1>
  2118. <StatusSummaryBar printers={printers} />
  2119. </div>
  2120. <div className="flex items-center gap-3">
  2121. {/* Sort dropdown */}
  2122. <div className="flex items-center gap-1">
  2123. <select
  2124. value={sortBy}
  2125. onChange={(e) => handleSortChange(e.target.value as SortOption)}
  2126. 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"
  2127. >
  2128. <option value="name">Name</option>
  2129. <option value="status">Status</option>
  2130. <option value="model">Model</option>
  2131. <option value="location">Location</option>
  2132. </select>
  2133. <button
  2134. onClick={toggleSortDirection}
  2135. className="p-1.5 rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  2136. title={sortAsc ? 'Sort descending' : 'Sort ascending'}
  2137. >
  2138. {sortAsc ? (
  2139. <ArrowUp className="w-4 h-4 text-bambu-gray" />
  2140. ) : (
  2141. <ArrowDown className="w-4 h-4 text-bambu-gray" />
  2142. )}
  2143. </button>
  2144. </div>
  2145. {/* View mode toggle */}
  2146. <button
  2147. onClick={toggleViewMode}
  2148. className="p-1.5 rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  2149. title={viewMode === 'expanded' ? 'Switch to compact view' : 'Switch to expanded view'}
  2150. >
  2151. {viewMode === 'expanded' ? (
  2152. <LayoutList className="w-5 h-5 text-bambu-gray" />
  2153. ) : (
  2154. <LayoutGrid className="w-5 h-5 text-bambu-gray" />
  2155. )}
  2156. </button>
  2157. <div className="w-px h-6 bg-bambu-dark-tertiary" />
  2158. <label className="flex items-center gap-2 text-sm text-bambu-gray cursor-pointer">
  2159. <input
  2160. type="checkbox"
  2161. checked={hideDisconnected}
  2162. onChange={toggleHideDisconnected}
  2163. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  2164. />
  2165. Hide offline
  2166. </label>
  2167. {/* Power dropdown for offline printers with smart plugs */}
  2168. {hideDisconnected && Object.keys(smartPlugByPrinter).length > 0 && (
  2169. <div className="relative">
  2170. <button
  2171. onClick={() => setShowPowerDropdown(!showPowerDropdown)}
  2172. 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"
  2173. >
  2174. <Power className="w-4 h-4" />
  2175. Power On
  2176. <ChevronDown className={`w-3 h-3 transition-transform ${showPowerDropdown ? 'rotate-180' : ''}`} />
  2177. </button>
  2178. {showPowerDropdown && (
  2179. <>
  2180. {/* Backdrop to close dropdown */}
  2181. <div
  2182. className="fixed inset-0 z-10"
  2183. onClick={() => setShowPowerDropdown(false)}
  2184. />
  2185. <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">
  2186. <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">
  2187. Offline printers with smart plugs
  2188. </div>
  2189. {printers?.filter(p => smartPlugByPrinter[p.id]).map(printer => (
  2190. <PowerDropdownItem
  2191. key={printer.id}
  2192. printer={printer}
  2193. plug={smartPlugByPrinter[printer.id]}
  2194. onPowerOn={(plugId) => {
  2195. setPoweringOn(plugId);
  2196. powerOnMutation.mutate(plugId);
  2197. }}
  2198. isPowering={poweringOn === smartPlugByPrinter[printer.id]?.id}
  2199. />
  2200. ))}
  2201. {printers?.filter(p => smartPlugByPrinter[p.id]).length === 0 && (
  2202. <div className="px-3 py-2 text-sm text-bambu-gray">
  2203. No printers with smart plugs
  2204. </div>
  2205. )}
  2206. </div>
  2207. </>
  2208. )}
  2209. </div>
  2210. )}
  2211. <Button onClick={() => setShowAddModal(true)}>
  2212. <Plus className="w-4 h-4" />
  2213. Add Printer
  2214. </Button>
  2215. </div>
  2216. </div>
  2217. {isLoading ? (
  2218. <div className="text-center py-12 text-bambu-gray">Loading printers...</div>
  2219. ) : printers?.length === 0 ? (
  2220. <Card>
  2221. <CardContent className="text-center py-12">
  2222. <p className="text-bambu-gray mb-4">No printers configured yet</p>
  2223. <Button onClick={() => setShowAddModal(true)}>
  2224. <Plus className="w-4 h-4" />
  2225. Add Your First Printer
  2226. </Button>
  2227. </CardContent>
  2228. </Card>
  2229. ) : groupedPrinters ? (
  2230. /* Grouped by location view */
  2231. <div className="space-y-6">
  2232. {Object.entries(groupedPrinters).map(([location, locationPrinters]) => (
  2233. <div key={location}>
  2234. <h2 className="text-lg font-semibold text-white mb-3 flex items-center gap-2">
  2235. <span className="w-2 h-2 rounded-full bg-bambu-green" />
  2236. {location}
  2237. <span className="text-sm font-normal text-bambu-gray">({locationPrinters.length})</span>
  2238. </h2>
  2239. <div className={`grid gap-4 ${
  2240. viewMode === 'compact'
  2241. ? 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4'
  2242. : 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6'
  2243. }`}>
  2244. {locationPrinters.map((printer) => (
  2245. <PrinterCard
  2246. key={printer.id}
  2247. printer={printer}
  2248. hideIfDisconnected={hideDisconnected}
  2249. maintenanceInfo={maintenanceByPrinter[printer.id]}
  2250. viewMode={viewMode}
  2251. amsThresholds={settings ? {
  2252. humidityGood: Number(settings.ams_humidity_good) || 40,
  2253. humidityFair: Number(settings.ams_humidity_fair) || 60,
  2254. tempGood: Number(settings.ams_temp_good) || 28,
  2255. tempFair: Number(settings.ams_temp_fair) || 35,
  2256. } : undefined}
  2257. />
  2258. ))}
  2259. </div>
  2260. </div>
  2261. ))}
  2262. </div>
  2263. ) : (
  2264. /* Regular grid view */
  2265. <div className={`grid gap-4 ${
  2266. viewMode === 'compact'
  2267. ? 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4'
  2268. : 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6'
  2269. }`}>
  2270. {sortedPrinters.map((printer) => (
  2271. <PrinterCard
  2272. key={printer.id}
  2273. printer={printer}
  2274. hideIfDisconnected={hideDisconnected}
  2275. maintenanceInfo={maintenanceByPrinter[printer.id]}
  2276. viewMode={viewMode}
  2277. amsThresholds={settings ? {
  2278. humidityGood: Number(settings.ams_humidity_good) || 40,
  2279. humidityFair: Number(settings.ams_humidity_fair) || 60,
  2280. tempGood: Number(settings.ams_temp_good) || 28,
  2281. tempFair: Number(settings.ams_temp_fair) || 35,
  2282. } : undefined}
  2283. />
  2284. ))}
  2285. </div>
  2286. )}
  2287. {showAddModal && (
  2288. <AddPrinterModal
  2289. onClose={() => setShowAddModal(false)}
  2290. onAdd={(data) => addMutation.mutate(data)}
  2291. existingSerials={printers?.map(p => p.serial_number) || []}
  2292. />
  2293. )}
  2294. </div>
  2295. );
  2296. }