1import{j as e}from"./ui-vendor-vRbYWRwc.js";import{r as t,L as s}from"./react-vendor-D511M1J9.js";import{a as r,I as a,C as n,B as i,b as o,H as d,l}from"./main-C17z2jjy.js";import{C as c}from"./checkbox-B1BjrGiT.js";import{T as m,a as u,b as x,c as h,d as p,e as g}from"./table-Dw1fOqYa.js";import"./data-vendor-tgy-DeSy.js";import"./utils-vendor-vsWCwOh-.js";async function _(e,t={}){const s=o.getState().token;if(!s)throw new Error("Not authenticated");const r=await fetch(`https://api.anyangle.ai${e}`,{...t,headers:{...t.headers||{},Authorization:`Bearer ${s}`,"Content-Type":"application/json"}}),a=await r.json().catch(()=>({}));if(!r.ok||!1===a.success)throw new Error(a.error||`HTTP ${r.status}`);return a}function j({status:t}){const s="succeeded"===t?"bg-emerald-500/15 text-emerald-300 border-emerald-500/30":"failed"===t?"bg-red-500/15 text-red-300 border-red-500/40":"queued"===t||"processing"===t?"bg-blue-500/15 text-blue-300 border-blue-500/30":"bg-ivory/5 text-ivory/70 border-white/10";return e.jsx("span",{className:`inline-block px-1.5 py-0.5 rounded border text-[10px] font-mono ${s}`,children:t})}function f({row:t,progress:s}){if(s&&!s.done&&"idle"!==s.stage&&"unknown"!==s.stage)return e.jsxs("div",{className:"w-24",children:[e.jsx("div",{className:"h-1.5 bg-ivory/10 rounded overflow-hidden",children:e.jsx("div",{className:"h-full bg-blue-400 transition-all",style:{width:`${Math.max(0,Math.min(100,s.pct))}%`}})}),e.jsxs("div",{className:"text-[10px] opacity-60 mt-0.5 font-mono",children:[s.completed_images??0,"/",s.total_images," · ",s.pct,"%"]})]});if(t.qc_completed){const s=t.qc_outliers>0;return e.jsx("span",{className:"text-[10px] font-mono px-1.5 py-0.5 rounded border "+(s?"bg-amber-500/15 text-amber-300 border-amber-500/30":"bg-emerald-500/10 text-emerald-300 border-emerald-500/20"),title:t.qc_completed_at?new Date(t.qc_completed_at).toLocaleString():"",children:s?`QC · ${t.qc_outliers} flagged`:"QC · ok"})}return"succeeded"===t.status?e.jsx("span",{className:"text-[10px] opacity-50",children:"pending"}):e.jsx("span",{className:"text-[10px] opacity-30",children:"â"})}function b({generationId:s}){const[r,a]=t.useState(null),[n,i]=t.useState(!0),[o,c]=t.useState(null);if(t.useEffect(()=>{let e=!1;return(async()=>{i(!0),c(null);try{const t=await _(`/api/admin/crm/generations/${encodeURIComponent(s)}/detail`);e||a(t.data||null)}catch(t){e||c(t.message)}finally{e||i(!1)}})(),()=>{e=!0}},[s]),n)return e.jsx("div",{className:"p-4 text-xs text-chocolate/60",children:"Loading full cardâ¦"});if(o)return e.jsx("div",{className:"p-4 text-xs text-destructive",children:o});if(!r)return null;const m=function(e){const t=Array.isArray(e.angles)?e.angles.length:"number"==typeof e.angles?e.angles:(e.results||[]).length||4,s=(e.results||[]).map(e=>({id:e.id,human_id:e.human_id??void 0,index:e.index,output_url:e.output_url,qc_status:"refunded"===e.qc_status?"refunded":null,qc_reason:e.qc_reason??void 0,refunded_at:e.refunded_at??void 0,similarity_score:e.similarity_score??void 0,generation_prompt:e.generation_prompt??null}));return{id:e.row.human_id||e.row.generation_id,db_id:e.row.generation_id,human_id:e.row.human_id??void 0,status:e.row.status,source_image_url:e.source_image_url??void 0,angles:t,credits_used:e.row.credits_used||0,created_at:e.row.created_at,completed_at:e.row.completed_at??void 0,error_category:e.row.error_category??void 0,user_error_message:e.row.user_error_message??void 0,error_message:e.row.admin_error_message??void 0,meta_prompt:e.prompt??void 0,generated_prompts:void 0,results:s,aa_results:s,qc_completed:e.row.qc_completed,qc_outliers:e.row.qc_outliers,qc_refunded_credits:e.row.credits_refunded??void 0,qc_completed_at:e.row.qc_completed_at??void 0,similarity_completed:e.row.similarity_completed,similarity_flagged_count:e.row.similarity_flagged_count}}(r);return e.jsxs("div",{className:"p-3 border-t border-chocolate/10 bg-chocolate/[0.02] space-y-3",children:[e.jsx(d,{fallback:e.jsxs("div",{className:"p-4 text-xs text-destructive",children:["History card failed to render. Fallback preview:",e.jsx("pre",{className:"mt-2 whitespace-pre-wrap text-[10px] opacity-70",children:JSON.stringify({id:m.id,status:m.status,results:m.results?.length},null,2)})]}),children:e.jsx(l,{generation:m,isExpanded:!0,isAdminMode:!0})}),(r.explain||r.row.admin_error_message||r.row.user_error_message)&&e.jsxs("div",{className:"text-[11px] space-y-1 text-chocolate/80",children:[r.explain&&e.jsxs("div",{children:[e.jsx("span",{className:"text-chocolate/60 mr-1",children:"error_class:"}),e.jsx("span",{className:"font-mono",children:r.explain.category})]}),r.row.user_error_message&&e.jsxs("div",{children:[e.jsx("span",{className:"text-chocolate/60 mr-1",children:"user_msg:"}),r.row.user_error_message]}),r.row.admin_error_message&&e.jsxs("details",{className:"text-chocolate/60",children:[e.jsx("summary",{className:"cursor-pointer",children:"admin_msg"}),e.jsx("pre",{className:"text-[10px] whitespace-pre-wrap font-mono mt-1",children:r.row.admin_error_message})]})]}),r.prompt&&e.jsxs("details",{className:"text-[11px]",children:[e.jsx("summary",{className:"cursor-pointer opacity-70",children:"Prompt"}),e.jsx("pre",{className:"whitespace-pre-wrap mt-1 opacity-80 text-[10px] font-mono",children:r.prompt})]})]})}function y(){const[o,d]=t.useState([]),[l,y]=t.useState({}),[w,v]=t.useState({}),[N,k]=t.useState(0),[S,C]=t.useState(!0),[q,$]=t.useState(null),[E,A]=t.useState(""),[I,z]=t.useState(""),[L,Q]=t.useState(""),[R,T]=t.useState(""),[D,M]=t.useState(new Set),[P,U]=t.useState(0),[H,K]=t.useState(new Set),[O,B]=t.useState(!1),[F,G]=t.useState(null),[J,X]=t.useState({}),V=t.useRef(new Set),W=t.useCallback(async()=>{C(!0),$(null);try{const e=new URLSearchParams;E&&e.set("status",E),I&&e.set("error_class",I),L.trim()&&e.set("human_id",L.trim()),R&&e.set("qc",R),e.set("limit","200");const t=await _(`/api/admin/crm/generations?${e.toString()}`);d(t.data?.rows||[]),y(t.data?.by_status||{}),v(t.data?.by_error_class||{}),k(t.data?.total||0),M(new Set),U(0),K(new Set)}catch(e){$(e.message)}finally{C(!1)}},[E,I,L,R]);t.useEffect(()=>{W()},[W]),t.useEffect(()=>{if(0===V.current.size)return;const e=setInterval(async()=>{const e=Array.from(V.current);if(0!==e.length)try{const t=await _(`/api/admin/crm/generations/qc-progress?ids=${encodeURIComponent(e.join(","))}`),s=t.data?.map||{};X(e=>({...e,...s}));for(const r of e){const e=s[r];e&&(e.done||"idle"===e.stage||"unknown"===e.stage)&&V.current.delete(r)}0===V.current.size&&W()}catch{}},1500);return()=>clearInterval(e)},[F,W]);const Y=t.useMemo(()=>o.filter(e=>!e.deleted_at).map(e=>e.generation_id),[o]),Z=Y.length>0&&Y.every(e=>D.has(e)),ee=e=>{M(t=>{const s=new Set(t);return s.has(e)?s.delete(e):s.add(e),s})},te=()=>{M(e=>Y.every(t=>e.has(t))?new Set:new Set(Y))},se=e=>{K(t=>{const s=new Set(t);return s.has(e)?s.delete(e):s.add(e),s})};t.useEffect(()=>{const e=e=>{const t=e.target?.tagName;if("INPUT"!==t&&"TEXTAREA"!==t&&!e.target?.isContentEditable&&!(e.metaKey||e.ctrlKey||e.altKey))if("j"===e.key)U(e=>Math.min(e+1,o.length-1));else if("k"===e.key)U(e=>Math.max(e-1,0));else if("x"===e.key||" "===e.key){e.preventDefault();const t=o[P];t&&!t.deleted_at&&ee(t.generation_id)}else if("a"!==e.key||e.shiftKey)if("o"===e.key){const e=o[P];e&&se(e.generation_id)}else"q"===e.key?D.size>0&&re():"/"===e.key&&(e.preventDefault(),document.getElementById("gens-humanid-filter")?.focus());else te()};return window.addEventListener("keydown",e),()=>window.removeEventListener("keydown",e)},[o,P,D]);const re=async()=>
1{if(0!==D.size){B(!0),$(null);try{const e=await _("/api/admin/crm/generations/qc-batch",{method:"POST",body:JSON.stringify({ids:Array.from(D)})});G(e.data||null);for(const t of e.data?.jobs||[])V.current.add(t.id),t.human_id&&V.current.add(t.human_id);M(new Set)}catch(e){$(e.message)}finally{B(!1)}}},ae=t.useMemo(()=>Object.entries(w).sort((e,t)=>t[1]-e[1]).slice(0,12),[w]);return e.jsxs("section",{className:"p-6 max-w-full",children:[e.jsxs("header",{className:"mb-4 flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold",children:"Generations Explorer"}),e.jsx("p",{className:"text-sm opacity-60 mt-1",children:"Action-first: engine à error_category à QC â batched QC (q), keyboard nav (j/k/x/o)."})]}),e.jsxs("div",{className:"flex gap-2 items-center",children:[e.jsx(r,{variant:"outline",size:"sm",onClick:W,disabled:S,"aria-label":"Refresh",children:S?"Loadingâ¦":"Refresh"}),e.jsx(r,{size:"sm",onClick:re,disabled:0===D.size||O,"data-testid":"qc-batch-btn",children:O?"Launchingâ¦":`Run QC · ${D.size} (q)`})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2 mb-2",children:[e.jsxs("button",{className:"text-xs px-2 py-1 rounded border "+(""===E?"bg-ivory/10 border-white/30":"border-white/10"),onClick:()=>A(""),"data-testid":"chip-status-all",children:["all statuses (",N,")"]}),["succeeded","failed","queued","processing"].map(t=>e.jsxs("button",{className:"text-xs px-2 py-1 rounded border font-mono "+(E===t?"bg-ivory/10 border-white/30":"border-white/10 hover:bg-ivory/5"),onClick:()=>A(t),"data-testid":`chip-status-${t}`,children:[t," (",l[t]||0,")"]},t))]}),ae.length>0&&e.jsxs("div",{className:"flex flex-wrap gap-2 mb-2",children:[e.jsx("button",{className:"text-xs px-2 py-1 rounded border "+(""===I?"bg-ivory/10 border-white/30":"border-white/10"),onClick:()=>z(""),children:"all error_class"}),ae.map(([t,s])=>e.jsxs("button",{className:"text-xs px-2 py-1 rounded border font-mono "+(I===t?"bg-ivory/10 border-white/30":"border-white/10 hover:bg-ivory/5"),onClick:()=>z(t),"data-testid":`chip-error-${t}`,children:[t," (",s,")"]},t))]}),e.jsxs("div",{className:"flex flex-wrap gap-2 mb-3 items-center",children:[e.jsx("div",{className:"flex gap-1",children:["","pending","completed","flagged"].map(t=>e.jsxs("button",{className:"text-xs px-2 py-1 rounded border "+(R===t?"bg-ivory/10 border-white/30":"border-white/10 hover:bg-ivory/5"),onClick:()=>T(t),"data-testid":`chip-qc-${t||"all"}`,children:["QC: ",t||"any"]},t||"all"))}),e.jsxs("div",{className:"ml-auto flex items-center gap-2",children:[e.jsx("label",{className:"text-xs opacity-60",children:"Human ID:"}),e.jsx(a,{id:"gens-humanid-filter",className:"h-7 w-56 text-xs font-mono",placeholder:"AA-â¦",value:L,onChange:e=>Q(e.target.value),onKeyDown:e=>{"Enter"===e.key&&W()}})]})]}),q&&e.jsx(n,{className:"p-3 mb-3 border-red-500/40 bg-red-500/10 text-sm text-red-300",children:q}),F&&e.jsxs(n,{className:"p-3 mb-3 border-blue-500/40 bg-blue-500/10 text-sm text-blue-300",children:["QC launched on ",F.launched,"/",F.requested," generations",F.errors.length>0&&e.jsxs("details",{className:"mt-1",children:[e.jsxs("summary",{className:"cursor-pointer text-xs opacity-80",children:[F.errors.length," skipped"]}),e.jsx("ul",{className:"mt-1 text-xs font-mono",children:F.errors.map((t,s)=>e.jsxs("li",{children:[t.human_id||t.id,": ",t.error]},s))})]})]}),e.jsx("div",{className:"border border-white/10 rounded",children:e.jsxs(m,{children:[e.jsx(u,{children:e.jsxs(x,{children:[e.jsx(h,{className:"w-8",children:e.jsx(c,{checked:Z,onCheckedChange:te,"aria-label":"Select all"})}),e.jsx(h,{children:"Human ID"}),e.jsx(h,{children:"Status"}),e.jsx(h,{children:"Error class"}),e.jsx(h,{children:"User"}),e.jsx(h,{className:"text-right",children:"Credits"}),e.jsx(h,{children:"QC"}),e.jsx(h,{children:"Created"})]})}),e.jsxs(p,{children:[S&&e.jsx(x,{children:e.jsx(g,{colSpan:9,className:"text-center opacity-60 py-8",children:"Loading generationsâ¦"})}),!S&&0===o.length&&!q&&e.jsx(x,{children:e.jsx(g,{colSpan:9,className:"text-center opacity-60 py-8",children:"No generations match the current filters."})}),!S&&o.map((t,r)=>{const a=r===P,n=D.has(t.generation_id),o=H.has(t.generation_id),d=J[t.generation_id]||(t.human_id?J[t.human_id]:void 0);return e.jsxs(e.Fragment,{children:[e.jsxs(x,{"data-testid":`row-${t.human_id||t.generation_id}`,className:`${a?"bg-ivory/5 outline outline-1 outline-white/20":""} ${t.deleted_at?"opacity-40":""}`,onClick:()=>U(r),children:[e.jsx(g,{children:e.jsx(c,{checked:n,disabled:!!t.deleted_at,onCheckedChange:()=>ee(t.generation_id),"aria-label":`Select ${t.human_id||t.generation_id}`})}),e.jsx(g,{className:"font-mono text-xs",children:e.jsxs("button",{className:"hover:underline",onClick:()=>se(t.generation_id),"data-testid":`toggle-${t.human_id||t.generation_id}`,children:[o?"â¾ ":"⸠",t.human_id||t.generation_id.slice(0,8)]})}),e.jsx(g,{children:e.jsx(j,{status:t.status})}),e.jsx(g,{children:t.error_category?e.jsx(i,{variant:"outline",className:"font-mono text-[10px]",title:t.admin_error_message||t.user_error_message||"",children:t.error_category}):e.jsx("span",{className:"opacity-30 text-xs",children:"â"})}),e.jsx(g,{className:"text-xs",children:e.jsxs(s,{to:`/admin/ops/users/${t.user_id}`,onClick:e=>e.stopPropagation(),className:"block hover:underline hover:text-chocolate",title:"Open full history in User 360",children:[e.jsx("div",{children:t.user_email||e.jsx("span",{className:"opacity-60",children:"â"})}),e.jsx("div",{className:"opacity-50 font-mono",children:t.user_id.slice(0,8)})]})}),e.jsxs(g,{className:"text-right font-mono text-xs",children:[e.jsx("div",{children:t.credits_net}),t.credits_refunded>0&&e.jsxs("div",{className:"opacity-50",children:["â",t.credits_refunded," refund"]})]}),e.jsx(g,{children:e.jsx(f,{row:t,progress:d})}),e.jsx(g,{className:"text-xs opacity-60",children:new Date(t.created_at).toLocaleStr
1ing()})]},t.generation_id),o&&e.jsx(x,{children:e.jsx(g,{colSpan:9,className:"p-0",children:e.jsx(b,{generationId:t.human_id||t.generation_id})})},`${t.generation_id}-detail`)]})})]})]})}),e.jsxs("div",{className:"mt-2 flex items-center justify-between text-xs opacity-60",children:[e.jsxs("span",{children:[o.length,"/",N," shown · ",D.size," selected"]}),e.jsxs("span",{children:["keys: ",e.jsx("kbd",{children:"j"}),"/",e.jsx("kbd",{children:"k"})," nav · ",e.jsx("kbd",{children:"x"})," select · ",e.jsx("kbd",{children:"a"})," all ·"," ",e.jsx("kbd",{children:"o"})," open · ",e.jsx("kbd",{children:"q"})," QC · ",e.jsx("kbd",{children:"/"})," id filter"]})]})]})}export{y as GenerationsExplorerPage,y 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.