spoolCost-CP8is3c8.js 12 KB

1
  1. import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{T as t,a as n,k as r,t as i,w as a}from"./client-Bol9xAus.js";import{n as ee}from"./QueryClientProvider-CPZTuCm3.js";import{t as o}from"./useQuery-VntlVx44.js";import{t as te}from"./useMutation-Bln19oAR.js";import{t as ne}from"./arrow-left-Ck_Er4Hk.js";import{n as re}from"./ToastContext-Ba_la89E.js";import{t as s}from"./cpu-WpheLTUs.js";import{t as c}from"./loader-circle-DCtnN61i.js";import{t as l}from"./map-pin-B0EgY-5e.js";import{t as u}from"./qr-code-Dw96BeEF.js";import{t as d}from"./x-CDFhisoK.js";import{t as f}from"./Button-D7wJJlop.js";import{B as p,G as ie,M as m,_ as ae,y as h,z as g}from"./amsHelpers-m0tbln8b.js";import{a as _}from"./colors-D_-ITfUn.js";import{n as oe}from"./useUiSettings-hktWVJHa.js";import{i as v,s as se}from"./index-Cjhk_gRM.js";import{a as ce,i as y,o as le,r as ue,s as b}from"./inventorySearch-BGQerUtC.js";var x=e(r(),1),S=a();function de({tray:e}){let{t:n}=t(),r=h(e);return r===`physical`?(0,S.jsx)(`span`,{className:`text-[10px] font-normal text-bambu-gray`,children:n(`ams.slotEmpty`)}):r===`reset`||!e?(0,S.jsx)(`span`,{className:`text-xs font-normal text-bambu-gray`,children:n(`ams.slotUnconfigured`)}):(0,S.jsxs)(`span`,{className:`flex flex-col items-center gap-0.5 max-w-full`,children:[(0,S.jsxs)(`span`,{className:`flex items-center gap-1 max-w-full`,children:[e.tray_color&&(0,S.jsx)(`span`,{className:`w-2.5 h-2.5 shrink-0 rounded-full border border-bambu-gray/50`,style:_(e.tray_color)}),(0,S.jsx)(`span`,{className:`text-[10px] font-normal text-white truncate`,children:e.tray_type})]}),e.remain>=0&&(0,S.jsxs)(S.Fragment,{children:[` `,(0,S.jsxs)(`span`,{className:`text-[10px] font-normal text-bambu-gray`,children:[e.remain,`%`]})]})]})}var fe=[{key:`ams`,Icon:s,labelKey:`inventory.qrAssign.tabAms`},{key:`storage`,Icon:l,labelKey:`inventory.qrAssign.tabStorage`}];function C(e){return e.kind===`ams`?e.printerName?`${e.printerName} · ${e.label}`:e.label:e.locationName}function pe(e,t,n,r=!1){return r?m(p(n)):e(t>1?n===0?`printers.extL`:`printers.extR`:`printers.external`)}function w({isOpen:e,onClose:r,spoolmanMode:a=!1}){let{t:s}=t(),{showToast:l}=re(),p=ee(),[m,h]=(0,x.useState)(`target`),[_,w]=(0,x.useState)(`ams`),[T,E]=(0,x.useState)(null),[D,O]=(0,x.useState)(null),[k,A]=(0,x.useState)(null),[j,M]=(0,x.useState)(``),[N,P]=(0,x.useState)(!1),[F,I]=(0,x.useState)(null),[L,R]=(0,x.useState)(null),[z,B]=(0,x.useState)(!1),[V,H]=(0,x.useState)(null),U=(0,x.useRef)(!1),W=(0,x.useRef)(0);(0,x.useEffect)(()=>(e&&(h(`target`),O(null),A(null),M(``),P(!1),I(null),R(null),B(!1),H(null),U.current=!1),()=>{W.current+=1}),[e]);let{data:me}=oe({enabled:e}),{data:G,isLoading:he}=o({queryKey:[`printers`],queryFn:()=>n.getPrinters(),enabled:e}),{data:K=[],isLoading:q}=o({queryKey:se,queryFn:n.getLocations,enabled:e&&m===`target`}),J=T??G?.[0]?.id??null,{data:Y,isLoading:ge}=o({queryKey:[`printerStatus`,J],queryFn:()=>n.getPrinterStatus(J),enabled:e&&m===`target`&&_===`ams`&&J!==null}),X=(0,x.useMemo)(()=>{if(!Y)return[];let e=[];for(let t of Y.ams??[])for(let n of t.tray??[])e.push({amsId:t.id,trayId:n.id,isHt:t.is_ams_ht,isExternal:!1,label:ae(t.id,n.id,t.is_ams_ht,!1,t.ams_type),tray:n});let t=Y.vt_tray??[],n=ie(Y.ams,t).rack;for(let r of t){let i=g(r.id??254);e.push({amsId:255,trayId:i,isHt:!1,isExternal:!0,label:pe(s,t.length,i,n),tray:r})}return e},[Y,s]),Z=te({mutationFn:async({spoolId:e,tg:t})=>t.kind===`ams`?a?(await n.assignSpoolmanSlot({spoolman_spool_id:e,printer_id:t.printerId,ams_id:t.amsId,tray_id:t.trayId,move:!0}),{pendingConfig:!1}):{pendingConfig:!!(await n.assignSpool({spool_id:e,printer_id:t.printerId,ams_id:t.amsId,tray_id:t.trayId,move:!0})).pending_config}:(a?await n.updateSpoolmanInventorySpool(e,{location_id:t.locationId}):await n.updateSpool(e,{location_id:t.locationId}),{pendingConfig:!1}),onSuccess:({pendingConfig:e},{tg:t})=>{l(e?s(`inventory.assignPendingInsert`):s(t.kind===`ams`?`inventory.qrAssign.assignSuccess`:`inventory.qrAssign.storageSuccess`,{location:C(t)}),`success`),p.invalidateQueries({queryKey:[`spool-assignments`]}),p.invalidateQueries({queryKey:a?[`spoolman-inventory-spools`]:[`inventory-spools`]}),p.invalidateQueries({queryKey:[`spoolman-slot-assignments`]}),t.kind===`ams`?(n.refreshPrinterStatus(t.printerId).catch(()=>{}),p.invalidateQueries({queryKey:[`printerStatus`,t.printerId]}),a&&p.invalidateQueries({queryKey:[`linked-spools`]})):v(p),r()},onError:(e,{tg:t})=>{U.current=!1,R(`${s(`inventory.qrAssign.assignFailed`,{location:C(t)})}: ${e.message}`)}});if(!e)return null;let _e=G?.find(e=>e.id===J)?.name??``,ve=_===`ams`?D!==null&&J!==null:k!==null,Q=Z.isPending||z||V!==null,$=k&&!K.some(e=>e.id===k.id)?[...K,k]:K,ye=async()=>{let e=j.trim();if(!(!e||N)){P(!0);try{let t=await n.createLocation({name:e});v(p),A({id:t.id,name:t.name}),M(``)}catch(e){l(e instanceof Error?e.message:s(`locations.saveFailed`),`error`)}finally{P(!1)}}},be=()=>{let e;if(_===`ams`){if(!D||J===null)return;e={kind:`ams`,printerId:J,printerName:_e,amsId:D.amsId,trayId:D.trayId,isExternal:D.isExternal,label:D.label,filament:{type:D.tray?.tray_type||``,profile:D.tray?.tray_sub_brands||void 0}}}else{if(!k)return;e={kind:`storage`,locationId:k.id,locationName:k.name}}I(e),R(null),h(`scan`)},xe=async(e,t)=>{let r=W.current;B(!0);try{let i=await n.getSpool(e);if(W.current!==r)return;if(i.archived_at){R(s(`inventory.spoolIdArchived`,{id:e})),U.current=!1;return}let a=me?.disable_filament_warnings?null:b(i,t.filament);if(a){H({spoolId:e,tg:t,details:a});return}Z.mutate({spoolId:e,tg:t})}catch(t){if(W.current!==r)return;R(t instanceof i&&t.status===404?s(`inventory.spoolIdNotFound`,{id:e}):t instanceof Error?t.message:s(`inventory.assignFailed`)),U.current=!1}finally{B(!1)}};return(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`div`,{className:`fixed inset-0 z-[100] flex items-start sm:items-center justify-center p-4 overflow-y-auto`,children:[(0,S.jsx)(`div`,{className:`absolute inset-0 bg-black/60 backdrop-blur-sm`,onClick:r}),(0,S.jsxs)(`div`,{className:`relative w-full max-w-lg bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] overflow-hidden flex flex-col my-auto`,children:[(0,S.jsxs)(`div`,{className:`flex items-center justify-between p-4 border-b border-bambu-dark-tertiary`,children:[(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsx)(u,{className:`w-5 h-5 text-bambu-green`}),(0,S.jsx)(`h2`,{className:`text-lg font-semibold text-white`,children:s(m===`target`?`inventory.qrAssign.title`:`inventory.qrAssign.scanTitle`)})]}),(0,S.jsx)(`button`,{onClick:r,className:`p-1 text-bambu-gray hover:text-white rounded transition-colors`,children:(0,S.jsx)(d,{className:`w-5 h-5`})})]}),(0,S.jsx)(`div`,{className:`p-4 space-y-4 overflow-y-auto`,children:m===`target`?(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`p`,{className:`text-sm text-bambu-gray`,children:s(`inventory.qrAssign.description`)}),(0,S.jsx)(`div`,{className:`flex gap-2`,children:fe.map(({key:e,Icon:t,labelKey:n})=>(0,S.jsxs)(`button`,{onClick:()=>w(e),className:`flex-1 flex items-center justify-center gap-2 py-2 rounded-lg text-sm font-medium border transition-colors ${_===e?`bg-bambu-green/20 border-bambu-green text-bambu-green`:`bg-bambu-dark border-bambu-dark-tertiary text-bambu-gray hover:border-bambu-gray`}`,children:[(0,S.jsx)(t,{className:`w-4 h-4`}),s(n)]},e))}),_===`ams`?(0,S.jsxs)(`div`,{className:`space-y-3`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`label`,{htmlFor:`qr-printer-select`,className:`block text-xs font-medium text-bambu-gray uppercase tracking-wide mb-1`,children:s(`inventory.qrAssign.printer`)}),(0,S.jsx)(`select`,{id:`qr-printer-select`,value:J??``,onChange:e=>{E(Number(e.target.value)),O(null)},disabled:he||!G?.length,className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,children:G?.map(e=>(0,S.jsx)(`option`,{value:e.id,children:e.name},e.id))})]}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`span`,{className:`block text-xs font-medium text-bambu-gray uppercase tracking-wide mb-1`,children:s(`inventory.qrAssign.selectSlot`)}),ge?(0,S.jsx)(`div`,{className:`flex justify-center py-6`,children:(0,S.jsx)(c,{className:`w-6 h-6 text-bambu-green animate-spin`})}):X.length>0?(0,S.jsx)(`div`,{className:`grid grid-cols-4 gap-2`,children:X.map(e=>(0,S.jsxs)(`button`,{onClick:()=>O(e),className:`aspect-square min-w-0 px-1 flex flex-col items-center justify-center gap-1 rounded-lg border text-sm font-semibold transition-colors ${D?.amsId===e.amsId&&D?.trayId===e.trayId?`bg-bambu-green/20 border-bambu-green text-white`:`bg-bambu-dark border-bambu-dark-tertiary text-bambu-gray hover:border-bambu-gray`}`,children:[(0,S.jsx)(`span`,{children:e.label}),` `,(0,S.jsx)(de,{tray:e.tray})]},`${e.amsId}-${e.trayId}`))}):(0,S.jsx)(`p`,{className:`text-center py-6 text-sm text-bambu-gray`,children:s(`inventory.qrAssign.noSlots`)})]})]}):(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`label`,{htmlFor:`qr-storage-select`,className:`block text-xs font-medium text-bambu-gray uppercase tracking-wide mb-1`,children:s(`inventory.qrAssign.storageLabel`)}),q?(0,S.jsx)(`div`,{className:`flex justify-center py-6`,children:(0,S.jsx)(c,{className:`w-6 h-6 text-bambu-green animate-spin`})}):(0,S.jsxs)(`select`,{id:`qr-storage-select`,value:k?.id??``,onChange:e=>{let t=e.target.value?Number(e.target.value):null,n=t===null?null:$.find(e=>e.id===t);A(n?{id:n.id,name:n.name}:null)},className:`w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:outline-none focus:border-bambu-green`,children:[(0,S.jsx)(`option`,{value:``,children:s(`inventory.qrAssign.storagePick`)}),$.map(e=>(0,S.jsx)(`option`,{value:e.id,children:e.name},e.id))]}),!q&&$.length===0&&(0,S.jsx)(`p`,{className:`mt-2 text-xs text-bambu-gray`,children:s(`locations.empty`)}),(0,S.jsxs)(`div`,{className:`mt-2 flex gap-2`,children:[(0,S.jsx)(`input`,{type:`text`,maxLength:255,value:j,onChange:e=>M(e.target.value),placeholder:s(`locations.createPlaceholder`),className:`flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm placeholder:text-bambu-gray/50 focus:outline-none focus:border-bambu-green`}),(0,S.jsx)(`button`,{type:`button`,onClick:ye,disabled:!j.trim()||N,className:`px-3 py-2 text-sm rounded-lg bg-bambu-dark-tertiary text-white hover:bg-bambu-gray-dark disabled:opacity-50`,children:N?(0,S.jsx)(c,{className:`w-4 h-4 animate-spin`}):s(`locations.addShort`)})]})]})]}):(0,S.jsx)(ce,{onDecode:e=>{if(U.current||Q||!F)return;let t=ue(e);if(!t){R(s(`inventory.qrAssign.invalidQr`));return}let n=y(t,a);if(n===null){R(s(`inventory.qrAssign.spoolmanLabel`));return}if(R(null),U.current=!0,F.kind===`ams`&&!a){xe(n,F);return}Z.mutate({spoolId:n,tg:F})},busy:Q,error:L,targetLabel:F?C(F):void 0})}),(0,S.jsxs)(`div`,{className:`flex justify-between gap-2 p-4 border-t border-bambu-dark-tertiary`,children:[m===`scan`?(0,S.jsxs)(f,{variant:`secondary`,onClick:()=>h(`target`),disabled:Q,children:[(0,S.jsx)(ne,{className:`w-4 h-4`}),s(`inventory.qrAssign.back`)]}):(0,S.jsx)(`span`,{}),(0,S.jsxs)(`div`,{className:`flex gap-2`,children:[(0,S.jsx)(f,{variant:`secondary`,onClick:r,children:s(`common.cancel`)}),m===`target`&&(0,S.jsxs)(f,{onClick:be,disabled:!ve,children:[(0,S.jsx)(u,{className:`w-4 h-4`}),s(`inventory.qrAssign.startScan`)]})]})]})]})]}),V&&(0,S.jsx)(le,{mismatch:V.details,location:C(V.tg),onConfirm:()=>{V&&(H(null),Z.mutate({spoolId:V.spoolId,tg:V.tg}))},onCancel:()=>{H(null),U.current=!1}})]})}function T(e){let t=e.cost_per_kg;if(t!=null&&t>0)return{rate:t,fromFilament:!1};let n=e.effective_cost_per_kg;return n==null?{rate:t??null,fromFilament:!1}:{rate:n,fromFilament:!0}}export{w as n,T as t};