spoolCost-CoxrGS9D.js 12 KB

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