| 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 ee}from"./arrow-left-MSgMxOFV.js";import{t as c}from"./check-D1Pn8T1J.js";import{n as l}from"./ToastContext-VFOrCCoA.js";import{t as u}from"./loader-circle-CB4leTZa.js";import{t as d}from"./minus-D0aY9wbr.js";import{t as f}from"./search-BpAJdxRw.js";import{t as p}from"./shield-CfkeOiiL.js";import{t as m}from"./triangle-alert-gIik0G2C.js";import{t as h}from"./Card-OUAj0Ayh.js";import{t as g}from"./Button-pwVKPiYn.js";import{Jt as _,Rn as v,Un as y,Ut as b,Vn as x}from"./index-BoVbGTav.js";var S=e(n(),1),C=t();function w(){let{id:e}=y(),t=x(),n=a(),{t:w}=i(),{showToast:T}=l(),E=!!e,[D,O]=(0,S.useState)(``),[k,A]=(0,S.useState)(``),[j,M]=(0,S.useState)([]),[N,P]=(0,S.useState)(``),[F,I]=(0,S.useState)(!1),{data:L,isLoading:R}=o({queryKey:[`group`,e],queryFn:()=>r.getGroup(Number(e)),enabled:E}),{data:z,isLoading:B}=o({queryKey:[`permissions`],queryFn:()=>r.getPermissions()});E&&L&&!F&&(O(L.name),A(L.description||``),M(L.permissions),I(!0));let V=s({mutationFn:e=>r.createGroup(e),onSuccess:()=>{n.invalidateQueries({queryKey:[`groups`]}),n.invalidateQueries({queryKey:[`group`]}),T(w(`groups.toast.created`)),t(`/settings?tab=users`)},onError:e=>{T(e.message,`error`)}}),H=s({mutationFn:t=>r.updateGroup(Number(e),t),onSuccess:r=>{n.invalidateQueries({queryKey:[`groups`]}),r&&n.setQueryData([`group`,e],r),T(w(`groups.toast.updated`)),t(`/settings?tab=users`)},onError:e=>{T(e.message,`error`)}}),U=V.isPending||H.isPending,W=E&&L?.is_system===!0&&L.name===`Administrators`,G=()=>{if(!D.trim()){T(w(`groups.toast.enterGroupName`),`error`);return}if(E){let e=!L||j.length!==L.permissions.length||j.some(e=>!L.permissions.includes(e));H.mutate({name:D===L?.name?void 0:D,description:k,permissions:L?.is_system&&!e?void 0:j})}else V.mutate({name:D,description:k||void 0,permissions:j})},K=e=>{M(t=>t.includes(e)?t.filter(t=>t!==e):[...t,e])},q=(e,t)=>{let n=e.permissions.map(e=>e.value);M(e=>{let r=e.filter(e=>!n.includes(e));return t?[...r,...n]:r})},J=e=>e.permissions.every(e=>j.includes(e.value)),Y=e=>{let t=e.permissions.filter(e=>j.includes(e.value)).length;return t>0&&t<e.permissions.length},X=()=>{z&&M(z.all_permissions)},Z=()=>{M([])},Q=N.toLowerCase(),$=(0,S.useMemo)(()=>z?Q?z.categories.map(e=>({...e,permissions:e.permissions.filter(e=>e.label.toLowerCase().includes(Q))})).filter(e=>e.permissions.length>0):z.categories:[],[z,Q]),te=z?.all_permissions.length??0;return R||B?(0,C.jsx)(`div`,{className:`flex items-center justify-center py-16`,children:(0,C.jsx)(u,{className:`w-8 h-8 text-bambu-green animate-spin`})}):(0,C.jsxs)(`div`,{className:`space-y-6 max-w-5xl mx-auto`,children:[(0,C.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,C.jsx)(`button`,{onClick:()=>t(`/settings?tab=users`),className:`p-2 rounded-lg hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors`,children:(0,C.jsx)(ee,{className:`w-5 h-5`})}),(0,C.jsx)(`h1`,{className:`text-xl font-bold text-white`,children:w(E?`groups.editor.title`:`groups.editor.createTitle`)})]}),E&&L?.is_system&&(0,C.jsxs)(`div`,{className:`flex items-center gap-3 px-4 py-3 rounded-lg bg-yellow-50 dark:bg-yellow-500/10 border border-yellow-300 dark:border-yellow-500/20 text-yellow-700 dark:text-yellow-400 text-sm`,children:[(0,C.jsx)(m,{className:`w-4 h-4 shrink-0`}),w(`groups.form.systemGroupWarning`)]}),(0,C.jsxs)(`div`,{className:`grid grid-cols-1 md:grid-cols-2 gap-4`,children:[(0,C.jsxs)(`div`,{children:[(0,C.jsx)(`label`,{className:`block text-sm font-medium text-white mb-2`,children:w(`groups.form.groupName`)}),(0,C.jsx)(`input`,{type:`text`,value:D,onChange:e=>O(e.target.value),disabled:E&&L?.is_system,className:`w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors disabled:opacity-50`,placeholder:w(`groups.form.groupNamePlaceholder`)})]}),(0,C.jsxs)(`div`,{children:[(0,C.jsx)(`label`,{className:`block text-sm font-medium text-white mb-2`,children:w(`groups.form.description`)}),(0,C.jsx)(`input`,{type:`text`,value:k,onChange:e=>A(e.target.value),className:`w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors`,placeholder:w(`groups.form.descriptionPlaceholder`)})]})]}),(0,C.jsx)(h,{children:(0,C.jsxs)(`div`,{className:`p-4 flex flex-wrap items-center justify-between gap-3`,children:[(0,C.jsxs)(`div`,{className:`flex items-center gap-2 min-w-0`,children:[(0,C.jsx)(_,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,C.jsx)(`span`,{className:`text-white font-medium text-sm`,children:w(`groups.editor.printerAccess`)}),(0,C.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:W?w(`groups.editor.adminsSeeAllPrinters`):E?L?.restrict_printers?w(`groups.editor.printerAccessLimited`,{printers:L.printer_ids.length,locations:L.locations?.length??0}):w(`groups.editor.printerAccessOpen`):w(`groups.editor.printerAccessAfterCreate`)})]}),E&&!W&&(0,C.jsx)(v,{to:`/settings?tab=users&sub=printer-access&group=${e}`,className:`text-sm text-bambu-green hover:underline shrink-0`,children:w(`groups.editor.managePrinterAccess`)})]})}),(0,C.jsxs)(`div`,{className:`flex items-center justify-between flex-wrap gap-3`,children:[(0,C.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,C.jsxs)(`span`,{className:`text-sm text-bambu-gray`,children:[w(`groups.editor.permissionsSelected`,{count:j.length}),` / `,te]}),(0,C.jsx)(g,{size:`sm`,variant:`ghost`,onClick:X,children:w(`groups.editor.selectAll`)}),(0,C.jsx)(g,{size:`sm`,variant:`ghost`,onClick:Z,children:w(`groups.editor.clearAll`)})]}),(0,C.jsxs)(`div`,{className:`relative`,children:[(0,C.jsx)(f,{className:`w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-bambu-gray`}),(0,C.jsx)(`input`,{type:`text`,value:N,onChange:e=>P(e.target.value),placeholder:w(`groups.editor.search`),className:`pl-9 pr-4 py-2 text-sm bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors w-64`})]})]}),$.length===0?(0,C.jsx)(`div`,{className:`text-center py-12 text-bambu-gray`,children:w(`groups.editor.noResults`)}):(0,C.jsx)(`div`,{className:`grid grid-cols-1 md:grid-cols-2 gap-4`,children:$.map(e=>{let t=z.categories.find(t=>t.name===e.name),n=t.permissions.filter(e=>j.includes(e.value)).length,r=t.permissions.length,i=J(t),a=Y(t);return(0,C.jsxs)(h,{children:[(0,C.jsxs)(`div`,{className:`sticky top-0 z-10 flex items-center justify-between px-4 py-3 bg-bambu-dark-secondary border-b border-bambu-dark-tertiary rounded-t-xl`,children:[(0,C.jsxs)(`div`,{className:`flex items-center gap-3`,children:[(0,C.jsxs)(`button`,{type:`button`,onClick:()=>q(t,!i),className:`w-5 h-5 rounded border flex items-center justify-center transition-colors shrink-0 ${i?`bg-bambu-green border-bambu-green`:a?`bg-bambu-green/50 border-bambu-green`:`border-bambu-gray hover:border-white`}`,children:[i&&(0,C.jsx)(c,{className:`w-3 h-3 text-white`}),a&&!i&&(0,C.jsx)(d,{className:`w-3 h-3 text-white`})]}),(0,C.jsx)(p,{className:`w-4 h-4 text-bambu-gray shrink-0`}),(0,C.jsx)(`span`,{className:`text-white font-medium text-sm`,children:e.name})]}),(0,C.jsxs)(`span`,{className:`text-xs text-bambu-gray tabular-nums`,children:[n,`/`,r]})]}),(0,C.jsx)(`div`,{className:`p-3 space-y-1`,children:e.permissions.map(e=>(0,C.jsxs)(`label`,{className:`flex items-center gap-3 px-2 py-1.5 rounded hover:bg-bambu-dark-secondary cursor-pointer`,children:[(0,C.jsx)(`input`,{type:`checkbox`,checked:j.includes(e.value),onChange:()=>K(e.value),className:`w-4 h-4 shrink-0 rounded border-bambu-gray text-bambu-green focus:ring-bambu-green focus:ring-offset-0 bg-bambu-dark-secondary`}),(0,C.jsxs)(`span`,{className:`flex flex-col`,children:[(0,C.jsx)(`span`,{className:`text-sm text-bambu-gray`,children:e.label}),e.value===`websocket:connect`&&(0,C.jsx)(`span`,{className:`text-xs text-bambu-gray/60`,children:w(`groups.editor.websocketHint`)})]})]},e.value))})]},e.name)})}),(0,C.jsx)(`div`,{className:`h-16`}),(0,C.jsxs)(`div`,{className:`fixed bottom-0 left-0 right-0 z-20 px-6 py-3 bg-bambu-dark-secondary border-t border-bambu-dark-tertiary flex items-center justify-center gap-3`,children:[(0,C.jsx)(g,{variant:`secondary`,onClick:()=>t(`/settings?tab=users`),children:w(`common.cancel`)}),(0,C.jsx)(g,{onClick:G,disabled:U||!D.trim(),children:U?(0,C.jsxs)(C.Fragment,{children:[(0,C.jsx)(u,{className:`w-4 h-4 animate-spin`}),w(`common.saving`)]}):(0,C.jsxs)(C.Fragment,{children:[(0,C.jsx)(b,{className:`w-4 h-4`}),w(`common.save`)]})})]})]})}export{w as GroupEditPage};
|