1import{r as e,b as s,j as a}from"./vendor-react-BH6F5oQJ.js";import{A as i}from"./AdminLayout-DnnQLcVN.js";import{w as t,s as n,C as r,M as o,N as l,m as d,n as c,o as u,q as m,B as h,f as p}from"./index-C_oSc_qP.js";import{T as j,a as x,b as v,c as f,d as y,e as g}from"./table-BexhSyg2.js";import{S as w,a as b,b as N,c as S,d as k}from"./select-DR0VJVi7.js";import{m as _,y as E,a0 as q}from"./vendor-icons-CaI5__By.js";import"./vendor-ui-B4WC06Sp.js";import"./separator-Cc2qsRsv.js";import"./index-CCdGLatk.js";import"./Combination-DzDAyqvK.js";import"./vendor-supabase-COwyE4EW.js";import"./index-B3FPD4RS.js";import"./en-US-DUAJ-5Wt.js";import"./vendor-query-CC4VRGyP.js";const A=["super_admin","admin","editor","author","viewer"],C=async(e,s)=>{try{const a=await e.json();return[null==a?void 0:a.error,null==a?void 0:a.details].filter(Boolean).join(" â ")||s}catch{return s}},z=()=>{const[z,B]=e.useState([]),[T,U]=e.useState(!0),[F,O]=e.useState(null),{toast:R}=t(),$=s();e.useEffect(()=>{I()},[]);const I=async()=>{try{const{data:{session:e}}=await n.auth.getSession();if(!e)return void $("/admin/login");const s=await fetch("https://ypsdkqazkbvieilcqyke.supabase.co/functions/v1/admin-user-management",{method:"POST",headers:{Authorization:`Bearer ${e.access_token}`,"Content-Type":"application/json"},body:JSON.stringify({action:"listUsers"})});if(!s.ok)throw new Error(await C(s,"Failed to fetch users"));const a=await s.json();B(a.users)}catch(e){R({title:"Error",description:e instanceof Error?e.message:"Failed to load users",variant:"destructive"})}finally{U(!1)}};return T?a.jsx(i,{children:a.jsx("div",{className:"flex items-center justify-center h-64",children:a.jsx(_,{className:"h-8 w-8 animate-spin"})})}):a.jsx(i,{children:a.jsx("div",{className:"container mx-auto p-6 space-y-6",children:a.jsxs(r,{children:[a.jsx(o,{children:a.jsx(l,{children:"User Management"})}),a.jsx(d,{children:a.jsxs(j,{children:[a.jsx(x,{children:a.jsxs(v,{children:[a.jsx(f,{children:"User"}),a.jsx(f,{children:"Email"}),a.jsx(f,{children:"Roles"}),a.jsx(f,{children:"Last Sign In"}),a.jsx(f,{children:"Status"}),a.jsx(f,{children:"Actions"})]})}),a.jsx(y,{children:z.map(e=>{var s,i,t,r,o,l;return a.jsxs(v,{children:[a.jsx(g,{children:a.jsxs("div",{className:"flex items-center gap-2",children:[a.jsxs(c,{children:[a.jsx(u,{src:(null==(s=e.profile)?void 0:s.avatar_url)||""}),a.jsx(m,{children:(null==(t=null==(i=e.profile)?void 0:i.display_name)?void 0:t[0])||(null==(o=null==(r=e.email)?void 0:r[0])?void 0:o.toUpperCase())})]}),a.jsx("span",{children:(null==(l=e.profile)?void 0:l.display_name)||"No name"})]})}),a.jsx(g,{children:e.email}),a.jsx(g,{children:a.jsx("div",{className:"flex flex-wrap gap-1",children:e.roles.length>0?e.roles.map(e=>a.jsx(h,{variant:"secondary",children:e},e)):a.jsx("span",{className:"text-muted-foreground text-sm",children:"No roles"})})}),a.jsx(g,{children:e.last_sign_in_at?new Date(e.last_sign_in_at).toLocaleDateString():"Never"}),a.jsx(g,{children:e.banned?a.jsx(h,{variant:"destructive",children:"Banned"}):a.jsx(h,{variant:"default",children:"Active"})}),a.jsx(g,{children:a.jsxs("div",{className:"flex gap-2",children:[a.jsxs(w,{disabled:F===e.id,onValueChange:s=>(async(e,s,a)=>{O(e);try{const{data:{session:i}}=await n.auth.getSession();if(!i)return;const t=a.includes(s)?"removeRole":"updateRole";if("removeRole"===t&&"super_admin"===s&&e===i.user.id)return void R({title:"Not allowed",description:"You cannot remove your own super_admin role.",variant:"destructive"});const r=await fetch("https://ypsdkqazkbvieilcqyke.supabase.co/functions/v1/admin-user-management",{method:"POST",headers:{Authorization:`Bearer ${i.access_token}`,"Content-Type":"application/json"},body:JSON.stringify({action:t,userId:e,role:s})});if(!r.ok)throw new Error(await C(r,"Failed to update role"));R({title:"Success",description:`Role ${"removeRole"===t?"removed":"assigned"} successfully`}),await I()}catch(i){R({title:"Error",description:i instanceof Error?i.message:"Failed to update role",variant:"destructive"})}finally{O(null)}})(e.id,s,e.roles),children:[a.jsx(b,{className:"w-32",children:a.jsx(N,{placeholder:"Add role"})}),a.jsx(S,{children:A.map(e=>a.jsx(k,{value:e,children:e},e))})]}),a.jsx(p,{variant:e.banned?"default":"destructive",size:"sm",onClick:()=>(async(e,s)=>{O(e);try{const{data:{session:a}}=await n.auth.getSession();if(!a)return;const i=await fetch("https://ypsdkqazkbvieilcqyke.supabase.co/functions/v1/admin-user-management",{method:"POST",headers:{Authorization:`Bearer ${a.access_token}`,"Content-Type":"application/json"},body:JSON.stringify({action:"toggleBan",userId:e,disabled:!s}
1)});if(!i.ok)throw new Error(await C(i,"Failed to toggle ban status"));R({title:"Success",description:s?"User unbanned successfully":"User banned successfully"}),await I()}catch(a){R({title:"Error",description:a instanceof Error?a.message:"Failed to update ban status",variant:"destructive"})}finally{O(null)}})(e.id,e.banned),disabled:F===e.id,children:F===e.id?a.jsx(_,{className:"h-4 w-4 animate-spin"}):e.banned?a.jsx(E,{className:"h-4 w-4"}):a.jsx(q,{className:"h-4 w-4"})})]})})]},e.id)})})]})})]})})})};export{z 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.