| 1 |
- import{i as e}from"./chunk-aKtaBQYM.js";import{C as t,D as n,a as r,t as i,v as a,w as o}from"./client-BdOyoZJz.js";import{n as s}from"./QueryClientProvider-Clbyr4Jg.js";import{t as c}from"./useQuery-CCy0EwsM.js";import{t as l}from"./useMutation-qh3Iyscy.js";import{t as u}from"./useQueries-C87lFrjE.js";import{t as d}from"./check-D1Pn8T1J.js";import{n as f}from"./ToastContext-VFOrCCoA.js";import{t as p}from"./triangle-alert-gIik0G2C.js";import{t as m}from"./unlink-DJBJ04VB.js";import{t as h}from"./x-Dctff5l9.js";import{Hn as g,Kt as ee,ft as _,pt as v}from"./index-BoVbGTav.js";import{t as y}from"./AssignToAmsModal-kr-0EzwV.js";import{t as b}from"./SpoolIcon-OZKzbEc8.js";var x=e(n(),1),S=t(),C=`spoolbuddy-default-core-weight`;function w(){try{let e=localStorage.getItem(C);if(e){let t=parseInt(e,10);if(t>=0&&t<=500)return t}}catch{}return 250}function T({spool:e,scaleWeight:t,onClose:n,onSyncWeight:r,onAssignToAms:i,isAssigned:s,onUnassignFromAms:c}){let{t:l}=o(),[u,f]=(0,x.useState)(!1),[h,g]=(0,x.useState)(!1),y=v(e.rgba),C=_(e.color_name,e.rgba,e.color_name_is_synthesized),T=e.core_weight&&e.core_weight>0?e.core_weight:w(),E=t===null?null:Math.round(Math.max(0,t)),D=E===null?null:Math.round(Math.max(0,E-T)),O=Math.round(e.label_weight||1e3),k=D===null?null:Math.min(100,Math.round(D/O*100)),A=k===null?`#808080`:k>50?`#22c55e`:k>20?`#eab308`:`#ef4444`,te=Math.max(0,(e.label_weight||0)-(e.weight_used||0))+T,j=E===null?null:E-te,M=j===null?null:Math.abs(j)<=50,N=async()=>{if(t!==null){f(!0);try{await a.updateSpoolWeight(e.id,Math.round(t)),g(!0),r?.(),setTimeout(()=>g(!1),3e3)}catch(e){console.error(`Failed to sync weight:`,e)}finally{f(!1)}}};return(0,S.jsxs)(`div`,{className:`flex flex-col items-center space-y-4 max-w-md`,children:[(0,S.jsxs)(`div`,{className:`flex items-start gap-5`,children:[(0,S.jsxs)(`div`,{className:`relative shrink-0`,children:[(0,S.jsx)(b,{color:y,isEmpty:!1,size:100,spool:e}),k!==null&&(0,S.jsxs)(`div`,{className:`absolute -bottom-2 -right-2 px-2 py-0.5 rounded-full text-xs font-bold text-white shadow-lg`,style:{backgroundColor:A},children:[k,`%`]})]}),(0,S.jsxs)(`div`,{className:`flex-1 min-w-0 pt-1`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`h3`,{className:`text-lg font-semibold text-zinc-100`,children:C||l(`spoolbuddy.spool.unknownColor`)}),(0,S.jsxs)(`span`,{className:`text-xs font-mono text-zinc-500 shrink-0`,children:[`#`,e.id]})]}),(0,S.jsxs)(`p`,{className:`text-sm text-zinc-400`,children:[e.brand,` • `,e.material,e.subtype&&` ${e.subtype}`]}),D!==null&&(0,S.jsxs)(`div`,{className:`mt-3`,children:[(0,S.jsxs)(`div`,{className:`flex items-baseline gap-2`,children:[(0,S.jsxs)(`span`,{className:`text-3xl font-bold font-mono text-zinc-100`,children:[D,`g`]}),(0,S.jsxs)(`span`,{className:`text-sm text-zinc-500`,children:[`/ `,O,`g`]})]}),(0,S.jsx)(`p`,{className:`text-xs text-zinc-500 mt-0.5`,children:l(`spoolbuddy.spool.remaining`,`Remaining`)}),(0,S.jsx)(`div`,{className:`mt-2 max-w-xs`,children:(0,S.jsx)(`div`,{className:`h-2 bg-zinc-700 rounded-full overflow-hidden`,children:(0,S.jsx)(`div`,{className:`h-full rounded-full transition-all duration-500`,style:{width:`${k}%`,backgroundColor:A}})})})]})]})]}),(0,S.jsxs)(`div`,{className:`grid grid-cols-2 gap-x-6 gap-y-2 text-sm bg-zinc-800 rounded-lg p-4 w-full`,children:[(0,S.jsxs)(`div`,{className:`flex justify-between`,children:[(0,S.jsx)(`span`,{className:`text-zinc-500`,children:l(`spoolbuddy.dashboard.grossWeight`,`Gross weight`)}),(0,S.jsx)(`span`,{className:`font-mono text-zinc-300`,children:E===null?`—`:`${E}g`})]}),(0,S.jsxs)(`div`,{className:`flex justify-between`,children:[(0,S.jsx)(`span`,{className:`text-zinc-500`,children:l(`spoolbuddy.spool.coreWeight`,`Core`)}),(0,S.jsxs)(`span`,{className:`font-mono text-zinc-300`,children:[T,`g`]})]}),(0,S.jsxs)(`div`,{className:`flex justify-between`,children:[(0,S.jsx)(`span`,{className:`text-zinc-500`,children:l(`spoolbuddy.dashboard.spoolSize`,`Spool size`)}),(0,S.jsxs)(`span`,{className:`font-mono text-zinc-300`,children:[O,`g`]})]}),(0,S.jsxs)(`div`,{className:`flex justify-between items-center`,children:[(0,S.jsx)(`span`,{className:`text-zinc-500`,children:l(`spoolbuddy.spool.scaleWeight`,`Scale`)}),E===null?(0,S.jsx)(`span`,{className:`text-zinc-500`,children:`—`}):(0,S.jsxs)(`span`,{className:`flex items-center gap-1 font-mono ${M?`text-green-500`:`text-yellow-500`}`,children:[E,`g`,M?(0,S.jsx)(d,{className:`w-3.5 h-3.5`}):(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(p,{className:`w-3.5 h-3.5`}),(0,S.jsx)(`button`,{onClick:N,className:`p-1 hover:bg-green-500/20 rounded transition-colors text-green-500`,title:l(`spoolbuddy.dashboard.syncWeight`,`Sync Weight`),children:(0,S.jsx)(ee,{className:`w-4 h-4`})})]})]})]}),(0,S.jsxs)(`div`,{className:`flex justify-between items-center`,children:[(0,S.jsx)(`span`,{className:`text-zinc-500`,children:l(`spoolbuddy.dashboard.tagId`,`Tag`)}),(0,S.jsx)(`span`,{className:`font-mono text-xs text-zinc-400 truncate max-w-[120px]`,title:e.tag_uid||``,children:e.tag_uid?e.tag_uid.slice(-8):`—`})]})]}),(0,S.jsxs)(`div`,{className:`flex gap-2 justify-center`,children:[i&&(0,S.jsx)(`button`,{onClick:s?void 0:i,disabled:!!s,className:`px-5 py-2.5 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 transition-colors min-h-[44px] disabled:opacity-50 disabled:cursor-not-allowed`,children:l(`spoolbuddy.modal.assignToAms`,`Assign to AMS`)}),c&&(0,S.jsxs)(`button`,{onClick:c,className:`px-5 py-2.5 rounded-lg text-sm font-medium bg-red-600/20 text-red-400 hover:bg-red-600/30 transition-colors min-h-[44px]`,children:[(0,S.jsx)(m,{className:`w-4 h-4 inline mr-1`}),l(`inventory.unassignSpool`)]}),(0,S.jsx)(`button`,{onClick:N,disabled:t===null||u,className:`px-5 py-2.5 rounded-lg text-sm font-medium transition-colors min-h-[44px] ${h?`bg-green-600/20 text-green-400`:i?`bg-zinc-700 text-zinc-300 hover:bg-zinc-600 disabled:opacity-40 disabled:cursor-not-allowed`:`bg-green-600 text-white hover:bg-green-700 disabled:opacity-40 disabled:cursor-not-allowed`}`,children:u?`...`:h?l(`spoolbuddy.dashboard.weightSynced`,`Synced!`):l(`spoolbuddy.dashboard.syncWeight`,`Sync Weight`)}),n&&(0,S.jsx)(`button`,{onClick:n,className:`px-5 py-2.5 rounded-lg text-sm font-medium bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors min-h-[44px]`,children:l(`spoolbuddy.dashboard.close`,`Close`)})]})]})}function E({tagUid:e,scaleWeight:t,coreWeight:n,onLinkSpool:r,onAddToInventory:i,onClose:a}){let{t:s}=o(),c=n??w(),l=t===null?null:Math.round(Math.max(0,t)),u=l===null?null:Math.round(Math.max(0,l-c));return(0,S.jsxs)(`div`,{className:`flex flex-col items-center text-center space-y-5`,children:[(0,S.jsx)(`div`,{className:`w-20 h-20 rounded-2xl bg-green-500/15 flex items-center justify-center`,children:(0,S.jsx)(`svg`,{className:`w-10 h-10 text-green-500`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`2`,d:`M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A2 2 0 013 12V7a4 4 0 014-4z`})})}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h3`,{className:`text-lg font-semibold text-zinc-100`,children:s(`spoolbuddy.dashboard.newTag`,`New Tag Detected`)}),(0,S.jsx)(`p`,{className:`text-sm text-zinc-500 font-mono mt-1`,children:e})]}),l!==null&&(0,S.jsxs)(`div`,{className:`text-sm text-zinc-400`,children:[(0,S.jsxs)(`span`,{className:`font-mono font-semibold`,children:[l,`g`]}),` `,s(`spoolbuddy.dashboard.onScale`,`on scale`),u!==null&&u>0&&(0,S.jsxs)(`span`,{className:`text-zinc-500`,children:[` • ~`,u,`g filament`]})]}),(0,S.jsxs)(`div`,{className:`flex flex-wrap gap-2 justify-center`,children:[i&&(0,S.jsx)(`button`,{onClick:i,className:`px-5 py-2.5 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 transition-colors min-h-[44px]`,children:s(`spoolbuddy.modal.addToInventory`,`Add to Inventory`)}),r&&(0,S.jsxs)(`button`,{onClick:r,className:`px-5 py-2.5 rounded-lg text-sm font-medium bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors min-h-[44px]`,children:[(0,S.jsx)(`svg`,{className:`w-4 h-4 inline-block mr-1.5 -mt-0.5`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`2`,d:`M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1`})}),s(`inventory.assignSpool`,`Assign Spool`)]}),a&&(0,S.jsx)(`button`,{onClick:a,className:`px-5 py-2.5 rounded-lg text-sm font-medium bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors min-h-[44px]`,children:s(`spoolbuddy.dashboard.close`,`Close`)})]})]})}function D({isOpen:e,onClose:t,tagId:n,untaggedSpools:r,onLink:i}){let{t:a}=o(),[s,c]=(0,x.useState)(null),l=(0,x.useCallback)(()=>{c(null),t()},[t]),u=(0,x.useCallback)(e=>{e.key===`Escape`&&l()},[l]);return(0,x.useEffect)(()=>(e&&(document.addEventListener(`keydown`,u),document.body.style.overflow=`hidden`),()=>{document.removeEventListener(`keydown`,u),document.body.style.overflow=``}),[e,u]),e?(0,S.jsx)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center bg-black/70 animate-fade-in`,onClick:l,children:(0,S.jsxs)(`div`,{className:`bg-zinc-800 rounded-xl shadow-2xl w-full max-w-lg mx-4 animate-slide-up`,onClick:e=>e.stopPropagation(),children:[(0,S.jsxs)(`div`,{className:`flex items-center justify-between px-5 py-4 border-b border-zinc-700`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h2`,{className:`text-base font-semibold text-zinc-100`,children:a(`spoolbuddy.dashboard.linkTagTitle`,`Link Tag to Spool`)}),(0,S.jsx)(`p`,{className:`text-sm text-zinc-500 font-mono`,children:n})]}),(0,S.jsx)(`button`,{onClick:l,className:`p-2 rounded-lg text-zinc-500 hover:text-zinc-300 hover:bg-zinc-700 transition-colors`,children:(0,S.jsx)(h,{className:`w-5 h-5`})})]}),(0,S.jsxs)(`div`,{className:`px-5 py-4 space-y-3 max-h-[400px] overflow-y-auto`,children:[(0,S.jsx)(`p`,{className:`text-sm text-zinc-400`,children:a(`spoolbuddy.dashboard.selectSpool`,`Select a spool to link this tag to:`)}),r.length===0?(0,S.jsx)(`div`,{className:`text-center py-8 text-zinc-500`,children:a(`spoolbuddy.dashboard.noUntagged`,`No spools without tags found`)}):(0,S.jsx)(`div`,{className:`space-y-2`,children:r.map(e=>(0,S.jsxs)(`button`,{type:`button`,onClick:()=>c(e),className:`w-full flex items-center gap-3 p-3 rounded-lg border-2 transition-all text-left ${s?.id===e.id?`border-green-500 bg-green-500/10`:`border-zinc-700 hover:border-green-500/50 hover:bg-zinc-700/50`}`,children:[(0,S.jsx)(b,{color:v(e.rgba),spool:e,isEmpty:!1,size:40}),(0,S.jsxs)(`div`,{className:`flex-1 min-w-0`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`div`,{className:`font-medium text-zinc-100 truncate`,children:_(e.color_name,e.rgba,e.color_name_is_synthesized)||a(`spoolbuddy.spool.unknownColor`)}),(0,S.jsxs)(`span`,{className:`text-[10px] font-mono text-zinc-500 shrink-0`,children:[`#`,e.id]})]}),(0,S.jsxs)(`div`,{className:`text-sm text-zinc-400 truncate`,children:[e.brand,` • `,e.material,e.subtype&&` ${e.subtype}`]})]}),(0,S.jsxs)(`div`,{className:`text-sm font-mono text-zinc-500`,children:[Math.max(0,e.label_weight-e.weight_used),`g`]})]},e.id))})]}),(0,S.jsxs)(`div`,{className:`flex justify-end gap-2 px-5 py-4 border-t border-zinc-700`,children:[(0,S.jsx)(`button`,{onClick:l,className:`px-4 py-2.5 rounded-lg text-sm font-medium bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors min-h-[44px]`,children:a(`common.cancel`,`Cancel`)}),(0,S.jsx)(`button`,{onClick:()=>{s&&(i(s),c(null))},disabled:!s,className:`px-4 py-2.5 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 disabled:opacity-40 disabled:cursor-not-allowed transition-colors min-h-[44px]`,children:a(`spoolbuddy.dashboard.linkTag`,`Link Tag`)})]})]})}):null}function O(e){return e?e.replace(/[^0-9a-f]/gi,``).toUpperCase():``}function k(e,t){let n=O(e),r=O(t);return!n||!r?!1:n===r?!0:n.endsWith(r)||r.endsWith(n)}var A=[`#00AE42`,`#FF6B35`,`#3B82F6`,`#EF4444`,`#A855F7`,`#FBBF24`,`#14B8A6`,`#EC4899`,`#F97316`,`#22C55E`];function te(){let{t:e}=o(),[t,n]=(0,x.useState)(0);(0,x.useEffect)(()=>{let e=setInterval(()=>{n(e=>(e+1)%A.length)},5e3);return()=>clearInterval(e)},[]);let r=A[t];return(0,S.jsxs)(`div`,{className:`flex flex-col items-center text-center`,children:[(0,S.jsxs)(`div`,{className:`relative mb-6 flex items-center justify-center`,style:{width:160,height:160},children:[(0,S.jsx)(`div`,{className:`absolute inset-0 flex items-center justify-center pointer-events-none`,children:[0,1].map(e=>(0,S.jsx)(`div`,{className:`absolute rounded-full border spoolbuddy-optimized-ping`,style:{width:80,height:80,borderColor:`${r}4D`,transition:`border-color 140ms linear`,animationDelay:`${e*.8}s`}},e))}),(0,S.jsxs)(`div`,{className:`relative overflow-hidden rounded-full`,children:[(0,S.jsx)(`div`,{className:`absolute inset-0 rounded-full opacity-30 spoolbuddy-spool-glow`,style:{background:`radial-gradient(circle, ${r} 0%, transparent 70%)`}}),(0,S.jsx)(`div`,{className:`relative`,style:{transition:`opacity 140ms linear`},children:(0,S.jsx)(b,{color:r,isEmpty:!1,size:100})})]})]}),(0,S.jsxs)(`div`,{className:`space-y-2`,children:[(0,S.jsx)(`p`,{className:`text-xl font-medium text-zinc-300`,children:e(`spoolbuddy.dashboard.readyToScan`,`Ready to scan`)}),(0,S.jsx)(`p`,{className:`text-sm text-zinc-500`,children:e(`spoolbuddy.dashboard.idleMessage`,`Place a spool on the scale to identify it`)})]}),(0,S.jsxs)(`div`,{className:`mt-6 flex items-center gap-2 text-sm text-zinc-600`,children:[(0,S.jsx)(`svg`,{className:`w-4 h-4`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`2`,d:`M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z`})}),(0,S.jsx)(`span`,{children:e(`spoolbuddy.dashboard.nfcHint`,`NFC tag will be read automatically`)})]})]})}function j(){let{t:e}=o();return(0,S.jsxs)(`div`,{className:`flex flex-col items-center text-center`,children:[(0,S.jsx)(`div`,{className:`relative mb-6 flex items-center justify-center`,style:{width:160,height:160},children:(0,S.jsx)(`div`,{className:`w-24 h-24 rounded-full bg-zinc-800 flex items-center justify-center`,children:(0,S.jsx)(`svg`,{className:`w-12 h-12 text-zinc-600`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`1.5`,d:`M18.364 5.636a9 9 0 010 12.728m0 0l-12.728-12.728m12.728 12.728L5.636 5.636m12.728 0a9 9 0 00-12.728 0m0 12.728a9 9 0 010-12.728`})})})}),(0,S.jsxs)(`div`,{className:`space-y-2`,children:[(0,S.jsx)(`p`,{className:`text-lg font-medium text-zinc-500`,children:e(`spoolbuddy.status.deviceOffline`,`Device Offline`)}),(0,S.jsx)(`p`,{className:`text-sm text-zinc-600`,children:e(`spoolbuddy.status.connectDisplay`,`Connect the SpoolBuddy display to scan spools`)})]}),(0,S.jsxs)(`div`,{className:`mt-6 flex items-center gap-2 text-xs text-zinc-600`,children:[(0,S.jsx)(`svg`,{className:`w-4 h-4`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`2`,d:`M8.111 16.404a5.5 5.5 0 017.778 0M12 20h.01m-7.08-7.071c3.904-3.905 10.236-3.905 14.14 0M1.394 9.393c5.857-5.857 15.355-5.857 21.213 0`})}),(0,S.jsx)(`span`,{children:e(`spoolbuddy.status.waitingConnection`,`Waiting for device connection...`)})]})]})}function M(){let{sbState:e,selectedPrinterId:t}=g(),{t:n}=o(),{showToast:a}=f(),{data:d}=c({queryKey:[`spoolman-settings`],queryFn:r.getSpoolmanSettings,staleTime:300*1e3}),p=d?.spoolman_enabled===`true`&&!!d?.spoolman_url,{data:m=[],refetch:h}=c({queryKey:p?[`spoolman-inventory-spools`]:[`inventory-spools`],queryFn:()=>p?r.getSpoolmanInventorySpools(!1):r.getSpools(!1),enabled:d!==void 0}),{data:ee=[]}=c({queryKey:[`spoolman-slot-assignments`],queryFn:()=>r.getSpoolmanSlotAssignments(),enabled:p,staleTime:3*1e3,refetchInterval:3*1e3,refetchIntervalInBackground:!1}),{data:_=[]}=c({queryKey:[`printers`],queryFn:()=>r.getPrinters()}),v=u({queries:_.map(e=>({queryKey:[`printerStatus`,e.id],queryFn:()=>r.getPrinterStatus(e.id),refetchInterval:1e4,select:e=>({connected:e?.connected,awaiting_plate_clear:e?.awaiting_plate_clear===!0})}))}),b=_.map((e,t)=>({printer:e,pending:v[t]?.data?.awaiting_plate_clear===!0})).filter(e=>e.pending),C=s(),w=l({mutationFn:e=>r.clearPlate(e),onSuccess:(e,t)=>{C.setQueryData([`printerStatus`,t],e=>e&&{...e,awaiting_plate_clear:!1}),a(n(`spoolbuddy.dashboard.plateClearedToast`,`Plate marked as cleared`),`success`)},onError:()=>{a(n(`spoolbuddy.dashboard.plateClearFailed`,`Could not mark plate as cleared`),`error`)}}),O=l({mutationFn:e=>r.unassignSpoolmanSlot(e),onSuccess:()=>{C.invalidateQueries({queryKey:[`spoolman-slot-assignments`]}),C.invalidateQueries({queryKey:[`spoolman-inventory-spools`]})},onError:()=>a(n(`inventory.unassignFailed`,`Failed to unassign spool`),`error`)}),[A,M]=(0,x.useState)(null),[N,P]=(0,x.useState)(null),[F,I]=(0,x.useState)(null),[L,R]=(0,x.useState)(null),[ne,z]=(0,x.useState)(!1),[re,B]=(0,x.useState)(!1),[ie,V]=(0,x.useState)(!1),[ae,oe]=(0,x.useState)(!1),[H,U]=(0,x.useState)(null),W=e.matchedSpool?.tag_uid??e.unknownTagUid??null,G=e.unknownTagUid?e.unknownTrayUuid:null,K=e.weight,q=e.weightStable,J=(0,x.useRef)(null);K===null?J.current=null:(J.current===null||Math.abs(K-J.current)>=3||q)&&(J.current=K);let Y=J.current,X=(0,x.useMemo)(()=>{if(e.matchedSpool?.id){let t=m.find(t=>t.id===e.matchedSpool?.id);if(t)return t}if(!A)return null;let t=m.find(e=>k(e.tag_uid,A));if(t)return t;if(N){let e=m.find(e=>e.tray_uuid?.toUpperCase()===N);if(e)return e}return H||null},[A,N,e.matchedSpool,m,H]),Z=(0,x.useMemo)(()=>{if(X&&!H)return X;let t=e.matchedSpool;return t?{id:t.id,tag_uid:t.tag_uid,material:t.material,subtype:t.subtype,color_name:t.color_name,color_name_is_synthesized:t.color_name_is_synthesized,rgba:t.rgba,brand:t.brand,label_weight:t.label_weight,core_weight:t.core_weight,weight_used:t.weight_used}:null},[X,H,e.matchedSpool]),se=p&&Z!=null?ee.some(e=>e.spoolman_spool_id===Z.id):!1,Q=(0,x.useMemo)(()=>p?m.filter(e=>!e.tag_uid&&!e.tray_uuid&&!e.archived_at):m.filter(e=>!e.tag_uid&&!e.archived_at),[m,p]);(0,x.useEffect)(()=>{if(W){let e=L===W;(A!==null&&A!==W||!e&&A!==W)&&(M(W),P(G),I(null),R(null),U(null)),!e&&K!==null&&q&&I(Math.round(Math.max(0,K)))}else L&&(M(null),P(null),R(null),I(null),U(null))},[W,G,K,q,A,L]);let ce=()=>{R(A)},le=async e=>{if(A)try{if(p){let t=await r.linkTagToSpoolmanSpool(e.id,{tag_uid:A,tray_uuid:N||void 0});if(!t){a(n(`spoolman.linkFailed`),`error`);return}let{id:i,material:o,subtype:s,color_name:c,color_name_is_synthesized:l,rgba:u,brand:d,label_weight:f,core_weight:p,weight_used:m}=t;U({id:i,material:o,subtype:s,color_name:c,color_name_is_synthesized:l,rgba:u,brand:d,label_weight:f,core_weight:p,weight_used:m}),a(n(`spoolman.linkSuccess`),`success`)}else await r.linkTagToSpool(e.id,{tag_uid:A,...N?{tray_uuid:N}:{},tag_type:N?`bambulab`:`generic`,data_origin:`nfc_link`});h()}catch(e){console.error(`Failed to link tag:`,e);let t=e instanceof i&&e.status===409&&e.code===`tag_already_linked`?e.detail?.spool_id:void 0;a(typeof t==`number`?n(`inventory.tagAlreadyLinked`,{id:t}):n(`spoolman.linkFailed`),`error`)}finally{z(!1)}},ue=async()=>{if(A){oe(!0);try{let e=$??F;if(p){let t=await r.createSpoolmanInventorySpool({material:`PLA`,subtype:null,color_name:null,rgba:null,extra_colors:null,effect_type:null,brand:null,label_weight:1e3,weight_used:0,slicer_filament:null,slicer_filament_name:null,nozzle_temp_min:null,nozzle_temp_max:null,note:null,added_full:null,last_used:null,encode_time:null,tag_uid:null,tray_uuid:null,data_origin:null,tag_type:null,cost_per_kg:null,last_scale_weight:e===null?null:Math.round(e),last_weighed_at:e===null?null:new Date().toISOString(),category:null,low_stock_threshold_pct:null});await r.linkTagToSpoolmanSpool(t.id,{tag_uid:A,tray_uuid:N||void 0})}else await r.createSpool({material:`PLA`,subtype:null,color_name:null,rgba:null,extra_colors:null,effect_type:null,brand:null,label_weight:1e3,core_weight:250,core_weight_catalog_id:null,weight_used:0,slicer_filament:null,slicer_filament_name:null,nozzle_temp_min:null,nozzle_temp_max:null,note:null,added_full:null,last_used:null,encode_time:null,tag_uid:A,tray_uuid:N,data_origin:`spoolbuddy`,tag_type:N?`bambulab`:`generic`,cost_per_kg:null,last_scale_weight:e===null?null:Math.round(e),last_weighed_at:e===null?null:new Date().toISOString(),category:null,low_stock_threshold_pct:null,material_number:null})}catch(e){let t=e instanceof Error?e.message:String(e);console.error(`Failed to quick-add spool:`,t),a(t||n(`spoolbuddy.errors.quickAddFailed`,`Failed to add spool`),`error`)}finally{V(!1),oe(!1),h()}}},$=K!==null&&(W===A||W===null&&A!==null)?K:null,de=m.length,fe=new Set(m.map(e=>e.material)).size,pe=new Set(m.filter(e=>e.brand).map(e=>e.brand)).size;return(0,S.jsxs)(`div`,{className:`h-full flex flex-col p-4`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-6 px-4 py-1.5 bg-zinc-800/50 rounded-xl border border-zinc-700/50 mb-3 shrink-0`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`span`,{className:`text-xl font-bold text-zinc-100`,children:de}),(0,S.jsx)(`span`,{className:`text-sm text-zinc-500`,children:n(`spoolbuddy.inventory.spools`,`Spools`)})]}),(0,S.jsx)(`div`,{className:`w-px h-5 bg-zinc-700`}),(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`span`,{className:`text-xl font-bold text-zinc-100`,children:fe}),(0,S.jsx)(`span`,{className:`text-sm text-zinc-500`,children:n(`spoolbuddy.spool.material`,`Materials`)})]}),(0,S.jsx)(`div`,{className:`w-px h-5 bg-zinc-700`}),(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`span`,{className:`text-xl font-bold text-zinc-100`,children:pe}),(0,S.jsx)(`span`,{className:`text-sm text-zinc-500`,children:n(`spoolbuddy.spool.brand`,`Brands`)})]})]}),(0,S.jsxs)(`div`,{className:`flex-1 flex gap-4 min-h-0`,children:[(0,S.jsxs)(`div`,{className:`w-5/12 flex flex-col min-h-0`,children:[(0,S.jsxs)(`div`,{className:`border border-dashed border-zinc-700/50 rounded-xl p-4`,children:[(0,S.jsx)(`h2`,{className:`text-sm font-semibold text-zinc-400 uppercase tracking-wide mb-3`,children:n(`spoolbuddy.dashboard.device`,`Device`)}),(0,S.jsxs)(`div`,{className:`space-y-2.5`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,S.jsx)(`div`,{className:`w-2.5 h-2.5 rounded-full ${e.deviceOnline?`bg-green-500`:`bg-red-500`}`}),(0,S.jsx)(`span`,{className:`text-base text-zinc-400`,children:e.deviceOnline?n(`spoolbuddy.status.online`,`Online`):n(`spoolbuddy.status.offline`,`Disconnected`)})]}),(0,S.jsxs)(`div`,{className:`flex items-center justify-between p-3 bg-zinc-800/50 rounded-lg`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`svg`,{className:`w-4 h-4 ${e.deviceOnline?`text-green-500`:`text-zinc-500`}`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`2`,d:`M3 6l3 1m0 0l-3 9a5.002 5.002 0 006.001 0M6 7l3 9M6 7l6-2m6 2l3-1m-3 1l-3 9a5.002 5.002 0 006.001 0M18 7l3 9m-3-9l-6-2m0-2v2m0 16V5m0 16H9m3 0h3`})}),(0,S.jsx)(`span`,{className:`text-sm text-zinc-500`,children:n(`spoolbuddy.spool.scaleWeight`,`Scale`)})]}),(0,S.jsx)(`span`,{className:`text-lg font-mono font-semibold text-zinc-100`,children:Y===null?`—`:`${Math.abs(Y)<=20?0:Math.round(Math.max(0,Y))}g`})]}),(0,S.jsxs)(`div`,{className:`flex items-center justify-between p-3 bg-zinc-800/50 rounded-lg`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(`svg`,{className:`w-4 h-4 ${e.deviceOnline?`text-green-500`:`text-zinc-500`}`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`2`,d:`M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A2 2 0 013 12V7a4 4 0 014-4z`})}),(0,S.jsx)(`span`,{className:`text-sm text-zinc-500`,children:`NFC`})]}),(0,S.jsx)(`span`,{className:`text-sm font-medium ${W?`text-green-500`:`text-zinc-500`}`,children:W?n(`spoolbuddy.dashboard.tagDetected`,`Tag detected`):n(`spoolbuddy.dashboard.noTag`,`No tag`)})]})]})]}),_.length>0&&(0,S.jsxs)(`div`,{className:`mt-3 border border-dashed border-zinc-700/50 rounded-xl p-4`,children:[(0,S.jsx)(`h2`,{className:`text-sm font-semibold text-zinc-400 uppercase tracking-wide mb-2.5`,children:n(`spoolbuddy.dashboard.printers`,`Printers`)}),(0,S.jsx)(`div`,{className:`flex flex-wrap gap-2 overflow-hidden`,children:_.map((e,t)=>{let n=v[t]?.data?.connected??!1;return(0,S.jsxs)(`div`,{className:`flex items-center gap-1.5 px-2.5 py-1 bg-zinc-800/50 rounded-lg`,title:`${e.name} — ${n?`Online`:`Offline`}`,children:[(0,S.jsx)(`div`,{className:`w-2 h-2 rounded-full shrink-0 ${n?`bg-green-500`:`bg-zinc-600`}`}),(0,S.jsx)(`span`,{className:`text-xs text-zinc-400 truncate max-w-[100px]`,children:e.name})]},e.id)})}),b.length>0&&(0,S.jsx)(`div`,{className:`mt-2 flex flex-wrap gap-2`,"data-testid":`plate-clear-section`,"aria-label":n(`spoolbuddy.dashboard.plateReadyLabel`,`Plates ready to clear`),children:b.map(({printer:e})=>(0,S.jsxs)(`button`,{type:`button`,onClick:()=>w.mutate(e.id),disabled:w.isPending,"data-testid":`plate-clear-button-${e.id}`,title:n(`spoolbuddy.dashboard.plateReady`,`Plate ready: {{name}}`,{name:e.name}),className:`flex items-center gap-1.5 px-2.5 py-1 rounded-lg bg-amber-500/10 hover:bg-amber-500/20 active:bg-amber-500/30 border border-amber-500/30 text-amber-200 transition-colors disabled:opacity-60 disabled:cursor-wait`,children:[(0,S.jsxs)(`svg`,{className:`w-3 h-3 shrink-0`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2.5`,strokeLinecap:`round`,strokeLinejoin:`round`,"aria-hidden":`true`,children:[(0,S.jsx)(`path`,{d:`M12 9v4`}),(0,S.jsx)(`path`,{d:`M12 17h.01`}),(0,S.jsx)(`path`,{d:`M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z`})]}),(0,S.jsx)(`span`,{className:`text-xs truncate max-w-[100px]`,children:e.name}),(0,S.jsx)(`span`,{className:`text-xs opacity-70`,"aria-hidden":`true`,children:`·`}),(0,S.jsx)(`span`,{className:`text-xs font-medium`,children:n(`spoolbuddy.dashboard.plateClearAction`,`Clear`)})]},e.id))})]})]}),(0,S.jsx)(`div`,{className:`w-7/12 min-h-0`,children:(0,S.jsxs)(`div`,{className:`border border-dashed border-zinc-700/50 rounded-xl p-6 h-full flex flex-col`,children:[(0,S.jsx)(`h2`,{className:`text-sm font-semibold text-zinc-400 uppercase tracking-wide mb-4 shrink-0`,children:n(`spoolbuddy.dashboard.currentSpool`,`Current Spool`)}),(0,S.jsx)(`div`,{className:`flex-1 flex items-center justify-center min-h-0`,children:e.deviceOnline?(X||e.matchedSpool)&&A&&L!==A?(0,S.jsx)(T,{spool:(()=>{let t=X??e.matchedSpool;return{id:t.id,tag_uid:A,material:t.material,subtype:t.subtype,color_name:t.color_name,color_name_is_synthesized:t.color_name_is_synthesized,rgba:t.rgba,brand:t.brand,label_weight:t.label_weight,core_weight:t.core_weight,weight_used:t.weight_used}})(),scaleWeight:$??F,onSyncWeight:()=>h(),onAssignToAms:()=>B(!0),isAssigned:se,onUnassignFromAms:se&&X?.id!=null?()=>O.mutate(X.id):void 0,onClose:ce}):W&&A&&!X&&!e.matchedSpool&&L!==A?(0,S.jsx)(E,{tagUid:A,scaleWeight:$??F,onLinkSpool:Q.length>0?()=>z(!0):void 0,onAddToInventory:()=>V(!0),onClose:ce}):(0,S.jsx)(te,{}):(0,S.jsx)(j,{})})]})})]}),Z&&!H&&A&&(0,S.jsx)(y,{isOpen:re,onClose:()=>B(!1),spool:Z,printerId:t,variant:`kiosk`,spoolmanMode:p}),A&&(0,S.jsx)(D,{isOpen:ne,onClose:()=>z(!1),tagId:A,untaggedSpools:Q,onLink:le}),ie&&A&&(0,S.jsx)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center bg-black/60`,children:(0,S.jsxs)(`div`,{className:`bg-zinc-800 rounded-2xl p-6 mx-4 max-w-sm w-full border border-zinc-700`,children:[(0,S.jsx)(`h3`,{className:`text-lg font-semibold text-zinc-100 mb-3`,children:n(`spoolbuddy.modal.addToInventory`,`Add to Inventory`)}),(0,S.jsxs)(`div`,{className:`flex gap-2.5 p-3 bg-amber-500/10 border border-amber-500/20 rounded-lg mb-4`,children:[(0,S.jsx)(`svg`,{className:`w-5 h-5 text-amber-500 shrink-0 mt-0.5`,fill:`none`,viewBox:`0 0 24 24`,stroke:`currentColor`,children:(0,S.jsx)(`path`,{strokeLinecap:`round`,strokeLinejoin:`round`,strokeWidth:`2`,d:`M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z`})}),(0,S.jsx)(`p`,{className:`text-sm text-amber-200/80`,children:n(`spoolbuddy.modal.quickAddHint`,`For best results, add the spool in the Bambuddy web interface first (with material, color, brand), then use "Assign Spool" here to assign the NFC tag.`)})]}),(0,S.jsx)(`p`,{className:`text-sm text-zinc-400 mb-1`,children:n(`spoolbuddy.modal.quickAddDesc`,`This will create a basic PLA spool entry with this NFC tag. You can edit the details later in Bambuddy.`)}),(0,S.jsx)(`p`,{className:`text-xs text-zinc-500 font-mono mb-5`,children:A}),(0,S.jsxs)(`div`,{className:`flex gap-3`,children:[(0,S.jsx)(`button`,{onClick:()=>V(!1),className:`flex-1 px-4 py-2.5 rounded-lg text-sm font-medium bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors min-h-[44px]`,children:n(`common.cancel`,`Cancel`)}),(0,S.jsx)(`button`,{onClick:ue,disabled:ae,className:`flex-1 px-4 py-2.5 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 disabled:opacity-50 transition-colors min-h-[44px]`,children:ae?n(`common.saving`,`Saving...`):n(`spoolbuddy.modal.addAnyway`,`Add Anyway`)})]})]})})]})}export{M as SpoolBuddyDashboard};
|