1import{I as P,r as j,t as n,b as c,j as e,B as l,C as h}from"./index-C3Kh6bvC.js";import{u as b}from"./useQuery-FRjIRqQF.js";import{u as d}from"./useMutation-Df-Esq8_.js";import{I as S}from"./input-Bt2TQZyj.js";import{L as g}from"./label-JL66tkz_.js";import{T as A}from"./textarea-3qnRSAoP.js";import{S as M}from"./switch-CcPFey3K.js";import{S as K}from"./skeleton-mgbBjFuC.js";import{D as Q,a as $,b as I,c as B,e as z}from"./dialog-DDmsBt2x.js";import{S as p}from"./StatusBadge-Bo6lEDRQ.js";import{u as C}from"./useServerFn-DwWjV4T0.js";import{v as O,r as R}from"./native-accounts.functions-M18PmUwO.js";import{P as V}from"./plus-DDIR1gjq.js";import{S as G}from"./server-cog-Bd7YvwjT.js";import{L as q}from"./loader-circle-C0Yvu2wb.js";import{S as _}from"./shield-check-b6hiqpX2.js";import{T as H}from"./trash-2-B8IuZzP8.js";import{S as J}from"./shield-alert-BxXuI6K_.js";import"./useBaseQuery-CbpYqjK1.js";import"./index-CySJwTvP.js";import"./index-DXwEYyUl.js";import"./index-YLkiCrKH.js";import"./index-CKpNCyvW.js";import"./index-CpUG-_qh.js";import"./index-BWvjwfsx.js";import"./x-Ogv46MMR.js";import"./badge-usPDkjdH.js";function U({mode:o}){return o==="native_pool"?e.jsx(p,{status:"Master pool",toneMap:{"master pool":"success"}}):o==="native_caller_token"?e.jsx(p,{status:"Caller token",toneMap:{"caller token":"warning"}}):e.jsx(p,{status:"Unknown"})}function ke(){const o=P(),[F,v]=j.useState(!1),[t,m]=j.useState({label:"",session_token:"",notes:""}),[i,u]=j.useState(null),N=C(O),T=C(R),f=d({mutationFn:async s=>await N({data:{token:s}}),onSuccess:s=>{u(s),s.valid?n.success("Token is valid"):n.error(s.reason)},onError:s=>n.error(s.message)}),x=d({mutationFn:async s=>await T({data:{id:s}}),onSuccess:s=>{s.valid?n.success("Token still valid"):n.error(`Invalid â account paused: ${s.reason}`),o.invalidateQueries({queryKey:["native_accounts"]})},onError:s=>n.error(s.message)}),{data:w,isLoading:L}=b({queryKey:["native_accounts"],queryFn:async()=>{const s=c,{data:r,error:a}=await s.from("native_accounts").select("*").order("created_at",{ascending:!1});if(a)throw a;return r}}),{data:k}=b({queryKey:["native_dispatch_logs"],refetchInterval:15e3,queryFn:async()=>{const s=c,{data:r,error:a}=await s.from("dispatch_logs").select("id, created_at, license_key, status, http_status, mode, native_account_label, error_message").in("mode",["native_pool","native_caller_token"]).order("created_at",{ascending:!1}).limit(25);if(a)throw a;return r}}),y=d({mutationFn:async()=>{const s=await N({data:{token:t.session_token.trim()}});if(u(s),!s.valid)throw new Error(`Token rejected: ${s.reason}`);const r=c,{error:a}=await r.from("native_accounts").insert({label:t.label.trim(),session_token:t.session_token.trim(),notes:t.notes.trim()||null});if(a)throw a},onSuccess:()=>{n.success("Account added"),v(!1),m({label:"",session_token:"",notes:""}),u(null),o.invalidateQueries({queryKey:["native_accounts"]})},onError:s=>n.error(s.message)}),D=d({mutationFn:async s=>{const r=c,{error:a}=await r.from("native_accounts").update({status:s.status==="active"?"paused":"active"}).eq("id",s.id);if(a)throw a},onSuccess:()=>o.invalidateQueries({queryKey:["native_accounts"]}),onError:s=>n.error(s.message)}),E=d({mutationFn:async s=>{const r=c,{error:a}=await r.from("native_accounts").delete().eq("id",s);if(a)throw a},onSuccess:()=>{n.success("Deleted"),o.invalidateQueries({queryKey:["native_accounts"]})},onError:s=>n.error(s.message)});return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Native Accounts"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Master account pool for the experimental relay at ",e.jsx("code",{children:"/api/public/v1/fn-px08"}),". Live traffic on fn-px01 is untouched."]})]}),e.jsxs(l,{onClick:()=>v(!0),children:[e.jsx(V,{className:"w-4 h-4 mr-2"}),"Add Account"]})]}),L?e.jsx(K,{className:"h-40 w-full"}):(w?.length??0)===0?e.jsxs(h,{className:"p-10 text-center text-muted-foreground",children:[e.jsx(G,{className:"w-8 h-8 mx-auto mb-3 opacity-60"}),"No master accounts yet. The native relay stays offline until at least one is added."]}):e.jsx("div",{className:"grid gap-3",children:w.map(s=>e.jsxs(h,{className:"p-4 flex flex-wrap items-center gap-4",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("div",{className:"font-semibold truncate",children:s.label}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[s.dispatch_count," dispatches",s.last_used_at?` · last used ${new Date(s.last_used_at).toLocaleStr
1ing()}`:""]}),s.last_error&&e.jsx("div",{className:"text-xs text-destructive mt-1 line-clamp-2",children:s.last_error}),s.notes&&e.jsx("div",{className:"text-xs text-muted-foreground mt-1",children:s.notes})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(M,{checked:s.status==="active",onCheckedChange:()=>D.mutate(s)}),e.jsx("span",{className:"text-xs text-muted-foreground w-14",children:s.status}),e.jsxs(l,{variant:"outline",size:"sm",onClick:()=>x.mutate(s.id),disabled:x.isPending,children:[x.isPending&&x.variables===s.id?e.jsx(q,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(_,{className:"w-4 h-4 mr-2"}),"Verify"]}),e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>E.mutate(s.id),children:e.jsx(H,{className:"w-4 h-4 text-destructive"})})]})]},s.id))}),e.jsx(Q,{open:F,onOpenChange:v,children:e.jsxs($,{children:[e.jsx(I,{children:e.jsx(B,{children:"Add master account"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Label"}),e.jsx(S,{value:t.label,onChange:s=>m({...t,label:s.target.value}),placeholder:"master-01"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Session token"}),e.jsx(A,{rows:4,value:t.session_token,onChange:s=>{m({...t,session_token:s.target.value}),u(null)},placeholder:"eyJhbGciOi..."}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(l,{type:"button",variant:"outline",size:"sm",disabled:!t.session_token.trim()||f.isPending,onClick:()=>f.mutate(t.session_token.trim()),children:[f.isPending?e.jsx(q,{className:"w-4 h-4 mr-2 animate-spin"}):e.jsx(_,{className:"w-4 h-4 mr-2"}),"Test token"]}),i&&e.jsxs("span",{className:`text-xs flex items-center gap-1 ${i.valid?"text-emerald-500":"text-destructive"}`,children:[i.valid?e.jsx(_,{className:"w-3 h-3"}):e.jsx(J,{className:"w-3 h-3"}),i.valid?`Valid${i.account?.email?` · ${i.account.email}`:""}`:i.reason]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"The token is verified against Lovable before it is saved â invalid or expired tokens are rejected."})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(g,{children:"Notes (optional)"}),e.jsx(S,{value:t.notes,onChange:s=>m({...t,notes:s.target.value})})]})]}),e.jsx(z,{children:e.jsx(l,{onClick:()=>y.mutate(),disabled:!t.label.trim()||!t.session_token.trim()||y.isPending,children:"Save"})})]})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold",children:"Recent fn-px08 dispatches"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Shows whether each request was dispatched with one of our master pool tokens or fell back to the caller's own Lovable session token."})]}),(k?.length??0)===0?e.jsx(h,{className:"p-6 text-center text-sm text-muted-foreground",children:"No native dispatches recorded yet."}):e.jsx("div",{className:"grid gap-2",children:k.map(s=>e.jsxs(h,{className:"p-3 flex flex-wrap items-center gap-3 text-sm",children:[e.jsx(U,{mode:s.mode}),e.jsx("span",{className:"font-mono text-xs",children:s.license_key}),s.native_account_label&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["via ",s.native_account_label]}),e.jsx(p,{status:s.status,toneMap:{success:"success",failed:"danger",denied:"warning"}}),e.jsx("span",{className:"text-xs text-muted-foreground",children:s.http_status??"â"}),e.jsx("span",{className:"ml-auto text-xs text-muted-foreground",children:new Date(s.created_at).toLocaleString()}),s.error_message&&e.jsx("div",{className:"w-full text-xs text-destructive line-clamp-2",children:s.error_message})]},s.id))})]})]})}export{ke as component};
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.