| 1 |
- import{i as e}from"./chunk-aKtaBQYM.js";import{C as t,D as n,a as r,w as i}from"./client-BdOyoZJz.js";import{n as a}from"./QueryClientProvider-Clbyr4Jg.js";import{t as o}from"./useQuery-CCy0EwsM.js";import{t as s}from"./useMutation-qh3Iyscy.js";import{t as c}from"./createLucideIcon-Yo2YoYjm.js";import{n as l}from"./ToastContext-VFOrCCoA.js";import{t as u}from"./loader-circle-CB4leTZa.js";import{t as d}from"./x-Dctff5l9.js";import{t as f}from"./Button-pwVKPiYn.js";import{$t as p,At as m,Zt as h,_t as g,o as _,r as v}from"./index-BoVbGTav.js";var y=c(`map-pin`,[[`path`,{d:`M20 10c0 4.993-5.539 10.193-7.399 11.799a1 1 0 0 1-1.202 0C9.539 20.193 4 14.993 4 10a8 8 0 0 1 16 0`,key:`1r0f0z`}],[`circle`,{cx:`12`,cy:`10`,r:`3`,key:`ilqhr7`}]]),b=c(`pen`,[[`path`,{d:`M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z`,key:`1a8usu`}]]),x=e(n(),1),S=t();function C({open:e,onClose:t,onPickLocation:n,startCreating:c}){let{t:b}=i(),C=a(),{showToast:w}=l(),[T,E]=(0,x.useState)(!1),[D,O]=(0,x.useState)(null),[k,A]=(0,x.useState)(``),[j,M]=(0,x.useState)(null),{data:N=[],isLoading:P}=o({queryKey:_,queryFn:r.getLocations,enabled:e}),{data:F=[]}=o({queryKey:[`locationHaSensors`],queryFn:()=>r.getLocationHASensors(),enabled:e}),I=F.reduce((e,t)=>(e[t.location_id]=(e[t.location_id]??0)+1,e),{}),L=()=>{v(C),C.invalidateQueries({queryKey:[`inventory-spools`]}),C.invalidateQueries({queryKey:[`spoolman-inventory-spools`]}),C.invalidateQueries({queryKey:[`locationHaSensors`]}),C.invalidateQueries({queryKey:[`locationHaSensorReadings`]})},R=s({mutationFn:async()=>{let e=k.trim();if(!e)throw Error(b(`locations.nameRequired`));return D?r.updateLocation(D.id,{name:e}):r.createLocation({name:e})},onSuccess:e=>{if(w(b(D?`locations.updated`:`locations.created`),`success`),L(),!D&&c){n?.(e.id),t();return}E(!1),O(null),A(``)},onError:e=>{w(e.message||b(`locations.saveFailed`),`error`)}}),z=s({mutationFn:e=>r.deleteLocation(e),onSuccess:()=>{w(b(`locations.deleted`),`success`),M(null),L()},onError:e=>{w(e.message||b(`locations.deleteFailed`),`error`)}}),B=()=>{O(null),A(``),E(!0)};(0,x.useEffect)(()=>{e&&c&&(O(null),A(``),E(!0))},[e,c]);let V=e=>{O(e),A(e.name),E(!0)},H=(0,x.useCallback)(()=>{if(!R.isPending){if(c){t();return}E(!1),O(null),A(``)}},[R.isPending,c,t]);(0,x.useEffect)(()=>{if(!e)return;let n=e=>{e.key===`Escape`&&(R.isPending||z.isPending||(T?H():j||t()))};return document.addEventListener(`keydown`,n),()=>document.removeEventListener(`keydown`,n)},[e,T,j,R.isPending,z.isPending,H,t]);let U=e=>{e.preventDefault(),R.mutate()};if(!e)return null;let W=`locations-modal-title`,G=`location-editor-title`,K=(0,S.jsxs)(`form`,{onSubmit:U,children:[(0,S.jsx)(`label`,{className:`block text-sm font-medium text-bambu-gray mb-1`,htmlFor:`location-name`,children:b(`locations.name`)}),(0,S.jsx)(`input`,{id:`location-name`,type:`text`,maxLength:255,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 mb-4`,placeholder:b(`locations.createPlaceholder`),value:k,onChange:e=>A(e.target.value),autoFocus:!0}),(0,S.jsxs)(`div`,{className:`flex justify-end gap-2`,children:[(0,S.jsx)(f,{type:`button`,variant:`secondary`,onClick:H,children:b(`common.cancel`)}),(0,S.jsxs)(f,{type:`submit`,disabled:R.isPending||!k.trim(),children:[R.isPending&&(0,S.jsx)(u,{className:`w-4 h-4 animate-spin`}),b(`common.save`)]})]})]});return c?T?(0,S.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,S.jsx)(`div`,{className:`absolute inset-0 bg-black/60`,onClick:H}),(0,S.jsxs)(`div`,{className:`relative w-full max-w-md mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl p-6 shadow-2xl`,role:`dialog`,"aria-modal":`true`,"aria-labelledby":G,children:[(0,S.jsx)(`h3`,{id:G,className:`text-lg font-semibold text-white mb-4`,children:b(`locations.add`)}),K]})]}):null:(0,S.jsxs)(`div`,{className:`fixed inset-0 z-50 flex items-center justify-center`,children:[(0,S.jsx)(`div`,{className:`absolute inset-0 bg-black/60`,onClick:()=>{R.isPending||z.isPending||t()}}),(0,S.jsxs)(`div`,{className:`relative w-full max-w-2xl mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl shadow-2xl max-h-[90vh] flex flex-col`,role:`dialog`,"aria-modal":`true`,"aria-labelledby":W,children:[(0,S.jsxs)(`div`,{className:`flex items-center justify-between gap-4 px-6 py-4 border-b border-bambu-dark-tertiary`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsxs)(`h2`,{id:W,className:`text-lg font-semibold text-white flex items-center gap-2`,children:[(0,S.jsx)(y,{className:`w-5 h-5 text-bambu-green`}),b(`locations.title`)]}),(0,S.jsx)(`p`,{className:`text-bambu-gray text-sm mt-0.5`,children:b(`locations.subtitle`)})]}),(0,S.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,S.jsxs)(f,{onClick:B,children:[(0,S.jsx)(h,{className:`w-4 h-4`}),b(`locations.add`)]}),(0,S.jsx)(`button`,{type:`button`,className:`p-1.5 text-bambu-gray hover:text-white rounded`,onClick:t,"aria-label":b(`common.close`),children:(0,S.jsx)(d,{className:`w-5 h-5`})})]})]}),(0,S.jsx)(`div`,{className:`overflow-y-auto`,children:P?(0,S.jsxs)(`div`,{className:`flex items-center justify-center py-16 text-bambu-gray`,children:[(0,S.jsx)(u,{className:`w-6 h-6 animate-spin mr-2`}),b(`common.loading`)]}):N.length===0?(0,S.jsx)(`div`,{className:`py-16 text-center text-bambu-gray`,children:b(`locations.empty`)}):(0,S.jsxs)(`table`,{className:`w-full text-sm table-fixed`,children:[(0,S.jsx)(`thead`,{children:(0,S.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary text-left text-bambu-gray`,children:[(0,S.jsx)(`th`,{className:`px-4 py-3 font-medium`,children:b(`locations.name`)}),(0,S.jsx)(`th`,{className:`px-2 py-3 font-medium text-right w-24`,children:b(`locations.sensors`)}),(0,S.jsx)(`th`,{className:`pl-[28px] pr-4 py-3 font-medium text-right w-28`,children:b(`locations.spools`)}),(0,S.jsx)(`th`,{className:`px-4 py-3 font-medium text-right w-32`,children:b(`common.actions`)})]})}),(0,S.jsx)(`tbody`,{children:N.map(e=>(0,S.jsxs)(`tr`,{className:`border-b border-bambu-dark-tertiary/60 hover:bg-bambu-dark-tertiary/30 cursor-pointer`,onClick:()=>{n&&(n(e.id),t())},children:[(0,S.jsx)(`td`,{className:`px-4 py-3 text-white font-medium truncate`,children:e.name}),(0,S.jsx)(`td`,{className:`px-2 py-3 text-right text-bambu-gray`,children:I[e.id]??0}),(0,S.jsx)(`td`,{className:`pl-[28px] pr-4 py-3 text-right text-bambu-gray`,children:e.spool_count}),(0,S.jsx)(`td`,{className:`px-4 py-3 text-right`,onClick:e=>e.stopPropagation(),children:(0,S.jsxs)(`div`,{className:`flex items-center justify-end gap-1`,children:[(0,S.jsx)(`button`,{type:`button`,className:`p-1.5 text-bambu-gray hover:text-bambu-green rounded`,onClick:()=>V(e),title:b(`common.edit`),"aria-label":b(`locations.editAria`,{name:e.name,defaultValue:`Edit ${e.name}`}),children:(0,S.jsx)(p,{className:`w-4 h-4`})}),(0,S.jsx)(`button`,{type:`button`,className:`p-1.5 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 rounded disabled:opacity-40`,disabled:e.spool_count>0,onClick:()=>M(e),title:e.spool_count>0?b(`locations.deleteBlocked`):b(`common.delete`),"aria-label":b(`locations.deleteAria`,{name:e.name,defaultValue:`Delete ${e.name}`}),children:(0,S.jsx)(m,{className:`w-4 h-4`})})]})})]},e.id))})]})})]}),T&&(0,S.jsxs)(`div`,{className:`fixed inset-0 z-[60] flex items-center justify-center`,children:[(0,S.jsx)(`div`,{className:`absolute inset-0 bg-black/60`,onClick:H}),(0,S.jsxs)(`div`,{className:`relative w-full max-w-md mx-4 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-xl p-6 shadow-2xl`,role:`dialog`,"aria-modal":`true`,"aria-labelledby":G,children:[(0,S.jsx)(`h3`,{id:G,className:`text-lg font-semibold text-white mb-4`,children:b(D?`locations.edit`:`locations.add`)}),K]})]}),j&&(0,S.jsx)(g,{title:b(`locations.confirmDelete`,{name:j.name}),message:I[j.id]?b(`locations.confirmDeleteMessageWithSensors`):b(`locations.confirmDeleteMessage`),confirmText:b(`common.delete`),variant:`danger`,isLoading:z.isPending,onConfirm:()=>z.mutate(j.id),onCancel:()=>M(null)})]})}var w=`bambuddy-location-sensor-show-on-card-defaults`,T={alertAbove:``,alertBelow:``,notifyOnAlert:!1,showOnCard:!0};function E(){return{temperature:{...T,alertAbove:`30`,alertBelow:`20`},humidity:{...T,alertAbove:`30`,alertBelow:`10`},battery:{...T,alertBelow:`10`}}}function D(){try{let e=localStorage.getItem(w);return e?JSON.parse(e):{}}catch{return{}}}function O(e){let t=E();if(e)try{let n=JSON.parse(e);Object.keys(t).forEach(e=>{let r=n[e];r&&(typeof r.alertAbove==`string`&&(t[e].alertAbove=r.alertAbove),typeof r.alertBelow==`string`&&(t[e].alertBelow=r.alertBelow),typeof r.notifyOnAlert==`boolean`&&(t[e].notifyOnAlert=r.notifyOnAlert))})}catch{}let n=D();return Object.keys(t).forEach(e=>{typeof n[e]==`boolean`&&(t[e].showOnCard=n[e])}),t}function k(e){let t={};return Object.keys(e).forEach(n=>{t[n]={alertAbove:e[n].alertAbove,alertBelow:e[n].alertBelow,notifyOnAlert:e[n].notifyOnAlert}}),JSON.stringify(t)}function A(e){try{let t={};Object.keys(e).forEach(n=>{t[n]=e[n].showOnCard}),localStorage.setItem(w,JSON.stringify(t))}catch{return}}var j=`bambuddy-location-sensor-colorize-values`;function M(){try{let e=localStorage.getItem(j);return e?e===`true`:!0}catch{return!0}}var N=`bambuddy:location-sensor-color-prefs-changed`;function P(){window.dispatchEvent(new Event(N))}function F(e){try{localStorage.setItem(j,String(e))}catch{return}P()}var I=[`red`,`orange`,`yellow`,`green`,`blue`,`purple`,`pink`],L={red:`text-red-400`,orange:`text-orange-400`,yellow:`text-yellow-400`,green:`text-green-400`,blue:`text-blue-400`,purple:`text-purple-400`,pink:`text-pink-400`},R=`bambuddy-location-sensor-alert-above-color`,z=`bambuddy-location-sensor-alert-below-color`,B=`bambuddy-location-sensor-alert-optimal-color`;function V(e){return!!e&&I.includes(e)}function H(){try{let e=localStorage.getItem(R);return V(e)?e:`purple`}catch{return`purple`}}function U(e){try{localStorage.setItem(R,e)}catch{return}P()}function W(){try{let e=localStorage.getItem(z);return V(e)?e:`red`}catch{return`red`}}function G(e){try{localStorage.setItem(z,e)}catch{return}P()}function K(){try{let e=localStorage.getItem(B);return V(e)?e:`green`}catch{return`green`}}function q(e){try{localStorage.setItem(B,e)}catch{return}P()}function J(){return{colorize:M(),aboveColor:H(),belowColor:W(),optimalColor:K()}}function Y(){let[e,t]=(0,x.useState)(J);return(0,x.useEffect)(()=>{let e=()=>t(J());return window.addEventListener(N,e),()=>window.removeEventListener(N,e)},[]),e}function X(e){return!e.reachable||e.state===null?null:e.kind===`numeric`?e.alert_above===null&&e.alert_below===null||e.value===null?null:e.alert_above!==null&&e.value>e.alert_above?`above`:e.alert_below!==null&&e.value<e.alert_below?`below`:`ok`:e.alert_state===null?null:e.state.toLowerCase()===e.alert_state?`above`:`ok`}function Z(e,t,n,r){return e===`above`?L[t]:e===`below`?L[n]:e===`ok`?L[r]:``}export{b as _,M as a,Z as c,q as d,F as f,C as g,Y as h,K as i,U as l,k as m,H as n,O as o,A as p,W as r,X as s,I as t,G as u,y as v};
|