1import{r,j as e,a9 as ie,aa as te,d as l,ab as re}from"./app-BLMRy3ND.js";import{s as n}from"./client-DFcKuRWt.js";import{u as le}from"./AuthContext-BLjZ6q2b.js";import{R as ne}from"./RoleGuard-DxvMoiFA.js";import{B as h}from"./button-eTpDmruB.js";import{I as S}from"./input-BXPdIWBw.js";import{L as u}from"./label-D4ROs2jv.js";import{S as L,a as H,b as O,c as M,d as V}from"./select-Teaj6WmT.js";import{T as oe,a as ce,b as D,c as v,d as de,e as x}from"./table-Cty9tooR.js";import{B as Y}from"./badge-CSJPku74.js";import{S as G}from"./switch-CZzaml8N.js";import{D as me,a as he,b as ue,c as xe,d as pe,f as je}from"./dialog-BL_qyA11.js";import{A as fe,b as ve,c as ge,d as _e,e as we,f as ye,g as Ne,h as Ce}from"./alert-dialog-B7TBJZ7X.js";import{P as be}from"./plus-D2M3Dc6D.js";import{L as g}from"./loader-circle-7wASCDvj.js";import{R as Se}from"./refresh-cw-r4jsczZC.js";import{T as De}from"./trash-2-55dD_Ryc.js";import"./index-BdQq_4o_.js";import"./index-CRUKlXD4.js";import"./index-CZRbC-mx.js";import"./index-DqazF8Fx.js";import"./chevron-down-BSv1l6bf.js";import"./check-BZE7qzTX.js";import"./index-ExqC9qOE.js";function Ze(){const{role:J,user:t}=le(),[K,Q]=r.useState([]),[W,_]=r.useState(!1),[X,p]=r.useState(!1),[w,T]=r.useState(""),[y,A]=r.useState(""),[I,R]=r.useState("author"),[j,k]=r.useState(!0),[N,U]=r.useState(""),[E,P]=r.useState(!1),[o,C]=r.useState(null),[F,b]=r.useState(!1),[q,z]=r.useState(null),m=async()=>{_(!0);const{data:s}=await n.from("user_roles").select("id, user_id, role");if(!s){_(!1);return}const a=s.map(c=>c.user_id),{data:i}=await n.from("profiles").select("id, full_name, is_active").in("id",a),f=s.map(c=>{const d=i==null?void 0:i.find(ae=>ae.id===c.user_id);return{id:c.user_id,full_name:(d==null?void 0:d.full_name)??"Unknown",role:c.role,role_id:c.id,is_active:(d==null?void 0:d.is_active)??!0}});Q(f),_(!1)};r.useEffect(()=>{t&&m()},[t]);const Z=async(s,a,i)=>{const{error:f}=await n.from("user_roles").update({role:i}).eq("id",s);f?l.error(f.message):(l.success("Role updated"),await n.from("audit_logs").insert({action:"role_changed",entity_type:"user_role",entity_id:a,user_id:t==null?void 0:t.id,metadata:{new_role:i}}),m())},$=async s=>{const a=!s.is_active,{error:i}=await n.from("profiles").update({is_active:a}).eq("id",s.id);if(i){l.error(i.message);return}l.success(a?"User activated":"User deactivated"),await n.from("audit_logs").insert({action:a?"activate_user":"deactivate_user",entity_type:"user",entity_id:s.id,user_id:t==null?void 0:t.id,metadata:{full_name:s.full_name}}),m()},ee=async()=>{if(!o)return;b(!0);const{error:s}=await n.from("user_roles").delete().eq("id",o.role_id);if(s){l.error(s.message),b(!1);return}await n.from("profiles").update({is_active:!1}).eq("id",o.id),await n.from("audit_logs").insert({action:"delete_user",entity_type:"user",entity_id:o.id,user_id:t==null?void 0:t.id,metadata:{full_name:o.full_name}}),l.success("User removed"),C(null),b(!1),m()},se=async()=>{if(!w||!y){l.error("Email and name are required");return}P(!0);const s={email:w,fullName:y,role:I};j&&N&&(s.password=N);const{data:a,error:i}=await n.functions.invoke("invite-user",{body:s});i||a!=null&&a.error?l.error((a==null?void 0:a.error)||(i==null?void 0:i.message)||"Failed to invite user"):(l.success("User invited successfully"),p(!1),T(""),A(""),R("author"),k(!1),U(""),m()),P(!1)},B=J==="super_admin"?["super_admin","admin","editor","author"]:["editor","author"];return e.jsxs(ne,{allowed:["super_admin","admin"],fallback:e.jsx("p",{className:"text-muted-foreground",children:"Access denied"}),children:[e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Users"}),e.jsxs(h,{onClick:()=>p(!0),children:[e.jsx(be,{className:"h-4 w-4 mr-2"}),"Invite User"]})]}),e.jsx("div",{className:"rounded-md border",children:e.jsxs(oe,{children:[e.jsx(ce,{children:e.jsxs(D,{children:[e.jsx(v,{children:"Name"}),e.jsx(v,{children:"Role"}),e.jsx(v,{children:"Status"}),e.jsx(v,{children:"Actions"})]})}),e.jsx(de,{children:W?e.jsx(D,{children:e.jsx(x,{colSpan:4,className:"text-center py-8",children:e.jsx(g,{className:"h-5 w-5 animate-spin mx-auto"})})}):K.map(s=>e.jsxs(D,{className:s.is_active?"":"opacity-50",children:[e.jsx(x,{className:"font-medium",children:s.full_name}),e.jsx(x,{children:e.jsx(Y,{variant:"outline",className:"capitalize",children:s.role.replace("_"," ")})}),e.jsx(x,{children:s.id!==(t==null?void 0:t.id)?e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(G,{checked:s.is_active,onCheckedChange:()=>$(s)}),e.jsx("span",{className:"text-xs text-muted-foreground",children:s.is_active?"Active":"Inactive"})]}):e.jsx(Y,{variant:"secondary",children:"You"})}),e.jsx(x,{children:e.jsx("div",{className:"flex items-center gap-2",children:s.id!==(t==null?void 0:t.id)?e.jsxs(e.Fragment,{children:[e.jsxs(L,{value:s.role,onValueChange:a=>Z(s.role_id,s.id,a),children:[e.jsx(H,{className:"w-[140px]",children:e.jsx(O,{})}),e.jsx(M,{children:B.map(a=>e.jsx(V,{value:a,className:"capitalize",children:a.replace("_"," ")},a))})]}),e.jsxs(ie,{children:[e.jsx(te,{asChild:!0,children:e.jsx(h,{variant:"ghost",size:"icon",disabled:q===s.id,onClick:async()=>{z(s.id);const{data:a,error:i}=await n.functions.invoke("invite-user",{body:{reinvite:!0,userId:s.id}});i||a!=null&&a.error?l.error((a==null?void 0:a.error)||(i==null?void 0:i.message)||"Failed to re-invite"):l.success("Re-invite sent successfully"),z(null)},children:q===s.id?e.jsx(g,{className:"h-4 w-4 animate-spin"}):e.jsx(Se,{className:"h-4 w-4"})})}),e.jsx(re,{children:"Re-invite"})]}),e.jsx(h,{variant:"ghost",size:"icon",className:"text-destructive hover:text-destructive",onClick:()=>C(s),children:e.jsx(De,{className:"h-4 w-4"})})]}):e.jsx("span",{className:"text-sm text-muted-foreground",children:"Current user"})})})]},s.role_id))})]})})]}),e.jsx(me,{open:X,onOpenChange:p,children:e.jsxs(he,{children:[e.jsxs(ue,{children:[e.jsx(xe,{children:"Invite User"}),e.jsx(pe,{children:"Create a new CMS user account and assign a role."})]}),e.jsxs("div",{className:"space-y-4 py-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Full Name"}),e.jsx(S,{value:y,onChange:s=>A(s.target.value),placeholder:"John Doe"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Email"}),e.jsx(S,{type:"email",value:w,onChange:s=>T(s.target.value),placeholder:"[email protected]"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Role"}
1),e.jsxs(L,{value:I,onValueChange:R,children:[e.jsx(H,{children:e.jsx(O,{})}),e.jsx(M,{children:B.map(s=>e.jsx(V,{value:s,className:"capitalize",children:s.replace("_"," ")},s))})]})]}),e.jsxs("div",{className:"flex items-center justify-between pt-2",children:[e.jsxs("div",{children:[e.jsx(u,{children:"Set password manually"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:j?"You'll share the password with the user":"An invite email will be sent"})]}),e.jsx(G,{checked:j,onCheckedChange:k})]}),j&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(u,{children:"Temporary Password"}),e.jsx(S,{type:"password",value:N,onChange:s=>U(s.target.value),placeholder:"Min 6 characters"})]})]}),e.jsxs(je,{children:[e.jsx(h,{variant:"outline",onClick:()=>p(!1),children:"Cancel"}),e.jsxs(h,{onClick:se,disabled:E,children:[E&&e.jsx(g,{className:"h-4 w-4 mr-2 animate-spin"}),"Invite"]})]})]})}),e.jsx(fe,{open:!!o,onOpenChange:s=>!s&&C(null),children:e.jsxs(ve,{children:[e.jsxs(ge,{children:[e.jsx(_e,{children:"Delete User"}),e.jsxs(we,{children:["Are you sure you want to remove ",e.jsx("strong",{children:o==null?void 0:o.full_name}),"? Their role will be removed and their account will be deactivated."]})]}),e.jsxs(ye,{children:[e.jsx(Ne,{children:"Cancel"}),e.jsxs(Ce,{onClick:ee,disabled:F,className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:[F&&e.jsx(g,{className:"h-4 w-4 mr-2 animate-spin"}),"Delete"]})]})]})})]})}export{Ze as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.