PageSourceSearch

https://duckmerce.lovable.app/assets/RfmSegmentation-D3k3YYfw.js

js duckmerce.lovable.app collected 2026-10-03 03:23:14 UTC 10,192 bytes, 1 lines download raw bytes

1import{r as w,j as t}from"./index-CKitxjzp.js";function m(n){return parseFloat(n||"0")||0}const S=["SAR","AED","EGP","USD"],g=[{id:"champions",name:"Champions",emoji:"🏆",desc:"Bought recently, buy often, spend the most",action:"🎁 Reward & upsell — VIP early access",priority:"🟢 This Month",color:"bg-rose-600"},{id:"loyal",name:"Loyal",emoji:"💝",desc:"Buy regularly, respond well to offers",action:"💝 Loyalty program invite",priority:"🟢 This Month",color:"bg-rose-400"},{id:"potential",name:"Potential Loyalist",emoji:"🌱",desc:"Recent customers, above avg frequency",action:"📧 Nurture sequence — 2nd purchase incentive",priority:"🟡 This Week",color:"bg-rose-400"},{id:"new",name:"New Customers",emoji:"👋",desc:"Bought recently for the first time",action:"👋 Welcome flow — educate & build habit",priority:"🟡 This Week",color:"bg-rose-200"},{id:"atrisk",name:"At Risk",emoji:"⚡",desc:"Above avg spend, haven't bought recently",action:"⚡ Win-back offer — 15% off, expires in 7 days",priority:"🔴 Urgent",color:"bg-amber-400"},{id:"cantlose",name:"Can't Lose Them",emoji:"🚨",desc:"Used to buy often but haven't in a while",action:"🚨 Personal outreach — premium win-back",priority:"🔴 Urgent",color:"bg-amber-400"},{id:"hibernating",name:"Hibernating",emoji:"😴",desc:"Low recency, low frequency, low spend",action:"📱 Re-engagement campaign — remind value",priority:"🟡 This Week",color:"bg-stone-300"},{id:"lost",name:"Lost",emoji:"💸",desc:"Lowest scores across all RFM dimensions",action:"💸 Last chance offer or suppress",priority:"🟢 This Month",color:"bg-stone-300"}],L=({data:n,onChange:c})=>{const l=n.currency||"SAR";m(n.totalCustomers);const a=w.useMemo(()=>{const e=g.map(o=>{const i=m(n[`rfm_${o.id}_count`]),d=m(n[`rfm_${o.id}_aov`]),N=i*d;return{...o,count:i,aov:d,revenue:N}}),s=e.reduce((o,i)=>o+i.revenue,0),r=e.reduce((o,i)=>o+i.count,0);return e.map(o=>({...o,segShare:r>0?o.count/r*100:0,revShare:s>0?o.revenue/s*100:0}))},[n]),x=a.reduce((e,s)=>e+s.revenue,0),u=a.reduce((e,s)=>e+s.count,0),j=a.filter(e=>e.id==="champions"||e.id==="loyal").reduce((e,s)=>e+s.revenue,0),h=x>0?j/x*100:0,v=a.filter(e=>e.id==="champions"||e.id==="loyal").reduce((e,s)=>e+s.count,0),f=u>0?v/u*100:0,y=Math.max(...a.map(e=>e.revenue),1),p=a.some(e=>e.count>0),b={champions:{row:0,col:2},loyal:{row:0,col:1},potential:{row:1,col:2},new:{row:2,col:2},atrisk:{row:1,col:0},cantlose:{row:0,col:0},hibernating:{row:2,col:0},lost:{row:2,col:1}};return t.jsxs("div",{className:"space-y-5",children:[t.jsxs("div",{className:"bg-white border border-stone-200 rounded-xl p-4 shadow-sm",children:[t.jsx("p",{className:"text-xs font-bold text-stone-500 uppercase tracking-widest mb-3",children:"Store Settings"}),t.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[t.jsxs("div",{children:[t.jsx("label",{className:"text-xs text-stone-600",children:"Currency"}),t.jsx("select",{value:l,onChange:e=>c("currency",e.target.value),className:"w-full text-sm border border-stone-200 rounded-lg px-3 py-2 bg-stone-50 focus:ring-2 focus:ring-rose-500 outline-none",children:S.map(e=>t.jsx("option",{children:e},e))})]}),t.jsxs("div",{children:[t.jsx("label",{className:"text-xs text-stone-600",children:"Total Active Customer Base"}),t.jsx("input",{type:"number",value:n.totalCustomers||"",onChange:e=>c("totalCustomers",e.target.value),placeholder:"e.g. 5000",className:"w-full text-sm border border-stone-200 rounded-lg px-3 py-2 bg-stone-50 text-right focus:ring-2 focus:ring-rose-500 outline-none"})]})]})]}),t.jsxs("div",{className:"bg-white border border-stone-200 rounded-xl p-4 shadow-sm",children:[t.jsx("p",{className:"text-xs font-bold text-stone-500 uppercase tracking-widest mb-3",children:"RFM Segment Data"}),g.map(e=>t.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[t.jsxs("span",{className:"text-xs font-medium text-stone-700 w-36 flex items-center gap-1",title:e.desc,children:[t.jsx("span",{children:e.emoji})," ",e.name]}),t.jsx("input",{type:"number",value:n[`rfm_${e.id}_count`]||"",onChange:s=>
1c(`rfm_${e.id}_count`,s.target.value),placeholder:"Customers",className:"w-28 text-sm border border-stone-200 rounded-lg px-3 py-1.5 bg-stone-50 text-right focus:ring-2 focus:ring-rose-500 outline-none"}),t.jsx("input",{type:"number",value:n[`rfm_${e.id}_aov`]||"",onChange:s=>c(`rfm_${e.id}_aov`,s.target.value),placeholder:`AOV (${l})`,className:"w-28 text-sm border border-stone-200 rounded-lg px-3 py-1.5 bg-stone-50 text-right focus:ring-2 focus:ring-rose-500 outline-none"})]},e.id))]}),t.jsxs("div",{className:"bg-white border border-stone-200 rounded-xl p-4 shadow-sm",children:[t.jsx("p",{className:"text-xs font-bold text-stone-500 uppercase tracking-widest mb-3",children:"RFM Score Thresholds"}),t.jsx("div",{className:"grid grid-cols-3 gap-3",children:[{id:"recencyGoodDays",label:"Recency: Good if ≤ X days",ph:"30"},{id:"frequencyGoodCount",label:"Frequency: Good if ≥ X purchases",ph:"3"},{id:"monetaryGoodAmt",label:`Monetary: Good if ≥ X ${l}`,ph:"500"}].map(e=>t.jsxs("div",{children:[t.jsx("label",{className:"text-xs text-stone-600",children:e.label}),t.jsx("input",{type:"number",value:n[e.id]||"",onChange:s=>c(e.id,s.target.value),placeholder:e.ph,className:"w-full text-sm border border-stone-200 rounded-lg px-3 py-2 bg-stone-50 text-right focus:ring-2 focus:ring-rose-500 outline-none"})]},e.id))})]}),p&&t.jsxs("div",{className:"space-y-4 transition-all duration-200",children:[t.jsxs("div",{className:"bg-white border border-stone-200 rounded-xl p-4 shadow-sm",children:[t.jsx("p",{className:"text-xs font-bold text-stone-500 uppercase tracking-widest mb-3",children:"RFM Matrix"}),t.jsx("div",{className:"grid grid-cols-3 gap-1.5",children:[0,1,2].map(e=>[0,1,2].map(s=>{const r=a.find(o=>{var i,d;return((i=b[o.id])==null?void 0:i.row)===e&&((d=b[o.id])==null?void 0:d.col)===s});return t.jsx("div",{className:`p-3 rounded-lg text-center ${r?r.color+" text-white":"bg-stone-100 text-stone-400"}`,style:{minHeight:60},children:r&&t.jsxs(t.Fragment,{children:[t.jsxs("p",{className:"text-[10px] font-bold",children:[r.emoji," ",r.name]}),t.jsx("p",{className:"text-sm font-mono font-bold",children:r.count.toLocaleString()})]})},`${e}-${s}`)}))}),t.jsxs("div",{className:"flex justify-between text-[9px] text-stone-400 mt-1",children:[t.jsx("span",{children:"← Old"}),t.jsx("span",{children:"Recency →"})]})]}),t.jsxs("div",{className:"bg-white border border-stone-200 rounded-xl p-4 shadow-sm",children:[t.jsx("p",{className:"text-xs font-bold text-stone-500 uppercase tracking-widest mb-3",children:"Segment Breakdown"}),a.filter(e=>e.count>0).sort((e,s)=>s.revenue-e.revenue).map((e,s)=>t.jsxs("div",{className:"mb-3 p-3 bg-stone-50 rounded-lg",children:[t.jsxs("div",{className:"flex justify-between items-center text-xs mb-1",children:[t.jsxs("span",{className:"font-medium text-stone-700",children:[e.emoji," ",e.name," — ",e.count.toLocaleString()," (",e.segShare.toFixed(0),"%)"]}),t.jsxs("span",{className:"text-stone-500",children:["AOV: ",e.aov.toLocaleString()," ",l]})]}),t.jsx("div",{className:"h-2.5 bg-stone-200 rounded-full overflow-hidden mb-1",children:t.jsx("div",{className:"h-full bg-rose-500 rounded-full transition-all duration-200",style:{width:`${e.revenue/y*100}%`}})}),t.jsxs("div",{className:"flex justify-between text-[10px]",children:[t.jsxs("span",{className:"text-stone-400",children:[e.revenue.toLocaleString()," ",l," (",e.revShare.toFixed(0),"% of revenue)"]}),t.jsx("span",{className:"text-rose-600 font-medium",children:e.action})]})]},s))]}),t.jsxs("div",{className:"bg-white border border-stone-200 rounded-xl p-4 shadow-sm",children:[t.jsx("p",{className:"text-xs font-bold text-stone-500 uppercase tracking-widest mb-3",children:"Revenue by Segment"}),a.filter(e=>e.revenue>0).sort((e,s)=>s.revenue-e.revenue).map((e,s)=>t.jsxs("div",{className:"mb-2",children:[t.jsxs("div",{className:"flex justify-between text-xs mb-0.5",children:[t.jsx("span",{className:"text-stone-600",children:e.name}),t.jsxs("span",{className:"text-stone-500",children:[e.revenue.toLocaleString()," ",l]})]}),t.jsx("div",{className:"h-3 bg-stone-100 rounded-full overflow-hidden",children:t.jsx("div",{className:"h-full bg-rose-400 rounded-full transition-all duration-200",style:{width:`${e.revShare}%`}})})]},s)),h>0&&t.jsxs("p",{className:"text-xs text-stone-500 mt-2",children:["Top 2 segments (Champions + Loyal) = ",t.jsxs("span",{className:"font-bold text-rose-600",children:[f.toFixed(0),"% of customers → ",h.toFixed(0),"% of revenue"]})]})]}),t.jsxs("div",{className:"bg-stone-900 border border-stone-700 rounded-xl p-4 text-white",children:[t.jsx("p",{className:"text-xs font-bold text-stone-400 uppercase tracking-widest mb-3",children:"Action Priority Matrix"}),t.jsx("div",{className:"overflow-x-auto",children:t.jsxs("table",{className:"w-full text-sm min-w-[500px]",children:[t.jsx("thead",{children:t.jsxs("tr",{className:"text-[10px] text-stone-400 uppercase border-b border-stone-700",children:[t.jsx("th",{className:"text-left py-2 px-2",children:"Segment"}),t.jsx("th",{className:"px-2",children:"Customers"}),t.jsx("th",{className:"px-2",children:"Revenue"}),t.jsx("th",{className:"px-2",children:"Action"}),t.jsx("th",{className:"px-2",children:"Priority"})]})}),t.jsx("tbody",{children:[...a].filter(e=>e.count>0).sort((e,s)=>{const r={"🔴 Urgent":0,"🟡 This Week":1,"🟢 This Month":2};return(r[e.priority]??2)-(r[s.priority]??2)}).map((e,s)=>t.jsxs("tr",{className:"border-b border-stone-800",children:[t.jsxs("td",{className:"py-2 px-2",children:[e.emoji," ",e.name]}),t.jsx("td",{className:"py-2 px-2 text-right",children:e.count.toLocaleStr
1ing()}),t.jsxs("td",{className:"py-2 px-2 text-right",children:[e.revenue.toLocaleString()," ",l]}),t.jsx("td",{className:"py-2 px-2 text-[11px]",children:e.action}),t.jsx("td",{className:"py-2 px-2 text-right",children:e.priority})]},s))})]})})]})]}),!p&&t.jsx("div",{className:"border border-dashed border-stone-300 rounded-xl py-12 text-center text-stone-400 text-sm",children:"Enter customer counts per RFM segment to see analysis 🎯"})]})};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.