PrintersPage.tsx 203 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713
  1. import { useState, useEffect, useMemo, useRef, useCallback } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { useTheme } from '../contexts/ThemeContext';
  4. import {
  5. Plus,
  6. Link,
  7. Unlink,
  8. Signal,
  9. Clock,
  10. MoreVertical,
  11. Trash2,
  12. RefreshCw,
  13. Box,
  14. HardDrive,
  15. AlertTriangle,
  16. AlertCircle,
  17. Terminal,
  18. Power,
  19. PowerOff,
  20. Zap,
  21. Wrench,
  22. ChevronDown,
  23. Pencil,
  24. ArrowUp,
  25. ArrowDown,
  26. Layers,
  27. Video,
  28. Search,
  29. Loader2,
  30. Square,
  31. Pause,
  32. Play,
  33. X,
  34. Monitor,
  35. Fan,
  36. Wind,
  37. AirVent,
  38. Download,
  39. ScanSearch,
  40. CheckCircle,
  41. XCircle,
  42. } from 'lucide-react';
  43. // Custom Skip Objects icon - arrow jumping over boxes
  44. const SkipObjectsIcon = ({ className }: { className?: string }) => (
  45. <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className={className}>
  46. {/* Three boxes at the bottom */}
  47. <rect x="2" y="15" width="5" height="5" rx="0.5" />
  48. <rect x="9.5" y="15" width="5" height="5" rx="0.5" fill="currentColor" opacity="0.3" />
  49. <rect x="17" y="15" width="5" height="5" rx="0.5" />
  50. {/* Curved arrow jumping over first box */}
  51. <path d="M4 12 C4 6, 14 6, 14 12" />
  52. <polyline points="12,10 14,12 12,14" />
  53. </svg>
  54. );
  55. import { useNavigate } from 'react-router-dom';
  56. import { api, discoveryApi, firmwareApi } from '../api/client';
  57. import { formatDateOnly } from '../utils/date';
  58. import type { Printer, PrinterCreate, AMSUnit, DiscoveredPrinter, FirmwareUpdateInfo, FirmwareUploadStatus } from '../api/client';
  59. import { Card, CardContent } from '../components/Card';
  60. import { Button } from '../components/Button';
  61. import { ConfirmModal } from '../components/ConfirmModal';
  62. import { FileManagerModal } from '../components/FileManagerModal';
  63. import { EmbeddedCameraViewer } from '../components/EmbeddedCameraViewer';
  64. import { MQTTDebugModal } from '../components/MQTTDebugModal';
  65. import { HMSErrorModal, filterKnownHMSErrors } from '../components/HMSErrorModal';
  66. import { PrinterQueueWidget } from '../components/PrinterQueueWidget';
  67. import { AMSHistoryModal } from '../components/AMSHistoryModal';
  68. import { FilamentHoverCard, EmptySlotHoverCard } from '../components/FilamentHoverCard';
  69. import { LinkSpoolModal } from '../components/LinkSpoolModal';
  70. import { ConfigureAmsSlotModal } from '../components/ConfigureAmsSlotModal';
  71. import { useToast } from '../contexts/ToastContext';
  72. import { ChamberLight } from '../components/icons/ChamberLight';
  73. // Complete Bambu Lab filament color mapping by tray_id_name
  74. // Source: https://github.com/queengooborg/Bambu-Lab-RFID-Library
  75. const BAMBU_FILAMENT_COLORS: Record<string, string> = {
  76. // PLA Basic (A00)
  77. 'A00-W1': 'Jade White',
  78. 'A00-P0': 'Beige',
  79. 'A00-D2': 'Light Gray',
  80. 'A00-Y0': 'Yellow',
  81. 'A00-Y2': 'Sunflower Yellow',
  82. 'A00-A1': 'Pumpkin Orange',
  83. 'A00-A0': 'Orange',
  84. 'A00-Y4': 'Gold',
  85. 'A00-G3': 'Bright Green',
  86. 'A00-G1': 'Bambu Green',
  87. 'A00-G2': 'Mistletoe Green',
  88. 'A00-R3': 'Hot Pink',
  89. 'A00-P6': 'Magenta',
  90. 'A00-R0': 'Red',
  91. 'A00-R2': 'Maroon Red',
  92. 'A00-P5': 'Purple',
  93. 'A00-P2': 'Indigo Purple',
  94. 'A00-B5': 'Turquoise',
  95. 'A00-B8': 'Cyan',
  96. 'A00-B3': 'Cobalt Blue',
  97. 'A00-N0': 'Brown',
  98. 'A00-N1': 'Cocoa Brown',
  99. 'A00-Y3': 'Bronze',
  100. 'A00-D0': 'Gray',
  101. 'A00-D1': 'Silver',
  102. 'A00-B1': 'Blue Grey',
  103. 'A00-D3': 'Dark Gray',
  104. 'A00-K0': 'Black',
  105. // PLA Basic Gradient (A00-M*)
  106. 'A00-M3': 'Pink Citrus',
  107. 'A00-M6': 'Dusk Glare',
  108. 'A00-M0': 'Arctic Whisper',
  109. 'A00-M1': 'Solar Breeze',
  110. 'A00-M5': 'Blueberry Bubblegum',
  111. 'A00-M4': 'Mint Lime',
  112. 'A00-M2': 'Ocean to Meadow',
  113. 'A00-M7': 'Cotton Candy Cloud',
  114. // PLA Lite (A18)
  115. 'A18-K0': 'Black',
  116. 'A18-D0': 'Gray',
  117. 'A18-W0': 'White',
  118. 'A18-R0': 'Red',
  119. 'A18-Y0': 'Yellow',
  120. 'A18-B0': 'Cyan',
  121. 'A18-B1': 'Blue',
  122. 'A18-P0': 'Matte Beige',
  123. // PLA Matte (A01)
  124. 'A01-W2': 'Ivory White',
  125. 'A01-W3': 'Bone White',
  126. 'A01-Y2': 'Lemon Yellow',
  127. 'A01-A2': 'Mandarin Orange',
  128. 'A01-P3': 'Sakura Pink',
  129. 'A01-P4': 'Lilac Purple',
  130. 'A01-R3': 'Plum',
  131. 'A01-R1': 'Scarlet Red',
  132. 'A01-R4': 'Dark Red',
  133. 'A01-G0': 'Apple Green',
  134. 'A01-G1': 'Grass Green',
  135. 'A01-G7': 'Dark Green',
  136. 'A01-B4': 'Ice Blue',
  137. 'A01-B0': 'Sky Blue',
  138. 'A01-B3': 'Marine Blue',
  139. 'A01-B6': 'Dark Blue',
  140. 'A01-Y3': 'Desert Tan',
  141. 'A01-N1': 'Latte Brown',
  142. 'A01-N3': 'Caramel',
  143. 'A01-R2': 'Terracotta',
  144. 'A01-N2': 'Dark Brown',
  145. 'A01-N0': 'Dark Chocolate',
  146. 'A01-D3': 'Ash Gray',
  147. 'A01-D0': 'Nardo Gray',
  148. 'A01-K1': 'Charcoal',
  149. // PLA Glow (A12)
  150. 'A12-G0': 'Green',
  151. 'A12-R0': 'Pink',
  152. 'A12-A0': 'Orange',
  153. 'A12-Y0': 'Yellow',
  154. 'A12-B0': 'Blue',
  155. // PLA Marble (A07)
  156. 'A07-R5': 'Red Granite',
  157. 'A07-D4': 'White Marble',
  158. // PLA Aero (A11)
  159. 'A11-W0': 'White',
  160. 'A11-K0': 'Black',
  161. // PLA Sparkle (A08)
  162. 'A08-G3': 'Alpine Green Sparkle',
  163. 'A08-D5': 'Slate Gray Sparkle',
  164. 'A08-B7': 'Royal Purple Sparkle',
  165. 'A08-R2': 'Crimson Red Sparkle',
  166. 'A08-K2': 'Onyx Black Sparkle',
  167. 'A08-Y1': 'Classic Gold Sparkle',
  168. // PLA Metal (A02)
  169. 'A02-B2': 'Cobalt Blue Metallic',
  170. 'A02-G2': 'Oxide Green Metallic',
  171. 'A02-Y1': 'Iridium Gold Metallic',
  172. 'A02-D2': 'Iron Gray Metallic',
  173. // PLA Translucent (A17)
  174. 'A17-B1': 'Blue',
  175. 'A17-A0': 'Orange',
  176. 'A17-P0': 'Purple',
  177. // PLA Silk+ (A06)
  178. 'A06-Y1': 'Gold',
  179. 'A06-D0': 'Titan Gray',
  180. 'A06-D1': 'Silver',
  181. 'A06-W0': 'White',
  182. 'A06-R0': 'Candy Red',
  183. 'A06-G0': 'Candy Green',
  184. 'A06-G1': 'Mint',
  185. 'A06-B1': 'Blue',
  186. 'A06-B0': 'Baby Blue',
  187. 'A06-P0': 'Purple',
  188. 'A06-R1': 'Rose Gold',
  189. 'A06-R2': 'Pink',
  190. 'A06-Y0': 'Champagne',
  191. // PLA Silk Multi-Color (A05)
  192. 'A05-M8': 'Dawn Radiance',
  193. 'A05-M4': 'Aurora Purple',
  194. 'A05-M1': 'South Beach',
  195. 'A05-T3': 'Neon City',
  196. 'A05-T2': 'Midnight Blaze',
  197. 'A05-T1': 'Gilded Rose',
  198. 'A05-T4': 'Blue Hawaii',
  199. 'A05-T5': 'Velvet Eclipse',
  200. // PLA Galaxy (A15)
  201. 'A15-B0': 'Purple',
  202. 'A15-G0': 'Green',
  203. 'A15-G1': 'Nebulae',
  204. 'A15-R0': 'Brown',
  205. // PLA Wood (A16)
  206. 'A16-K0': 'Black Walnut',
  207. 'A16-R0': 'Rosewood',
  208. 'A16-N0': 'Clay Brown',
  209. 'A16-G0': 'Classic Birch',
  210. 'A16-W0': 'White Oak',
  211. 'A16-Y0': 'Ochre Yellow',
  212. // PLA-CF (A50)
  213. 'A50-D6': 'Lava Gray',
  214. 'A50-K0': 'Black',
  215. 'A50-B6': 'Royal Blue',
  216. // PLA Tough+ (A10)
  217. 'A10-W0': 'White',
  218. 'A10-D0': 'Gray',
  219. // PLA Tough (A09)
  220. 'A09-B5': 'Lavender Blue',
  221. 'A09-B4': 'Light Blue',
  222. 'A09-A0': 'Orange',
  223. 'A09-D1': 'Silver',
  224. 'A09-R3': 'Vermilion Red',
  225. 'A09-Y0': 'Yellow',
  226. // PETG HF (G02)
  227. 'G02-K0': 'Black',
  228. 'G02-W0': 'White',
  229. 'G02-R0': 'Red',
  230. 'G02-D0': 'Gray',
  231. 'G02-D1': 'Dark Gray',
  232. 'G02-Y1': 'Cream',
  233. 'G02-Y0': 'Yellow',
  234. 'G02-A0': 'Orange',
  235. 'G02-N1': 'Peanut Brown',
  236. 'G02-G1': 'Lime Green',
  237. 'G02-G0': 'Green',
  238. 'G02-G2': 'Forest Green',
  239. 'G02-B1': 'Lake Blue',
  240. 'G02-B0': 'Blue',
  241. // PETG Translucent (G01)
  242. 'G01-G1': 'Translucent Teal',
  243. 'G01-B0': 'Translucent Light Blue',
  244. 'G01-C0': 'Clear',
  245. 'G01-D0': 'Translucent Gray',
  246. 'G01-G0': 'Translucent Olive',
  247. 'G01-N0': 'Translucent Brown',
  248. 'G01-A0': 'Translucent Orange',
  249. 'G01-P1': 'Translucent Pink',
  250. 'G01-P0': 'Translucent Purple',
  251. // PETG-CF (G50)
  252. 'G50-P7': 'Violet Purple',
  253. 'G50-K0': 'Black',
  254. // ABS (B00)
  255. 'B00-D1': 'Silver',
  256. 'B00-K0': 'Black',
  257. 'B00-W0': 'White',
  258. 'B00-G6': 'Bambu Green',
  259. 'B00-G7': 'Olive',
  260. 'B00-Y1': 'Tangerine Yellow',
  261. 'B00-A0': 'Orange',
  262. 'B00-R0': 'Red',
  263. 'B00-B4': 'Azure',
  264. 'B00-B0': 'Blue',
  265. 'B00-B6': 'Navy Blue',
  266. // ABS-GF (B50)
  267. 'B50-A0': 'Orange',
  268. 'B50-K0': 'Black',
  269. // ASA (B01)
  270. 'B01-W0': 'White',
  271. 'B01-K0': 'Black',
  272. 'B01-D0': 'Gray',
  273. // ASA Aero (B02)
  274. 'B02-W0': 'White',
  275. // PC (C00)
  276. 'C00-C1': 'Transparent',
  277. 'C00-C0': 'Clear Black',
  278. 'C00-K0': 'Black',
  279. 'C00-W0': 'White',
  280. // PC FR (C01)
  281. 'C01-K0': 'Black',
  282. // TPU for AMS (U02)
  283. 'U02-B0': 'Blue',
  284. 'U02-D0': 'Gray',
  285. 'U02-K0': 'Black',
  286. // PAHT-CF (N04)
  287. 'N04-K0': 'Black',
  288. // PA6-GF (N08)
  289. 'N08-K0': 'Black',
  290. // Support for PLA/PETG (S02, S05)
  291. 'S02-W0': 'Nature',
  292. 'S02-W1': 'White',
  293. 'S05-C0': 'Black',
  294. // Support for ABS (S06)
  295. 'S06-W0': 'White',
  296. // Support for PA/PET (S03)
  297. 'S03-G1': 'Green',
  298. // PVA (S04)
  299. 'S04-Y0': 'Clear',
  300. };
  301. // Fallback color codes for unknown material prefixes
  302. const BAMBU_COLOR_CODE_FALLBACK: Record<string, string> = {
  303. 'W0': 'White', 'W1': 'Jade White', 'W2': 'Ivory White', 'W3': 'Bone White',
  304. 'Y0': 'Yellow', 'Y1': 'Gold', 'Y2': 'Sunflower Yellow', 'Y3': 'Bronze', 'Y4': 'Gold',
  305. 'A0': 'Orange', 'A1': 'Pumpkin Orange', 'A2': 'Mandarin Orange',
  306. 'R0': 'Red', 'R1': 'Scarlet Red', 'R2': 'Maroon Red', 'R3': 'Hot Pink', 'R4': 'Dark Red', 'R5': 'Red Granite',
  307. 'P0': 'Beige', 'P1': 'Pink', 'P2': 'Indigo Purple', 'P3': 'Sakura Pink', 'P4': 'Lilac Purple', 'P5': 'Purple', 'P6': 'Magenta', 'P7': 'Violet Purple',
  308. 'B0': 'Blue', 'B1': 'Blue Grey', 'B2': 'Cobalt Blue', 'B3': 'Cobalt Blue', 'B4': 'Ice Blue', 'B5': 'Turquoise', 'B6': 'Navy Blue', 'B7': 'Royal Purple', 'B8': 'Cyan',
  309. 'G0': 'Green', 'G1': 'Grass Green', 'G2': 'Mistletoe Green', 'G3': 'Bright Green', 'G6': 'Bambu Green', 'G7': 'Dark Green',
  310. 'N0': 'Brown', 'N1': 'Peanut Brown', 'N2': 'Dark Brown', 'N3': 'Caramel',
  311. 'D0': 'Gray', 'D1': 'Silver', 'D2': 'Light Gray', 'D3': 'Dark Gray', 'D4': 'White Marble', 'D5': 'Slate Gray', 'D6': 'Lava Gray',
  312. 'K0': 'Black', 'K1': 'Charcoal', 'K2': 'Onyx Black',
  313. 'C0': 'Clear Black', 'C1': 'Transparent',
  314. 'M0': 'Arctic Whisper', 'M1': 'Solar Breeze', 'M2': 'Ocean to Meadow', 'M3': 'Pink Citrus', 'M4': 'Aurora Purple', 'M5': 'Blueberry Bubblegum', 'M6': 'Dusk Glare', 'M7': 'Cotton Candy Cloud', 'M8': 'Dawn Radiance',
  315. 'T1': 'Gilded Rose', 'T2': 'Midnight Blaze', 'T3': 'Neon City', 'T4': 'Blue Hawaii', 'T5': 'Velvet Eclipse',
  316. };
  317. // Get color name from Bambu Lab tray_id_name (e.g., "A00-Y2" -> "Sunflower Yellow")
  318. function getBambuColorName(trayIdName: string | null | undefined): string | null {
  319. if (!trayIdName) return null;
  320. // First try exact match with full tray_id_name
  321. if (BAMBU_FILAMENT_COLORS[trayIdName]) {
  322. return BAMBU_FILAMENT_COLORS[trayIdName];
  323. }
  324. // Fall back to color code suffix lookup for unknown material prefixes
  325. const parts = trayIdName.split('-');
  326. if (parts.length < 2) return null;
  327. const colorCode = parts[1];
  328. return BAMBU_COLOR_CODE_FALLBACK[colorCode] || null;
  329. }
  330. // Convert hex color to basic color name
  331. function hexToBasicColorName(hex: string | null | undefined): string {
  332. if (!hex || hex.length < 6) return 'Unknown';
  333. // Parse RGB from hex (format: RRGGBBAA or RRGGBB)
  334. const r = parseInt(hex.substring(0, 2), 16);
  335. const g = parseInt(hex.substring(2, 4), 16);
  336. const b = parseInt(hex.substring(4, 6), 16);
  337. // Calculate HSL for better color classification
  338. const max = Math.max(r, g, b) / 255;
  339. const min = Math.min(r, g, b) / 255;
  340. const l = (max + min) / 2;
  341. let h = 0;
  342. let s = 0;
  343. if (max !== min) {
  344. const d = max - min;
  345. s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
  346. const rNorm = r / 255;
  347. const gNorm = g / 255;
  348. const bNorm = b / 255;
  349. if (max === rNorm) {
  350. h = ((gNorm - bNorm) / d + (gNorm < bNorm ? 6 : 0)) / 6;
  351. } else if (max === gNorm) {
  352. h = ((bNorm - rNorm) / d + 2) / 6;
  353. } else {
  354. h = ((rNorm - gNorm) / d + 4) / 6;
  355. }
  356. }
  357. // Convert to degrees
  358. h = h * 360;
  359. // Classify by lightness first
  360. if (l < 0.15) return 'Black';
  361. if (l > 0.85) return 'White';
  362. // Low saturation = gray
  363. if (s < 0.15) {
  364. if (l < 0.4) return 'Dark Gray';
  365. if (l > 0.6) return 'Light Gray';
  366. return 'Gray';
  367. }
  368. // Classify by hue
  369. // Brown is orange/yellow hue with lower lightness
  370. if (h >= 15 && h < 45 && l < 0.45) return 'Brown';
  371. if (h >= 45 && h < 70 && l < 0.40) return 'Brown';
  372. if (h < 15 || h >= 345) return 'Red';
  373. if (h < 45) return 'Orange';
  374. if (h < 70) return 'Yellow';
  375. if (h < 150) return 'Green';
  376. if (h < 200) return 'Cyan';
  377. if (h < 260) return 'Blue';
  378. if (h < 290) return 'Purple';
  379. if (h < 345) return 'Pink';
  380. return 'Unknown';
  381. }
  382. // Format K value with 3 decimal places, default to 0.020 if null
  383. function formatKValue(k: number | null | undefined): string {
  384. const value = k ?? 0.020;
  385. return value.toFixed(3);
  386. }
  387. // Nozzle side indicators (Bambu Lab style - square badge with L/R)
  388. function NozzleBadge({ side }: { side: 'L' | 'R' }) {
  389. const { mode } = useTheme();
  390. // Light mode: #e7f5e9 (light green), Dark mode: #1a4d2e (dark green)
  391. const bgColor = mode === 'dark' ? '#1a4d2e' : '#e7f5e9';
  392. return (
  393. <span
  394. className="inline-flex items-center justify-center w-4 h-4 text-[10px] font-bold rounded"
  395. style={{ backgroundColor: bgColor, color: '#00ae42' }}
  396. >
  397. {side}
  398. </span>
  399. );
  400. }
  401. // Water drop SVG - empty outline (Bambu Lab style from bambu-humidity)
  402. function WaterDropEmpty({ className }: { className?: string }) {
  403. return (
  404. <svg className={className} viewBox="0 0 36 54" fill="none" xmlns="http://www.w3.org/2000/svg">
  405. <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"/>
  406. </svg>
  407. );
  408. }
  409. // Water drop SVG - half filled with blue water (Bambu Lab style from bambu-humidity)
  410. function WaterDropHalf({ className }: { className?: string }) {
  411. return (
  412. <svg className={className} viewBox="0 0 35 53" fill="none" xmlns="http://www.w3.org/2000/svg">
  413. <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"/>
  414. <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"/>
  415. </svg>
  416. );
  417. }
  418. // Water drop SVG - fully filled with blue water (Bambu Lab style from bambu-humidity)
  419. function WaterDropFull({ className }: { className?: string }) {
  420. return (
  421. <svg className={className} viewBox="0 0 36 54" fill="none" xmlns="http://www.w3.org/2000/svg">
  422. <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"/>
  423. <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"/>
  424. </svg>
  425. );
  426. }
  427. // Thermometer SVG - empty outline
  428. function ThermometerEmpty({ className }: { className?: string }) {
  429. return (
  430. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  431. <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"/>
  432. <circle cx="6" cy="15" r="2.5" stroke="#C3C2C1" strokeWidth="1" fill="none"/>
  433. </svg>
  434. );
  435. }
  436. // Thermometer SVG - half filled (gold - same as humidity fair)
  437. function ThermometerHalf({ className }: { className?: string }) {
  438. return (
  439. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  440. <rect x="4.5" y="8" width="3" height="4.5" fill="#d4a017" rx="0.5"/>
  441. <circle cx="6" cy="15" r="2" fill="#d4a017"/>
  442. <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"/>
  443. </svg>
  444. );
  445. }
  446. // Thermometer SVG - fully filled (red - same as humidity bad)
  447. function ThermometerFull({ className }: { className?: string }) {
  448. return (
  449. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  450. <rect x="4.5" y="3" width="3" height="9.5" fill="#c62828" rx="0.5"/>
  451. <circle cx="6" cy="15" r="2" fill="#c62828"/>
  452. <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"/>
  453. </svg>
  454. );
  455. }
  456. // Heater thermometer icon - filled when heating, outline when off
  457. interface HeaterThermometerProps {
  458. className?: string;
  459. color: string; // The color class (e.g., "text-orange-400")
  460. isHeating: boolean;
  461. }
  462. function HeaterThermometer({ className, color, isHeating }: HeaterThermometerProps) {
  463. // Extract the actual color from Tailwind class for SVG fill
  464. const colorMap: Record<string, string> = {
  465. 'text-orange-400': '#fb923c',
  466. 'text-blue-400': '#60a5fa',
  467. 'text-green-400': '#4ade80',
  468. };
  469. const fillColor = colorMap[color] || '#888';
  470. // Glow style when heating
  471. const glowStyle = isHeating ? {
  472. filter: `drop-shadow(0 0 4px ${fillColor}) drop-shadow(0 0 8px ${fillColor})`,
  473. } : {};
  474. if (isHeating) {
  475. // Filled thermometer with glow - heater is ON
  476. return (
  477. <svg className={className} style={glowStyle} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  478. <rect x="4.5" y="3" width="3" height="9.5" fill={fillColor} rx="0.5"/>
  479. <circle cx="6" cy="15" r="2" fill={fillColor}/>
  480. <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"/>
  481. </svg>
  482. );
  483. }
  484. // Empty thermometer - heater is OFF
  485. return (
  486. <svg className={className} viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg">
  487. <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"/>
  488. <circle cx="6" cy="15" r="2.5" stroke={fillColor} strokeWidth="1" fill="none"/>
  489. </svg>
  490. );
  491. }
  492. // Humidity indicator with water drop that fills based on level (Bambu Lab style)
  493. // Reference: https://github.com/theicedmango/bambu-humidity
  494. interface HumidityIndicatorProps {
  495. humidity: number | string;
  496. goodThreshold?: number; // <= this is green
  497. fairThreshold?: number; // <= this is orange, > is red
  498. onClick?: () => void;
  499. compact?: boolean; // Smaller version for grid layout
  500. }
  501. function HumidityIndicator({ humidity, goodThreshold = 40, fairThreshold = 60, onClick, compact }: HumidityIndicatorProps) {
  502. const humidityValue = typeof humidity === 'string' ? parseInt(humidity, 10) : humidity;
  503. const good = typeof goodThreshold === 'number' ? goodThreshold : 40;
  504. const fair = typeof fairThreshold === 'number' ? fairThreshold : 60;
  505. // Status thresholds (configurable via settings)
  506. // Good: ≤goodThreshold (green #22a352), Fair: ≤fairThreshold (gold #d4a017), Bad: >fairThreshold (red #c62828)
  507. let textColor: string;
  508. let statusText: string;
  509. if (isNaN(humidityValue)) {
  510. textColor = '#C3C2C1';
  511. statusText = 'Unknown';
  512. } else if (humidityValue <= good) {
  513. textColor = '#22a352'; // Green - Good
  514. statusText = 'Good';
  515. } else if (humidityValue <= fair) {
  516. textColor = '#d4a017'; // Gold - Fair
  517. statusText = 'Fair';
  518. } else {
  519. textColor = '#c62828'; // Red - Bad
  520. statusText = 'Bad';
  521. }
  522. // Fill level based on status: Good=Empty (dry), Fair=Half, Bad=Full (wet)
  523. let DropComponent: React.FC<{ className?: string }>;
  524. if (isNaN(humidityValue)) {
  525. DropComponent = WaterDropEmpty;
  526. } else if (humidityValue <= good) {
  527. DropComponent = WaterDropEmpty; // Good - empty drop (dry)
  528. } else if (humidityValue <= fair) {
  529. DropComponent = WaterDropHalf; // Fair - half filled
  530. } else {
  531. DropComponent = WaterDropFull; // Bad - full (too humid)
  532. }
  533. return (
  534. <button
  535. type="button"
  536. onClick={onClick}
  537. className={`flex items-center gap-1 ${onClick ? 'cursor-pointer hover:opacity-80 transition-opacity' : ''}`}
  538. title={`Humidity: ${humidityValue}% - ${statusText}${onClick ? ' (click for history)' : ''}`}
  539. >
  540. <DropComponent className={compact ? "w-2.5 h-3" : "w-3 h-4"} />
  541. <span className={`font-medium tabular-nums ${compact ? 'text-[10px]' : 'text-xs'}`} style={{ color: textColor }}>{humidityValue}%</span>
  542. </button>
  543. );
  544. }
  545. // Temperature indicator with dynamic icon and coloring
  546. interface TemperatureIndicatorProps {
  547. temp: number;
  548. goodThreshold?: number; // <= this is blue
  549. fairThreshold?: number; // <= this is orange, > is red
  550. onClick?: () => void;
  551. compact?: boolean; // Smaller version for grid layout
  552. }
  553. function TemperatureIndicator({ temp, goodThreshold = 28, fairThreshold = 35, onClick, compact }: TemperatureIndicatorProps) {
  554. // Ensure thresholds are numbers
  555. const good = typeof goodThreshold === 'number' ? goodThreshold : 28;
  556. const fair = typeof fairThreshold === 'number' ? fairThreshold : 35;
  557. let textColor: string;
  558. let statusText: string;
  559. let ThermoComponent: React.FC<{ className?: string }>;
  560. if (temp <= good) {
  561. textColor = '#22a352'; // Green - good (same as humidity)
  562. statusText = 'Good';
  563. ThermoComponent = ThermometerEmpty;
  564. } else if (temp <= fair) {
  565. textColor = '#d4a017'; // Gold - fair (same as humidity)
  566. statusText = 'Fair';
  567. ThermoComponent = ThermometerHalf;
  568. } else {
  569. textColor = '#c62828'; // Red - bad (same as humidity)
  570. statusText = 'Bad';
  571. ThermoComponent = ThermometerFull;
  572. }
  573. return (
  574. <button
  575. type="button"
  576. onClick={onClick}
  577. className={`flex items-center gap-1 ${onClick ? 'cursor-pointer hover:opacity-80 transition-opacity' : ''}`}
  578. title={`Temperature: ${temp}°C - ${statusText}${onClick ? ' (click for history)' : ''}`}
  579. >
  580. <ThermoComponent className={compact ? "w-2.5 h-3" : "w-3 h-4"} />
  581. <span className={`tabular-nums text-right ${compact ? 'text-[10px] w-8' : 'w-12'}`} style={{ color: textColor }}>{temp}°C</span>
  582. </button>
  583. );
  584. }
  585. // Get AMS label: AMS-A/B/C/D for regular AMS, HT-A/B for AMS-HT (single spool)
  586. // Always use tray count as the source of truth (1 tray = AMS-HT, 4 trays = regular AMS)
  587. // AMS-HT uses IDs 128+ while regular AMS uses 0-3
  588. function getAmsLabel(amsId: number | string, trayCount: number): string {
  589. // Ensure amsId is a number (backend might send string)
  590. const id = typeof amsId === 'string' ? parseInt(amsId, 10) : amsId;
  591. const safeId = isNaN(id) ? 0 : id;
  592. const isHt = trayCount === 1;
  593. // AMS-HT uses IDs starting at 128, regular AMS uses 0-3
  594. const normalizedId = safeId >= 128 ? safeId - 128 : safeId;
  595. const letter = String.fromCharCode(65 + normalizedId); // 0=A, 1=B, 2=C, 3=D
  596. return isHt ? `HT-${letter}` : `AMS-${letter}`;
  597. }
  598. // Get fill bar color based on spool fill level
  599. function getFillBarColor(fillLevel: number): string {
  600. if (fillLevel > 50) return '#00ae42'; // Green - good
  601. if (fillLevel >= 15) return '#f59e0b'; // Amber - warning (<= 50%)
  602. return '#ef4444'; // Red - critical (< 15%)
  603. }
  604. function formatTime(seconds: number): string {
  605. const hours = Math.floor(seconds / 3600);
  606. const minutes = Math.floor((seconds % 3600) / 60);
  607. return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
  608. }
  609. function formatETA(remainingMinutes: number, timeFormat: 'system' | '12h' | '24h' = 'system'): string {
  610. const now = new Date();
  611. const eta = new Date(now.getTime() + remainingMinutes * 60 * 1000);
  612. const today = new Date();
  613. today.setHours(0, 0, 0, 0);
  614. const etaDay = new Date(eta);
  615. etaDay.setHours(0, 0, 0, 0);
  616. // Build time format options based on setting
  617. const timeOptions: Intl.DateTimeFormatOptions = { hour: '2-digit', minute: '2-digit' };
  618. if (timeFormat === '12h') {
  619. timeOptions.hour12 = true;
  620. } else if (timeFormat === '24h') {
  621. timeOptions.hour12 = false;
  622. }
  623. // 'system' leaves hour12 undefined, letting the browser decide
  624. const timeStr = eta.toLocaleTimeString([], timeOptions);
  625. // Check if it's tomorrow or later
  626. const dayDiff = Math.floor((etaDay.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
  627. if (dayDiff === 0) {
  628. return timeStr;
  629. } else if (dayDiff === 1) {
  630. return `Tomorrow ${timeStr}`;
  631. } else {
  632. return eta.toLocaleDateString([], { weekday: 'short' }) + ' ' + timeStr;
  633. }
  634. }
  635. function getPrinterImage(model: string | null | undefined): string {
  636. if (!model) return '/img/printers/default.png';
  637. const modelLower = model.toLowerCase().replace(/\s+/g, '');
  638. // Map model names to image files
  639. if (modelLower.includes('x1e')) return '/img/printers/x1e.png';
  640. if (modelLower.includes('x1c') || modelLower.includes('x1carbon')) return '/img/printers/x1c.png';
  641. if (modelLower.includes('x1')) return '/img/printers/x1c.png';
  642. if (modelLower.includes('h2d')) return '/img/printers/h2d.png';
  643. if (modelLower.includes('h2c') || modelLower.includes('h2s')) return '/img/printers/h2d.png';
  644. if (modelLower.includes('p2s')) return '/img/printers/p1s.png';
  645. if (modelLower.includes('p1s')) return '/img/printers/p1s.png';
  646. if (modelLower.includes('p1p')) return '/img/printers/p1p.png';
  647. if (modelLower.includes('a1mini')) return '/img/printers/a1mini.png';
  648. if (modelLower.includes('a1')) return '/img/printers/a1.png';
  649. return '/img/printers/default.png';
  650. }
  651. function getWifiStrength(rssi: number | null | undefined): { label: string; color: string; bars: number } {
  652. if (rssi == null) return { label: '', color: 'text-bambu-gray', bars: 0 };
  653. if (rssi >= -50) return { label: 'Excellent', color: 'text-bambu-green', bars: 4 };
  654. if (rssi >= -60) return { label: 'Good', color: 'text-bambu-green', bars: 3 };
  655. if (rssi >= -70) return { label: 'Fair', color: 'text-yellow-400', bars: 2 };
  656. if (rssi >= -80) return { label: 'Weak', color: 'text-orange-400', bars: 1 };
  657. return { label: 'Very weak', color: 'text-red-400', bars: 1 };
  658. }
  659. /**
  660. * Check if a tray contains a Bambu Lab spool.
  661. * Uses same logic as backend: tray_info_idx (GF*), tray_uuid, or tag_uid.
  662. */
  663. function isBambuLabSpool(tray: {
  664. tray_uuid?: string | null;
  665. tag_uid?: string | null;
  666. tray_info_idx?: string | null;
  667. } | null | undefined): boolean {
  668. if (!tray) return false;
  669. // Check tray_info_idx first (most reliable - Bambu preset IDs start with "GF")
  670. if (tray.tray_info_idx && tray.tray_info_idx.startsWith('GF')) {
  671. return true;
  672. }
  673. // Check tray_uuid (32 hex chars, non-zero)
  674. if (tray.tray_uuid && tray.tray_uuid !== '00000000000000000000000000000000') {
  675. return true;
  676. }
  677. // Check tag_uid (16 hex chars, non-zero)
  678. if (tray.tag_uid && tray.tag_uid !== '0000000000000000') {
  679. return true;
  680. }
  681. return false;
  682. }
  683. function CoverImage({ url, printName }: { url: string | null; printName?: string }) {
  684. const [loaded, setLoaded] = useState(false);
  685. const [error, setError] = useState(false);
  686. const [showOverlay, setShowOverlay] = useState(false);
  687. return (
  688. <>
  689. <div
  690. 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' : ''}`}
  691. onClick={() => url && loaded && setShowOverlay(true)}
  692. >
  693. {url && !error ? (
  694. <>
  695. <img
  696. src={url}
  697. alt="Print preview"
  698. className={`w-full h-full object-cover ${loaded ? 'block' : 'hidden'}`}
  699. onLoad={() => setLoaded(true)}
  700. onError={() => setError(true)}
  701. />
  702. {!loaded && <Box className="w-8 h-8 text-bambu-gray" />}
  703. </>
  704. ) : (
  705. <Box className="w-8 h-8 text-bambu-gray" />
  706. )}
  707. </div>
  708. {/* Cover Image Overlay */}
  709. {showOverlay && url && (
  710. <div
  711. className="fixed inset-0 bg-black/80 flex items-center justify-center z-50 p-8"
  712. onClick={() => setShowOverlay(false)}
  713. >
  714. <div className="relative max-w-2xl max-h-full">
  715. <img
  716. src={url}
  717. alt="Print preview"
  718. className="max-w-full max-h-[80vh] rounded-lg shadow-2xl"
  719. />
  720. {printName && (
  721. <p className="text-white text-center mt-4 text-lg">{printName}</p>
  722. )}
  723. </div>
  724. </div>
  725. )}
  726. </>
  727. );
  728. }
  729. interface PrinterMaintenanceInfo {
  730. due_count: number;
  731. warning_count: number;
  732. total_print_hours: number;
  733. }
  734. // Status summary bar component - uses queryClient to read cached statuses
  735. function StatusSummaryBar({ printers }: { printers: Printer[] | undefined }) {
  736. const queryClient = useQueryClient();
  737. const counts = useMemo(() => {
  738. let printing = 0;
  739. let idle = 0;
  740. let offline = 0;
  741. let loading = 0;
  742. printers?.forEach((printer) => {
  743. const status = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', printer.id]);
  744. if (status === undefined) {
  745. // Status not yet loaded - don't count as offline yet
  746. loading++;
  747. } else if (!status.connected) {
  748. offline++;
  749. } else if (status.state === 'RUNNING') {
  750. printing++;
  751. } else {
  752. idle++;
  753. }
  754. });
  755. return { printing, idle, offline, loading, total: (printers?.length || 0) };
  756. }, [printers, queryClient]);
  757. // Subscribe to query cache changes to re-render when status updates
  758. // Throttled to prevent rapid re-renders from causing tab crashes
  759. const [, setTick] = useState(0);
  760. useEffect(() => {
  761. let pending = false;
  762. const unsubscribe = queryClient.getQueryCache().subscribe(() => {
  763. if (!pending) {
  764. pending = true;
  765. requestAnimationFrame(() => {
  766. setTick(t => t + 1);
  767. pending = false;
  768. });
  769. }
  770. });
  771. return () => unsubscribe();
  772. }, [queryClient]);
  773. if (!printers?.length) return null;
  774. return (
  775. <div className="flex items-center gap-4 text-sm">
  776. {counts.printing > 0 && (
  777. <div className="flex items-center gap-1.5">
  778. <div className="w-2 h-2 rounded-full bg-bambu-green animate-pulse" />
  779. <span className="text-bambu-gray">
  780. <span className="text-white font-medium">{counts.printing}</span> printing
  781. </span>
  782. </div>
  783. )}
  784. {counts.idle > 0 && (
  785. <div className="flex items-center gap-1.5">
  786. <div className="w-2 h-2 rounded-full bg-blue-400" />
  787. <span className="text-bambu-gray">
  788. <span className="text-white font-medium">{counts.idle}</span> idle
  789. </span>
  790. </div>
  791. )}
  792. {counts.offline > 0 && (
  793. <div className="flex items-center gap-1.5">
  794. <div className="w-2 h-2 rounded-full bg-gray-400" />
  795. <span className="text-bambu-gray">
  796. <span className="text-white font-medium">{counts.offline}</span> offline
  797. </span>
  798. </div>
  799. )}
  800. </div>
  801. );
  802. }
  803. type SortOption = 'name' | 'status' | 'model' | 'location';
  804. type ViewMode = 'expanded' | 'compact';
  805. /**
  806. * Get human-readable status display text for a printer.
  807. * Uses stg_cur_name for detailed calibration/preparation stages,
  808. * otherwise formats the gcode_state nicely.
  809. */
  810. function getStatusDisplay(state: string | null | undefined, stg_cur_name: string | null | undefined): string {
  811. // If we have a specific stage name (calibration, heating, etc.), use it
  812. if (stg_cur_name) {
  813. return stg_cur_name;
  814. }
  815. // Format the gcode_state nicely
  816. switch (state) {
  817. case 'RUNNING':
  818. return 'Printing';
  819. case 'PAUSE':
  820. return 'Paused';
  821. case 'FINISH':
  822. return 'Finished';
  823. case 'FAILED':
  824. return 'Failed';
  825. case 'IDLE':
  826. return 'Idle';
  827. default:
  828. return state ? state.charAt(0) + state.slice(1).toLowerCase() : 'Idle';
  829. }
  830. }
  831. function PrinterCard({
  832. printer,
  833. hideIfDisconnected,
  834. maintenanceInfo,
  835. viewMode = 'expanded',
  836. cardSize = 2,
  837. amsThresholds,
  838. spoolmanEnabled = false,
  839. hasUnlinkedSpools = false,
  840. timeFormat = 'system',
  841. cameraViewMode = 'window',
  842. onOpenEmbeddedCamera,
  843. checkPrinterFirmware = true,
  844. }: {
  845. printer: Printer;
  846. hideIfDisconnected?: boolean;
  847. maintenanceInfo?: PrinterMaintenanceInfo;
  848. viewMode?: ViewMode;
  849. cardSize?: number;
  850. amsThresholds?: {
  851. humidityGood: number;
  852. humidityFair: number;
  853. tempGood: number;
  854. tempFair: number;
  855. };
  856. spoolmanEnabled?: boolean;
  857. hasUnlinkedSpools?: boolean;
  858. timeFormat?: 'system' | '12h' | '24h';
  859. cameraViewMode?: 'window' | 'embedded';
  860. onOpenEmbeddedCamera?: (printerId: number, printerName: string) => void;
  861. checkPrinterFirmware?: boolean;
  862. }) {
  863. const queryClient = useQueryClient();
  864. const navigate = useNavigate();
  865. const { showToast } = useToast();
  866. const [showMenu, setShowMenu] = useState(false);
  867. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  868. const [deleteArchives, setDeleteArchives] = useState(true);
  869. const [showEditModal, setShowEditModal] = useState(false);
  870. const [showFileManager, setShowFileManager] = useState(false);
  871. const [showMQTTDebug, setShowMQTTDebug] = useState(false);
  872. const [showPowerOnConfirm, setShowPowerOnConfirm] = useState(false);
  873. const [showPowerOffConfirm, setShowPowerOffConfirm] = useState(false);
  874. const [showHMSModal, setShowHMSModal] = useState(false);
  875. const [showStopConfirm, setShowStopConfirm] = useState(false);
  876. const [showPauseConfirm, setShowPauseConfirm] = useState(false);
  877. const [showResumeConfirm, setShowResumeConfirm] = useState(false);
  878. const [showSkipObjectsModal, setShowSkipObjectsModal] = useState(false);
  879. const [amsHistoryModal, setAmsHistoryModal] = useState<{
  880. amsId: number;
  881. amsLabel: string;
  882. mode: 'humidity' | 'temperature';
  883. } | null>(null);
  884. const [linkSpoolModal, setLinkSpoolModal] = useState<{
  885. trayUuid: string;
  886. trayInfo: { type: string; color: string; location: string };
  887. } | null>(null);
  888. const [configureSlotModal, setConfigureSlotModal] = useState<{
  889. amsId: number;
  890. trayId: number;
  891. trayCount: number;
  892. trayType?: string;
  893. trayColor?: string;
  894. traySubBrands?: string;
  895. trayInfoIdx?: string;
  896. } | null>(null);
  897. const [showFirmwareModal, setShowFirmwareModal] = useState(false);
  898. const [plateCheckResult, setPlateCheckResult] = useState<{
  899. is_empty: boolean;
  900. confidence: number;
  901. difference_percent: number;
  902. message: string;
  903. debug_image_url?: string;
  904. needs_calibration: boolean;
  905. light_warning?: boolean;
  906. reference_count?: number;
  907. max_references?: number;
  908. roi?: { x: number; y: number; w: number; h: number };
  909. } | null>(null);
  910. const [isCheckingPlate, setIsCheckingPlate] = useState(false);
  911. const [isCalibrating, setIsCalibrating] = useState(false);
  912. const [editingRoi, setEditingRoi] = useState<{ x: number; y: number; w: number; h: number } | null>(null);
  913. const [isSavingRoi, setIsSavingRoi] = useState(false);
  914. const [plateCheckLightWasOff, setPlateCheckLightWasOff] = useState(false);
  915. const { data: status } = useQuery({
  916. queryKey: ['printerStatus', printer.id],
  917. queryFn: () => api.getPrinterStatus(printer.id),
  918. refetchInterval: 30000, // Fallback polling, WebSocket handles real-time
  919. });
  920. // Check for firmware updates (cached for 5 minutes, can be disabled in settings)
  921. const { data: firmwareInfo } = useQuery({
  922. queryKey: ['firmwareUpdate', printer.id],
  923. queryFn: () => firmwareApi.checkPrinterUpdate(printer.id),
  924. staleTime: 5 * 60 * 1000,
  925. refetchInterval: 5 * 60 * 1000,
  926. enabled: checkPrinterFirmware,
  927. });
  928. // Collect unique tray_info_idx values for cloud filament info lookup
  929. const trayInfoIds = useMemo(() => {
  930. const ids = new Set<string>();
  931. if (status?.ams) {
  932. for (const ams of status.ams) {
  933. for (const tray of ams.tray || []) {
  934. if (tray.tray_info_idx) {
  935. ids.add(tray.tray_info_idx);
  936. }
  937. }
  938. }
  939. }
  940. if (status?.vt_tray?.tray_info_idx) {
  941. ids.add(status.vt_tray.tray_info_idx);
  942. }
  943. return Array.from(ids);
  944. }, [status?.ams, status?.vt_tray]);
  945. // Fetch cloud filament info for tooltips (name includes color, also has K value)
  946. const { data: filamentInfo } = useQuery({
  947. queryKey: ['filamentInfo', trayInfoIds],
  948. queryFn: () => api.getFilamentInfo(trayInfoIds),
  949. enabled: trayInfoIds.length > 0,
  950. staleTime: 5 * 60 * 1000, // 5 minutes
  951. });
  952. // Fetch slot preset mappings (stores preset name for user-configured slots)
  953. const { data: slotPresets } = useQuery({
  954. queryKey: ['slotPresets', printer.id],
  955. queryFn: () => api.getSlotPresets(printer.id),
  956. staleTime: 2 * 60 * 1000, // 2 minutes
  957. });
  958. // Cache WiFi signal to prevent it disappearing on updates
  959. const [cachedWifiSignal, setCachedWifiSignal] = useState<number | null>(null);
  960. useEffect(() => {
  961. if (status?.wifi_signal != null) {
  962. setCachedWifiSignal(status.wifi_signal);
  963. }
  964. }, [status?.wifi_signal]);
  965. const wifiSignal = status?.wifi_signal ?? cachedWifiSignal;
  966. // Cache connected state to prevent flicker when status briefly becomes undefined
  967. const cachedConnected = useRef<boolean | undefined>(undefined);
  968. useEffect(() => {
  969. if (status?.connected !== undefined) {
  970. cachedConnected.current = status.connected;
  971. }
  972. }, [status?.connected]);
  973. const isConnected = status?.connected ?? cachedConnected.current;
  974. // Cache ams_extruder_map to prevent L/R indicators bouncing on updates
  975. const cachedAmsExtruderMap = useRef<Record<string, number>>({});
  976. useEffect(() => {
  977. if (status?.ams_extruder_map && Object.keys(status.ams_extruder_map).length > 0) {
  978. cachedAmsExtruderMap.current = status.ams_extruder_map;
  979. }
  980. }, [status?.ams_extruder_map]);
  981. const amsExtruderMap = (status?.ams_extruder_map && Object.keys(status.ams_extruder_map).length > 0)
  982. ? status.ams_extruder_map
  983. : cachedAmsExtruderMap.current;
  984. // Cache AMS data to prevent it disappearing on idle/offline printers
  985. const cachedAmsData = useRef<AMSUnit[]>([]);
  986. useEffect(() => {
  987. if (status?.ams && status.ams.length > 0) {
  988. cachedAmsData.current = status.ams;
  989. }
  990. }, [status?.ams]);
  991. const amsData = (status?.ams && status.ams.length > 0) ? status.ams : cachedAmsData.current;
  992. // Cache tray_now to prevent flickering when 255 (unloaded) or undefined values come in
  993. // Only update cache when we get a valid tray ID (0-253 or 254 for external)
  994. const cachedTrayNow = useRef<number>(255);
  995. const currentTrayNow = status?.tray_now;
  996. // Update cache synchronously during render if we have a valid value
  997. if (currentTrayNow !== undefined && currentTrayNow !== 255) {
  998. cachedTrayNow.current = currentTrayNow;
  999. }
  1000. // Use cached value if current is 255/undefined but we had a valid value before
  1001. const effectiveTrayNow = (currentTrayNow === undefined || currentTrayNow === 255)
  1002. ? cachedTrayNow.current
  1003. : currentTrayNow;
  1004. // Fetch smart plug for this printer
  1005. const { data: smartPlug } = useQuery({
  1006. queryKey: ['smartPlugByPrinter', printer.id],
  1007. queryFn: () => api.getSmartPlugByPrinter(printer.id),
  1008. });
  1009. // Fetch smart plug status if plug exists (faster refresh for energy monitoring)
  1010. const { data: plugStatus } = useQuery({
  1011. queryKey: ['smartPlugStatus', smartPlug?.id],
  1012. queryFn: () => smartPlug ? api.getSmartPlugStatus(smartPlug.id) : null,
  1013. enabled: !!smartPlug,
  1014. refetchInterval: 10000, // 10 seconds for real-time power display
  1015. });
  1016. // Fetch queue count for this printer
  1017. const { data: queueItems } = useQuery({
  1018. queryKey: ['queue', printer.id, 'pending'],
  1019. queryFn: () => api.getQueue(printer.id, 'pending'),
  1020. });
  1021. const queueCount = queueItems?.length || 0;
  1022. // Fetch last completed print for this printer
  1023. const { data: lastPrints } = useQuery({
  1024. queryKey: ['archives', printer.id, 'last'],
  1025. queryFn: () => api.getArchives(printer.id, 1, 0),
  1026. enabled: status?.connected && status?.state !== 'RUNNING',
  1027. });
  1028. const lastPrint = lastPrints?.[0];
  1029. // Determine if this card should be hidden (use cached connected state to prevent flicker)
  1030. const shouldHide = hideIfDisconnected && isConnected === false;
  1031. const deleteMutation = useMutation({
  1032. mutationFn: (options: { deleteArchives: boolean }) =>
  1033. api.deletePrinter(printer.id, options.deleteArchives),
  1034. onSuccess: () => {
  1035. queryClient.invalidateQueries({ queryKey: ['printers'] });
  1036. queryClient.invalidateQueries({ queryKey: ['archives'] });
  1037. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1038. },
  1039. });
  1040. const connectMutation = useMutation({
  1041. mutationFn: () => api.connectPrinter(printer.id),
  1042. onSuccess: () => {
  1043. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  1044. },
  1045. });
  1046. // Smart plug control mutations
  1047. const powerControlMutation = useMutation({
  1048. mutationFn: (action: 'on' | 'off') =>
  1049. smartPlug ? api.controlSmartPlug(smartPlug.id, action) : Promise.reject('No plug'),
  1050. onSuccess: () => {
  1051. queryClient.invalidateQueries({ queryKey: ['smartPlugStatus', smartPlug?.id] });
  1052. },
  1053. });
  1054. const toggleAutoOffMutation = useMutation({
  1055. mutationFn: (enabled: boolean) =>
  1056. smartPlug ? api.updateSmartPlug(smartPlug.id, { auto_off: enabled }) : Promise.reject('No plug'),
  1057. onSuccess: () => {
  1058. queryClient.invalidateQueries({ queryKey: ['smartPlugByPrinter', printer.id] });
  1059. // Also invalidate the smart-plugs list to keep Settings page in sync
  1060. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  1061. },
  1062. });
  1063. // Print control mutations
  1064. const stopPrintMutation = useMutation({
  1065. mutationFn: () => api.stopPrint(printer.id),
  1066. onSuccess: () => {
  1067. showToast('Print stopped');
  1068. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  1069. },
  1070. onError: (error: Error) => showToast(error.message || 'Failed to stop print', 'error'),
  1071. });
  1072. const pausePrintMutation = useMutation({
  1073. mutationFn: () => api.pausePrint(printer.id),
  1074. onSuccess: () => {
  1075. showToast('Print paused');
  1076. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  1077. },
  1078. onError: (error: Error) => showToast(error.message || 'Failed to pause print', 'error'),
  1079. });
  1080. const resumePrintMutation = useMutation({
  1081. mutationFn: () => api.resumePrint(printer.id),
  1082. onSuccess: () => {
  1083. showToast('Print resumed');
  1084. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  1085. },
  1086. onError: (error: Error) => showToast(error.message || 'Failed to resume print', 'error'),
  1087. });
  1088. // Chamber light mutation with optimistic update
  1089. const chamberLightMutation = useMutation({
  1090. mutationFn: (on: boolean) => api.setChamberLight(printer.id, on),
  1091. onMutate: async (on) => {
  1092. // Cancel any outgoing refetches
  1093. await queryClient.cancelQueries({ queryKey: ['printerStatus', printer.id] });
  1094. // Snapshot the previous value
  1095. const previousStatus = queryClient.getQueryData(['printerStatus', printer.id]);
  1096. // Optimistically update
  1097. queryClient.setQueryData(['printerStatus', printer.id], (old: typeof status) => ({
  1098. ...old,
  1099. chamber_light: on,
  1100. }));
  1101. return { previousStatus };
  1102. },
  1103. onSuccess: (_, on) => {
  1104. showToast(`Chamber light ${on ? 'on' : 'off'}`);
  1105. },
  1106. onError: (error: Error, _, context) => {
  1107. // Rollback on error
  1108. if (context?.previousStatus) {
  1109. queryClient.setQueryData(['printerStatus', printer.id], context.previousStatus);
  1110. }
  1111. showToast(error.message || 'Failed to control chamber light', 'error');
  1112. },
  1113. });
  1114. // Plate detection setting mutation
  1115. const plateDetectionMutation = useMutation({
  1116. mutationFn: (enabled: boolean) => api.updatePrinter(printer.id, { plate_detection_enabled: enabled }),
  1117. onSuccess: () => {
  1118. queryClient.invalidateQueries({ queryKey: ['printers'] });
  1119. showToast(plateDetectionMutation.variables ? 'Plate check enabled' : 'Plate check disabled');
  1120. },
  1121. onError: (error: Error) => showToast(error.message || 'Failed to update setting', 'error'),
  1122. });
  1123. // Query for printable objects (for skip functionality)
  1124. // Fetch when printing with 2+ objects OR when modal is open
  1125. const isPrintingWithObjects = (status?.state === 'RUNNING' || status?.state === 'PAUSE' || status?.state === 'PAUSED') && (status?.printable_objects_count ?? 0) >= 2;
  1126. const { data: objectsData, refetch: refetchObjects } = useQuery({
  1127. queryKey: ['printableObjects', printer.id],
  1128. queryFn: () => api.getPrintableObjects(printer.id),
  1129. enabled: showSkipObjectsModal || isPrintingWithObjects,
  1130. refetchInterval: showSkipObjectsModal ? 5000 : (isPrintingWithObjects ? 30000 : false), // 5s when modal open, 30s otherwise
  1131. });
  1132. // Skip objects mutation
  1133. const skipObjectsMutation = useMutation({
  1134. mutationFn: (objectIds: number[]) => api.skipObjects(printer.id, objectIds),
  1135. onSuccess: (data) => {
  1136. showToast(data.message || 'Objects skipped');
  1137. refetchObjects();
  1138. },
  1139. onError: (error: Error) => showToast(error.message || 'Failed to skip objects', 'error'),
  1140. });
  1141. // State for tracking which AMS slot is being refreshed
  1142. const [refreshingSlot, setRefreshingSlot] = useState<{ amsId: number; slotId: number } | null>(null);
  1143. // Track if we've seen the printer enter "busy" state (ams_status_main !== 0)
  1144. const seenBusyStateRef = useRef<boolean>(false);
  1145. // Fallback timeout ref
  1146. const refreshTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  1147. // Minimum display time passed
  1148. const minTimePassedRef = useRef<boolean>(false);
  1149. // AMS slot refresh mutation
  1150. const refreshAmsSlotMutation = useMutation({
  1151. mutationFn: ({ amsId, slotId }: { amsId: number; slotId: number }) =>
  1152. api.refreshAmsSlot(printer.id, amsId, slotId),
  1153. onMutate: ({ amsId, slotId }) => {
  1154. // Clear any existing timeout
  1155. if (refreshTimeoutRef.current) {
  1156. clearTimeout(refreshTimeoutRef.current);
  1157. }
  1158. // Reset state
  1159. seenBusyStateRef.current = false;
  1160. minTimePassedRef.current = false;
  1161. setRefreshingSlot({ amsId, slotId });
  1162. // Minimum display time (2 seconds)
  1163. setTimeout(() => {
  1164. minTimePassedRef.current = true;
  1165. }, 2000);
  1166. // Fallback timeout (30 seconds max)
  1167. refreshTimeoutRef.current = setTimeout(() => {
  1168. setRefreshingSlot(null);
  1169. }, 30000);
  1170. },
  1171. onSuccess: (data) => {
  1172. showToast(data.message || 'RFID re-read initiated');
  1173. },
  1174. onError: (error: Error) => {
  1175. showToast(error.message || 'Failed to re-read RFID', 'error');
  1176. if (refreshTimeoutRef.current) {
  1177. clearTimeout(refreshTimeoutRef.current);
  1178. }
  1179. setRefreshingSlot(null);
  1180. },
  1181. });
  1182. // Plate references state
  1183. const [plateReferences, setPlateReferences] = useState<{
  1184. references: Array<{ index: number; label: string; timestamp: string; has_image: boolean; thumbnail_url: string }>;
  1185. max_references: number;
  1186. } | null>(null);
  1187. const [editingRefLabel, setEditingRefLabel] = useState<{ index: number; label: string } | null>(null);
  1188. // Fetch plate references
  1189. const fetchPlateReferences = async () => {
  1190. try {
  1191. const data = await api.getPlateReferences(printer.id);
  1192. setPlateReferences(data);
  1193. } catch {
  1194. // Ignore errors - references will show as empty
  1195. }
  1196. };
  1197. // Toggle plate detection enabled/disabled
  1198. const handleTogglePlateDetection = () => {
  1199. plateDetectionMutation.mutate(!printer.plate_detection_enabled);
  1200. };
  1201. // Open plate detection management modal (for calibration/references)
  1202. const handleOpenPlateManagement = async () => {
  1203. setIsCheckingPlate(true);
  1204. setPlateCheckResult(null);
  1205. // Auto-turn on light if it's off
  1206. const lightWasOff = status?.chamber_light === false;
  1207. setPlateCheckLightWasOff(lightWasOff);
  1208. if (lightWasOff) {
  1209. await api.setChamberLight(printer.id, true);
  1210. // Wait for light to physically turn on and camera to adjust exposure
  1211. // (MQTT command is async, light takes ~1s to turn on, camera needs time to adjust)
  1212. await new Promise(resolve => setTimeout(resolve, 2500));
  1213. }
  1214. try {
  1215. const result = await api.checkPlateEmpty(printer.id, { includeDebugImage: true });
  1216. setPlateCheckResult(result);
  1217. fetchPlateReferences();
  1218. } catch (error) {
  1219. showToast(error instanceof Error ? error.message : 'Failed to check plate', 'error');
  1220. // Restore light if check failed
  1221. if (lightWasOff) {
  1222. await api.setChamberLight(printer.id, false);
  1223. setPlateCheckLightWasOff(false);
  1224. }
  1225. } finally {
  1226. setIsCheckingPlate(false);
  1227. }
  1228. };
  1229. // Close plate check modal and restore light state
  1230. const closePlateCheckModal = useCallback(async () => {
  1231. setPlateCheckResult(null);
  1232. // Restore light to original state if we turned it on
  1233. if (plateCheckLightWasOff) {
  1234. await api.setChamberLight(printer.id, false);
  1235. setPlateCheckLightWasOff(false);
  1236. }
  1237. }, [plateCheckLightWasOff, printer.id]);
  1238. // Calibrate plate detection handler
  1239. const handleCalibratePlate = async (label?: string) => {
  1240. setIsCalibrating(true);
  1241. try {
  1242. const result = await api.calibratePlateDetection(printer.id, { label });
  1243. if (result.success) {
  1244. showToast(result.message || 'Calibration saved!', 'success');
  1245. // Refresh references and re-check
  1246. fetchPlateReferences();
  1247. const checkResult = await api.checkPlateEmpty(printer.id, { includeDebugImage: true });
  1248. setPlateCheckResult(checkResult);
  1249. } else {
  1250. showToast(result.message || 'Calibration failed', 'error');
  1251. }
  1252. } catch (error) {
  1253. showToast(error instanceof Error ? error.message : 'Calibration failed', 'error');
  1254. } finally {
  1255. setIsCalibrating(false);
  1256. }
  1257. };
  1258. // Update reference label
  1259. const handleUpdateRefLabel = async (index: number, label: string) => {
  1260. try {
  1261. await api.updatePlateReferenceLabel(printer.id, index, label);
  1262. setEditingRefLabel(null);
  1263. fetchPlateReferences();
  1264. } catch (error) {
  1265. showToast(error instanceof Error ? error.message : 'Failed to update label', 'error');
  1266. }
  1267. };
  1268. // Delete reference
  1269. const handleDeleteRef = async (index: number) => {
  1270. try {
  1271. await api.deletePlateReference(printer.id, index);
  1272. showToast('Reference deleted', 'success');
  1273. fetchPlateReferences();
  1274. // Re-check to update counts
  1275. const checkResult = await api.checkPlateEmpty(printer.id, { includeDebugImage: true });
  1276. setPlateCheckResult(checkResult);
  1277. } catch (error) {
  1278. showToast(error instanceof Error ? error.message : 'Failed to delete reference', 'error');
  1279. }
  1280. };
  1281. // Save ROI settings
  1282. const handleSaveRoi = async () => {
  1283. if (!editingRoi) return;
  1284. setIsSavingRoi(true);
  1285. try {
  1286. await api.updatePrinter(printer.id, { plate_detection_roi: editingRoi });
  1287. showToast('Detection area saved', 'success');
  1288. setEditingRoi(null);
  1289. // Re-check to see new ROI in action
  1290. const checkResult = await api.checkPlateEmpty(printer.id, { includeDebugImage: true });
  1291. setPlateCheckResult(checkResult);
  1292. } catch (error) {
  1293. showToast(error instanceof Error ? error.message : 'Failed to save detection area', 'error');
  1294. } finally {
  1295. setIsSavingRoi(false);
  1296. }
  1297. };
  1298. // Close plate check modal on Escape key
  1299. useEffect(() => {
  1300. const handleEscape = (e: KeyboardEvent) => {
  1301. if (e.key === 'Escape' && plateCheckResult) {
  1302. closePlateCheckModal();
  1303. }
  1304. };
  1305. window.addEventListener('keydown', handleEscape);
  1306. return () => window.removeEventListener('keydown', handleEscape);
  1307. }, [plateCheckResult, closePlateCheckModal]);
  1308. // Watch ams_status_main to detect when RFID read completes
  1309. // ams_status_main: 0=idle, 2=rfid_identifying
  1310. const deferredClearRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  1311. useEffect(() => {
  1312. if (!refreshingSlot) return;
  1313. const amsStatus = status?.ams_status_main ?? 0;
  1314. // Track when we see non-idle state (printer is working)
  1315. if (amsStatus !== 0) {
  1316. seenBusyStateRef.current = true;
  1317. // Cancel any deferred clear since we're back to busy
  1318. if (deferredClearRef.current) {
  1319. clearTimeout(deferredClearRef.current);
  1320. deferredClearRef.current = null;
  1321. }
  1322. }
  1323. // When we've seen busy and now idle, clear (with min time check)
  1324. if (seenBusyStateRef.current && amsStatus === 0) {
  1325. if (minTimePassedRef.current) {
  1326. // Min time passed - clear now
  1327. if (refreshTimeoutRef.current) {
  1328. clearTimeout(refreshTimeoutRef.current);
  1329. }
  1330. setRefreshingSlot(null);
  1331. } else {
  1332. // Schedule clear after min time (2 seconds from start)
  1333. if (!deferredClearRef.current) {
  1334. deferredClearRef.current = setTimeout(() => {
  1335. if (refreshTimeoutRef.current) {
  1336. clearTimeout(refreshTimeoutRef.current);
  1337. }
  1338. setRefreshingSlot(null);
  1339. }, 2000);
  1340. }
  1341. }
  1342. }
  1343. return () => {
  1344. if (deferredClearRef.current) {
  1345. clearTimeout(deferredClearRef.current);
  1346. }
  1347. };
  1348. }, [status?.ams_status_main, refreshingSlot]);
  1349. // State for AMS slot menu
  1350. const [amsSlotMenu, setAmsSlotMenu] = useState<{ amsId: number; slotId: number } | null>(null);
  1351. if (shouldHide) {
  1352. return null;
  1353. }
  1354. // Size-based styling helpers
  1355. const getImageSize = () => {
  1356. switch (cardSize) {
  1357. case 1: return 'w-10 h-10';
  1358. case 2: return 'w-14 h-14';
  1359. case 3: return 'w-16 h-16';
  1360. case 4: return 'w-20 h-20';
  1361. default: return 'w-14 h-14';
  1362. }
  1363. };
  1364. const getTitleSize = () => {
  1365. switch (cardSize) {
  1366. case 1: return 'text-base truncate';
  1367. case 2: return 'text-lg';
  1368. case 3: return 'text-xl';
  1369. case 4: return 'text-2xl';
  1370. default: return 'text-lg';
  1371. }
  1372. };
  1373. const getSpacing = () => {
  1374. switch (cardSize) {
  1375. case 1: return 'mb-2';
  1376. case 2: return 'mb-4';
  1377. case 3: return 'mb-5';
  1378. case 4: return 'mb-6';
  1379. default: return 'mb-4';
  1380. }
  1381. };
  1382. return (
  1383. <Card className="relative">
  1384. <CardContent className={cardSize >= 3 ? 'p-5' : ''}>
  1385. {/* Header */}
  1386. <div className={getSpacing()}>
  1387. {/* Top row: Image, Name, Menu */}
  1388. <div className="flex items-start justify-between gap-2">
  1389. <div className="flex items-center gap-3 min-w-0 flex-1">
  1390. {/* Printer Model Image */}
  1391. <img
  1392. src={getPrinterImage(printer.model)}
  1393. alt={printer.model || 'Printer'}
  1394. className={`object-contain rounded-lg bg-bambu-dark flex-shrink-0 ${getImageSize()}`}
  1395. />
  1396. <div className="min-w-0 flex-1">
  1397. <div className="flex items-center gap-2">
  1398. <h3 className={`font-semibold text-white ${getTitleSize()}`}>{printer.name}</h3>
  1399. {/* Connection indicator dot for compact mode */}
  1400. {viewMode === 'compact' && (
  1401. <div
  1402. className={`w-2 h-2 rounded-full flex-shrink-0 ${
  1403. status?.connected ? 'bg-status-ok' : 'bg-status-error'
  1404. }`}
  1405. title={status?.connected ? 'Connected' : 'Offline'}
  1406. />
  1407. )}
  1408. </div>
  1409. <p className="text-sm text-bambu-gray">
  1410. {printer.model || 'Unknown Model'}
  1411. {/* Nozzle Info - only in expanded */}
  1412. {viewMode === 'expanded' && status?.nozzles && status.nozzles[0]?.nozzle_diameter && (
  1413. <span className="ml-1.5 text-bambu-gray" title={status.nozzles[0].nozzle_type || 'Nozzle'}>
  1414. • {status.nozzles[0].nozzle_diameter}mm
  1415. </span>
  1416. )}
  1417. {viewMode === 'expanded' && maintenanceInfo && maintenanceInfo.total_print_hours > 0 && (
  1418. <span className="ml-2 text-bambu-gray">
  1419. <Clock className="w-3 h-3 inline-block mr-1" />
  1420. {Math.round(maintenanceInfo.total_print_hours)}h
  1421. </span>
  1422. )}
  1423. </p>
  1424. </div>
  1425. </div>
  1426. {/* Menu button */}
  1427. <div className="relative flex-shrink-0">
  1428. <Button
  1429. variant="ghost"
  1430. size="sm"
  1431. onClick={() => setShowMenu(!showMenu)}
  1432. >
  1433. <MoreVertical className="w-4 h-4" />
  1434. </Button>
  1435. {showMenu && (
  1436. <div className="absolute right-0 mt-2 w-48 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-lg z-10">
  1437. <button
  1438. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  1439. onClick={() => {
  1440. setShowEditModal(true);
  1441. setShowMenu(false);
  1442. }}
  1443. >
  1444. <Pencil className="w-4 h-4" />
  1445. Edit
  1446. </button>
  1447. <button
  1448. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  1449. onClick={() => {
  1450. connectMutation.mutate();
  1451. setShowMenu(false);
  1452. }}
  1453. >
  1454. <RefreshCw className="w-4 h-4" />
  1455. Reconnect
  1456. </button>
  1457. <button
  1458. className="w-full px-4 py-2 text-left text-sm hover:bg-bambu-dark-tertiary flex items-center gap-2"
  1459. onClick={() => {
  1460. setShowMQTTDebug(true);
  1461. setShowMenu(false);
  1462. }}
  1463. >
  1464. <Terminal className="w-4 h-4" />
  1465. MQTT Debug
  1466. </button>
  1467. <button
  1468. className="w-full px-4 py-2 text-left text-sm text-red-400 hover:bg-bambu-dark-tertiary flex items-center gap-2"
  1469. onClick={() => {
  1470. setShowDeleteConfirm(true);
  1471. setShowMenu(false);
  1472. }}
  1473. >
  1474. <Trash2 className="w-4 h-4" />
  1475. Delete
  1476. </button>
  1477. </div>
  1478. )}
  1479. </div>
  1480. </div>
  1481. {/* Badges row - only in expanded mode */}
  1482. {viewMode === 'expanded' && (
  1483. <div className="flex flex-wrap items-center gap-2 mt-2">
  1484. {/* Connection status badge */}
  1485. <span
  1486. className={`flex items-center gap-1.5 px-2 py-1 rounded-full text-xs ${
  1487. status?.connected
  1488. ? 'bg-status-ok/20 text-status-ok'
  1489. : 'bg-status-error/20 text-status-error'
  1490. }`}
  1491. >
  1492. {status?.connected ? (
  1493. <Link className="w-3 h-3" />
  1494. ) : (
  1495. <Unlink className="w-3 h-3" />
  1496. )}
  1497. {status?.connected ? 'Connected' : 'Offline'}
  1498. </span>
  1499. {/* WiFi signal strength indicator */}
  1500. {status?.connected && wifiSignal != null && (
  1501. <span
  1502. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs ${
  1503. wifiSignal >= -50
  1504. ? 'bg-status-ok/20 text-status-ok'
  1505. : wifiSignal >= -60
  1506. ? 'bg-status-ok/20 text-status-ok'
  1507. : wifiSignal >= -70
  1508. ? 'bg-status-warning/20 text-status-warning'
  1509. : wifiSignal >= -80
  1510. ? 'bg-orange-500/20 text-orange-600'
  1511. : 'bg-status-error/20 text-status-error'
  1512. }`}
  1513. title={`WiFi: ${wifiSignal} dBm - ${getWifiStrength(wifiSignal).label}`}
  1514. >
  1515. <Signal className="w-3 h-3" />
  1516. {wifiSignal}dBm
  1517. </span>
  1518. )}
  1519. {/* HMS Status Indicator */}
  1520. {status?.connected && (() => {
  1521. const knownErrors = status.hms_errors ? filterKnownHMSErrors(status.hms_errors) : [];
  1522. return (
  1523. <button
  1524. onClick={() => setShowHMSModal(true)}
  1525. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs cursor-pointer hover:opacity-80 transition-opacity ${
  1526. knownErrors.length > 0
  1527. ? knownErrors.some(e => e.severity <= 2)
  1528. ? 'bg-status-error/20 text-status-error'
  1529. : 'bg-status-warning/20 text-status-warning'
  1530. : 'bg-status-ok/20 text-status-ok'
  1531. }`}
  1532. title="Click to view HMS errors"
  1533. >
  1534. <AlertTriangle className="w-3 h-3" />
  1535. {knownErrors.length > 0 ? knownErrors.length : 'OK'}
  1536. </button>
  1537. );
  1538. })()}
  1539. {/* Maintenance Status Indicator */}
  1540. {maintenanceInfo && (
  1541. <button
  1542. onClick={() => navigate('/maintenance')}
  1543. className={`flex items-center gap-1 px-2 py-1 rounded-full text-xs cursor-pointer hover:opacity-80 transition-opacity ${
  1544. maintenanceInfo.due_count > 0
  1545. ? 'bg-status-error/20 text-status-error'
  1546. : maintenanceInfo.warning_count > 0
  1547. ? 'bg-status-warning/20 text-status-warning'
  1548. : 'bg-status-ok/20 text-status-ok'
  1549. }`}
  1550. title={
  1551. maintenanceInfo.due_count > 0 || maintenanceInfo.warning_count > 0
  1552. ? `${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`
  1553. : 'All maintenance up to date - Click to view'
  1554. }
  1555. >
  1556. <Wrench className="w-3 h-3" />
  1557. {maintenanceInfo.due_count > 0 || maintenanceInfo.warning_count > 0
  1558. ? maintenanceInfo.due_count + maintenanceInfo.warning_count
  1559. : 'OK'}
  1560. </button>
  1561. )}
  1562. {/* Queue Count Badge */}
  1563. {queueCount > 0 && (
  1564. <button
  1565. onClick={() => navigate('/queue')}
  1566. 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"
  1567. title={`${queueCount} print${queueCount > 1 ? 's' : ''} in queue`}
  1568. >
  1569. <Layers className="w-3 h-3" />
  1570. {queueCount}
  1571. </button>
  1572. )}
  1573. {/* Firmware Update Badge */}
  1574. {firmwareInfo?.update_available && (
  1575. <button
  1576. onClick={() => setShowFirmwareModal(true)}
  1577. className="flex items-center gap-1 px-2 py-1 rounded-full text-xs bg-orange-500/20 text-orange-400 hover:opacity-80 transition-opacity"
  1578. title={`Firmware update available: ${firmwareInfo.current_version} → ${firmwareInfo.latest_version}`}
  1579. >
  1580. <Download className="w-3 h-3" />
  1581. Update
  1582. </button>
  1583. )}
  1584. </div>
  1585. )}
  1586. </div>
  1587. {/* Delete Confirmation */}
  1588. {showDeleteConfirm && (
  1589. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
  1590. <Card className="w-full max-w-md mx-4">
  1591. <CardContent>
  1592. <div className="flex items-start gap-3 mb-4">
  1593. <div className="p-2 rounded-full bg-red-500/20">
  1594. <AlertTriangle className="w-5 h-5 text-red-400" />
  1595. </div>
  1596. <div>
  1597. <h3 className="text-lg font-semibold text-white">Delete Printer</h3>
  1598. <p className="text-sm text-bambu-gray mt-1">
  1599. Are you sure you want to delete "{printer.name}"? This will remove all connection settings.
  1600. </p>
  1601. </div>
  1602. </div>
  1603. <div className="bg-bambu-dark rounded-lg p-3 mb-4">
  1604. <label className="flex items-start gap-3 cursor-pointer">
  1605. <input
  1606. type="checkbox"
  1607. checked={deleteArchives}
  1608. onChange={(e) => setDeleteArchives(e.target.checked)}
  1609. 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"
  1610. />
  1611. <div>
  1612. <span className="text-sm text-white">Delete print archives</span>
  1613. <p className="text-xs text-bambu-gray mt-0.5">
  1614. {deleteArchives
  1615. ? 'All print history for this printer will be permanently deleted.'
  1616. : 'Print history will be kept but no longer associated with this printer.'}
  1617. </p>
  1618. </div>
  1619. </label>
  1620. </div>
  1621. <div className="flex justify-end gap-2">
  1622. <Button
  1623. variant="secondary"
  1624. onClick={() => {
  1625. setShowDeleteConfirm(false);
  1626. setDeleteArchives(true);
  1627. }}
  1628. >
  1629. Cancel
  1630. </Button>
  1631. <Button
  1632. variant="danger"
  1633. onClick={() => {
  1634. deleteMutation.mutate({ deleteArchives });
  1635. setShowDeleteConfirm(false);
  1636. setDeleteArchives(true);
  1637. }}
  1638. >
  1639. Delete
  1640. </Button>
  1641. </div>
  1642. </CardContent>
  1643. </Card>
  1644. </div>
  1645. )}
  1646. {/* Status */}
  1647. {status?.connected && (
  1648. <>
  1649. {/* Compact: Simple status bar */}
  1650. {viewMode === 'compact' ? (
  1651. <div className="mt-2">
  1652. {status.state === 'RUNNING' ? (
  1653. <div className="flex items-center gap-2">
  1654. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-1.5">
  1655. <div
  1656. className="bg-bambu-green h-1.5 rounded-full transition-all"
  1657. style={{ width: `${status.progress || 0}%` }}
  1658. />
  1659. </div>
  1660. <span className="text-xs text-white">{Math.round(status.progress || 0)}%</span>
  1661. </div>
  1662. ) : (
  1663. <p className="text-xs text-bambu-gray">{getStatusDisplay(status.state, status.stg_cur_name)}</p>
  1664. )}
  1665. </div>
  1666. ) : (
  1667. /* Expanded: Full status section */
  1668. <>
  1669. {/* Current Print or Idle Placeholder */}
  1670. <div className="mb-4 p-3 bg-bambu-dark rounded-lg relative">
  1671. {/* Skip Objects button - top right corner, always visible */}
  1672. <button
  1673. onClick={() => setShowSkipObjectsModal(true)}
  1674. disabled={!(status.state === 'RUNNING' || status.state === 'PAUSE' || status.state === 'PAUSED') || (status.printable_objects_count ?? 0) < 2}
  1675. className={`absolute top-2 right-2 p-1.5 rounded transition-colors z-10 ${
  1676. (status.state === 'RUNNING' || status.state === 'PAUSE' || status.state === 'PAUSED') && (status.printable_objects_count ?? 0) >= 2
  1677. ? 'text-bambu-gray hover:text-white hover:bg-white/10'
  1678. : 'text-bambu-gray/30 cursor-not-allowed'
  1679. }`}
  1680. title={
  1681. !(status.state === 'RUNNING' || status.state === 'PAUSE' || status.state === 'PAUSED')
  1682. ? "Skip objects (only while printing)"
  1683. : (status.printable_objects_count ?? 0) >= 2
  1684. ? "Skip objects"
  1685. : "Skip objects (requires 2+ objects)"
  1686. }
  1687. >
  1688. <SkipObjectsIcon className="w-4 h-4" />
  1689. {/* Badge showing skipped count */}
  1690. {objectsData && objectsData.skipped_count > 0 && (
  1691. <span className="absolute -top-1 -right-1 min-w-[16px] h-4 px-1 flex items-center justify-center text-[10px] font-bold bg-red-500 text-white rounded-full">
  1692. {objectsData.skipped_count}
  1693. </span>
  1694. )}
  1695. </button>
  1696. <div className="flex gap-3">
  1697. {/* Cover Image */}
  1698. <CoverImage
  1699. url={status.state === 'RUNNING' ? status.cover_url : null}
  1700. printName={status.state === 'RUNNING' ? (status.subtask_name || status.current_print || undefined) : undefined}
  1701. />
  1702. {/* Print Info */}
  1703. <div className="flex-1 min-w-0">
  1704. {status.current_print && status.state === 'RUNNING' ? (
  1705. <>
  1706. <p className="text-sm text-bambu-gray mb-1">{status.stg_cur_name || 'Printing'}</p>
  1707. <p className="text-white text-sm mb-2 truncate">
  1708. {status.subtask_name || status.current_print}
  1709. </p>
  1710. <div className="flex items-center justify-between text-sm">
  1711. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-2 mr-3">
  1712. <div
  1713. className="bg-bambu-green h-2 rounded-full transition-all"
  1714. style={{ width: `${status.progress || 0}%` }}
  1715. />
  1716. </div>
  1717. <span className="text-white">{Math.round(status.progress || 0)}%</span>
  1718. </div>
  1719. <div className="flex items-center gap-3 mt-2 text-xs text-bambu-gray">
  1720. {status.remaining_time != null && status.remaining_time > 0 && (
  1721. <>
  1722. <span className="flex items-center gap-1">
  1723. <Clock className="w-3 h-3" />
  1724. {formatTime(status.remaining_time * 60)}
  1725. </span>
  1726. <span className="text-bambu-green font-medium" title="Estimated completion time">
  1727. ETA {formatETA(status.remaining_time, timeFormat)}
  1728. </span>
  1729. </>
  1730. )}
  1731. {status.layer_num != null && status.total_layers != null && status.total_layers > 0 && (
  1732. <span className="flex items-center gap-1">
  1733. <Layers className="w-3 h-3" />
  1734. {status.layer_num}/{status.total_layers}
  1735. </span>
  1736. )}
  1737. </div>
  1738. </>
  1739. ) : (
  1740. <>
  1741. <p className="text-sm text-bambu-gray mb-1">Status</p>
  1742. <p className="text-white text-sm mb-2">
  1743. {getStatusDisplay(status.state, status.stg_cur_name)}
  1744. </p>
  1745. <div className="flex items-center justify-between text-sm">
  1746. <div className="flex-1 bg-bambu-dark-tertiary rounded-full h-2 mr-3">
  1747. <div className="bg-bambu-dark-tertiary h-2 rounded-full" />
  1748. </div>
  1749. <span className="text-bambu-gray">—</span>
  1750. </div>
  1751. {lastPrint ? (
  1752. <p className="text-xs text-bambu-gray mt-2 truncate" title={lastPrint.print_name || lastPrint.filename}>
  1753. Last: {lastPrint.print_name || lastPrint.filename}
  1754. {lastPrint.completed_at && (
  1755. <span className="ml-1 text-bambu-gray/60">
  1756. • {formatDateOnly(lastPrint.completed_at, { month: 'short', day: 'numeric' })}
  1757. </span>
  1758. )}
  1759. </p>
  1760. ) : (
  1761. <p className="text-xs text-bambu-gray mt-2">Ready to print</p>
  1762. )}
  1763. </>
  1764. )}
  1765. </div>
  1766. </div>
  1767. </div>
  1768. {/* Queue Widget - shows next scheduled print */}
  1769. {status.state !== 'RUNNING' && (
  1770. <PrinterQueueWidget printerId={printer.id} />
  1771. )}
  1772. </>
  1773. )}
  1774. {/* Temperatures */}
  1775. {status.temperatures && viewMode === 'expanded' && (() => {
  1776. // Use actual heater states from MQTT stream
  1777. const nozzleHeating = status.temperatures.nozzle_heating || status.temperatures.nozzle_2_heating || false;
  1778. const bedHeating = status.temperatures.bed_heating || false;
  1779. const chamberHeating = status.temperatures.chamber_heating || false;
  1780. const isDualNozzle = printer.nozzle_count === 2 || status.temperatures.nozzle_2 !== undefined;
  1781. // active_extruder: 0=right, 1=left
  1782. const activeNozzle = status.active_extruder === 1 ? 'L' : 'R';
  1783. return (
  1784. <div className="flex items-center gap-1.5">
  1785. {/* Nozzle temp - combined for dual nozzle */}
  1786. <div className="text-center px-2 py-1.5 bg-bambu-dark rounded-lg flex-1">
  1787. <HeaterThermometer className="w-3.5 h-3.5 mx-auto mb-0.5" color="text-orange-400" isHeating={nozzleHeating} />
  1788. {status.temperatures.nozzle_2 !== undefined ? (
  1789. <>
  1790. <p className="text-[9px] text-bambu-gray">L / R</p>
  1791. <p className="text-[11px] text-white">
  1792. {Math.round(status.temperatures.nozzle || 0)}° / {Math.round(status.temperatures.nozzle_2 || 0)}°
  1793. </p>
  1794. </>
  1795. ) : (
  1796. <>
  1797. <p className="text-[9px] text-bambu-gray">Nozzle</p>
  1798. <p className="text-[11px] text-white">
  1799. {Math.round(status.temperatures.nozzle || 0)}°C
  1800. </p>
  1801. </>
  1802. )}
  1803. </div>
  1804. <div className="text-center px-2 py-1.5 bg-bambu-dark rounded-lg flex-1">
  1805. <HeaterThermometer className="w-3.5 h-3.5 mx-auto mb-0.5" color="text-blue-400" isHeating={bedHeating} />
  1806. <p className="text-[9px] text-bambu-gray">Bed</p>
  1807. <p className="text-[11px] text-white">
  1808. {Math.round(status.temperatures.bed || 0)}°C
  1809. </p>
  1810. </div>
  1811. {status.temperatures.chamber !== undefined && (
  1812. <div className="text-center px-2 py-1.5 bg-bambu-dark rounded-lg flex-1">
  1813. <HeaterThermometer className="w-3.5 h-3.5 mx-auto mb-0.5" color="text-green-400" isHeating={chamberHeating} />
  1814. <p className="text-[9px] text-bambu-gray">Chamber</p>
  1815. <p className="text-[11px] text-white">
  1816. {Math.round(status.temperatures.chamber || 0)}°C
  1817. </p>
  1818. </div>
  1819. )}
  1820. {/* Active nozzle indicator for dual-nozzle printers */}
  1821. {isDualNozzle && (
  1822. <div className="text-center px-2 py-1.5 bg-bambu-dark rounded-lg" title={`Active: ${activeNozzle === 'L' ? 'Left' : 'Right'} nozzle`}>
  1823. <p className={`text-[11px] font-bold ${activeNozzle === 'L' ? 'text-amber-400' : 'text-gray-500'}`}>L</p>
  1824. <p className="text-[9px] text-bambu-gray">Nozzle</p>
  1825. <p className={`text-[11px] font-bold ${activeNozzle === 'R' ? 'text-amber-400' : 'text-gray-500'}`}>R</p>
  1826. </div>
  1827. )}
  1828. </div>
  1829. );
  1830. })()}
  1831. {/* Controls - Fans + Print Buttons */}
  1832. {viewMode === 'expanded' && (() => {
  1833. // Determine print state for control buttons
  1834. const isRunning = status.state === 'RUNNING';
  1835. const isPaused = status.state === 'PAUSED' || status.state === 'PAUSE';
  1836. const isPrinting = isRunning || isPaused;
  1837. const isControlBusy = stopPrintMutation.isPending || pausePrintMutation.isPending || resumePrintMutation.isPending;
  1838. // Fan data
  1839. const partFan = status.cooling_fan_speed;
  1840. const auxFan = status.big_fan1_speed;
  1841. const chamberFan = status.big_fan2_speed;
  1842. return (
  1843. <div className="mt-3">
  1844. {/* Section Header */}
  1845. <div className="flex items-center gap-2 mb-2">
  1846. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  1847. Controls
  1848. </span>
  1849. <div className="flex-1 h-px bg-bambu-dark-tertiary/30" />
  1850. </div>
  1851. <div className="flex items-center justify-between gap-2">
  1852. {/* Left: Fan Status - always visible, dynamic coloring */}
  1853. <div className="flex items-center gap-2">
  1854. {/* Part Cooling Fan */}
  1855. <div
  1856. className={`flex items-center gap-1 px-1.5 py-1 rounded ${partFan && partFan > 0 ? 'bg-cyan-500/10' : 'bg-bambu-dark'}`}
  1857. title="Part Cooling Fan"
  1858. >
  1859. <Fan className={`w-3.5 h-3.5 ${partFan && partFan > 0 ? 'text-cyan-400' : 'text-bambu-gray/50'}`} />
  1860. <span className={`text-[10px] ${partFan && partFan > 0 ? 'text-cyan-400' : 'text-bambu-gray/50'}`}>
  1861. {partFan ?? 0}%
  1862. </span>
  1863. </div>
  1864. {/* Auxiliary Fan */}
  1865. <div
  1866. className={`flex items-center gap-1 px-1.5 py-1 rounded ${auxFan && auxFan > 0 ? 'bg-blue-500/10' : 'bg-bambu-dark'}`}
  1867. title="Auxiliary Fan"
  1868. >
  1869. <Wind className={`w-3.5 h-3.5 ${auxFan && auxFan > 0 ? 'text-blue-400' : 'text-bambu-gray/50'}`} />
  1870. <span className={`text-[10px] ${auxFan && auxFan > 0 ? 'text-blue-400' : 'text-bambu-gray/50'}`}>
  1871. {auxFan ?? 0}%
  1872. </span>
  1873. </div>
  1874. {/* Chamber Fan */}
  1875. <div
  1876. className={`flex items-center gap-1 px-1.5 py-1 rounded ${chamberFan && chamberFan > 0 ? 'bg-green-500/10' : 'bg-bambu-dark'}`}
  1877. title="Chamber Fan"
  1878. >
  1879. <AirVent className={`w-3.5 h-3.5 ${chamberFan && chamberFan > 0 ? 'text-green-400' : 'text-bambu-gray/50'}`} />
  1880. <span className={`text-[10px] ${chamberFan && chamberFan > 0 ? 'text-green-400' : 'text-bambu-gray/50'}`}>
  1881. {chamberFan ?? 0}%
  1882. </span>
  1883. </div>
  1884. </div>
  1885. {/* Right: Print Control Buttons */}
  1886. <div className="flex items-center gap-2">
  1887. {/* Stop button */}
  1888. <button
  1889. onClick={() => setShowStopConfirm(true)}
  1890. disabled={!isPrinting || isControlBusy}
  1891. className={`
  1892. flex items-center justify-center gap-1 px-3 py-1.5 rounded-lg text-xs font-medium
  1893. transition-colors
  1894. ${isPrinting
  1895. ? 'bg-red-500/20 text-red-400 hover:bg-red-500/30'
  1896. : 'bg-bambu-dark text-bambu-gray/50 cursor-not-allowed'
  1897. }
  1898. `}
  1899. title="Stop print"
  1900. >
  1901. <Square className="w-3 h-3" />
  1902. Stop
  1903. </button>
  1904. {/* Pause/Resume button */}
  1905. <button
  1906. onClick={() => isPaused ? setShowResumeConfirm(true) : setShowPauseConfirm(true)}
  1907. disabled={!isPrinting || isControlBusy}
  1908. className={`
  1909. flex items-center justify-center gap-1 px-3 py-1.5 rounded-lg text-xs font-medium
  1910. transition-colors
  1911. ${isPrinting
  1912. ? isPaused
  1913. ? 'bg-bambu-green/20 text-bambu-green hover:bg-bambu-green/30'
  1914. : 'bg-yellow-500/20 text-yellow-400 hover:bg-yellow-500/30'
  1915. : 'bg-bambu-dark text-bambu-gray/50 cursor-not-allowed'
  1916. }
  1917. `}
  1918. title={isPaused ? 'Resume print' : 'Pause print'}
  1919. >
  1920. {isPaused ? <Play className="w-3 h-3" /> : <Pause className="w-3 h-3" />}
  1921. {isPaused ? 'Resume' : 'Pause'}
  1922. </button>
  1923. </div>
  1924. </div>
  1925. </div>
  1926. );
  1927. })()}
  1928. {/* AMS Units - 2-Column Grid Layout */}
  1929. {amsData && amsData.length > 0 && viewMode === 'expanded' && (() => {
  1930. // Separate regular AMS (4-tray) from HT AMS (1-tray)
  1931. const regularAms = amsData.filter(ams => ams.tray.length > 1);
  1932. const htAms = amsData.filter(ams => ams.tray.length === 1);
  1933. const isDualNozzle = printer.nozzle_count === 2 || status?.temperatures?.nozzle_2 !== undefined;
  1934. return (
  1935. <div className="mt-3">
  1936. {/* Section Header */}
  1937. <div className="flex items-center gap-2 mb-2">
  1938. <span className="text-[10px] uppercase tracking-wider text-bambu-gray font-medium">
  1939. Filaments
  1940. </span>
  1941. <div className="flex-1 h-px bg-bambu-dark-tertiary/30" />
  1942. </div>
  1943. {/* AMS Content */}
  1944. <div className="space-y-3">
  1945. {/* Row 1-2: Regular AMS (4-tray) in 2-column grid */}
  1946. {regularAms.length > 0 && (
  1947. <div className="grid grid-cols-2 gap-3">
  1948. {regularAms.map((ams) => {
  1949. const mappedExtruderId = amsExtruderMap[String(ams.id)];
  1950. const normalizedId = ams.id >= 128 ? ams.id - 128 : ams.id;
  1951. const extruderId = mappedExtruderId !== undefined ? mappedExtruderId : normalizedId;
  1952. const isLeftNozzle = extruderId === 1;
  1953. const isRightNozzle = extruderId === 0;
  1954. return (
  1955. <div key={ams.id} className="p-2.5 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary/30">
  1956. {/* Header: Label + Stats (no icon) */}
  1957. <div className="flex items-center justify-between mb-2">
  1958. <div className="flex items-center gap-1.5">
  1959. <span className="text-[10px] text-white font-medium">
  1960. {getAmsLabel(ams.id, ams.tray.length)}
  1961. </span>
  1962. {isDualNozzle && (isLeftNozzle || isRightNozzle) && (
  1963. <NozzleBadge side={isLeftNozzle ? 'L' : 'R'} />
  1964. )}
  1965. </div>
  1966. {(ams.humidity != null || ams.temp != null) && (
  1967. <div className="flex items-center gap-1.5">
  1968. {ams.humidity != null && (
  1969. <HumidityIndicator
  1970. humidity={ams.humidity}
  1971. goodThreshold={amsThresholds?.humidityGood}
  1972. fairThreshold={amsThresholds?.humidityFair}
  1973. onClick={() => setAmsHistoryModal({
  1974. amsId: ams.id,
  1975. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1976. mode: 'humidity',
  1977. })}
  1978. compact
  1979. />
  1980. )}
  1981. {ams.temp != null && (
  1982. <TemperatureIndicator
  1983. temp={ams.temp}
  1984. goodThreshold={amsThresholds?.tempGood}
  1985. fairThreshold={amsThresholds?.tempFair}
  1986. onClick={() => setAmsHistoryModal({
  1987. amsId: ams.id,
  1988. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  1989. mode: 'temperature',
  1990. })}
  1991. compact
  1992. />
  1993. )}
  1994. </div>
  1995. )}
  1996. </div>
  1997. {/* Slots grid: 4 columns - always render 4 slots */}
  1998. <div className="grid grid-cols-4 gap-1.5">
  1999. {[0, 1, 2, 3].map((slotIdx) => {
  2000. // Find tray data for this slot (may be undefined if data incomplete)
  2001. // Use array index if available, as tray.id may not always be set
  2002. const tray = ams.tray[slotIdx] || ams.tray.find(t => t.id === slotIdx);
  2003. const hasFillLevel = tray?.tray_type && tray.remain >= 0;
  2004. const isEmpty = !tray?.tray_type;
  2005. // Check if this is the currently loaded tray
  2006. // Global tray ID = ams.id * 4 + slot index (for standard AMS)
  2007. const globalTrayId = ams.id * 4 + slotIdx;
  2008. const isActive = effectiveTrayNow === globalTrayId;
  2009. // Get cloud preset info if available
  2010. const cloudInfo = tray?.tray_info_idx ? filamentInfo?.[tray.tray_info_idx] : null;
  2011. // Get saved slot preset mapping (for user-configured slots)
  2012. const slotPreset = slotPresets?.[globalTrayId];
  2013. // Build filament data for hover card
  2014. const filamentData = tray?.tray_type ? {
  2015. vendor: (isBambuLabSpool(tray) ? 'Bambu Lab' : 'Generic') as 'Bambu Lab' | 'Generic',
  2016. profile: cloudInfo?.name || slotPreset?.preset_name || tray.tray_sub_brands || tray.tray_type,
  2017. colorName: getBambuColorName(tray.tray_id_name) || hexToBasicColorName(tray.tray_color),
  2018. colorHex: tray.tray_color || null,
  2019. kFactor: formatKValue(tray.k),
  2020. fillLevel: hasFillLevel ? tray.remain : null,
  2021. trayUuid: tray.tray_uuid || null,
  2022. } : null;
  2023. // Check if this specific slot is being refreshed
  2024. const isRefreshing = refreshingSlot?.amsId === ams.id &&
  2025. refreshingSlot?.slotId === slotIdx;
  2026. // Slot visual content (goes inside hover card)
  2027. const slotVisual = (
  2028. <div
  2029. className={`bg-bambu-dark-tertiary rounded p-1 text-center ${isEmpty ? 'opacity-50' : ''} ${isActive ? 'ring-2 ring-bambu-green ring-offset-1 ring-offset-bambu-dark' : ''}`}
  2030. >
  2031. <div
  2032. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2"
  2033. style={{
  2034. backgroundColor: tray?.tray_color ? `#${tray.tray_color}` : (tray?.tray_type ? '#333' : 'transparent'),
  2035. borderColor: isEmpty ? '#666' : 'rgba(255,255,255,0.1)',
  2036. borderStyle: isEmpty ? 'dashed' : 'solid',
  2037. }}
  2038. />
  2039. <div className="text-[9px] text-white font-bold truncate">
  2040. {tray?.tray_type || '—'}
  2041. </div>
  2042. {/* Fill bar */}
  2043. <div className="mt-1 h-1.5 bg-black/30 rounded-full overflow-hidden">
  2044. {hasFillLevel && tray ? (
  2045. <div
  2046. className="h-full rounded-full transition-all"
  2047. style={{
  2048. width: `${tray.remain}%`,
  2049. backgroundColor: getFillBarColor(tray.remain),
  2050. }}
  2051. />
  2052. ) : tray?.tray_type ? (
  2053. <div className="h-full w-full rounded-full bg-white/50 dark:bg-gray-500/40" />
  2054. ) : null}
  2055. </div>
  2056. </div>
  2057. );
  2058. // Wrapper with menu button, dropdown, and loading overlay (outside hover card)
  2059. return (
  2060. <div key={slotIdx} className="relative group">
  2061. {/* Loading overlay during RFID re-read */}
  2062. {isRefreshing && (
  2063. <div className="absolute inset-0 bg-bambu-dark-tertiary/80 rounded flex items-center justify-center z-20">
  2064. <RefreshCw className="w-4 h-4 text-bambu-green animate-spin" />
  2065. </div>
  2066. )}
  2067. {/* Menu button - appears on hover, hidden when printer busy */}
  2068. {status?.state !== 'RUNNING' && (
  2069. <button
  2070. onClick={(e) => {
  2071. e.stopPropagation();
  2072. setAmsSlotMenu(
  2073. amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === slotIdx
  2074. ? null
  2075. : { amsId: ams.id, slotId: slotIdx }
  2076. );
  2077. }}
  2078. className="absolute -top-1 -right-1 w-4 h-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity z-10 hover:bg-bambu-dark-tertiary"
  2079. title="Slot options"
  2080. >
  2081. <MoreVertical className="w-2.5 h-2.5 text-bambu-gray" />
  2082. </button>
  2083. )}
  2084. {/* Dropdown menu */}
  2085. {status?.state !== 'RUNNING' && amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === slotIdx && (
  2086. <div className="absolute top-full left-0 mt-1 z-50 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]">
  2087. <button
  2088. className="w-full px-3 py-1.5 text-left text-xs text-white hover:bg-bambu-dark-tertiary flex items-center gap-2"
  2089. onClick={(e) => {
  2090. e.stopPropagation();
  2091. refreshAmsSlotMutation.mutate({ amsId: ams.id, slotId: slotIdx });
  2092. setAmsSlotMenu(null);
  2093. }}
  2094. disabled={isRefreshing}
  2095. >
  2096. <RefreshCw className={`w-3 h-3 ${isRefreshing ? 'animate-spin' : ''}`} />
  2097. Re-read RFID
  2098. </button>
  2099. </div>
  2100. )}
  2101. {/* Hover card wraps only the visual content */}
  2102. {filamentData ? (
  2103. <FilamentHoverCard
  2104. data={filamentData}
  2105. spoolman={{
  2106. enabled: spoolmanEnabled,
  2107. hasUnlinkedSpools,
  2108. onLinkSpool: spoolmanEnabled && filamentData.trayUuid ? (uuid) => {
  2109. setLinkSpoolModal({
  2110. trayUuid: uuid,
  2111. trayInfo: {
  2112. type: filamentData.profile,
  2113. color: filamentData.colorHex || '',
  2114. location: `${getAmsLabel(ams.id, ams.tray.length)} Slot ${slotIdx + 1}`,
  2115. },
  2116. });
  2117. } : undefined,
  2118. }}
  2119. configureSlot={{
  2120. enabled: true,
  2121. onConfigure: () => setConfigureSlotModal({
  2122. amsId: ams.id,
  2123. trayId: slotIdx,
  2124. trayCount: ams.tray.length,
  2125. trayType: tray?.tray_type || undefined,
  2126. trayColor: tray?.tray_color || undefined,
  2127. traySubBrands: tray?.tray_sub_brands || undefined,
  2128. trayInfoIdx: tray?.tray_info_idx || undefined,
  2129. }),
  2130. }}
  2131. >
  2132. {slotVisual}
  2133. </FilamentHoverCard>
  2134. ) : (
  2135. <EmptySlotHoverCard
  2136. configureSlot={{
  2137. enabled: true,
  2138. onConfigure: () => setConfigureSlotModal({
  2139. amsId: ams.id,
  2140. trayId: slotIdx,
  2141. trayCount: ams.tray.length,
  2142. }),
  2143. }}
  2144. >
  2145. {slotVisual}
  2146. </EmptySlotHoverCard>
  2147. )}
  2148. </div>
  2149. );
  2150. })}
  2151. </div>
  2152. </div>
  2153. );
  2154. })}
  2155. </div>
  2156. )}
  2157. {/* Row 3: HT AMS + External spools (same style as regular AMS, 4 across) */}
  2158. {(htAms.length > 0 || (status.vt_tray && status.vt_tray.tray_type)) && (
  2159. <div className="grid grid-cols-4 gap-3">
  2160. {/* HT AMS units - name/badge top, slot left, stats right */}
  2161. {htAms.map((ams) => {
  2162. const mappedExtruderId = amsExtruderMap[String(ams.id)];
  2163. const normalizedId = ams.id >= 128 ? ams.id - 128 : ams.id;
  2164. const extruderId = mappedExtruderId !== undefined ? mappedExtruderId : normalizedId;
  2165. const isLeftNozzle = extruderId === 1;
  2166. const isRightNozzle = extruderId === 0;
  2167. const tray = ams.tray[0];
  2168. const hasFillLevel = tray?.tray_type && tray.remain >= 0;
  2169. const isEmpty = !tray?.tray_type;
  2170. // Check if this is the currently loaded tray
  2171. // Global tray ID = ams.id * 4 + tray.id
  2172. const globalTrayId = ams.id * 4 + (tray?.id ?? 0);
  2173. const isActive = effectiveTrayNow === globalTrayId;
  2174. // Get cloud preset info if available
  2175. const cloudInfo = tray?.tray_info_idx ? filamentInfo?.[tray.tray_info_idx] : null;
  2176. // Get saved slot preset mapping (for user-configured slots)
  2177. const slotPreset = slotPresets?.[globalTrayId];
  2178. // Build filament data for hover card
  2179. const filamentData = tray?.tray_type ? {
  2180. vendor: (isBambuLabSpool(tray) ? 'Bambu Lab' : 'Generic') as 'Bambu Lab' | 'Generic',
  2181. profile: cloudInfo?.name || slotPreset?.preset_name || tray.tray_sub_brands || tray.tray_type,
  2182. colorName: getBambuColorName(tray.tray_id_name) || hexToBasicColorName(tray.tray_color),
  2183. colorHex: tray.tray_color || null,
  2184. kFactor: formatKValue(tray.k),
  2185. fillLevel: hasFillLevel ? tray.remain : null,
  2186. trayUuid: tray.tray_uuid || null,
  2187. } : null;
  2188. const htSlotId = tray?.id ?? 0;
  2189. // Check if this specific slot is being refreshed
  2190. const isHtRefreshing = refreshingSlot?.amsId === ams.id &&
  2191. refreshingSlot?.slotId === htSlotId;
  2192. // Slot visual content (goes inside hover card)
  2193. const slotVisual = (
  2194. <div
  2195. className={`bg-bambu-dark-tertiary rounded p-1 text-center ${isEmpty ? 'opacity-50' : ''} ${isActive ? 'ring-2 ring-bambu-green ring-offset-1 ring-offset-bambu-dark' : ''}`}
  2196. >
  2197. <div
  2198. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2"
  2199. style={{
  2200. backgroundColor: tray?.tray_color ? `#${tray.tray_color}` : (tray?.tray_type ? '#333' : 'transparent'),
  2201. borderColor: isEmpty ? '#666' : 'rgba(255,255,255,0.1)',
  2202. borderStyle: isEmpty ? 'dashed' : 'solid',
  2203. }}
  2204. />
  2205. <div className="text-[9px] text-white font-bold truncate">
  2206. {tray?.tray_type || '—'}
  2207. </div>
  2208. {/* Fill bar */}
  2209. <div className="mt-1 h-1.5 bg-black/30 rounded-full overflow-hidden">
  2210. {hasFillLevel ? (
  2211. <div
  2212. className="h-full rounded-full transition-all"
  2213. style={{
  2214. width: `${tray.remain}%`,
  2215. backgroundColor: getFillBarColor(tray.remain),
  2216. }}
  2217. />
  2218. ) : tray?.tray_type ? (
  2219. <div className="h-full w-full rounded-full bg-white/50 dark:bg-gray-500/40" />
  2220. ) : null}
  2221. </div>
  2222. </div>
  2223. );
  2224. return (
  2225. <div key={ams.id} className="p-2.5 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary/30">
  2226. {/* Row 1: Label + Nozzle */}
  2227. <div className="flex items-center gap-1 mb-2">
  2228. <span className="text-[10px] text-white font-medium">
  2229. {getAmsLabel(ams.id, ams.tray.length)}
  2230. </span>
  2231. {isDualNozzle && (isLeftNozzle || isRightNozzle) && (
  2232. <NozzleBadge side={isLeftNozzle ? 'L' : 'R'} />
  2233. )}
  2234. </div>
  2235. {/* Row 2: Slot (left) + Stats (right stacked) */}
  2236. <div className="flex gap-1.5">
  2237. {/* Slot wrapper with menu button, dropdown, and loading overlay */}
  2238. <div className="relative group flex-1">
  2239. {/* Loading overlay during RFID re-read */}
  2240. {isHtRefreshing && (
  2241. <div className="absolute inset-0 bg-bambu-dark-tertiary/80 rounded flex items-center justify-center z-20">
  2242. <RefreshCw className="w-4 h-4 text-bambu-green animate-spin" />
  2243. </div>
  2244. )}
  2245. {/* Menu button - appears on hover, hidden when printer busy */}
  2246. {status?.state !== 'RUNNING' && (
  2247. <button
  2248. onClick={(e) => {
  2249. e.stopPropagation();
  2250. setAmsSlotMenu(
  2251. amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === htSlotId
  2252. ? null
  2253. : { amsId: ams.id, slotId: htSlotId }
  2254. );
  2255. }}
  2256. className="absolute -top-1 -right-1 w-4 h-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity z-10 hover:bg-bambu-dark-tertiary"
  2257. title="Slot options"
  2258. >
  2259. <MoreVertical className="w-2.5 h-2.5 text-bambu-gray" />
  2260. </button>
  2261. )}
  2262. {/* Dropdown menu */}
  2263. {status?.state !== 'RUNNING' && amsSlotMenu?.amsId === ams.id && amsSlotMenu?.slotId === htSlotId && (
  2264. <div className="absolute top-full left-0 mt-1 z-50 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg shadow-xl py-1 min-w-[120px]">
  2265. <button
  2266. className="w-full px-3 py-1.5 text-left text-xs text-white hover:bg-bambu-dark-tertiary flex items-center gap-2"
  2267. onClick={(e) => {
  2268. e.stopPropagation();
  2269. refreshAmsSlotMutation.mutate({ amsId: ams.id, slotId: htSlotId });
  2270. setAmsSlotMenu(null);
  2271. }}
  2272. disabled={isHtRefreshing}
  2273. >
  2274. <RefreshCw className={`w-3 h-3 ${isHtRefreshing ? 'animate-spin' : ''}`} />
  2275. Re-read RFID
  2276. </button>
  2277. </div>
  2278. )}
  2279. {/* Hover card wraps only the visual content */}
  2280. {filamentData ? (
  2281. <FilamentHoverCard
  2282. data={filamentData}
  2283. spoolman={{
  2284. enabled: spoolmanEnabled,
  2285. hasUnlinkedSpools,
  2286. onLinkSpool: spoolmanEnabled && filamentData.trayUuid ? (uuid) => {
  2287. setLinkSpoolModal({
  2288. trayUuid: uuid,
  2289. trayInfo: {
  2290. type: filamentData.profile,
  2291. color: filamentData.colorHex || '',
  2292. location: getAmsLabel(ams.id, ams.tray.length),
  2293. },
  2294. });
  2295. } : undefined,
  2296. }}
  2297. configureSlot={{
  2298. enabled: true,
  2299. onConfigure: () => setConfigureSlotModal({
  2300. amsId: ams.id,
  2301. trayId: htSlotId,
  2302. trayCount: ams.tray.length,
  2303. trayType: tray?.tray_type || undefined,
  2304. trayColor: tray?.tray_color || undefined,
  2305. traySubBrands: tray?.tray_sub_brands || undefined,
  2306. trayInfoIdx: tray?.tray_info_idx || undefined,
  2307. }),
  2308. }}
  2309. >
  2310. {slotVisual}
  2311. </FilamentHoverCard>
  2312. ) : (
  2313. <EmptySlotHoverCard
  2314. configureSlot={{
  2315. enabled: true,
  2316. onConfigure: () => setConfigureSlotModal({
  2317. amsId: ams.id,
  2318. trayId: htSlotId,
  2319. trayCount: ams.tray.length,
  2320. }),
  2321. }}
  2322. >
  2323. {slotVisual}
  2324. </EmptySlotHoverCard>
  2325. )}
  2326. </div>
  2327. {/* Stats stacked vertically: Temp on top, Humidity below */}
  2328. {(ams.humidity != null || ams.temp != null) && (
  2329. <div className="flex flex-col justify-center gap-1 shrink-0">
  2330. {ams.temp != null && (
  2331. <TemperatureIndicator
  2332. temp={ams.temp}
  2333. goodThreshold={amsThresholds?.tempGood}
  2334. fairThreshold={amsThresholds?.tempFair}
  2335. onClick={() => setAmsHistoryModal({
  2336. amsId: ams.id,
  2337. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  2338. mode: 'temperature',
  2339. })}
  2340. compact
  2341. />
  2342. )}
  2343. {ams.humidity != null && (
  2344. <HumidityIndicator
  2345. humidity={ams.humidity}
  2346. goodThreshold={amsThresholds?.humidityGood}
  2347. fairThreshold={amsThresholds?.humidityFair}
  2348. onClick={() => setAmsHistoryModal({
  2349. amsId: ams.id,
  2350. amsLabel: getAmsLabel(ams.id, ams.tray.length),
  2351. mode: 'humidity',
  2352. })}
  2353. compact
  2354. />
  2355. )}
  2356. </div>
  2357. )}
  2358. </div>
  2359. </div>
  2360. );
  2361. })}
  2362. {/* External spool - name top, slot below (no stats) */}
  2363. {status.vt_tray && status.vt_tray.tray_type && (() => {
  2364. const extTray = status.vt_tray;
  2365. // Check if external spool is active (tray_now = 254)
  2366. const isExtActive = effectiveTrayNow === 254;
  2367. // Get cloud preset info if available
  2368. const extCloudInfo = extTray.tray_info_idx ? filamentInfo?.[extTray.tray_info_idx] : null;
  2369. // Get saved slot preset mapping (external spool uses amsId=255, trayId=0)
  2370. const extSlotPreset = slotPresets?.[255 * 4 + 0];
  2371. // Build filament data for hover card
  2372. const extFilamentData = {
  2373. vendor: (isBambuLabSpool(extTray) ? 'Bambu Lab' : 'Generic') as 'Bambu Lab' | 'Generic',
  2374. profile: extCloudInfo?.name || extSlotPreset?.preset_name || extTray.tray_sub_brands || extTray.tray_type || 'Unknown',
  2375. colorName: getBambuColorName(extTray.tray_id_name) || hexToBasicColorName(extTray.tray_color),
  2376. colorHex: extTray.tray_color || null,
  2377. kFactor: formatKValue(extTray.k),
  2378. fillLevel: null, // External spool has unknown fill level
  2379. trayUuid: extTray.tray_uuid || null,
  2380. };
  2381. const extSlotContent = (
  2382. <div className={`bg-bambu-dark-tertiary rounded p-1 text-center cursor-default ${isExtActive ? 'ring-2 ring-bambu-green ring-offset-1 ring-offset-bambu-dark' : ''}`}>
  2383. <div
  2384. className="w-3.5 h-3.5 rounded-full mx-auto mb-0.5 border-2"
  2385. style={{
  2386. backgroundColor: extTray.tray_color ? `#${extTray.tray_color}` : '#333',
  2387. borderColor: isExtActive ? 'var(--accent)' : 'rgba(255,255,255,0.1)',
  2388. }}
  2389. />
  2390. <div className="text-[9px] text-white font-bold truncate">
  2391. {extTray.tray_type || 'Spool'}
  2392. </div>
  2393. {/* Unknown fill level - subtle bar */}
  2394. <div className="mt-1 h-1.5 bg-black/30 rounded-full overflow-hidden">
  2395. <div className="h-full w-full rounded-full bg-white/50 dark:bg-gray-500/40" />
  2396. </div>
  2397. </div>
  2398. );
  2399. return (
  2400. <div className="p-2.5 bg-bambu-dark rounded-lg border border-bambu-dark-tertiary/30">
  2401. {/* Row 1: Label */}
  2402. <div className="flex items-center gap-1 mb-2">
  2403. <span className="text-[10px] text-white font-medium">External</span>
  2404. </div>
  2405. {/* Row 2: Slot (full width since no stats) */}
  2406. <FilamentHoverCard
  2407. data={extFilamentData}
  2408. spoolman={{
  2409. enabled: spoolmanEnabled,
  2410. hasUnlinkedSpools,
  2411. onLinkSpool: spoolmanEnabled && extFilamentData.trayUuid ? (uuid) => {
  2412. setLinkSpoolModal({
  2413. trayUuid: uuid,
  2414. trayInfo: {
  2415. type: extFilamentData.profile,
  2416. color: extFilamentData.colorHex || '',
  2417. location: 'External Spool',
  2418. },
  2419. });
  2420. } : undefined,
  2421. }}
  2422. configureSlot={{
  2423. enabled: true,
  2424. onConfigure: () => setConfigureSlotModal({
  2425. amsId: 255, // External spool indicator
  2426. trayId: 0,
  2427. trayCount: 1, // External = single slot
  2428. trayType: extTray.tray_type || undefined,
  2429. trayColor: extTray.tray_color || undefined,
  2430. traySubBrands: extTray.tray_sub_brands || undefined,
  2431. trayInfoIdx: extTray.tray_info_idx || undefined,
  2432. }),
  2433. }}
  2434. >
  2435. {extSlotContent}
  2436. </FilamentHoverCard>
  2437. </div>
  2438. );
  2439. })()}
  2440. </div>
  2441. )}
  2442. </div>
  2443. </div>
  2444. );
  2445. })()}
  2446. </>
  2447. )}
  2448. {/* Smart Plug Controls - hidden in compact mode */}
  2449. {smartPlug && viewMode === 'expanded' && (
  2450. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary">
  2451. <div className="flex items-center gap-3">
  2452. {/* Plug name and status */}
  2453. <div className="flex items-center gap-2 min-w-0">
  2454. <Zap className="w-4 h-4 text-bambu-gray flex-shrink-0" />
  2455. <span className="text-sm text-white truncate">{smartPlug.name}</span>
  2456. {plugStatus && (
  2457. <span
  2458. className={`text-xs px-1.5 py-0.5 rounded flex-shrink-0 ${
  2459. plugStatus.state === 'ON'
  2460. ? 'bg-bambu-green/20 text-bambu-green'
  2461. : plugStatus.state === 'OFF'
  2462. ? 'bg-red-500/20 text-red-400'
  2463. : 'bg-bambu-gray/20 text-bambu-gray'
  2464. }`}
  2465. >
  2466. {plugStatus.state || '?'}
  2467. {plugStatus.state === 'ON' && plugStatus.energy?.power != null && (
  2468. <span className="text-yellow-400 ml-1.5">· {plugStatus.energy.power}W</span>
  2469. )}
  2470. </span>
  2471. )}
  2472. </div>
  2473. {/* Spacer */}
  2474. <div className="flex-1" />
  2475. {/* Power buttons */}
  2476. <div className="flex items-center gap-1">
  2477. <button
  2478. onClick={() => setShowPowerOnConfirm(true)}
  2479. disabled={powerControlMutation.isPending || plugStatus?.state === 'ON'}
  2480. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  2481. plugStatus?.state === 'ON'
  2482. ? 'bg-bambu-green text-white'
  2483. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  2484. }`}
  2485. >
  2486. <Power className="w-3 h-3" />
  2487. On
  2488. </button>
  2489. <button
  2490. onClick={() => setShowPowerOffConfirm(true)}
  2491. disabled={powerControlMutation.isPending || plugStatus?.state === 'OFF'}
  2492. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  2493. plugStatus?.state === 'OFF'
  2494. ? 'bg-red-500/30 text-red-400'
  2495. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  2496. }`}
  2497. >
  2498. <PowerOff className="w-3 h-3" />
  2499. Off
  2500. </button>
  2501. </div>
  2502. {/* Auto-off toggle */}
  2503. <div className="flex items-center gap-2 flex-shrink-0">
  2504. <span className={`text-xs hidden sm:inline ${smartPlug.auto_off_executed ? 'text-bambu-green' : 'text-bambu-gray'}`}>
  2505. {smartPlug.auto_off_executed ? 'Auto-off done' : 'Auto-off'}
  2506. </span>
  2507. <button
  2508. onClick={() => toggleAutoOffMutation.mutate(!smartPlug.auto_off)}
  2509. disabled={toggleAutoOffMutation.isPending || smartPlug.auto_off_executed}
  2510. title={smartPlug.auto_off_executed ? 'Auto-off was executed - turn printer on to reset' : 'Auto power-off after print'}
  2511. className={`relative w-9 h-5 rounded-full transition-colors flex-shrink-0 ${
  2512. smartPlug.auto_off_executed
  2513. ? 'bg-bambu-green/50 cursor-not-allowed'
  2514. : smartPlug.auto_off ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  2515. }`}
  2516. >
  2517. <span
  2518. className={`absolute top-[2px] left-[2px] w-4 h-4 bg-white rounded-full transition-transform ${
  2519. smartPlug.auto_off || smartPlug.auto_off_executed ? 'translate-x-4' : 'translate-x-0'
  2520. }`}
  2521. />
  2522. </button>
  2523. </div>
  2524. </div>
  2525. </div>
  2526. )}
  2527. {/* Connection Info & Actions - hidden in compact mode */}
  2528. {viewMode === 'expanded' && (
  2529. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
  2530. <div className="text-xs text-bambu-gray">
  2531. <p>{printer.ip_address}</p>
  2532. <p className="truncate">{printer.serial_number}</p>
  2533. </div>
  2534. <div className="flex items-center gap-2 flex-wrap">
  2535. {/* Chamber Light Toggle */}
  2536. <Button
  2537. variant="secondary"
  2538. size="sm"
  2539. onClick={() => chamberLightMutation.mutate(!status?.chamber_light)}
  2540. disabled={!status?.connected || chamberLightMutation.isPending}
  2541. title={status?.chamber_light ? 'Turn off chamber light' : 'Turn on chamber light'}
  2542. className={status?.chamber_light ? 'bg-yellow-500/20 hover:bg-yellow-500/30 border-yellow-500/30' : ''}
  2543. >
  2544. <ChamberLight on={status?.chamber_light ?? false} className="w-4 h-4" />
  2545. </Button>
  2546. {/* Camera Button */}
  2547. <Button
  2548. variant="secondary"
  2549. size="sm"
  2550. onClick={() => {
  2551. if (cameraViewMode === 'embedded' && onOpenEmbeddedCamera) {
  2552. onOpenEmbeddedCamera(printer.id, printer.name);
  2553. } else {
  2554. // Use saved window state or defaults
  2555. const saved = localStorage.getItem('cameraWindowState');
  2556. const state = saved ? JSON.parse(saved) : { width: 640, height: 400 };
  2557. const features = [
  2558. `width=${state.width}`,
  2559. `height=${state.height}`,
  2560. state.left !== undefined ? `left=${state.left}` : '',
  2561. state.top !== undefined ? `top=${state.top}` : '',
  2562. 'menubar=no,toolbar=no,location=no,status=no,noopener',
  2563. ].filter(Boolean).join(',');
  2564. window.open(`/camera/${printer.id}`, `camera-${printer.id}`, features);
  2565. }
  2566. }}
  2567. disabled={!status?.connected}
  2568. title={cameraViewMode === 'embedded' ? 'Open camera overlay' : 'Open camera in new window'}
  2569. >
  2570. <Video className="w-4 h-4" />
  2571. </Button>
  2572. {/* Split button: main part toggles detection, chevron opens modal */}
  2573. <div className={`inline-flex rounded-md ${printer.plate_detection_enabled ? 'ring-1 ring-green-500' : ''}`}>
  2574. <Button
  2575. variant="secondary"
  2576. size="sm"
  2577. onClick={handleTogglePlateDetection}
  2578. disabled={!status?.connected || plateDetectionMutation.isPending}
  2579. title={printer.plate_detection_enabled ? "Plate check enabled - Click to disable" : "Plate check disabled - Click to enable"}
  2580. className={`!rounded-r-none !border-r-0 ${printer.plate_detection_enabled ? "!border-green-500 !text-green-400 hover:!bg-green-500/20" : ""}`}
  2581. >
  2582. {plateDetectionMutation.isPending ? (
  2583. <Loader2 className="w-4 h-4 animate-spin" />
  2584. ) : (
  2585. <ScanSearch className="w-4 h-4" />
  2586. )}
  2587. </Button>
  2588. <Button
  2589. variant="secondary"
  2590. size="sm"
  2591. onClick={handleOpenPlateManagement}
  2592. disabled={!status?.connected || isCheckingPlate}
  2593. title="Manage plate detection calibration"
  2594. className={`!rounded-l-none !px-1.5 ${printer.plate_detection_enabled ? "!border-green-500 !text-green-400 hover:!bg-green-500/20" : ""}`}
  2595. >
  2596. {isCheckingPlate ? (
  2597. <Loader2 className="w-3 h-3 animate-spin" />
  2598. ) : (
  2599. <ChevronDown className="w-3 h-3" />
  2600. )}
  2601. </Button>
  2602. </div>
  2603. <Button
  2604. variant="secondary"
  2605. size="sm"
  2606. onClick={() => setShowFileManager(true)}
  2607. title="Browse printer files"
  2608. >
  2609. <HardDrive className="w-4 h-4" />
  2610. Files
  2611. </Button>
  2612. </div>
  2613. </div>
  2614. )}
  2615. </CardContent>
  2616. {/* File Manager Modal */}
  2617. {showFileManager && (
  2618. <FileManagerModal
  2619. printerId={printer.id}
  2620. printerName={printer.name}
  2621. onClose={() => setShowFileManager(false)}
  2622. />
  2623. )}
  2624. {/* MQTT Debug Modal */}
  2625. {showMQTTDebug && (
  2626. <MQTTDebugModal
  2627. printerId={printer.id}
  2628. printerName={printer.name}
  2629. onClose={() => setShowMQTTDebug(false)}
  2630. />
  2631. )}
  2632. {/* Plate Check Result Modal */}
  2633. {plateCheckResult && (
  2634. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4" onClick={() => closePlateCheckModal()}>
  2635. <div className="bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-w-lg w-full" onClick={e => e.stopPropagation()}>
  2636. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary">
  2637. <div className="flex items-center gap-2">
  2638. {plateCheckResult.needs_calibration ? (
  2639. <ScanSearch className="w-5 h-5 text-blue-500" />
  2640. ) : plateCheckResult.is_empty ? (
  2641. <CheckCircle className="w-5 h-5 text-green-500" />
  2642. ) : (
  2643. <XCircle className="w-5 h-5 text-yellow-500" />
  2644. )}
  2645. <h2 className="text-lg font-semibold text-white">
  2646. Build Plate Check
  2647. </h2>
  2648. {plateCheckResult.reference_count !== undefined && plateCheckResult.max_references && (
  2649. <span className="text-xs text-bambu-gray bg-bambu-dark-tertiary px-2 py-1 rounded">
  2650. {plateCheckResult.reference_count}/{plateCheckResult.max_references} refs
  2651. </span>
  2652. )}
  2653. </div>
  2654. <button
  2655. onClick={() => closePlateCheckModal()}
  2656. className="p-1 text-bambu-gray hover:text-white rounded transition-colors"
  2657. >
  2658. <X className="w-5 h-5" />
  2659. </button>
  2660. </div>
  2661. <div className="p-4 space-y-4">
  2662. {plateCheckResult.needs_calibration ? (
  2663. <>
  2664. <div className="p-3 rounded-lg bg-blue-500/20 border border-blue-500/50">
  2665. <p className="font-medium text-blue-400">
  2666. Calibration Required
  2667. </p>
  2668. <p className="text-sm text-bambu-gray mt-1">
  2669. Please ensure the build plate is <strong>completely empty</strong>, then click Calibrate.
  2670. </p>
  2671. </div>
  2672. <div className="text-sm text-bambu-gray space-y-2">
  2673. <p>Calibration captures a reference image of the empty plate. Future checks will compare against this reference to detect objects.</p>
  2674. <p><strong>Tip:</strong> You can store up to 5 calibrations for different plates. The system automatically uses the best match when checking.</p>
  2675. </div>
  2676. </>
  2677. ) : (
  2678. <>
  2679. <div className={`p-3 rounded-lg ${plateCheckResult.is_empty ? 'bg-green-500/20 border border-green-500/50' : 'bg-yellow-500/20 border border-yellow-500/50'}`}>
  2680. <p className={`font-medium ${plateCheckResult.is_empty ? 'text-green-400' : 'text-yellow-400'}`}>
  2681. {plateCheckResult.is_empty ? 'Plate appears empty' : 'Objects detected on plate'}
  2682. </p>
  2683. <p className="text-sm text-bambu-gray mt-1">
  2684. Confidence: {Math.round(plateCheckResult.confidence * 100)}% | Difference: {plateCheckResult.difference_percent.toFixed(1)}%
  2685. </p>
  2686. </div>
  2687. {plateCheckResult.debug_image_url && (
  2688. <div>
  2689. <p className="text-sm text-bambu-gray mb-2">Analysis preview:</p>
  2690. <img
  2691. src={plateCheckResult.debug_image_url}
  2692. alt="Plate detection analysis"
  2693. className="w-full rounded-lg border border-bambu-dark-tertiary"
  2694. />
  2695. <p className="text-xs text-bambu-gray mt-2">
  2696. Green box = detection area, Red overlay = differences from calibration
  2697. </p>
  2698. </div>
  2699. )}
  2700. <p className="text-xs text-bambu-gray">
  2701. {plateCheckResult.message}
  2702. </p>
  2703. </>
  2704. )}
  2705. {/* Saved References Grid */}
  2706. {plateReferences && plateReferences.references.length > 0 && (
  2707. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary">
  2708. <p className="text-sm font-medium text-white mb-2">
  2709. Saved References ({plateReferences.references.length}/{plateReferences.max_references})
  2710. </p>
  2711. <div className="grid grid-cols-5 gap-2">
  2712. {plateReferences.references.map((ref) => (
  2713. <div key={ref.index} className="relative group">
  2714. <img
  2715. src={api.getPlateReferenceThumbnailUrl(printer.id, ref.index)}
  2716. alt={ref.label || `Reference ${ref.index + 1}`}
  2717. className="w-full aspect-video object-cover rounded border border-bambu-dark-tertiary"
  2718. />
  2719. {/* Delete button */}
  2720. <button
  2721. onClick={() => handleDeleteRef(ref.index)}
  2722. className="absolute top-1 right-1 p-0.5 bg-red-500/80 rounded opacity-0 group-hover:opacity-100 transition-opacity"
  2723. title="Delete reference"
  2724. >
  2725. <X className="w-3 h-3 text-white" />
  2726. </button>
  2727. {/* Label */}
  2728. {editingRefLabel?.index === ref.index ? (
  2729. <input
  2730. type="text"
  2731. value={editingRefLabel.label}
  2732. onChange={(e) => setEditingRefLabel({ ...editingRefLabel, label: e.target.value })}
  2733. onBlur={() => handleUpdateRefLabel(ref.index, editingRefLabel.label)}
  2734. onKeyDown={(e) => {
  2735. if (e.key === 'Enter') handleUpdateRefLabel(ref.index, editingRefLabel.label);
  2736. if (e.key === 'Escape') setEditingRefLabel(null);
  2737. }}
  2738. className="w-full mt-1 px-1 py-0.5 text-xs bg-bambu-dark-tertiary border border-bambu-green rounded text-white"
  2739. autoFocus
  2740. placeholder="Label..."
  2741. />
  2742. ) : (
  2743. <p
  2744. className="text-xs text-bambu-gray mt-1 truncate cursor-pointer hover:text-white"
  2745. onClick={() => setEditingRefLabel({ index: ref.index, label: ref.label })}
  2746. title={ref.label ? `${ref.label} - Click to edit` : 'Click to add label'}
  2747. >
  2748. {ref.label || <span className="italic opacity-50">No label</span>}
  2749. </p>
  2750. )}
  2751. {/* Timestamp */}
  2752. <p className="text-[10px] text-bambu-gray/60">
  2753. {ref.timestamp ? new Date(ref.timestamp).toLocaleDateString() : ''}
  2754. </p>
  2755. </div>
  2756. ))}
  2757. </div>
  2758. </div>
  2759. )}
  2760. {/* ROI Editor */}
  2761. {!plateCheckResult.needs_calibration && (
  2762. <div className="mt-4 pt-4 border-t border-bambu-dark-tertiary">
  2763. <div className="flex items-center justify-between mb-2">
  2764. <p className="text-sm font-medium text-white">Detection Area (ROI)</p>
  2765. {!editingRoi ? (
  2766. <Button
  2767. variant="ghost"
  2768. size="sm"
  2769. onClick={() => setEditingRoi(plateCheckResult.roi || { x: 0.15, y: 0.35, w: 0.70, h: 0.55 })}
  2770. >
  2771. <Pencil className="w-3 h-3 mr-1" />
  2772. Edit
  2773. </Button>
  2774. ) : (
  2775. <div className="flex gap-1">
  2776. <Button
  2777. variant="ghost"
  2778. size="sm"
  2779. onClick={() => setEditingRoi(null)}
  2780. disabled={isSavingRoi}
  2781. >
  2782. Cancel
  2783. </Button>
  2784. <Button
  2785. size="sm"
  2786. onClick={handleSaveRoi}
  2787. disabled={isSavingRoi}
  2788. >
  2789. {isSavingRoi ? <Loader2 className="w-3 h-3 animate-spin" /> : 'Save'}
  2790. </Button>
  2791. </div>
  2792. )}
  2793. </div>
  2794. {editingRoi ? (
  2795. <div className="space-y-3 bg-bambu-dark-tertiary/50 p-3 rounded-lg">
  2796. <div className="grid grid-cols-2 gap-3">
  2797. <div>
  2798. <label className="text-xs text-bambu-gray">X Start</label>
  2799. <input
  2800. type="range"
  2801. min="0"
  2802. max="0.9"
  2803. step="0.01"
  2804. value={editingRoi.x}
  2805. onChange={(e) => setEditingRoi({ ...editingRoi, x: parseFloat(e.target.value) })}
  2806. className="w-full h-1.5 bg-bambu-dark-tertiary rounded-lg cursor-pointer accent-green-500"
  2807. />
  2808. <span className="text-xs text-bambu-gray">{Math.round(editingRoi.x * 100)}%</span>
  2809. </div>
  2810. <div>
  2811. <label className="text-xs text-bambu-gray">Y Start</label>
  2812. <input
  2813. type="range"
  2814. min="0"
  2815. max="0.9"
  2816. step="0.01"
  2817. value={editingRoi.y}
  2818. onChange={(e) => setEditingRoi({ ...editingRoi, y: parseFloat(e.target.value) })}
  2819. className="w-full h-1.5 bg-bambu-dark-tertiary rounded-lg cursor-pointer accent-green-500"
  2820. />
  2821. <span className="text-xs text-bambu-gray">{Math.round(editingRoi.y * 100)}%</span>
  2822. </div>
  2823. <div>
  2824. <label className="text-xs text-bambu-gray">Width</label>
  2825. <input
  2826. type="range"
  2827. min="0.1"
  2828. max="1"
  2829. step="0.01"
  2830. value={editingRoi.w}
  2831. onChange={(e) => setEditingRoi({ ...editingRoi, w: parseFloat(e.target.value) })}
  2832. className="w-full h-1.5 bg-bambu-dark-tertiary rounded-lg cursor-pointer accent-green-500"
  2833. />
  2834. <span className="text-xs text-bambu-gray">{Math.round(editingRoi.w * 100)}%</span>
  2835. </div>
  2836. <div>
  2837. <label className="text-xs text-bambu-gray">Height</label>
  2838. <input
  2839. type="range"
  2840. min="0.1"
  2841. max="1"
  2842. step="0.01"
  2843. value={editingRoi.h}
  2844. onChange={(e) => setEditingRoi({ ...editingRoi, h: parseFloat(e.target.value) })}
  2845. className="w-full h-1.5 bg-bambu-dark-tertiary rounded-lg cursor-pointer accent-green-500"
  2846. />
  2847. <span className="text-xs text-bambu-gray">{Math.round(editingRoi.h * 100)}%</span>
  2848. </div>
  2849. </div>
  2850. <p className="text-xs text-bambu-gray">
  2851. Adjust the detection area to focus on the build plate. The green box in the preview shows the current area.
  2852. </p>
  2853. </div>
  2854. ) : (
  2855. <p className="text-xs text-bambu-gray">
  2856. Current: X={Math.round((plateCheckResult.roi?.x || 0.15) * 100)}%, Y={Math.round((plateCheckResult.roi?.y || 0.35) * 100)}%,
  2857. W={Math.round((plateCheckResult.roi?.w || 0.70) * 100)}%, H={Math.round((plateCheckResult.roi?.h || 0.55) * 100)}%
  2858. </p>
  2859. )}
  2860. </div>
  2861. )}
  2862. </div>
  2863. <div className="flex justify-end gap-2 p-4 border-t border-bambu-dark-tertiary">
  2864. {plateCheckResult.needs_calibration ? (
  2865. <>
  2866. <Button variant="ghost" onClick={() => closePlateCheckModal()}>
  2867. Cancel
  2868. </Button>
  2869. <Button
  2870. onClick={() => handleCalibratePlate()}
  2871. disabled={isCalibrating}
  2872. >
  2873. {isCalibrating ? (
  2874. <>
  2875. <Loader2 className="w-4 h-4 mr-2 animate-spin" />
  2876. Calibrating...
  2877. </>
  2878. ) : (
  2879. 'Calibrate Empty Plate'
  2880. )}
  2881. </Button>
  2882. </>
  2883. ) : (
  2884. <>
  2885. <Button variant="ghost" onClick={() => handleCalibratePlate()} disabled={isCalibrating}>
  2886. {isCalibrating ? 'Adding...' : `Add Reference (${plateReferences?.references.length || 0}/${plateReferences?.max_references || 5})`}
  2887. </Button>
  2888. <Button onClick={() => closePlateCheckModal()}>
  2889. Close
  2890. </Button>
  2891. </>
  2892. )}
  2893. </div>
  2894. </div>
  2895. </div>
  2896. )}
  2897. {/* Power On Confirmation */}
  2898. {showPowerOnConfirm && smartPlug && (
  2899. <ConfirmModal
  2900. title="Power On Printer"
  2901. message={`Are you sure you want to turn ON the power for "${printer.name}"?`}
  2902. confirmText="Power On"
  2903. variant="default"
  2904. onConfirm={() => {
  2905. powerControlMutation.mutate('on');
  2906. setShowPowerOnConfirm(false);
  2907. }}
  2908. onCancel={() => setShowPowerOnConfirm(false)}
  2909. />
  2910. )}
  2911. {/* Power Off Confirmation */}
  2912. {showPowerOffConfirm && smartPlug && (
  2913. <ConfirmModal
  2914. title="Power Off Printer"
  2915. message={
  2916. status?.state === 'RUNNING'
  2917. ? `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.`
  2918. : `Are you sure you want to turn OFF the power for "${printer.name}"?`
  2919. }
  2920. confirmText="Power Off"
  2921. variant="danger"
  2922. onConfirm={() => {
  2923. powerControlMutation.mutate('off');
  2924. setShowPowerOffConfirm(false);
  2925. }}
  2926. onCancel={() => setShowPowerOffConfirm(false)}
  2927. />
  2928. )}
  2929. {/* Stop Print Confirmation */}
  2930. {showStopConfirm && (
  2931. <ConfirmModal
  2932. title="Stop Print"
  2933. message={`Are you sure you want to stop the current print on "${printer.name}"? This will cancel the print job.`}
  2934. confirmText="Stop Print"
  2935. variant="danger"
  2936. onConfirm={() => {
  2937. stopPrintMutation.mutate();
  2938. setShowStopConfirm(false);
  2939. }}
  2940. onCancel={() => setShowStopConfirm(false)}
  2941. />
  2942. )}
  2943. {/* Pause Print Confirmation */}
  2944. {showPauseConfirm && (
  2945. <ConfirmModal
  2946. title="Pause Print"
  2947. message={`Are you sure you want to pause the current print on "${printer.name}"?`}
  2948. confirmText="Pause Print"
  2949. variant="default"
  2950. onConfirm={() => {
  2951. pausePrintMutation.mutate();
  2952. setShowPauseConfirm(false);
  2953. }}
  2954. onCancel={() => setShowPauseConfirm(false)}
  2955. />
  2956. )}
  2957. {/* Resume Print Confirmation */}
  2958. {showResumeConfirm && (
  2959. <ConfirmModal
  2960. title="Resume Print"
  2961. message={`Are you sure you want to resume the print on "${printer.name}"?`}
  2962. confirmText="Resume Print"
  2963. variant="default"
  2964. onConfirm={() => {
  2965. resumePrintMutation.mutate();
  2966. setShowResumeConfirm(false);
  2967. }}
  2968. onCancel={() => setShowResumeConfirm(false)}
  2969. />
  2970. )}
  2971. {/* Skip Objects Popup */}
  2972. {showSkipObjectsModal && (
  2973. <div
  2974. className="fixed inset-0 z-50 flex items-center justify-center"
  2975. onClick={() => setShowSkipObjectsModal(false)}
  2976. onKeyDown={(e) => e.key === 'Escape' && setShowSkipObjectsModal(false)}
  2977. tabIndex={-1}
  2978. ref={(el) => el?.focus()}
  2979. >
  2980. {/* Backdrop */}
  2981. <div className="absolute inset-0 bg-black/50 z-0" />
  2982. {/* Modal */}
  2983. <div
  2984. className="relative z-10 bg-white dark:bg-bambu-dark border border-gray-200 dark:border-bambu-dark-tertiary rounded-xl shadow-2xl w-[560px] max-h-[85vh] flex flex-col overflow-hidden"
  2985. onClick={(e) => e.stopPropagation()}
  2986. >
  2987. {/* Header */}
  2988. <div className="flex items-center justify-between px-4 py-3 border-b border-gray-200 dark:border-bambu-dark-tertiary bg-gray-50 dark:bg-bambu-dark">
  2989. <div className="flex items-center gap-2">
  2990. <SkipObjectsIcon className="w-4 h-4 text-bambu-green" />
  2991. <span className="text-sm font-medium text-gray-900 dark:text-white">Skip Objects</span>
  2992. </div>
  2993. <button
  2994. onClick={() => setShowSkipObjectsModal(false)}
  2995. className="p-1 text-gray-500 dark:text-bambu-gray hover:text-gray-900 dark:hover:text-white rounded transition-colors"
  2996. >
  2997. <X className="w-4 h-4" />
  2998. </button>
  2999. </div>
  3000. {!objectsData ? (
  3001. <div className="flex items-center justify-center py-12">
  3002. <Loader2 className="w-5 h-5 animate-spin text-bambu-gray" />
  3003. </div>
  3004. ) : objectsData.objects.length === 0 ? (
  3005. <div className="text-center py-8 px-4 text-bambu-gray">
  3006. <p className="text-sm">No objects found</p>
  3007. <p className="text-xs mt-1 opacity-70">Objects are loaded when a print starts</p>
  3008. </div>
  3009. ) : (
  3010. <div className="flex flex-col overflow-hidden">
  3011. {/* Info Banner */}
  3012. <div className="flex items-center gap-3 px-4 py-2.5 bg-blue-50 dark:bg-blue-500/10 border-b border-gray-200 dark:border-bambu-dark-tertiary">
  3013. <div className="flex-shrink-0 w-8 h-8 rounded-lg bg-blue-100 dark:bg-blue-500/20 flex items-center justify-center">
  3014. <Monitor className="w-4 h-4 text-blue-500 dark:text-blue-400" />
  3015. </div>
  3016. <div className="flex-1 min-w-0">
  3017. <p className="text-xs text-blue-600 dark:text-blue-300">Match IDs with your printer display</p>
  3018. <p className="text-[10px] text-blue-500/70 dark:text-blue-300/60">The printer screen shows object IDs on the build plate</p>
  3019. </div>
  3020. <div className="flex-shrink-0 text-xs text-gray-500 dark:text-bambu-gray">
  3021. {objectsData.skipped_count}/{objectsData.total} skipped
  3022. </div>
  3023. </div>
  3024. {/* Layer Warning */}
  3025. {(status?.layer_num ?? 0) <= 1 && (
  3026. <div className="flex items-center gap-2 px-4 py-2 bg-amber-50 dark:bg-amber-500/10 border-b border-gray-200 dark:border-bambu-dark-tertiary">
  3027. <AlertCircle className="w-4 h-4 text-amber-500 dark:text-amber-400 flex-shrink-0" />
  3028. <p className="text-xs text-amber-600 dark:text-amber-400">
  3029. Wait for layer 2+ to skip objects (currently layer {status?.layer_num ?? 0})
  3030. </p>
  3031. </div>
  3032. )}
  3033. {/* Content: Image + List side by side */}
  3034. <div className="flex flex-1 overflow-hidden">
  3035. {/* Left: Preview Image with object markers */}
  3036. <div className="w-52 flex-shrink-0 p-4 border-r border-gray-200 dark:border-bambu-dark-tertiary bg-gray-50 dark:bg-bambu-dark-secondary overflow-y-auto">
  3037. <div className="relative">
  3038. {status?.cover_url ? (
  3039. <img
  3040. src={`${status.cover_url}?view=top`}
  3041. alt="Print preview"
  3042. className="w-full aspect-square object-contain rounded-lg bg-gray-900 dark:bg-gray-900 border border-gray-300 dark:border-gray-600"
  3043. />
  3044. ) : (
  3045. <div className="w-full aspect-square rounded-lg bg-gray-100 dark:bg-bambu-dark flex items-center justify-center">
  3046. <Box className="w-8 h-8 text-gray-300 dark:text-bambu-gray/30" />
  3047. </div>
  3048. )}
  3049. {/* Object ID markers overlay - positioned based on object data */}
  3050. {objectsData.objects.length > 0 && (
  3051. <div className="absolute inset-0 pointer-events-none">
  3052. {objectsData.objects.map((obj, idx) => {
  3053. let x: number, y: number;
  3054. // Use position data if available, otherwise fall back to grid
  3055. if (obj.x != null && obj.y != null && objectsData.bbox_all) {
  3056. // bbox_all defines the visible area in the top_N.png image
  3057. // Format: [x_min, y_min, x_max, y_max] in mm
  3058. const [xMin, yMin, xMax, yMax] = objectsData.bbox_all;
  3059. const bboxWidth = xMax - xMin;
  3060. const bboxHeight = yMax - yMin;
  3061. // The image shows bbox_all area with some padding (~5-10%)
  3062. const padding = 8;
  3063. const contentArea = 100 - (padding * 2);
  3064. // Map object position to image percentage
  3065. x = padding + ((obj.x - xMin) / bboxWidth) * contentArea;
  3066. // Y axis: image Y increases downward, but 3D Y increases toward back
  3067. y = padding + ((yMax - obj.y) / bboxHeight) * contentArea;
  3068. // Clamp to valid range
  3069. x = Math.max(5, Math.min(95, x));
  3070. y = Math.max(5, Math.min(95, y));
  3071. } else if (obj.x != null && obj.y != null) {
  3072. // Fallback: use full build plate (256mm)
  3073. const buildPlate = 256;
  3074. x = (obj.x / buildPlate) * 100;
  3075. y = 100 - (obj.y / buildPlate) * 100;
  3076. x = Math.max(5, Math.min(95, x));
  3077. y = Math.max(5, Math.min(95, y));
  3078. } else {
  3079. // Fallback: arrange in a grid pattern over the build plate area
  3080. const cols = Math.ceil(Math.sqrt(objectsData.objects.length));
  3081. const row = Math.floor(idx / cols);
  3082. const col = idx % cols;
  3083. const rows = Math.ceil(objectsData.objects.length / cols);
  3084. x = 15 + (col * (70 / cols)) + (35 / cols);
  3085. y = 15 + (row * (70 / rows)) + (35 / rows);
  3086. }
  3087. return (
  3088. <div
  3089. key={obj.id}
  3090. className={`absolute flex items-center justify-center w-6 h-6 rounded-full text-[10px] font-bold shadow-lg ${
  3091. obj.skipped
  3092. ? 'bg-red-500 text-white line-through'
  3093. : 'bg-bambu-green text-black'
  3094. }`}
  3095. style={{
  3096. left: `${x}%`,
  3097. top: `${y}%`,
  3098. transform: 'translate(-50%, -50%)'
  3099. }}
  3100. title={obj.name}
  3101. >
  3102. {obj.id}
  3103. </div>
  3104. );
  3105. })}
  3106. </div>
  3107. )}
  3108. {/* Object count overlay */}
  3109. <div className="absolute bottom-2 right-2 px-2 py-1 bg-white/90 dark:bg-black/80 rounded text-[10px] text-gray-700 dark:text-white shadow-sm">
  3110. {objectsData.objects.filter(o => !o.skipped).length} active
  3111. </div>
  3112. </div>
  3113. </div>
  3114. {/* Right: Object List with prominent IDs */}
  3115. <div className="flex-1 min-w-0 overflow-y-auto">
  3116. {objectsData.objects.map((obj) => (
  3117. <div
  3118. key={obj.id}
  3119. className={`
  3120. flex items-center gap-3 px-4 py-3 border-b border-gray-200 dark:border-bambu-dark-tertiary/50 last:border-0
  3121. ${obj.skipped ? 'bg-red-50 dark:bg-red-500/10' : 'hover:bg-gray-50 dark:hover:bg-bambu-dark/50'}
  3122. `}
  3123. >
  3124. {/* Large prominent ID badge */}
  3125. <div className={`
  3126. w-12 h-12 flex-shrink-0 rounded-lg flex flex-col items-center justify-center
  3127. ${obj.skipped
  3128. ? 'bg-red-100 dark:bg-red-500/20 border border-red-300 dark:border-red-500/40'
  3129. : 'bg-green-100 dark:bg-bambu-green/20 border border-green-300 dark:border-bambu-green/40'}
  3130. `}>
  3131. <span className={`text-lg font-mono font-bold ${obj.skipped ? 'text-red-500 dark:text-red-400' : 'text-green-600 dark:text-bambu-green'}`}>
  3132. {obj.id}
  3133. </span>
  3134. <span className={`text-[8px] uppercase tracking-wider ${obj.skipped ? 'text-red-400/60' : 'text-green-500/60 dark:text-bambu-green/60'}`}>
  3135. ID
  3136. </span>
  3137. </div>
  3138. {/* Object name and status */}
  3139. <div className="flex-1 min-w-0">
  3140. <span className={`block text-sm truncate ${obj.skipped ? 'text-red-500 dark:text-red-400 line-through' : 'text-gray-900 dark:text-white'}`}>
  3141. {obj.name}
  3142. </span>
  3143. {obj.skipped && (
  3144. <span className="text-[10px] text-red-400/60">Will be skipped</span>
  3145. )}
  3146. </div>
  3147. {/* Skip button */}
  3148. {!obj.skipped ? (
  3149. <button
  3150. onClick={() => skipObjectsMutation.mutate([obj.id])}
  3151. disabled={skipObjectsMutation.isPending || (status?.layer_num ?? 0) <= 1}
  3152. className={`px-4 py-2 text-xs font-medium rounded-lg transition-colors ${
  3153. (status?.layer_num ?? 0) <= 1
  3154. ? 'bg-gray-100 dark:bg-bambu-dark text-gray-400 dark:text-bambu-gray/50 cursor-not-allowed'
  3155. : 'bg-red-100 dark:bg-red-500/20 text-red-600 dark:text-red-400 hover:bg-red-200 dark:hover:bg-red-500/30 border border-red-300 dark:border-red-500/30'
  3156. }`}
  3157. title={(status?.layer_num ?? 0) <= 1 ? 'Wait for layer 2+' : 'Skip this object'}
  3158. >
  3159. Skip
  3160. </button>
  3161. ) : (
  3162. <span className="px-4 py-2 text-xs text-red-500 dark:text-red-400/70 bg-red-100 dark:bg-red-500/10 rounded-lg">
  3163. Skipped
  3164. </span>
  3165. )}
  3166. </div>
  3167. ))}
  3168. </div>
  3169. </div>
  3170. </div>
  3171. )}
  3172. </div>
  3173. </div>
  3174. )}
  3175. {/* HMS Error Modal */}
  3176. {showHMSModal && (
  3177. <HMSErrorModal
  3178. printerName={printer.name}
  3179. errors={status?.hms_errors || []}
  3180. onClose={() => setShowHMSModal(false)}
  3181. />
  3182. )}
  3183. {/* AMS History Modal */}
  3184. {amsHistoryModal && (
  3185. <AMSHistoryModal
  3186. isOpen={!!amsHistoryModal}
  3187. onClose={() => setAmsHistoryModal(null)}
  3188. printerId={printer.id}
  3189. printerName={printer.name}
  3190. amsId={amsHistoryModal.amsId}
  3191. amsLabel={amsHistoryModal.amsLabel}
  3192. initialMode={amsHistoryModal.mode}
  3193. thresholds={amsThresholds}
  3194. />
  3195. )}
  3196. {/* Link Spool Modal */}
  3197. {linkSpoolModal && (
  3198. <LinkSpoolModal
  3199. isOpen={!!linkSpoolModal}
  3200. onClose={() => setLinkSpoolModal(null)}
  3201. trayUuid={linkSpoolModal.trayUuid}
  3202. trayInfo={linkSpoolModal.trayInfo}
  3203. />
  3204. )}
  3205. {/* Configure AMS Slot Modal */}
  3206. {configureSlotModal && (
  3207. <ConfigureAmsSlotModal
  3208. isOpen={!!configureSlotModal}
  3209. onClose={() => setConfigureSlotModal(null)}
  3210. printerId={printer.id}
  3211. slotInfo={configureSlotModal}
  3212. onSuccess={() => {
  3213. // Refresh slot presets to show updated profile name
  3214. queryClient.invalidateQueries({ queryKey: ['slotPresets', printer.id] });
  3215. // Printer status will update automatically via WebSocket when AMS data changes
  3216. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  3217. }}
  3218. />
  3219. )}
  3220. {/* Edit Printer Modal */}
  3221. {showEditModal && (
  3222. <EditPrinterModal
  3223. printer={printer}
  3224. onClose={() => setShowEditModal(false)}
  3225. />
  3226. )}
  3227. {/* Firmware Update Modal */}
  3228. {showFirmwareModal && firmwareInfo && (
  3229. <FirmwareUpdateModal
  3230. printer={printer}
  3231. firmwareInfo={firmwareInfo}
  3232. onClose={() => setShowFirmwareModal(false)}
  3233. />
  3234. )}
  3235. {/* AMS Slot Menu Backdrop - closes menu when clicking outside */}
  3236. {amsSlotMenu && (
  3237. <div
  3238. className="fixed inset-0 z-40"
  3239. onClick={() => setAmsSlotMenu(null)}
  3240. />
  3241. )}
  3242. </Card>
  3243. );
  3244. }
  3245. function AddPrinterModal({
  3246. onClose,
  3247. onAdd,
  3248. existingSerials,
  3249. }: {
  3250. onClose: () => void;
  3251. onAdd: (data: PrinterCreate) => void;
  3252. existingSerials: string[];
  3253. }) {
  3254. const [form, setForm] = useState<PrinterCreate>({
  3255. name: '',
  3256. serial_number: '',
  3257. ip_address: '',
  3258. access_code: '',
  3259. model: '',
  3260. auto_archive: true,
  3261. });
  3262. // Discovery state
  3263. const [discovering, setDiscovering] = useState(false);
  3264. const [discovered, setDiscovered] = useState<DiscoveredPrinter[]>([]);
  3265. const [discoveryError, setDiscoveryError] = useState('');
  3266. const [hasScanned, setHasScanned] = useState(false);
  3267. const [isDocker, setIsDocker] = useState(false);
  3268. const [subnet, setSubnet] = useState('192.168.1.0/24');
  3269. const [scanProgress, setScanProgress] = useState({ scanned: 0, total: 0 });
  3270. // Fetch discovery info on mount
  3271. useEffect(() => {
  3272. discoveryApi.getInfo().then(info => {
  3273. setIsDocker(info.is_docker);
  3274. }).catch(() => {
  3275. // Ignore errors, assume not Docker
  3276. });
  3277. }, []);
  3278. // Filter out already-added printers
  3279. const newPrinters = discovered.filter(p => !existingSerials.includes(p.serial));
  3280. const startDiscovery = async () => {
  3281. setDiscoveryError('');
  3282. setDiscovered([]);
  3283. setDiscovering(true);
  3284. setHasScanned(false);
  3285. setScanProgress({ scanned: 0, total: 0 });
  3286. try {
  3287. if (isDocker) {
  3288. // Use subnet scanning for Docker
  3289. await discoveryApi.startSubnetScan(subnet);
  3290. // Poll for scan status and results
  3291. const pollInterval = setInterval(async () => {
  3292. try {
  3293. const status = await discoveryApi.getScanStatus();
  3294. setScanProgress({ scanned: status.scanned, total: status.total });
  3295. const printers = await discoveryApi.getDiscoveredPrinters();
  3296. setDiscovered(printers);
  3297. if (!status.running) {
  3298. clearInterval(pollInterval);
  3299. setDiscovering(false);
  3300. setHasScanned(true);
  3301. }
  3302. } catch (e) {
  3303. console.error('Failed to get scan status:', e);
  3304. }
  3305. }, 500);
  3306. } else {
  3307. // Use SSDP discovery for native installs
  3308. await discoveryApi.startDiscovery(10);
  3309. // Poll for discovered printers every second
  3310. const pollInterval = setInterval(async () => {
  3311. try {
  3312. const printers = await discoveryApi.getDiscoveredPrinters();
  3313. setDiscovered(printers);
  3314. } catch (e) {
  3315. console.error('Failed to get discovered printers:', e);
  3316. }
  3317. }, 1000);
  3318. // Stop after 10 seconds
  3319. setTimeout(async () => {
  3320. clearInterval(pollInterval);
  3321. try {
  3322. await discoveryApi.stopDiscovery();
  3323. } catch {
  3324. // Ignore stop errors
  3325. }
  3326. setDiscovering(false);
  3327. setHasScanned(true);
  3328. // Final fetch
  3329. try {
  3330. const printers = await discoveryApi.getDiscoveredPrinters();
  3331. setDiscovered(printers);
  3332. } catch (e) {
  3333. console.error('Failed to get final discovered printers:', e);
  3334. }
  3335. }, 10000);
  3336. }
  3337. } catch (e) {
  3338. console.error('Failed to start discovery:', e);
  3339. setDiscoveryError(e instanceof Error ? e.message : 'Failed to start discovery');
  3340. setDiscovering(false);
  3341. setHasScanned(true);
  3342. }
  3343. };
  3344. // Map SSDP model codes to dropdown values
  3345. const mapModelCode = (ssdpModel: string | null): string => {
  3346. if (!ssdpModel) return '';
  3347. const modelMap: Record<string, string> = {
  3348. // H2 Series
  3349. 'O1D': 'H2D',
  3350. 'O1C': 'H2C',
  3351. 'O1S': 'H2S',
  3352. // X1 Series
  3353. 'BL-P001': 'X1C',
  3354. 'BL-P002': 'X1',
  3355. 'BL-P003': 'X1E',
  3356. // P Series
  3357. 'C11': 'P1S',
  3358. 'C12': 'P1P',
  3359. 'C13': 'P2S',
  3360. // A1 Series
  3361. 'N2S': 'A1',
  3362. 'N1': 'A1 Mini',
  3363. // Direct matches
  3364. 'X1C': 'X1C',
  3365. 'X1': 'X1',
  3366. 'X1E': 'X1E',
  3367. 'P1S': 'P1S',
  3368. 'P1P': 'P1P',
  3369. 'P2S': 'P2S',
  3370. 'A1': 'A1',
  3371. 'A1 Mini': 'A1 Mini',
  3372. 'H2D': 'H2D',
  3373. 'H2C': 'H2C',
  3374. 'H2S': 'H2S',
  3375. };
  3376. return modelMap[ssdpModel] || ssdpModel;
  3377. };
  3378. const selectPrinter = (printer: DiscoveredPrinter) => {
  3379. // Don't pre-fill serial if it's a placeholder (unknown-*) - user needs to enter actual serial
  3380. const serialNumber = printer.serial.startsWith('unknown-') ? '' : printer.serial;
  3381. setForm({
  3382. ...form,
  3383. name: printer.name || '',
  3384. serial_number: serialNumber,
  3385. ip_address: printer.ip_address,
  3386. model: mapModelCode(printer.model),
  3387. });
  3388. // Clear discovery results after selection
  3389. setDiscovered([]);
  3390. };
  3391. // Cleanup discovery on unmount
  3392. useEffect(() => {
  3393. return () => {
  3394. discoveryApi.stopDiscovery().catch(() => {});
  3395. discoveryApi.stopSubnetScan().catch(() => {});
  3396. };
  3397. }, []);
  3398. // Close on Escape key
  3399. useEffect(() => {
  3400. const handleKeyDown = (e: KeyboardEvent) => {
  3401. if (e.key === 'Escape') onClose();
  3402. };
  3403. window.addEventListener('keydown', handleKeyDown);
  3404. return () => window.removeEventListener('keydown', handleKeyDown);
  3405. }, [onClose]);
  3406. return (
  3407. <div
  3408. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"
  3409. onClick={onClose}
  3410. >
  3411. <Card className="w-full max-w-md" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
  3412. <CardContent>
  3413. <h2 className="text-xl font-semibold mb-4">Add Printer</h2>
  3414. {/* Discovery Section */}
  3415. <div className="mb-4 pb-4 border-b border-bambu-dark-tertiary">
  3416. {isDocker && (
  3417. <div className="mb-3">
  3418. <label className="block text-sm text-bambu-gray mb-1">
  3419. Subnet to scan
  3420. </label>
  3421. <input
  3422. type="text"
  3423. 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"
  3424. value={subnet}
  3425. onChange={(e) => setSubnet(e.target.value)}
  3426. placeholder="192.168.1.0/24"
  3427. disabled={discovering}
  3428. />
  3429. <p className="mt-1 text-xs text-bambu-gray">
  3430. Docker detected. Enter your printer's subnet in CIDR notation.
  3431. Requires <code className="text-bambu-green">network_mode: host</code> in docker-compose.yml.
  3432. </p>
  3433. </div>
  3434. )}
  3435. <Button
  3436. type="button"
  3437. variant="secondary"
  3438. onClick={startDiscovery}
  3439. disabled={discovering}
  3440. className="w-full"
  3441. >
  3442. {discovering ? (
  3443. <>
  3444. <Loader2 className="w-4 h-4 animate-spin" />
  3445. {isDocker && scanProgress.total > 0
  3446. ? `Scanning... ${scanProgress.scanned}/${scanProgress.total}`
  3447. : 'Scanning...'}
  3448. </>
  3449. ) : (
  3450. <>
  3451. <Search className="w-4 h-4" />
  3452. {isDocker ? 'Scan Subnet for Printers' : 'Discover Printers on Network'}
  3453. </>
  3454. )}
  3455. </Button>
  3456. {discoveryError && (
  3457. <div className="mt-2 text-sm text-red-400">{discoveryError}</div>
  3458. )}
  3459. {newPrinters.length > 0 && (
  3460. <div className="mt-3 space-y-2 max-h-40 overflow-y-auto">
  3461. {newPrinters.map((printer) => (
  3462. <div
  3463. key={printer.serial}
  3464. className="flex items-center justify-between p-2 bg-bambu-dark rounded-lg hover:bg-bambu-dark-secondary cursor-pointer transition-colors"
  3465. onClick={() => selectPrinter(printer)}
  3466. >
  3467. <div className="min-w-0 flex-1">
  3468. <p className="font-medium text-white text-sm truncate">
  3469. {printer.name || printer.serial}
  3470. </p>
  3471. <p className="text-xs text-bambu-gray truncate">
  3472. {mapModelCode(printer.model) || 'Unknown'} • {printer.ip_address}
  3473. {printer.serial.startsWith('unknown-') && (
  3474. <span className="text-yellow-500"> • Serial required</span>
  3475. )}
  3476. </p>
  3477. </div>
  3478. <ChevronDown className="w-4 h-4 text-bambu-gray -rotate-90 flex-shrink-0 ml-2" />
  3479. </div>
  3480. ))}
  3481. </div>
  3482. )}
  3483. {discovering && (
  3484. <p className="mt-2 text-sm text-bambu-gray text-center">
  3485. {isDocker ? 'Scanning subnet for Bambu printers...' : 'Scanning network...'}
  3486. </p>
  3487. )}
  3488. {hasScanned && !discovering && discovered.length === 0 && (
  3489. <p className="mt-2 text-sm text-bambu-gray text-center">
  3490. No printers found{isDocker ? ' in the specified subnet' : ' on the network'}.
  3491. </p>
  3492. )}
  3493. {hasScanned && !discovering && discovered.length > 0 && newPrinters.length === 0 && (
  3494. <p className="mt-2 text-sm text-bambu-gray text-center">
  3495. All discovered printers are already configured.
  3496. </p>
  3497. )}
  3498. </div>
  3499. <form
  3500. onSubmit={(e) => {
  3501. e.preventDefault();
  3502. onAdd(form);
  3503. }}
  3504. className="space-y-4"
  3505. >
  3506. <div>
  3507. <label className="block text-sm text-bambu-gray mb-1">Name</label>
  3508. <input
  3509. type="text"
  3510. required
  3511. 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"
  3512. value={form.name}
  3513. onChange={(e) => setForm({ ...form, name: e.target.value })}
  3514. placeholder="My Printer"
  3515. />
  3516. </div>
  3517. <div>
  3518. <label className="block text-sm text-bambu-gray mb-1">IP Address</label>
  3519. <input
  3520. type="text"
  3521. required
  3522. pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
  3523. 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"
  3524. value={form.ip_address}
  3525. onChange={(e) => setForm({ ...form, ip_address: e.target.value })}
  3526. placeholder="192.168.1.100"
  3527. />
  3528. </div>
  3529. <div>
  3530. <label className="block text-sm text-bambu-gray mb-1">Serial Number</label>
  3531. <input
  3532. type="text"
  3533. required
  3534. 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"
  3535. value={form.serial_number}
  3536. onChange={(e) => setForm({ ...form, serial_number: e.target.value })}
  3537. placeholder="01P00A000000000"
  3538. />
  3539. </div>
  3540. <div>
  3541. <label className="block text-sm text-bambu-gray mb-1">Access Code</label>
  3542. <input
  3543. type="password"
  3544. required
  3545. 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"
  3546. value={form.access_code}
  3547. onChange={(e) => setForm({ ...form, access_code: e.target.value })}
  3548. placeholder="From printer settings"
  3549. />
  3550. </div>
  3551. <div>
  3552. <label className="block text-sm text-bambu-gray mb-1">Model (optional)</label>
  3553. <select
  3554. 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"
  3555. value={form.model || ''}
  3556. onChange={(e) => setForm({ ...form, model: e.target.value })}
  3557. >
  3558. <option value="">Select model...</option>
  3559. <optgroup label="H2 Series">
  3560. <option value="H2C">H2C</option>
  3561. <option value="H2D">H2D</option>
  3562. <option value="H2S">H2S</option>
  3563. </optgroup>
  3564. <optgroup label="X1 Series">
  3565. <option value="X1E">X1E</option>
  3566. <option value="X1C">X1 Carbon</option>
  3567. <option value="X1">X1</option>
  3568. </optgroup>
  3569. <optgroup label="P Series">
  3570. <option value="P2S">P2S</option>
  3571. <option value="P1S">P1S</option>
  3572. <option value="P1P">P1P</option>
  3573. </optgroup>
  3574. <optgroup label="A1 Series">
  3575. <option value="A1">A1</option>
  3576. <option value="A1 Mini">A1 Mini</option>
  3577. </optgroup>
  3578. </select>
  3579. </div>
  3580. <div className="flex items-center gap-2">
  3581. <input
  3582. type="checkbox"
  3583. id="auto_archive"
  3584. checked={form.auto_archive}
  3585. onChange={(e) => setForm({ ...form, auto_archive: e.target.checked })}
  3586. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  3587. />
  3588. <label htmlFor="auto_archive" className="text-sm text-bambu-gray">
  3589. Auto-archive completed prints
  3590. </label>
  3591. </div>
  3592. <div className="flex gap-3 pt-4">
  3593. <Button type="button" variant="secondary" onClick={onClose} className="flex-1">
  3594. Cancel
  3595. </Button>
  3596. <Button type="submit" className="flex-1">
  3597. Add Printer
  3598. </Button>
  3599. </div>
  3600. </form>
  3601. </CardContent>
  3602. </Card>
  3603. </div>
  3604. );
  3605. }
  3606. function FirmwareUpdateModal({
  3607. printer,
  3608. firmwareInfo,
  3609. onClose,
  3610. }: {
  3611. printer: Printer;
  3612. firmwareInfo: FirmwareUpdateInfo;
  3613. onClose: () => void;
  3614. }) {
  3615. const queryClient = useQueryClient();
  3616. const { showToast } = useToast();
  3617. const [uploadStatus, setUploadStatus] = useState<FirmwareUploadStatus | null>(null);
  3618. const [isUploading, setIsUploading] = useState(false);
  3619. const [pollInterval, setPollInterval] = useState<NodeJS.Timeout | null>(null);
  3620. // Prepare check query
  3621. const { data: prepareInfo, isLoading: isPreparing } = useQuery({
  3622. queryKey: ['firmwarePrepare', printer.id],
  3623. queryFn: () => firmwareApi.prepareUpload(printer.id),
  3624. staleTime: 30000,
  3625. });
  3626. // Start upload mutation
  3627. const uploadMutation = useMutation({
  3628. mutationFn: () => firmwareApi.startUpload(printer.id),
  3629. onSuccess: () => {
  3630. setIsUploading(true);
  3631. // Start polling for status
  3632. const interval = setInterval(async () => {
  3633. try {
  3634. const status = await firmwareApi.getUploadStatus(printer.id);
  3635. setUploadStatus(status);
  3636. if (status.status === 'complete' || status.status === 'error') {
  3637. clearInterval(interval);
  3638. setPollInterval(null);
  3639. setIsUploading(false);
  3640. if (status.status === 'complete') {
  3641. showToast('Firmware uploaded! Trigger update from printer screen.', 'success');
  3642. queryClient.invalidateQueries({ queryKey: ['firmwareUpdate', printer.id] });
  3643. }
  3644. }
  3645. } catch {
  3646. // Ignore errors during polling
  3647. }
  3648. }, 2000);
  3649. setPollInterval(interval);
  3650. },
  3651. onError: (error: Error) => {
  3652. showToast(`Failed to start upload: ${error.message}`, 'error');
  3653. setIsUploading(false);
  3654. },
  3655. });
  3656. // Cleanup on unmount
  3657. useEffect(() => {
  3658. return () => {
  3659. if (pollInterval) clearInterval(pollInterval);
  3660. };
  3661. }, [pollInterval]);
  3662. const handleStartUpload = () => {
  3663. setUploadStatus(null);
  3664. uploadMutation.mutate();
  3665. };
  3666. return (
  3667. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
  3668. <Card className="w-full max-w-md mx-4">
  3669. <CardContent>
  3670. <div className="flex items-start gap-3 mb-4">
  3671. <div className="p-2 rounded-full bg-orange-500/20">
  3672. <Download className="w-5 h-5 text-orange-400" />
  3673. </div>
  3674. <div className="flex-1">
  3675. <h3 className="text-lg font-semibold text-white">Firmware Update</h3>
  3676. <p className="text-sm text-bambu-gray mt-1">
  3677. {printer.name}
  3678. </p>
  3679. </div>
  3680. </div>
  3681. {/* Version Info */}
  3682. <div className="bg-bambu-dark rounded-lg p-3 mb-4">
  3683. <div className="flex justify-between items-center text-sm">
  3684. <span className="text-bambu-gray">Current:</span>
  3685. <span className="text-white font-mono">{firmwareInfo.current_version || 'Unknown'}</span>
  3686. </div>
  3687. <div className="flex justify-between items-center text-sm mt-1">
  3688. <span className="text-bambu-gray">Latest:</span>
  3689. <span className="text-orange-400 font-mono">{firmwareInfo.latest_version}</span>
  3690. </div>
  3691. {firmwareInfo.release_notes && (
  3692. <details className="mt-3 text-sm">
  3693. <summary className="text-orange-400 cursor-pointer hover:underline">
  3694. Release Notes
  3695. </summary>
  3696. <div className="mt-2 text-bambu-gray text-xs max-h-40 overflow-y-auto whitespace-pre-wrap">
  3697. {firmwareInfo.release_notes}
  3698. </div>
  3699. </details>
  3700. )}
  3701. </div>
  3702. {/* Status / Progress */}
  3703. {isPreparing ? (
  3704. <div className="flex items-center gap-2 text-bambu-gray text-sm mb-4">
  3705. <Loader2 className="w-4 h-4 animate-spin" />
  3706. Checking prerequisites...
  3707. </div>
  3708. ) : prepareInfo && !isUploading && !uploadStatus ? (
  3709. <div className="mb-4">
  3710. {prepareInfo.can_proceed ? (
  3711. <div className="flex items-center gap-2 text-bambu-green text-sm">
  3712. <Box className="w-4 h-4" />
  3713. SD card ready. Click below to upload firmware.
  3714. </div>
  3715. ) : (
  3716. <div className="space-y-1">
  3717. {prepareInfo.errors.map((error, i) => (
  3718. <div key={i} className="flex items-center gap-2 text-red-400 text-sm">
  3719. <AlertCircle className="w-4 h-4 flex-shrink-0" />
  3720. {error}
  3721. </div>
  3722. ))}
  3723. </div>
  3724. )}
  3725. </div>
  3726. ) : null}
  3727. {/* Upload Progress */}
  3728. {(isUploading || uploadStatus) && uploadStatus && (
  3729. <div className="mb-4">
  3730. <div className="flex items-center justify-between text-sm mb-1">
  3731. <span className="text-bambu-gray capitalize">{uploadStatus.status}</span>
  3732. <span className="text-white">{uploadStatus.progress}%</span>
  3733. </div>
  3734. <div className="w-full bg-bambu-dark-tertiary rounded-full h-2">
  3735. <div
  3736. className={`h-2 rounded-full transition-all ${
  3737. uploadStatus.status === 'error' ? 'bg-status-error' :
  3738. uploadStatus.status === 'complete' ? 'bg-status-ok' : 'bg-orange-500'
  3739. } ${uploadStatus.status === 'uploading' ? 'animate-pulse' : ''}`}
  3740. style={{ width: `${uploadStatus.progress}%` }}
  3741. />
  3742. </div>
  3743. <p className="text-xs text-bambu-gray mt-1">{uploadStatus.message}</p>
  3744. {uploadStatus.error && (
  3745. <p className="text-xs text-red-400 mt-1">{uploadStatus.error}</p>
  3746. )}
  3747. </div>
  3748. )}
  3749. {/* Success Message */}
  3750. {uploadStatus?.status === 'complete' && (
  3751. <div className="bg-bambu-green/10 border border-bambu-green/30 rounded-lg p-3 mb-4">
  3752. <p className="text-sm text-bambu-green font-medium mb-2">
  3753. Firmware uploaded to SD card!
  3754. </p>
  3755. <p className="text-xs text-bambu-gray">
  3756. To apply the update on your printer:
  3757. </p>
  3758. <ol className="text-xs text-bambu-gray mt-1 list-decimal list-inside space-y-1">
  3759. <li>On the printer's touchscreen, go to <strong className="text-white">Settings</strong></li>
  3760. <li>Navigate to <strong className="text-white">Firmware</strong></li>
  3761. <li>Select <strong className="text-white">Update from SD card</strong></li>
  3762. <li>The update will take 10-20 minutes</li>
  3763. </ol>
  3764. </div>
  3765. )}
  3766. {/* Buttons */}
  3767. <div className="flex gap-2 justify-end">
  3768. <Button variant="secondary" onClick={onClose}>
  3769. {uploadStatus?.status === 'complete' ? 'Done' : 'Cancel'}
  3770. </Button>
  3771. {prepareInfo?.can_proceed && !isUploading && uploadStatus?.status !== 'complete' && (
  3772. <Button
  3773. onClick={handleStartUpload}
  3774. disabled={uploadMutation.isPending}
  3775. >
  3776. {uploadMutation.isPending ? (
  3777. <>
  3778. <Loader2 className="w-4 h-4 animate-spin mr-2" />
  3779. Starting...
  3780. </>
  3781. ) : (
  3782. <>
  3783. <Download className="w-4 h-4 mr-2" />
  3784. Upload Firmware
  3785. </>
  3786. )}
  3787. </Button>
  3788. )}
  3789. </div>
  3790. </CardContent>
  3791. </Card>
  3792. </div>
  3793. );
  3794. }
  3795. function EditPrinterModal({
  3796. printer,
  3797. onClose,
  3798. }: {
  3799. printer: Printer;
  3800. onClose: () => void;
  3801. }) {
  3802. const queryClient = useQueryClient();
  3803. const [form, setForm] = useState({
  3804. name: printer.name,
  3805. ip_address: printer.ip_address,
  3806. access_code: '',
  3807. model: printer.model || '',
  3808. location: printer.location || '',
  3809. auto_archive: printer.auto_archive,
  3810. });
  3811. const updateMutation = useMutation({
  3812. mutationFn: (data: Partial<PrinterCreate>) => api.updatePrinter(printer.id, data),
  3813. onSuccess: () => {
  3814. queryClient.invalidateQueries({ queryKey: ['printers'] });
  3815. queryClient.invalidateQueries({ queryKey: ['printerStatus', printer.id] });
  3816. onClose();
  3817. },
  3818. });
  3819. // Close on Escape key
  3820. useEffect(() => {
  3821. const handleKeyDown = (e: KeyboardEvent) => {
  3822. if (e.key === 'Escape') onClose();
  3823. };
  3824. window.addEventListener('keydown', handleKeyDown);
  3825. return () => window.removeEventListener('keydown', handleKeyDown);
  3826. }, [onClose]);
  3827. const handleSubmit = (e: React.FormEvent) => {
  3828. e.preventDefault();
  3829. const data: Partial<PrinterCreate> = {
  3830. name: form.name,
  3831. ip_address: form.ip_address,
  3832. model: form.model || undefined,
  3833. location: form.location || undefined,
  3834. auto_archive: form.auto_archive,
  3835. };
  3836. // Only include access_code if it was changed
  3837. if (form.access_code) {
  3838. data.access_code = form.access_code;
  3839. }
  3840. updateMutation.mutate(data);
  3841. };
  3842. return (
  3843. <div
  3844. className="fixed inset-0 bg-black/50 flex items-center justify-center z-50"
  3845. onClick={onClose}
  3846. >
  3847. <Card className="w-full max-w-md" onClick={(e: React.MouseEvent) => e.stopPropagation()}>
  3848. <CardContent>
  3849. <h2 className="text-xl font-semibold mb-4">Edit Printer</h2>
  3850. <form onSubmit={handleSubmit} className="space-y-4">
  3851. <div>
  3852. <label className="block text-sm text-bambu-gray mb-1">Name</label>
  3853. <input
  3854. type="text"
  3855. required
  3856. 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"
  3857. value={form.name}
  3858. onChange={(e) => setForm({ ...form, name: e.target.value })}
  3859. placeholder="My Printer"
  3860. />
  3861. </div>
  3862. <div>
  3863. <label className="block text-sm text-bambu-gray mb-1">IP Address</label>
  3864. <input
  3865. type="text"
  3866. required
  3867. pattern="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
  3868. 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"
  3869. value={form.ip_address}
  3870. onChange={(e) => setForm({ ...form, ip_address: e.target.value })}
  3871. placeholder="192.168.1.100"
  3872. />
  3873. </div>
  3874. <div>
  3875. <label className="block text-sm text-bambu-gray mb-1">Serial Number</label>
  3876. <input
  3877. type="text"
  3878. disabled
  3879. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-bambu-gray cursor-not-allowed"
  3880. value={printer.serial_number}
  3881. />
  3882. <p className="text-xs text-bambu-gray mt-1">Serial number cannot be changed</p>
  3883. </div>
  3884. <div>
  3885. <label className="block text-sm text-bambu-gray mb-1">Access Code</label>
  3886. <input
  3887. type="password"
  3888. 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"
  3889. value={form.access_code}
  3890. onChange={(e) => setForm({ ...form, access_code: e.target.value })}
  3891. placeholder="Leave empty to keep current"
  3892. />
  3893. </div>
  3894. <div>
  3895. <label className="block text-sm text-bambu-gray mb-1">Model</label>
  3896. <select
  3897. 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"
  3898. value={form.model}
  3899. onChange={(e) => setForm({ ...form, model: e.target.value })}
  3900. >
  3901. <option value="">Select model...</option>
  3902. <optgroup label="H2 Series">
  3903. <option value="H2C">H2C</option>
  3904. <option value="H2D">H2D</option>
  3905. <option value="H2S">H2S</option>
  3906. </optgroup>
  3907. <optgroup label="X1 Series">
  3908. <option value="X1E">X1E</option>
  3909. <option value="X1C">X1 Carbon</option>
  3910. <option value="X1">X1</option>
  3911. </optgroup>
  3912. <optgroup label="P Series">
  3913. <option value="P2S">P2S</option>
  3914. <option value="P1S">P1S</option>
  3915. <option value="P1P">P1P</option>
  3916. </optgroup>
  3917. <optgroup label="A1 Series">
  3918. <option value="A1">A1</option>
  3919. <option value="A1 Mini">A1 Mini</option>
  3920. </optgroup>
  3921. </select>
  3922. </div>
  3923. <div>
  3924. <label className="block text-sm text-bambu-gray mb-1">Location / Group</label>
  3925. <input
  3926. type="text"
  3927. 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"
  3928. value={form.location}
  3929. onChange={(e) => setForm({ ...form, location: e.target.value })}
  3930. placeholder="e.g., Workshop, Office, Basement"
  3931. />
  3932. <p className="text-xs text-bambu-gray mt-1">Used to group printers on the dashboard</p>
  3933. </div>
  3934. <div className="flex items-center gap-2">
  3935. <input
  3936. type="checkbox"
  3937. id="edit_auto_archive"
  3938. checked={form.auto_archive}
  3939. onChange={(e) => setForm({ ...form, auto_archive: e.target.checked })}
  3940. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  3941. />
  3942. <label htmlFor="edit_auto_archive" className="text-sm text-bambu-gray">
  3943. Auto-archive completed prints
  3944. </label>
  3945. </div>
  3946. <div className="flex gap-3 pt-4">
  3947. <Button type="button" variant="secondary" onClick={onClose} className="flex-1">
  3948. Cancel
  3949. </Button>
  3950. <Button type="submit" className="flex-1" disabled={updateMutation.isPending}>
  3951. {updateMutation.isPending ? 'Saving...' : 'Save Changes'}
  3952. </Button>
  3953. </div>
  3954. </form>
  3955. </CardContent>
  3956. </Card>
  3957. </div>
  3958. );
  3959. }
  3960. // Component to check if a printer is offline (for power dropdown)
  3961. function usePrinterOfflineStatus(printerId: number) {
  3962. const { data: status } = useQuery({
  3963. queryKey: ['printerStatus', printerId],
  3964. queryFn: () => api.getPrinterStatus(printerId),
  3965. refetchInterval: 30000,
  3966. });
  3967. return !status?.connected;
  3968. }
  3969. // Power dropdown item for an offline printer
  3970. function PowerDropdownItem({
  3971. printer,
  3972. plug,
  3973. onPowerOn,
  3974. isPowering,
  3975. }: {
  3976. printer: Printer;
  3977. plug: { id: number; name: string };
  3978. onPowerOn: (plugId: number) => void;
  3979. isPowering: boolean;
  3980. }) {
  3981. const isOffline = usePrinterOfflineStatus(printer.id);
  3982. // Fetch plug status
  3983. const { data: plugStatus } = useQuery({
  3984. queryKey: ['smartPlugStatus', plug.id],
  3985. queryFn: () => api.getSmartPlugStatus(plug.id),
  3986. refetchInterval: 10000,
  3987. });
  3988. // Only show if printer is offline
  3989. if (!isOffline) {
  3990. return null;
  3991. }
  3992. return (
  3993. <div className="flex items-center justify-between px-3 py-2 hover:bg-gray-100 dark:hover:bg-bambu-dark-tertiary">
  3994. <div className="flex items-center gap-2 min-w-0">
  3995. <span className="text-sm text-gray-900 dark:text-white truncate">{printer.name}</span>
  3996. {plugStatus && (
  3997. <span
  3998. className={`text-xs px-1.5 py-0.5 rounded ${
  3999. plugStatus.state === 'ON'
  4000. ? 'bg-bambu-green/20 text-bambu-green'
  4001. : 'bg-red-500/20 text-red-400'
  4002. }`}
  4003. >
  4004. {plugStatus.state || '?'}
  4005. </span>
  4006. )}
  4007. </div>
  4008. <button
  4009. onClick={() => onPowerOn(plug.id)}
  4010. disabled={isPowering || plugStatus?.state === 'ON'}
  4011. className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
  4012. plugStatus?.state === 'ON'
  4013. ? 'bg-bambu-green/20 text-bambu-green cursor-default'
  4014. : 'bg-bambu-green/20 text-bambu-green hover:bg-bambu-green hover:text-white'
  4015. }`}
  4016. >
  4017. <Power className="w-3 h-3" />
  4018. {isPowering ? '...' : 'On'}
  4019. </button>
  4020. </div>
  4021. );
  4022. }
  4023. export function PrintersPage() {
  4024. const [showAddModal, setShowAddModal] = useState(false);
  4025. const [hideDisconnected, setHideDisconnected] = useState(() => {
  4026. return localStorage.getItem('hideDisconnectedPrinters') === 'true';
  4027. });
  4028. const [showPowerDropdown, setShowPowerDropdown] = useState(false);
  4029. const [poweringOn, setPoweringOn] = useState<number | null>(null);
  4030. const [sortBy, setSortBy] = useState<SortOption>(() => {
  4031. return (localStorage.getItem('printerSortBy') as SortOption) || 'name';
  4032. });
  4033. const [sortAsc, setSortAsc] = useState<boolean>(() => {
  4034. return localStorage.getItem('printerSortAsc') !== 'false';
  4035. });
  4036. // Card size: 1=small, 2=medium, 3=large, 4=xl
  4037. const [cardSize, setCardSize] = useState<number>(() => {
  4038. const saved = localStorage.getItem('printerCardSize');
  4039. return saved ? parseInt(saved, 10) : 2; // Default to medium
  4040. });
  4041. // Derive viewMode from cardSize: S=compact, M/L/XL=expanded
  4042. const viewMode: ViewMode = cardSize === 1 ? 'compact' : 'expanded';
  4043. const queryClient = useQueryClient();
  4044. // Embedded camera viewer state - supports multiple simultaneous viewers
  4045. // Persisted to localStorage so cameras reopen after navigation
  4046. const [embeddedCameraPrinters, setEmbeddedCameraPrinters] = useState<Map<number, { id: number; name: string }>>(() => {
  4047. // Initialize from localStorage if camera_view_mode is embedded
  4048. const saved = localStorage.getItem('openEmbeddedCameras');
  4049. if (saved) {
  4050. try {
  4051. const cameras = JSON.parse(saved) as Array<{ id: number; name: string }>;
  4052. return new Map(cameras.map(c => [c.id, c]));
  4053. } catch {
  4054. return new Map();
  4055. }
  4056. }
  4057. return new Map();
  4058. });
  4059. // Persist open cameras to localStorage when they change
  4060. useEffect(() => {
  4061. const cameras = Array.from(embeddedCameraPrinters.values());
  4062. if (cameras.length > 0) {
  4063. localStorage.setItem('openEmbeddedCameras', JSON.stringify(cameras));
  4064. } else {
  4065. localStorage.removeItem('openEmbeddedCameras');
  4066. }
  4067. }, [embeddedCameraPrinters]);
  4068. const { data: printers, isLoading } = useQuery({
  4069. queryKey: ['printers'],
  4070. queryFn: api.getPrinters,
  4071. });
  4072. // Fetch app settings for AMS thresholds
  4073. const { data: settings } = useQuery({
  4074. queryKey: ['settings'],
  4075. queryFn: api.getSettings,
  4076. });
  4077. // Close embedded cameras if mode changes to 'window'
  4078. useEffect(() => {
  4079. if (settings?.camera_view_mode === 'window' && embeddedCameraPrinters.size > 0) {
  4080. setEmbeddedCameraPrinters(new Map());
  4081. }
  4082. }, [settings?.camera_view_mode, embeddedCameraPrinters.size]);
  4083. // Fetch all smart plugs to know which printers have them
  4084. const { data: smartPlugs } = useQuery({
  4085. queryKey: ['smart-plugs'],
  4086. queryFn: api.getSmartPlugs,
  4087. });
  4088. // Fetch maintenance overview for all printers to show badges
  4089. const { data: maintenanceOverview } = useQuery({
  4090. queryKey: ['maintenanceOverview'],
  4091. queryFn: api.getMaintenanceOverview,
  4092. staleTime: 60 * 1000, // 1 minute
  4093. });
  4094. // Fetch Spoolman status to enable link spool feature
  4095. const { data: spoolmanStatus } = useQuery({
  4096. queryKey: ['spoolman-status'],
  4097. queryFn: api.getSpoolmanStatus,
  4098. staleTime: 60 * 1000, // 1 minute
  4099. });
  4100. const spoolmanEnabled = spoolmanStatus?.enabled && spoolmanStatus?.connected;
  4101. // Fetch unlinked spools to know if link button should be enabled
  4102. const { data: unlinkedSpools } = useQuery({
  4103. queryKey: ['unlinked-spools'],
  4104. queryFn: api.getUnlinkedSpools,
  4105. enabled: !!spoolmanEnabled,
  4106. staleTime: 30 * 1000, // 30 seconds
  4107. });
  4108. const hasUnlinkedSpools = unlinkedSpools && unlinkedSpools.length > 0;
  4109. // Create a map of printer_id -> maintenance info for quick lookup
  4110. const maintenanceByPrinter = maintenanceOverview?.reduce(
  4111. (acc, overview) => {
  4112. acc[overview.printer_id] = {
  4113. due_count: overview.due_count,
  4114. warning_count: overview.warning_count,
  4115. total_print_hours: overview.total_print_hours,
  4116. };
  4117. return acc;
  4118. },
  4119. {} as Record<number, PrinterMaintenanceInfo>
  4120. ) || {};
  4121. // Create a map of printer_id -> smart plug
  4122. const smartPlugByPrinter = smartPlugs?.reduce(
  4123. (acc, plug) => {
  4124. if (plug.printer_id) {
  4125. acc[plug.printer_id] = plug;
  4126. }
  4127. return acc;
  4128. },
  4129. {} as Record<number, typeof smartPlugs[0]>
  4130. ) || {};
  4131. const addMutation = useMutation({
  4132. mutationFn: api.createPrinter,
  4133. onSuccess: () => {
  4134. queryClient.invalidateQueries({ queryKey: ['printers'] });
  4135. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  4136. setShowAddModal(false);
  4137. },
  4138. });
  4139. const powerOnMutation = useMutation({
  4140. mutationFn: (plugId: number) => api.controlSmartPlug(plugId, 'on'),
  4141. onSuccess: () => {
  4142. queryClient.invalidateQueries({ queryKey: ['smart-plugs'] });
  4143. setPoweringOn(null);
  4144. },
  4145. onError: () => {
  4146. setPoweringOn(null);
  4147. },
  4148. });
  4149. const toggleHideDisconnected = () => {
  4150. const newValue = !hideDisconnected;
  4151. setHideDisconnected(newValue);
  4152. localStorage.setItem('hideDisconnectedPrinters', String(newValue));
  4153. };
  4154. const handleSortChange = (newSort: SortOption) => {
  4155. setSortBy(newSort);
  4156. localStorage.setItem('printerSortBy', newSort);
  4157. };
  4158. const toggleSortDirection = () => {
  4159. const newAsc = !sortAsc;
  4160. setSortAsc(newAsc);
  4161. localStorage.setItem('printerSortAsc', String(newAsc));
  4162. };
  4163. // Grid classes based on card size (1=small, 2=medium, 3=large, 4=xl)
  4164. const getGridClasses = () => {
  4165. switch (cardSize) {
  4166. case 1: return 'grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5'; // S: many small cards
  4167. case 2: return 'grid-cols-1 md:grid-cols-2 xl:grid-cols-3'; // M: medium cards
  4168. case 3: return 'grid-cols-1 lg:grid-cols-2'; // L: large cards, 2 columns max
  4169. case 4: return 'grid-cols-1'; // XL: single column, full width
  4170. default: return 'grid-cols-1 md:grid-cols-2 xl:grid-cols-3';
  4171. }
  4172. };
  4173. const cardSizeLabels = ['S', 'M', 'L', 'XL'];
  4174. // Sort printers based on selected option
  4175. const sortedPrinters = useMemo(() => {
  4176. if (!printers) return [];
  4177. const sorted = [...printers];
  4178. switch (sortBy) {
  4179. case 'name':
  4180. sorted.sort((a, b) => a.name.localeCompare(b.name));
  4181. break;
  4182. case 'model':
  4183. sorted.sort((a, b) => (a.model || '').localeCompare(b.model || ''));
  4184. break;
  4185. case 'location':
  4186. // Sort by location, with ungrouped printers last
  4187. sorted.sort((a, b) => {
  4188. const locA = a.location || '';
  4189. const locB = b.location || '';
  4190. if (!locA && locB) return 1;
  4191. if (locA && !locB) return -1;
  4192. return locA.localeCompare(locB) || a.name.localeCompare(b.name);
  4193. });
  4194. break;
  4195. case 'status':
  4196. // Sort by status: printing > idle > offline
  4197. sorted.sort((a, b) => {
  4198. const statusA = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', a.id]);
  4199. const statusB = queryClient.getQueryData<{ connected: boolean; state: string | null }>(['printerStatus', b.id]);
  4200. const getPriority = (s: typeof statusA) => {
  4201. if (!s?.connected) return 2; // offline
  4202. if (s.state === 'RUNNING') return 0; // printing
  4203. return 1; // idle
  4204. };
  4205. return getPriority(statusA) - getPriority(statusB);
  4206. });
  4207. break;
  4208. }
  4209. // Apply ascending/descending
  4210. if (!sortAsc) {
  4211. sorted.reverse();
  4212. }
  4213. return sorted;
  4214. }, [printers, sortBy, sortAsc, queryClient]);
  4215. // Group printers by location when sorted by location
  4216. const groupedPrinters = useMemo(() => {
  4217. if (sortBy !== 'location') return null;
  4218. const groups: Record<string, typeof sortedPrinters> = {};
  4219. sortedPrinters.forEach(printer => {
  4220. const location = printer.location || 'Ungrouped';
  4221. if (!groups[location]) groups[location] = [];
  4222. groups[location].push(printer);
  4223. });
  4224. return groups;
  4225. }, [sortBy, sortedPrinters]);
  4226. return (
  4227. <div className="p-4 md:p-8">
  4228. <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
  4229. <div>
  4230. <h1 className="text-2xl font-bold text-white">Printers</h1>
  4231. <StatusSummaryBar printers={printers} />
  4232. </div>
  4233. <div className="flex items-center gap-2 sm:gap-3 flex-wrap">
  4234. {/* Sort dropdown */}
  4235. <div className="flex items-center gap-1">
  4236. <select
  4237. value={sortBy}
  4238. onChange={(e) => handleSortChange(e.target.value as SortOption)}
  4239. 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"
  4240. >
  4241. <option value="name">Name</option>
  4242. <option value="status">Status</option>
  4243. <option value="model">Model</option>
  4244. <option value="location">Location</option>
  4245. </select>
  4246. <button
  4247. onClick={toggleSortDirection}
  4248. className="p-1.5 rounded-lg hover:bg-bambu-dark-tertiary transition-colors"
  4249. title={sortAsc ? 'Sort descending' : 'Sort ascending'}
  4250. >
  4251. {sortAsc ? (
  4252. <ArrowUp className="w-4 h-4 text-bambu-gray" />
  4253. ) : (
  4254. <ArrowDown className="w-4 h-4 text-bambu-gray" />
  4255. )}
  4256. </button>
  4257. </div>
  4258. {/* Card size selector */}
  4259. <div className="flex items-center bg-bambu-dark rounded-lg border border-bambu-dark-tertiary">
  4260. {cardSizeLabels.map((label, index) => {
  4261. const size = index + 1;
  4262. const isSelected = cardSize === size;
  4263. return (
  4264. <button
  4265. key={label}
  4266. onClick={() => {
  4267. setCardSize(size);
  4268. localStorage.setItem('printerCardSize', String(size));
  4269. }}
  4270. className={`px-2 py-1.5 text-xs font-medium transition-colors ${
  4271. index === 0 ? 'rounded-l-lg' : ''
  4272. } ${
  4273. index === cardSizeLabels.length - 1 ? 'rounded-r-lg' : ''
  4274. } ${
  4275. isSelected
  4276. ? 'bg-bambu-green text-white'
  4277. : 'text-bambu-gray hover:bg-bambu-dark-tertiary hover:text-white'
  4278. }`}
  4279. title={`${label === 'S' ? 'Small' : label === 'M' ? 'Medium' : label === 'L' ? 'Large' : 'Extra large'} cards`}
  4280. >
  4281. {label}
  4282. </button>
  4283. );
  4284. })}
  4285. </div>
  4286. <div className="w-px h-6 bg-bambu-dark-tertiary" />
  4287. <label className="flex items-center gap-2 text-sm text-bambu-gray cursor-pointer">
  4288. <input
  4289. type="checkbox"
  4290. checked={hideDisconnected}
  4291. onChange={toggleHideDisconnected}
  4292. className="rounded border-bambu-dark-tertiary bg-bambu-dark text-bambu-green focus:ring-bambu-green"
  4293. />
  4294. Hide offline
  4295. </label>
  4296. {/* Power dropdown for offline printers with smart plugs */}
  4297. {hideDisconnected && Object.keys(smartPlugByPrinter).length > 0 && (
  4298. <div className="relative">
  4299. <button
  4300. onClick={() => setShowPowerDropdown(!showPowerDropdown)}
  4301. 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"
  4302. >
  4303. <Power className="w-4 h-4" />
  4304. Power On
  4305. <ChevronDown className={`w-3 h-3 transition-transform ${showPowerDropdown ? 'rotate-180' : ''}`} />
  4306. </button>
  4307. {showPowerDropdown && (
  4308. <>
  4309. {/* Backdrop to close dropdown */}
  4310. <div
  4311. className="fixed inset-0 z-10"
  4312. onClick={() => setShowPowerDropdown(false)}
  4313. />
  4314. <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">
  4315. <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">
  4316. Offline printers with smart plugs
  4317. </div>
  4318. {printers?.filter(p => smartPlugByPrinter[p.id]).map(printer => (
  4319. <PowerDropdownItem
  4320. key={printer.id}
  4321. printer={printer}
  4322. plug={smartPlugByPrinter[printer.id]}
  4323. onPowerOn={(plugId) => {
  4324. setPoweringOn(plugId);
  4325. powerOnMutation.mutate(plugId);
  4326. }}
  4327. isPowering={poweringOn === smartPlugByPrinter[printer.id]?.id}
  4328. />
  4329. ))}
  4330. {printers?.filter(p => smartPlugByPrinter[p.id]).length === 0 && (
  4331. <div className="px-3 py-2 text-sm text-bambu-gray">
  4332. No printers with smart plugs
  4333. </div>
  4334. )}
  4335. </div>
  4336. </>
  4337. )}
  4338. </div>
  4339. )}
  4340. <Button onClick={() => setShowAddModal(true)}>
  4341. <Plus className="w-4 h-4" />
  4342. Add Printer
  4343. </Button>
  4344. </div>
  4345. </div>
  4346. {isLoading ? (
  4347. <div className="text-center py-12 text-bambu-gray">Loading printers...</div>
  4348. ) : printers?.length === 0 ? (
  4349. <Card>
  4350. <CardContent className="text-center py-12">
  4351. <p className="text-bambu-gray mb-4">No printers configured yet</p>
  4352. <Button onClick={() => setShowAddModal(true)}>
  4353. <Plus className="w-4 h-4" />
  4354. Add Your First Printer
  4355. </Button>
  4356. </CardContent>
  4357. </Card>
  4358. ) : groupedPrinters ? (
  4359. /* Grouped by location view */
  4360. <div className="space-y-6">
  4361. {Object.entries(groupedPrinters).map(([location, locationPrinters]) => (
  4362. <div key={location}>
  4363. <h2 className="text-lg font-semibold text-white mb-3 flex items-center gap-2">
  4364. <span className="w-2 h-2 rounded-full bg-bambu-green" />
  4365. {location}
  4366. <span className="text-sm font-normal text-bambu-gray">({locationPrinters.length})</span>
  4367. </h2>
  4368. <div className={`grid gap-4 ${cardSize >= 3 ? 'gap-6' : ''} ${getGridClasses()}`}>
  4369. {locationPrinters.map((printer) => (
  4370. <PrinterCard
  4371. key={printer.id}
  4372. printer={printer}
  4373. hideIfDisconnected={hideDisconnected}
  4374. maintenanceInfo={maintenanceByPrinter[printer.id]}
  4375. viewMode={viewMode}
  4376. cardSize={cardSize}
  4377. amsThresholds={settings ? {
  4378. humidityGood: Number(settings.ams_humidity_good) || 40,
  4379. humidityFair: Number(settings.ams_humidity_fair) || 60,
  4380. tempGood: Number(settings.ams_temp_good) || 28,
  4381. tempFair: Number(settings.ams_temp_fair) || 35,
  4382. } : undefined}
  4383. spoolmanEnabled={spoolmanEnabled}
  4384. hasUnlinkedSpools={hasUnlinkedSpools}
  4385. timeFormat={settings?.time_format || 'system'}
  4386. cameraViewMode={settings?.camera_view_mode || 'window'}
  4387. onOpenEmbeddedCamera={(id, name) => setEmbeddedCameraPrinters(prev => new Map(prev).set(id, { id, name }))}
  4388. checkPrinterFirmware={settings?.check_printer_firmware !== false}
  4389. />
  4390. ))}
  4391. </div>
  4392. </div>
  4393. ))}
  4394. </div>
  4395. ) : (
  4396. /* Regular grid view */
  4397. <div className={`grid gap-4 ${cardSize >= 3 ? 'gap-6' : ''} ${getGridClasses()}`}>
  4398. {sortedPrinters.map((printer) => (
  4399. <PrinterCard
  4400. key={printer.id}
  4401. printer={printer}
  4402. hideIfDisconnected={hideDisconnected}
  4403. maintenanceInfo={maintenanceByPrinter[printer.id]}
  4404. viewMode={viewMode}
  4405. cardSize={cardSize}
  4406. spoolmanEnabled={spoolmanEnabled}
  4407. hasUnlinkedSpools={hasUnlinkedSpools}
  4408. amsThresholds={settings ? {
  4409. humidityGood: Number(settings.ams_humidity_good) || 40,
  4410. humidityFair: Number(settings.ams_humidity_fair) || 60,
  4411. tempGood: Number(settings.ams_temp_good) || 28,
  4412. tempFair: Number(settings.ams_temp_fair) || 35,
  4413. } : undefined}
  4414. timeFormat={settings?.time_format || 'system'}
  4415. cameraViewMode={settings?.camera_view_mode || 'window'}
  4416. onOpenEmbeddedCamera={(id, name) => setEmbeddedCameraPrinters(prev => new Map(prev).set(id, { id, name }))}
  4417. checkPrinterFirmware={settings?.check_printer_firmware !== false}
  4418. />
  4419. ))}
  4420. </div>
  4421. )}
  4422. {showAddModal && (
  4423. <AddPrinterModal
  4424. onClose={() => setShowAddModal(false)}
  4425. onAdd={(data) => addMutation.mutate(data)}
  4426. existingSerials={printers?.map(p => p.serial_number) || []}
  4427. />
  4428. )}
  4429. {/* Embedded Camera Viewers - multiple viewers can be open simultaneously */}
  4430. {Array.from(embeddedCameraPrinters.values()).map((camera, index) => (
  4431. <EmbeddedCameraViewer
  4432. key={camera.id}
  4433. printerId={camera.id}
  4434. printerName={camera.name}
  4435. viewerIndex={index}
  4436. onClose={() => setEmbeddedCameraPrinters(prev => {
  4437. const next = new Map(prev);
  4438. next.delete(camera.id);
  4439. return next;
  4440. })}
  4441. />
  4442. ))}
  4443. </div>
  4444. );
  4445. }