PageSourceSearch

https://willowcommerce.com/assets/AiSuggestionsPage-sWpAtSy3.js

js willowcommerce.com collected 2026-10-04 03:01:46 UTC 5,544 bytes, 1 lines download raw bytes

1import{V as R,b as D,J as K,r as N,W as Q,w as n,j as t,C as $,at as B,B as l,R as O,D as q,bu as P,bv as E,bw as J,v as a}from"./index-Cz3wH2jj.js";function L(){const m=R(),x=D(),{companyUuids:g}=K(),k=g.join(","),[p,b]=N.useState(!1),[_,h]=N.useState(null),{data:i,isLoading:y,error:d,refetch:C}=Q({queryKey:["mfg-ai-suggestions",k],enabled:g.length>0,retry:1,queryFn:async()=>{const{data:e,error:s}=await n.from("manufacturing_ai_suggestions").select("*, products(id,name,vendor_sku,default_bom_id), raw_products(name,sku)").in("company_id",g).eq("status","new").order("created_at",{ascending:!1}).limit(100);if(s)throw s;return e??[]}}),S=async()=>{b(!0);const{data:e,error:s}=await n.functions.invoke("manufacturer-ai-suggestions",{body:{}});if(b(!1),s)return a.error(s.message);if(e!=null&&e.error)return a.error(String(e.error));const r=Number((e==null?void 0:e.count)??0);a.success(r>0?`${r} suggestions generated`:"No new suggestions found"),await m.invalidateQueries({queryKey:["mfg-ai-suggestions"]})},I=async e=>{var r;const{data:s}=await n.auth.getUser();await n.from("manufacturing_ai_suggestions").update({status:"dismissed",resolved_at:new Date().toISOString(),resolved_by:((r=s.user)==null?void 0:r.id)??null}).eq("id",e),m.invalidateQueries({queryKey:["mfg-ai-suggestions"]})},z=async e=>{var r,c,j;if(e.kind!=="produce"){a.error("This suggestion type can't be auto-converted to a job yet.");return}if(!e.product_id){a.error("Suggestion has no product attached.");return}const s=Number(e.recommended_qty??0);if(s<=0){a.error("Recommended quantity must be greater than zero.");return}h(e.id);try{let o=((r=e.products)==null?void 0:r.default_bom_id)??null;if(!o){const{data:f}=await n.from("product_boms").select("id").eq("product_id",e.product_id).eq("is_active",!0).order("version",{ascending:!1}).limit(1).maybeSingle();o=(f==null?void 0:f.id)??null}if(!o){a.error("No active BOM found for this product. Define one first.");return}const{data:v}=await n.auth.getUser(),{data:A}=await n.rpc("next_production_job_number",{_company_id:e.company_id}),{data:u,error:w}=await n.from("production_jobs").insert({company_id:e.company_id,job_number:A??`PJ-${Date.now()}`,product_id:e.product_id,bom_id:o,planned_qty:s,produced_qty:0,status:"pending",source:"ai_suggestion",source_suggestion_id:e.id,notes:`Auto-created from AI suggestion: ${e.reason??""}`.slice(0,1e3),created_by:((c=v.user)==null?void 0:c.id)??null}).select("id, job_number").maybeSingle();if(w)throw w;await n.from("manufacturing_ai_suggestions").update({status:"accepted",resolved_at:new Date().toISOString(),resolved_by:((j=v.user)==null?void 0:j.id)??null}).eq("id",e.id),a.success(`Production job ${(u==null?void 0:u.job_number)??""} created`,{action:u?{label:"Open",onClick:()=>x(`/brand/manufacturer/jobs/${u.id}`)}:void 0}),m.invalidateQueries({queryKey:["mfg-ai-suggestions"]}),m.invalidateQueries({queryKey:["production-jobs"]})}catch(o){a.error(o.message??"Failed to create job")}finally{h(null)}};return t.jsxs($,{className:"p-4",children:[t.jsxs("div",{className:"flex justify-between items-center mb-3",children:[t.jsxs("div",{className:"flex items-center gap-2",children:[t.jsx(B,{className:"h-5 w-5 text-primary"}),t.jsx("div",{className:"font-semibold",children:"AI Production & Demand Suggestions"})]}),t.jsxs(l,{onClick:S,disabled:p,children:[t.jsx(O,{className:`h-4 w-4 mr-1 ${p?"animate-spin":""}`}),p?"Analyzing…":"Generate Suggestions"]})]}),y&&t.jsx("div",{className:"text-muted-foreground text-sm",children:"Loading…"}),!y&&d&&t.jsxs("div",{className:"text-sm text-destructive py-6 text-center space-y-2",children:[t.jsxs("div",{children:["Couldn't load suggestions: ",(d==null?void 0:d.message)??"unknown error"]}),t.jsx(l,{size:"sm",variant:"outline",onClick:()=>C(),children:"Retry"})]}),!y&&!d&&(i==null?void 0:i.length)===0&&t.jsxs("div",{className:"text-sm text-muted-foreground py-8 text-center",children:["No active suggestions. Click ",t.jsx("strong",{children:"Generate Suggestions"})," to let AI analyze your sales, inventory, and BOMs."]}),t.jsx("div",{className:"grid gap-2",children:i==null?void 0:i.map(e=>{var r,c;const s=e.kind==="produce"&&e.product_id&&Number(e.recommended_qty??0)>0;return t.jsxs("div",{className:"border rounded p-3 flex items-start justify-between gap-3",children:[t.jsxs("div",{className:"min-w-0",children:[t.jsxs("div",{className:"flex items-center gap-2 mb-1 flex-wrap",children:[t.jsx(q,{variant:"outline",children:e.kind}),e.urgency&&t.jsx(q,{className:e.urgency==="critical"?"bg-rose-500/20 text-rose-700":e.urgency==="high"?"bg-amber-500/20 text-amber-700":"bg-muted text-muted-foreground",children:e.urgency}),t.jsx("span",{className:"font-medium truncate",children:((r=e.products)==null?void 0:r.name)||((c=e.raw_products)==null?void 0:c.name)||"—"}),e.recommended_qty!=null&&t.jsxs("span",{className:"text-sm text-muted-foreground",children:["· suggest ",Number(e.recommended_qty)]})]}),t.jsx("div",{className:"text-sm text-muted-foreground",children:e.reason})]}),t.jsxs("div",{className:"flex items-center gap-1 shrink-0",children:[s&&t.jsxs(l,{size:"sm",onClick:()=>z(e),disabled:_===e.id,children:[t.jsx(P,{className:"h-4 w-4 mr-1"}),_===e.id?"Creating…":"Create Job"]}),e.kind==="reorder_raw"&&t.jsxs(l,{size:"sm",variant:"outline",onClick:()=>x("/brand/manufacturer/raw-products"),children:[t.jsx(E,{className:"h-4 w-4 mr-1"})," Reorder"]}),t.jsx(l,{variant:"ghost",size:"sm",onClick:()=>I(e.id),children:t.jsx(J,{className:"h-4 w-4"})})]})]},e.id)})})]})}export{L 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.