PageSourceSearch

https://alvoff.ai/assets/admin/Credits-BEqQhqvF.js

js alvoff.ai collected 2026-10-05 11:50:23 UTC 26,723 bytes, 1 lines download raw bytes

1import{j as e,c6 as fe,b3 as ke,cF as Ne,d2 as Ce,bS as de,bi as ve,cv as Se,ew as we,ex as _e,bA as ie,c9 as ce,b6 as Ae,r,b4 as ge,aW as Ue,aY as Te,ci as Fe}from"../vendor-BOFhzXEM.js";import{l as z,M as Le,H as C,aU as De}from"../main-BWkid2wj.js";import{s as Re}from"./adminError-BfLxn-te.js";import{E as ue}from"../empty-state-Bxe1lfx0.js";import{A as ze}from"./AdminListPage-DnynW8sz.js";import{E as xe}from"./ExportCsvButton-D9d3wjqe.js";import{u as me}from"./useAdminList-vhOjemjF.js";import{S as I}from"./StatCard-ByE_URiw.js";import{M as pe}from"../modal-whXuikMx.js";import{l as he}from"../users-ByjlxEES.js";import"../vendor-radix-DH6hF3-0.js";import"../vendor-firebase-Bxv7fC6l.js";import"../vendor-i18n-DJTgP-Hw.js";import"../vendor-framer-DBjDHCRx.js";const Be=async({skip:t=0,limit:a=20,q:d=null}={})=>{const l=new URLSearchParams({skip:String(t),limit:String(a)});return d&&l.append("q",d),(await z.get(`/admin/credits/balances?${l}`)).data},$e=async({skip:t=0,limit:a=20,q:d=null,type:l=null,user_id:h=null}={})=>{const c=new URLSearchParams({skip:String(t),limit:String(a)});return d&&c.append("q",d),l&&c.append("type",l),h&&c.append("user_id",h),(await z.get(`/admin/credits/transactions?${c}`)).data},Qe=async()=>(await z.get("/admin/credits/stats")).data,Me=async t=>(await z.get(`/admin/credits/balance/${t}`)).data,Oe=async t=>(await z.post("/admin/credits/adjust",t)).data;function Ee({loading:t,stats:a}){return t?e.jsxs("div",{role:"status","aria-live":"polite","aria-busy":"true",className:"contents",children:[e.jsx("span",{className:"sr-only",children:"Loading credit statistics..."}),Array.from({length:4}).map((d,l)=>e.jsx("div",{"aria-hidden":"true",className:"bg-white dark:bg-gray-900 rounded-xl border border-gray-100 dark:border-gray-800 p-4 animate-pulse h-20"},l))]}):a?e.jsxs(e.Fragment,{children:[e.jsx(I,{icon:fe,label:"Credits Outstanding",value:a.total_credits_outstanding.toLocaleString(),color:"bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300"}),e.jsx(I,{icon:ke,label:"Users with Credits",value:a.total_users_with_credits,color:"bg-green-100 text-green-700 dark:bg-green-900/40 dark:text-green-300"}),e.jsx(I,{icon:Ne,label:"Total Issued",value:a.total_credits_issued.toLocaleString(),color:"bg-purple-100 text-purple-700 dark:bg-purple-900/40 dark:text-purple-300"}),e.jsx(I,{icon:Ce,label:"Total Transactions",value:a.total_transactions,color:"bg-orange-100 text-orange-700 dark:bg-orange-900/40 dark:text-orange-300"})]}):null}function Pe({activeTab:t,onChange:a}){const d=["balances","transactions"];return e.jsx("div",{role:"tablist",className:"flex gap-1 mt-4 border-b border-gray-200 dark:border-gray-700",children:d.map(l=>e.jsx("button",{role:"tab","aria-selected":t===l,"aria-controls":`panel-${l}`,id:`tab-${l}`,onClick:()=>a(l),className:`px-4 py-2.5 text-sm font-medium border-b-2 transition-colors ${t===l?"border-gray-900 dark:border-white text-gray-900 dark:text-white":"border-transparent text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"}`,children:l==="balances"?"Balances":"Transactions"},l))})}const Ie=Le,be=t=>new Date(t).toLocaleString(),q=t=>t?.charAt(0)?.toUpperCase()||"?";function qe({rows:t,onAdd:a,onDeduct:d}){return e.jsxs("table",{className:"w-full",children:[e.jsx("caption",{className:"sr-only",children:"User credit balances"}),e.jsx("thead",{className:"bg-gray-50 dark:bg-gray-800 border-b border-gray-100 dark:border-gray-800",children:e.jsxs("tr",{children:[e.jsx("th",{scope:"col",className:"text-left px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider",children:"User"}),e.jsx("th",{scope:"col",className:"text-right px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider",children:"Balance"}),e.jsx("th",{scope:"col",className:"text-left px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider hidden lg:table-cell",children:"Last Updated"}),e.jsx("th",{scope:"col",className:"text-right px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider",children:"Actions"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-100 dark:divide-gray-800",children:t.map(l=>e.jsx(Ve,{row:l,onAdd:a,onDeduct:d},l.user_id))})]})}function Ve({row:t,onAdd:a,onDeduct:d}){return e.jsxs("tr",{className:"animate-fadeIn hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors",children:[e.jsx("td",{className:"px-3 md:px-4 py-3",children:e.jsxs("div",{className:"flex items-center gap-2.5",children:[e.jsx("div",{className:"w-9 h-9 rounded-full bg-gray-200 dark:bg-gray-700 flex items-center justify-center flex-shrink-0",children:e.jsx("span",{className:"text-gray-600 dark:text-gray-300 font-medium text-sm",children:q(t.user_name)})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-medium text-gray-900 dark:text-white text-sm truncate",children:t.user_name||"Unnamed"}),e.jsx("p",{className:"text-xs text-gray-600 dark:text-gray-400 truncate",children:t.user_email||"-"})]})]})}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-right",children:e.jsxs("span",{className:"inline-flex items-center gap-1 px-2.5 py-1 text-sm font-bold rounded-full bg-blue-100 text-blue-800 dark:bg-blue-900/40 dark:text-blue-300",children:[e.jsx(de,{size:14,"aria-hidden":"true"}
1),t.balance.toLocaleString()]})}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-gray-600 dark:text-gray-400 text-sm hidden lg:table-cell",children:t.updated_at?e.jsx("time",{dateTime:t.updated_at,children:Ie(t.updated_at)}):"-"}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx(C,{variant:"ghost",size:"sm",onClick:()=>a(t.user_id,t.user_name,t.user_email),title:"Add credits","aria-label":`Add credits for ${t.user_name||t.user_email||"user"}`,children:e.jsx(ve,{size:16,className:"text-green-600"})}),e.jsx(C,{variant:"ghost",size:"sm",onClick:()=>d(t.user_id,t.user_name,t.user_email),title:"Deduct credits","aria-label":`Deduct credits for ${t.user_name||t.user_email||"user"}`,children:e.jsx(Se,{size:16,className:"text-red-600"})})]})})]})}const ye={credit:{icon:_e,label:"Credit",className:"bg-green-100 text-green-700 dark:bg-green-900/40 dark:text-green-300",amountClassName:"text-green-600",sign:"+"},debit:{icon:we,label:"Debit",className:"bg-red-100 text-red-700 dark:bg-red-900/40 dark:text-red-300",amountClassName:"text-red-600",sign:"-"}};function Ke({type:t}){const a=ye[t];if(!a)return null;const d=a.icon;return e.jsxs("span",{className:`inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium rounded-full ${a.className}`,children:[e.jsx(d,{size:12,"aria-hidden":"true"}),a.label]})}function We({rows:t}){return e.jsxs("table",{className:"w-full",children:[e.jsx("caption",{className:"sr-only",children:"Credit transactions"}),e.jsx("thead",{className:"bg-gray-50 dark:bg-gray-800 border-b border-gray-100 dark:border-gray-800",children:e.jsxs("tr",{children:[e.jsx("th",{scope:"col",className:"text-left px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider",children:"User"}),e.jsx("th",{scope:"col",className:"text-center px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider",children:"Type"}),e.jsx("th",{scope:"col",className:"text-right px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider",children:"Amount"}),e.jsx("th",{scope:"col",className:"text-right px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider hidden sm:table-cell",children:"Balance After"}),e.jsx("th",{scope:"col",className:"text-left px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider hidden md:table-cell",children:"Description"}),e.jsx("th",{scope:"col",className:"text-left px-3 md:px-4 py-3 text-xs font-semibold text-gray-600 dark:text-gray-400 uppercase tracking-wider hidden lg:table-cell",children:"Date"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-100 dark:divide-gray-800",children:t.map(a=>e.jsx(Xe,{txn:a},a.id))})]})}function Xe({txn:t}){const a=ye[t.type];return e.jsxs("tr",{className:"animate-fadeIn hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors",children:[e.jsx("td",{className:"px-3 md:px-4 py-3",children:e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-medium text-gray-900 dark:text-white text-sm truncate",children:t.user_name||"Unnamed"}),e.jsx("p",{className:"text-xs text-gray-600 dark:text-gray-400 truncate",children:t.user_email||"-"})]})}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-center",children:e.jsx(Ke,{type:t.type})}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-right font-medium text-sm",children:e.jsxs("span",{className:a?.amountClassName,children:[e.jsxs("span",{className:"sr-only",children:[a?.label,": "]}),a?.sign,t.amount.toLocaleString()]})}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-right text-sm text-gray-600 dark:text-gray-300 hidden sm:table-cell",children:t.balance_after.toLocaleString()}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-sm text-gray-600 dark:text-gray-400 hidden md:table-cell max-w-[200px] truncate",children:t.description||"-"}),e.jsx("td",{className:"px-3 md:px-4 py-3 text-sm text-gray-600 dark:text-gray-400 hidden lg:table-cell whitespace-nowrap",children:e.jsx("time",{dateTime:t.created_at,children:be(t.created_at)})})]})}function je({inputId:t,listboxId:a,query:d,onQueryChange:l,results:h,searching:c,show:p,onFocus:m,onSelect:f,containerRef:g,focusOnMount:y}){return e.jsxs("div",{ref:g,className:"relative",children:[e.jsxs("div",{className:"relative",children:[e.jsx(ie,{size:14,className:"absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"}),e.jsx("input",{id:t,type:"text",role:"combobox","aria-label":"Search user by name, email, or phone","aria-expanded":p,"aria-autocomplete":"list","aria-controls":a,"aria-haspopup":"listbox","aria-busy":c,value:d,onChange:n=>l(n.target.value),onFocus:m,placeholder:"Search by name, email, or phone...",className:"w-full pl-9 pr-3 py-2 border border-gray-200 dark:border-gray-700 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm",autoComplete:"off",autoFocus:y}),c&&e.jsx(ce,{size:14,className:"absolute right-3 top-1/2 -translate-y-1/2 animate-spin text-gray-400"})]}),p&&e.jsx("div",{id:a,role:"listbox",className:"absolute z-10 mt-1 w-full bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg max-h-48 overflow-y-auto",children:h.length===0?e.jsx("p",{className:"px-3 py-2 text-sm text-gray-500 dark:text-gray-400",children:"No users found"}):h.map(n=>e.jsxs("button",{type:"button",role:"option","aria-selected":!1,className:"w-full text-left px-3 py-2 hover:bg-gray-50 dark:hover:bg-gray-700 flex items-center gap-2 transition-colors",onClick:()=>f(n),children:[e.jsx("div",{className:"w-7 h-7 rounded-full bg-gray-200 dark:bg-gray-600 flex items-center justify-center flex-shrink-0",children:e.jsx("span",{className:"text-gray-600 dark:text-gray-300 font-medium text-xs",children:q(n.name)})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 dark:text-white truncate",children:n.name||"Unnamed"}),e.jsx("p",{className:"text-xs text-gray-500 dark:text-gray-400 truncate",children:n.email||n.phone||n.id})]})]},n.id))})]})}function Ye({open:t,busy:a,onClose:d,selectedUser:l,onClearSelectedUser:h,userSearchRef:c,userQuery:p,onUserQueryChange:m,userResults:f,userSearching:g,showUserDropdown:y,onUserFocus:n,onSelectUser:F,form:j,onTypeChange:w,onAmountChange:o,onReasonChange:L,fieldErrors:u,onSubmit:k}){return e.jsxs(pe,{open:t,onClose:d,size:"md",title:"Adjust Credits",preventClose:a,children:[e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"adjust-user-search",className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"User"}),l?e.jsxs("div",{className:"flex items-center justify-between px-3 py-2 border border-gray-200 dark:border-gray-700 rounded-lg bg-gray-50 dark:bg-gray-800",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[e.jsx("div",{className:"w-7 h-7 rounded-full bg-blue-100 dar
1k:bg-blue-900/40 flex items-center justify-center flex-shrink-0",children:e.jsx("span",{className:"text-blue-700 dark:text-blue-300 font-medium text-xs",children:q(l.name)})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 dark:text-white truncate",children:l.name}),e.jsx("p",{className:"text-xs text-gray-500 dark:text-gray-400 truncate",children:l.email||l.id})]})]}),e.jsx("button",{type:"button",onClick:h,"aria-label":"Clear selected user",className:"ml-2 p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 rounded",children:e.jsx(Ae,{size:14,"aria-hidden":"true"})})]}):e.jsx(je,{inputId:"adjust-user-search",listboxId:"user-search-listbox",query:p,onQueryChange:m,results:f,searching:g,show:y,onFocus:n,onSelect:F,containerRef:c})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("label",{htmlFor:"adjust-type",className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"Type"}),e.jsxs("select",{id:"adjust-type",value:j.type,onChange:b=>w(b.target.value),"aria-required":"true",className:"w-full px-3 py-2 border border-gray-200 dark:border-gray-700 rounded-lg focus:ring-2 focus:ring-blue-500 text-sm",children:[e.jsx("option",{value:"credit",children:"Add Credits"}),e.jsx("option",{value:"debit",children:"Deduct Credits"})]})]}),e.jsxs("div",{className:"flex-1",children:[e.jsx("label",{htmlFor:"adjust-amount",className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"Amount"}),e.jsx("input",{id:"adjust-amount",type:"number",min:"1",value:j.amount,onChange:b=>o(b.target.value),placeholder:"e.g. 100","aria-required":"true","aria-invalid":!!u.amount,"aria-describedby":u.amount?"adjust-amount-error":void 0,className:`w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 text-sm ${u.amount?"border-red-400":"border-gray-200 dark:border-gray-700"}`}),u.amount&&e.jsx("p",{id:"adjust-amount-error",role:"alert",className:"text-xs text-red-600 mt-1",children:u.amount})]})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"adjust-reason",className:"block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1",children:"Reason"}),e.jsx("input",{id:"adjust-reason",type:"text",value:j.reason,onChange:b=>L(b.target.value),placeholder:"Reason for adjustment...",maxLength:500,"aria-required":"true","aria-invalid":!!u.reason,"aria-describedby":u.reason?"adjust-reason-error":void 0,className:`w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 text-sm ${u.reason?"border-red-400":"border-gray-200 dark:border-gray-700"}`}),u.reason&&e.jsx("p",{id:"adjust-reason-error",role:"alert",className:"text-xs text-red-600 mt-1",children:u.reason})]})]}),e.jsxs("div",{className:"flex justify-end gap-3 mt-6",children:[e.jsx(C,{variant:"outline",size:"sm",onClick:d,disabled:a,children:"Cancel"}),e.jsxs(C,{size:"sm",onClick:k,disabled:a,className:j.type==="debit"?"bg-red-600 hover:bg-red-700 text-white":"",children:[a&&e.jsx(ce,{size:14,className:"animate-spin mr-2"}),j.type==="credit"?"Add Credits":"Deduct Credits"]})]})]})}function Ge({open:t,onClose:a,searchRef:d,query:l,onQueryChange:h,results:c,searching:p,show:m,onFocus:f,onSelect:g,loading:y,lookup:n}){return e.jsxs(pe,{open:t,onClose:a,size:"md",title:"Check User Balance",children:[e.jsx(je,{listboxId:"check-user-listbox",query:l,onQueryChange:h,results:c,searching:p,show:m,onFocus:f,onSelect:g,containerRef:d,focusOnMount:!0}),y&&e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(ce,{size:24,className:"animate-spin text-gray-400"})}),n&&!y&&e.jsxs("div",{className:"mt-4 space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-3 p-3 bg-gray-50 dark:bg-gray-800 rounded-lg",children:[e.jsx("div",{className:"w-10 h-10 rounded-full bg-blue-100 dark:bg-blue-900/40 flex items-center justify-center flex-shrink-0",children:e.jsx("span",{className:"text-blue-700 dark:text-blue-300 font-semibold",children:q(n.user_name)})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"font-medium text-gray-900 dark:text-white truncate",children:n.user_name||"Unnamed"}),e.jsx("p",{className:"text-sm text-gray-500 dark:text-gray-400 truncate",children:n.user_email||"-"}),n.user_phone&&e.jsx("p",{className:"text-xs text-gray-400 dark:text-gray-500 truncate",children:n.user_phone})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"p-3 bg-blue-50 dark:bg-blue-900/20 rounded-lg text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-blue-700 dark:text-blue-300",children:n.balance.toLocaleStr
1ing()}),e.jsx("p",{className:"text-xs text-blue-600 dark:text-blue-400 mt-0.5",children:"Current Balance"})]}),e.jsxs("div",{className:"p-3 bg-gray-50 dark:bg-gray-800 rounded-lg text-center",children:[e.jsx("p",{className:"text-2xl font-bold text-gray-700 dark:text-gray-300",children:n.transaction_count}),e.jsx("p",{className:"text-xs text-gray-500 dark:text-gray-400 mt-0.5",children:"Transactions"})]}),e.jsxs("div",{className:"p-3 bg-green-50 dark:bg-green-900/20 rounded-lg text-center",children:[e.jsxs("p",{className:"text-lg font-bold text-green-700 dark:text-green-300",children:["+",n.total_credited.toLocaleString()]}),e.jsx("p",{className:"text-xs text-green-600 dark:text-green-400 mt-0.5",children:"Total Credited"})]}),e.jsxs("div",{className:"p-3 bg-red-50 dark:bg-red-900/20 rounded-lg text-center",children:[e.jsxs("p",{className:"text-lg font-bold text-red-700 dark:text-red-300",children:["-",n.total_debited.toLocaleString()]}),e.jsx("p",{className:"text-xs text-red-600 dark:text-red-400 mt-0.5",children:"Total Debited"})]})]}),n.last_transaction_at&&e.jsxs("p",{className:"text-xs text-gray-500 dark:text-gray-400 text-center",children:["Last transaction: ",e.jsx("time",{dateTime:n.last_transaction_at,children:be(n.last_transaction_at)})]})]}),e.jsx("div",{className:"flex justify-end mt-6",children:e.jsx(C,{variant:"outline",size:"sm",onClick:a,children:"Close"})})]})}const He=[{header:"User ID",value:t=>t.user_id},{header:"Name",value:t=>t.user_name||""},{header:"Email",value:t=>t.user_email||""},{header:"Balance",value:t=>String(t.balance)},{header:"Updated",value:t=>t.updated_at||""}],Je=[{header:"ID",value:t=>t.id},{header:"User",value:t=>t.user_name||t.user_email||t.user_id},{header:"Type",value:t=>t.type},{header:"Amount",value:t=>String(t.amount)},{header:"Balance After",value:t=>String(t.balance_after)},{header:"Description",value:t=>t.description||""},{header:"Date",value:t=>t.created_at}];function Ze(){const[t,a]=r.useState(!1),[d,l]=r.useState(!1),[h,c]=r.useState({}),[p,m]=r.useState({user_id:"",amount:"",type:"credit",reason:""}),[f,g]=r.useState(""),[y,n]=r.useState([]),[F,j]=r.useState(!1),[w,o]=r.useState(null),[L,u]=r.useState(!1),k=r.useRef(null),b=r.useRef(void 0),[V,B]=r.useState(!1),[K,_]=r.useState(""),[W,A]=r.useState([]),[X,$]=r.useState(!1),[Y,Q]=r.useState(null),[G,M]=r.useState(!1),[H,N]=r.useState(!1),D=r.useRef(null),R=r.useRef(void 0),J=r.useCallback(()=>{d||(a(!1),o(null),g(""),n([]),c({}))},[d]),O=r.useCallback(()=>{B(!1),_(""),A([]),Q(null)},[]),E=r.useCallback(s=>{if(b.current&&clearTimeout(b.current),!s.trim()){n([]),u(!1);return}b.current=setTimeout(async()=>{j(!0);try{const i=await he({q:s,limit:8});n(i),u(!0)}catch{n([])}finally{j(!1)}},300)},[]),P=r.useCallback(s=>{if(R.current&&clearTimeout(R.current),!s.trim()){A([]),N(!1);return}R.current=setTimeout(async()=>{$(!0);try{const i=await he({q:s,limit:8});A(i),N(!0)}catch{A([])}finally{$(!1)}},300)},[]),Z=r.useCallback(async s=>{N(!1),_(""),M(!0);try{const i=await Me(s);Q(i)}catch{ge.error("Failed to load user credit balance")}finally{M(!1)}},[]);r.useEffect(()=>{const s=i=>{k.current&&!k.current.contains(i.target)&&u(!1),D.current&&!D.current.contains(i.target)&&N(!1)};return document.addEventListener("mousedown",s),()=>document.removeEventListener("mousedown",s)},[]);const ee=r.useCallback(s=>(i,x,v)=>{m({user_id:i,amount:"",type:s,reason:""}),o({id:i,name:x||"Unnamed",email:v||null}),g(""),n([]),a(!0)},[]),te=r.useCallback(s=>{o({id:s.id,name:s.name,email:s.email}),m(i=>({...i,user_id:s.id})),u(!1),g("")},[]),ae=r.useCallback(()=>{o(null),m(s=>({...s,user_id:""}))},[]),se=r.useCallback(s=>{g(s),E(s)},[E]),re=r.useCallback(s=>{_(s),P(s)},[P]),ne=r.useCallback(s=>{m(i=>({...i,amount:s})),c(i=>({...i,amount:""}))},[]),U=r.useCallback(s=>{m(i=>({...i,reason:s})),c(i=>({...i,reason:""}))},[]),T=r.useCallback(()=>{a(!1),m({user_id:"",amount:"",type:"credit",reason:""}),o(null),g(""),n([])},[]);return{adjustOpen:t,setAdjustOpen:a,adjustBusy:d,setAdjustBusy:l,adjustForm:p,setAdjustForm:m,adjustFieldErrors:h,setAdjustFieldErrors:c,closeAdjustModal:J,resetAdjustForm:T,userSearchRef:k,userQuery:f,userResults:y,userSearching:F,selectedUser:w,showUserDropdown:L,setShowUserDropdown:u,handleUserQueryChange:se,handleSelectAdjustUser:te,handleClearSelectedUser:ae,handleAmountChange:ne,handleReasonChange:U,openAdjustFor:ee,checkOpen:V,setCheckOpen:B,checkQuery:K,checkResults:W,checkSearching:X,checkLookup:Y,checkLoading:G,showCheckDropdown:H,setShowCheckDropdown:N,checkSearchRef:D,closeCheckModal:O,handleCheckQueryChange:re,handleCheckUser:Z}}function ht(){const[t,a]=r.useState("balances"),[d,l]=r.useState(""),h=Ue(),{data:c,isLoading:p}=Te({queryKey:["adminCreditStats"],queryFn:()=>Qe()}),[m,f]=r.useState(""),[g,y]=r.useState("");r.useEffect(()=>{y(t==="balances"?"Balances tab selected":"Transactions tab selected")},[t]);
1const{adjustOpen:n,setAdjustOpen:F,adjustBusy:j,setAdjustBusy:w,adjustForm:o,setAdjustForm:L,adjustFieldErrors:u,setAdjustFieldErrors:k,closeAdjustModal:b,resetAdjustForm:V,userSearchRef:B,userQuery:K,userResults:_,userSearching:W,selectedUser:A,showUserDropdown:X,setShowUserDropdown:$,handleUserQueryChange:Y,handleSelectAdjustUser:Q,handleClearSelectedUser:G,handleAmountChange:M,handleReasonChange:H,openAdjustFor:N,checkOpen:D,setCheckOpen:R,checkQuery:J,checkResults:O,checkSearching:E,checkLookup:P,checkLoading:Z,showCheckDropdown:ee,setShowCheckDropdown:te,checkSearchRef:ae,closeCheckModal:se,handleCheckQueryChange:re,handleCheckUser:ne}=Ze(),U=me(x=>Be({...x}),{},"Failed to load credit balances"),T=me(x=>$e({...x,type:x.txnType}),{txnType:d||null},"Failed to load transactions"),s=t==="balances"?U:T,i=async()=>{const x=parseInt(o.amount,10),v=De.safeParse({user_id:o.user_id,amount:isNaN(x)?o.amount:x,type:o.type,reason:o.reason});if(!v.success){const S={};for(const oe of v.error.issues){const le=oe.path[0];le&&!S[le]&&(S[le]=oe.message)}k(S);return}k({}),w(!0);try{const S=await Oe({user_id:o.user_id,amount:v.data.amount,type:o.type,reason:o.reason});ge.success(`${o.type==="credit"?"Added":"Deducted"} ${x} credits. New balance: ${S.new_balance}`),f("Credits adjusted successfully"),V(),U.refresh(),T.refresh(),h.invalidateQueries({queryKey:["adminCreditStats"]})}catch(S){Re(S,"Failed to adjust credits")}finally{w(!1)}};return e.jsxs(e.Fragment,{children:[e.jsx("span",{"aria-live":"polite",className:"sr-only",children:m}),e.jsx("span",{"aria-live":"polite","aria-atomic":"true",className:"sr-only",children:g}),e.jsx("div",{className:"p-4 md:p-6 pb-0",children:e.jsx("h1",{className:"text-xl md:text-2xl font-bold text-gray-900 dark:text-white mb-4",children:"Credit Management"})}),e.jsxs("div",{className:"p-4 md:p-6 pb-0",children:[e.jsx("div",{className:"grid grid-cols-2 lg:grid-cols-4 gap-3",children:e.jsx(Ee,{loading:p,stats:c})}),e.jsx(Pe,{activeTab:t,onChange:a})]}),e.jsx("div",{role:"tabpanel",id:`panel-${t}`,"aria-labelledby":`tab-${t}`,children:e.jsx(ze,{title:t==="balances"?"User Credit Balances":"Credit Transactions",subtitle:t==="balances"?"View and manage credit balances for all users":"View all credit transactions across users",actions:e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(C,{variant:"outline",size:"sm",onClick:()=>R(!0),children:[e.jsx(ie,{size:14,className:"mr-2","aria-hidden":"true"}),"Check Balance"]}),e.jsxs(C,{variant:"outline",size:"sm",onClick:()=>F(!0),children:[e.jsx(de,{size:14,className:"mr-2","aria-hidden":"true"}),"Adjust Credits"]}),t==="balances"?e.jsx(xe,{rows:U.data,columns:He,filenamePrefix:"credit-balances",disabled:s.data.length===0}):e.jsx(xe,{rows:T.data,columns:Je,filenamePrefix:"credit-transactions",disabled:s.data.length===0})]}),search:{value:s.search,onChange:s.setSearch,placeholder:"Search by name or email...",ariaLabel:"Search credits"},filters:t==="transactions"?e.jsxs(e.Fragment,{children:[e.jsx(Fe,{size:18,className:"text-gray-400 dark:text-gray-500"}),e.jsxs("select",{"aria-label":"Filter by transaction type",value:d,onChange:x=>l(x.target.value),className:"px-4 py-2 border border-gray-200 dark:border-gray-700 rounded-lg focus:ring-2 focus:ring-red-500",children:[e.jsx("option",{value:"",children:"All Types"}),e.jsx("option",{value:"credit",children:"Credit"}),e.jsx("option",{value:"debit",children:"Debit"})]})]}):void 0,error:s.error,loading:s.loading,isEmpty:s.data.length===0,emptyNode:s.search.trim()?e.jsx(ue,{icon:e.jsx(ie,{size:24}),title:`No results for "${s.search}"`,description:"Try a different name or email.",action:e.jsx(C,{variant:"outline",size:"sm",onClick:()=>s.setSearch(""),children:"Clear search"})}):e.jsx(ue,{icon:e.jsx(de,{size:24,"aria-hidden":"true"}),title:t==="balances"?"No credit balances":"No transactions",description:t==="balances"?"No users have credit balances yet.":"No credit transactions have occurred yet."}),pagination:{skip:s.pagination.skip,limit:s.pagination.limit,count:s.data.length,onNext:s.goNext,onPrev:s.goPrev,showingText:`Showing ${s.data.length} ${t}`},children:t==="balances"?e.jsx(qe,{rows:U.data,onAdd:N("credit"),onDeduct:N("debit")}):e.jsx(We,{rows:T.data})})}),e.jsx(Ye,{open:n,busy:j,onClose:b,selectedUser:A,onClearSelectedUser:G,userSearchRef:B,userQuery:K,onUserQueryChange:Y,userResults:_,userSearching:W,showUserDropdown:X,onUserFocus:()=>{_.length&&$(!0)},onSelectUser:Q,form:o,onTypeChange:x=>L(v=>({...v,type:x})),onAmountChange:M,onReasonChange:H,fieldErrors:u,onSubmit:i}),e.jsx(Ge,{open:D,onClose:se,searchRef:ae,query:J,onQueryChange:re,results:O,searching:E,show:ee,onF
1ocus:()=>{O.length&&te(!0)},onSelect:x=>ne(x.id),loading:Z,lookup:P})]})}export{ht 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.