(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[7758],{24508:(e,s,t)=>{Promise.resolve().then(t.bind(t,36894))},36894:(e,s,t)=>{"use strict";t.r(s),t.d(s,{default:()=>i});var n=t(95155),a=t(12115),l=t(35497);function i(){let[e,s]=(0,a.useState)([]),[t,i]=(0,a.useState)([]),[d,r]=(0,a.useState)(!0),[c,o]=(0,a.useState)(null),[u,x]=(0,a.useState)(""),[h,m]=(0,a.useState)(!1);async function p(){r(!0);try{let[e,t]=await Promise.all([fetch("/api/keys"),fetch("/api/users")]),n=await e.json(),a=await t.json();s(n.keys||[]),i(a.users||[])}finally{r(!1)}}async function f(e){m(!0);try{(await fetch(`/api/keys/${e.id}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({userId:u||null})})).ok&&(o(null),x(""),p())}finally{m(!1)}}return(0,a.useEffect)(()=>{fetch("/api/auth/me").then(e=>e.json()).then(e=>{"super_admin"!==e.role?window.location.href="/dashboard/usage":p()}).catch(()=>{window.location.href="/dashboard/usage"})},[]),(0,n.jsxs)("div",{className:"flex flex-col gap-6",children:[(0,n.jsxs)("div",{children:[(0,n.jsx)("h1",{className:"text-2xl font-bold",children:"API Key Management"}),(0,n.jsx)("p",{className:"text-sm text-text-muted mt-1",children:"Assign API keys to sub-users. Only the assigned user can use that key."})]}),d?(0,n.jsx)("p",{className:"text-text-muted",children:"Loading..."}):0===e.length?(0,n.jsx)(l.Zp,{children:(0,n.jsx)("p",{className:"text-text-muted text-center py-8",children:"No API keys yet. Create one in the Endpoint page."})}):(0,n.jsx)("div",{className:"space-y-3",children:e.map(e=>{let{label:s,color:a}=function(e){if(!e)return{label:"Unassigned (Admin)",color:"text-text-muted"};let s=t.find(s=>s.id===e);return{label:s?`${s.displayName} (@${s.username})`:e,color:"text-primary"}}(e.userId);return(0,n.jsxs)(l.Zp,{className:"flex items-center justify-between",children:[(0,n.jsxs)("div",{className:"flex items-center gap-4",children:[(0,n.jsx)("div",{className:"w-10 h-10 rounded-lg bg-surface-alt flex items-center justify-center",children:(0,n.jsx)("span",{className:"material-symbols-outlined text-text-muted text-[20px]",children:"key"})}),(0,n.jsxs)("div",{children:[(0,n.jsx)("p",{className:"font-semibold font-mono text-sm",children:e.name}),(0,n.jsxs)("p",{className:`text-xs ${a}`,children:[s,e.assignedAt&&(0,n.jsxs)("span",{className:"ml-2 text-text-muted",children:[(0,n.jsx)("span",{children:"assigned"})," ",new Date(e.assignedAt).toLocaleDateString()]})]})]})]}),(0,n.jsx)(l.$n,{variant:"secondary",size:"sm",onClick:()=>{o(e),x(e.userId||"")},children:e.userId?"Reassign":"Assign to User"})]},e.id)})}),c&&(0,n.jsx)(l.aF,{isOpen:!!c,title:(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)("span",{children:"Assign Key:"})," ",c.name]}),onClose:()=>o(null),children:(0,n.jsxs)("div",{className:"flex flex-col gap-4",children:[(0,n.jsxs)(l.l6,{label:"Assign to user",value:u,onChange:e=>x(e.target.value),children:[(0,n.jsx)("option",{value:"",children:"— Unassigned (Admin only) —"}),t.map(e=>(0,n.jsxs)("option",{value:e.id,children:[e.displayName," (@",e.username,")"]},e.id))]}),(0,n.jsxs)("div",{className:"flex justify-end gap-2",children:[(0,n.jsx)(l.$n,{variant:"secondary",onClick:()=>o(null),children:"Cancel"}),(0,n.jsx)(l.$n,{loading:h,onClick:()=>f(c),children:"Save"})]})]})})]})}}},e=>{e.O(0,[2574,3862,1051,1321,5497,8441,3794,7358],()=>e(e.s=24508)),_N_E=e.O()}]);