1import{j as e}from"./ui-vendor-B12AOeKi.js";import{r,N as z}from"./react-vendor-DZt7wVAJ.js";import{u as G,j as H,s as w,al as Y,B as J,Y as Q,q as _,ap as R,P as X,ac as Z,ad as ee}from"./index-CUOC2qQu.js";import{T as se,b as te,c as F}from"./tabs-Cesl5pOW.js";import{g as L}from"./api-Dnj7Wk6e.js";import"./charts-vendor-B6q1L48j.js";import"./supabase-vendor-dOy44Gxq.js";import"./animation-vendor-BAVy02gd.js";function ae(a,m){return(a==null?void 0:a.name)||(a==null?void 0:a.username)||(a==null?void 0:a.email)||m.slice(0,8)}function ne(a,m){if(a){const i=[a.year,a.make,a.model].filter(Boolean).join(" ").trim(),p=[a.stock_number?`STK ${a.stock_number}`:"",i].filter(Boolean).join(" · ");if(p)return p;if(a.vin)return`VIN ${a.vin}`}return m||"(no title)"}function S(a){const m=new Date,i=new Date(m);return i.setHours(0,0,0,0),a==="today"?i:a==="week"?(i.setDate(i.getDate()-6),i):(i.setDate(i.getDate()-29),i)}function B(a){if(!a)return"never";const m=Date.now()-new Date(a).getTime(),i=Math.floor(m/36e5);return i<1?`${Math.max(1,Math.floor(m/6e4))}m ago`:i<24?`${i}h ago`:`${Math.floor(i/24)}d ago`}function xe(){const{isAdmin:a,isPlatformAdmin:m}=G(),{isManager:i}=H(),h=a||m||i,[p,M]=r.useState("today"),[T,C]=r.useState(!0),[E,D]=r.useState(!1),[$,O]=r.useState(Date.now()),[P,U]=r.useState([]),[k,q]=r.useState({}),[V,W]=r.useState({}),[A,K]=r.useState(new Set),I=r.useRef(null),x=r.useCallback(async(t=!1)=>{t?C(!0):D(!0);const c=await L();if(I.current=c,!c){C(!1),D(!1);return}const l=new Date;l.setDate(l.getDate()-35);const{data:u}=await w.from("m360_post_records").select("id,user_id,vehicle_id,listing_title,facebook_listing_url,posted_at,started_at,status").eq("dealership_id",c).or(`posted_at.gte.${l.toISOString()},started_at.gte.${l.toISOString()}`).order("posted_at",{ascending:!1,nullsFirst:!1}),n=u||[];U(n);const j=Array.from(new Set(n.map(o=>o.user_id).filter(Boolean)));let N=[];try{const{data:o}=await w.from("registration_users").select("id").eq("default_dealership_id",c);N=(o||[]).map(s=>s.id)}catch{}const g=Array.from(new Set([...j,...N]));if(g.length){const{data:o}=await w.from("registration_users").select("id,username,name,email").in("id",g),s={};(o||[]).forEach(d=>{s[d.id]=d}),q(s)}const b=Array.from(new Set(n.map(o=>o.vehicle_id).filter(Boolean)));if(b.length){const{data:o}=await w.from("m360_vehicles").select("id,year,make,model,stock_number,vin").in("id",b),s={};(o||[]).forEach(d=>{s[d.id]=d}),W(s)}C(!1),D(!1),O(Date.now())},[]);r.useEffect(()=>{h&&x(!0)},[h,x]),r.useEffect(()=>{if(!h)return;const t=()=>x(!1);window.addEventListener("focus",t);const c=setInterval(()=>x(!1),6e4);return()=>{window.removeEventListener("focus",t),clearInterval(c)}},[h,x]),r.useEffect(()=>{if(!h)return;let t=null;return(async()=>{const l=I.current||await L();l&&(t=w.channel(`m360-team-activity:${l}`).on("postgres_changes",{event:"*",schema:"public",table:"m360_post_records",filter:`dealership_id=eq.${l}`},()=>x(!1)).subscribe())})(),()=>{t&&w.removeChannel(t)}},[h,x]);const v=r.useMemo(()=>
1{const t=S("today"),c=S("week"),l=S("month"),u=new Date(Date.now()-48*3600*1e3),n={},j=s=>(n[s]||(n[s]={user_id:s,today:0,week:0,month:0,lastPost:null,recent:[]}),n[s]);Object.keys(k).forEach(s=>j(s));const N=new Set(["posted","live","confirmed_live"]);for(const s of P){if(!s.user_id||!N.has((s.status||"").toLowerCase())||!s.posted_at)continue;const d=s.posted_at,y=new Date(d),f=j(s.user_id);y>=l&&f.month++,y>=c&&f.week++,y>=t&&f.today++,f.recent.push(s),(!f.lastPost||y>new Date(f.lastPost))&&(f.lastPost=d)}const g=Object.values(n).sort((s,d)=>d[p]-s[p]||(d.lastPost||"").localeCompare(s.lastPost||"")),b=g.filter(s=>s.today>0).length,o=g.filter(s=>!s.lastPost||new Date(s.lastPost)<u).length;return{list:g,activeToday:b,inactive:o}},[P,k,p]);return h?e.jsxs("div",{className:"p-6 space-y-4 max-w-5xl",children:[e.jsxs("div",{className:"flex items-center gap-3 justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Y,{className:"h-5 w-5 text-[#7FB0FF]"}),e.jsx("h1",{className:"text-2xl font-semibold text-[#E6EDF3]",children:"Team Activity"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"text-[10px] text-[#9FB3C8]",children:["Updated ",B(new Date($).toISOString())]}),e.jsxs(J,{type:"button",size:"sm",variant:"outline",onClick:()=>x(!1),disabled:E||T,className:"h-8 gap-1.5 border-white/15 bg-white/[0.03] text-[#E6EDF3] hover:bg-white/[0.06]",children:[e.jsx(Q,{className:`h-3.5 w-3.5 ${E?"animate-spin":""}`}),"Refresh"]})]})]}),e.jsx("p",{className:"text-xs text-[#9FB3C8]",children:"Posting activity for users at the current dealership. Managers and admins only."}),e.jsxs("div",{className:"grid grid-cols-2 gap-3 max-w-md",children:[e.jsxs(_,{className:"p-3 bg-white/[0.03] border-white/10",children:[e.jsx("div",{className:"text-[10px] uppercase tracking-wide text-[#9FB3C8]",children:"Active posters today"}),e.jsx("div",{className:"text-2xl font-semibold text-white",children:v.activeToday})]}),e.jsxs(_,{className:"p-3 bg-white/[0.03] border-white/10",children:[e.jsx("div",{className:"text-[10px] uppercase tracking-wide text-[#9FB3C8]",children:"Inactive > 48h"}),e.jsxs("div",{className:"text-2xl font-semibold text-amber-300 flex items-center gap-2",children:[v.inactive>0&&e.jsx(R,{className:"h-5 w-5"}),v.inactive]})]})]}),e.jsx(se,{value:p,onValueChange:t=>M(t),children:e.jsxs(te,{className:"bg-black/25 border border-white/10",children:[e.jsx(F,{value:"today",children:"Today"}),e.jsx(F,{value:"week",children:"This week"}),e.jsx(F,{value:"month",children:"This month"})]})}),T?e.jsxs("div",{className:"flex items-center gap-2 text-[#9FB3C8] text-sm",children:[e.jsx(X,{className:"h-4 w-4 animate-spin"}),"Loadingâ¦"]}):v.list.length===0?e.jsx(_,{className:"p-6 bg-white/[0.03] border-white/10 text-sm text-[#9FB3C8]",children:"No users found for this dealership."}):e.jsxs(_,{className:"bg-white/[0.03] border-white/10 overflow-hidden",children:[e.jsxs("div",{className:"hidden md:grid grid-cols-12 gap-2 px-4 py-2 text-[10px] uppercase tracking-wide text-[#9FB3C8] border-b border-white/10",children:[e.jsx("div",{className:"col-span-4",children:"User"}),e.jsx("div",{className:"col-span-1 text-right",children:"Today"}),e.jsx("div",{className:"col-span-1 text-right",children:"Week"}),e.jsx("div",{className:"col-span-1 text-right",children:"Month"}),e.jsx("div",{className:"col-span-3",children:"Last post"}),e.jsx("div",{className:"col-span-2",children:"Status"})]}),e.jsx("div",{className:"divide-y divide-white/5",children:v.list.map(t=>{const c=k[t.user_id],l=!t.lastPost||Date.now()-new Date(t.lastPost).getTime()>48*3600*1e3,u=A.has(t.user_id);return e.jsxs("div",{children:[e.jsxs("button",{type:"button",onClick:()=>{const n=new Set(A);n.has(t.user_id)?n.delete(t.user_id):n.add(t.user_id),K(n)},className:"w-full grid grid-cols-12 gap-2 px-4 py-3 text-sm text-[#E6EDF3] hover:bg-white/[0.03] items-center text-left",children:[e.jsxs("div",{className:"col-span-12 md:col-span-4 flex items-center gap-2",children:[u?e.jsx(Z,{className:"h-4 w-4 text-[#9FB3C8]"}):e.jsx(ee,{className:"h-4 w-4 text-[#9FB3C8]"}),e.jsx("span",{className:"font-medium",children:ae(c,t.user_id)})]}),e.jsxs("div",{className:"col-span-4 md:col-span-1 md:text-right",children:[e.jsx("span",{className:"md:hidden text-[#9FB3C8] text-xs mr-1",children:"Today:"}),t.today]}),e.jsxs("div",{className:"col-span-4 md:col-span-1 md:text-right",children:[e.jsx("span",{className:"md:hidden text-[#9FB3C8] text-xs mr-1",children:"Week:"}),t.week]}),e.jsxs("div",{className:"col-span-4 md:col-span-1 md:text-right",children:[e.jsx("span",{className:"md:hidden text-[#9FB3C8] text-xs mr-1",children:"Month:"}),t.month]}),e.jsx("div",{className:"col-span-6 md:col-span-3 text-[#B8C7D9] text-xs",children:B(t.lastPost)}),e.jsx("div",{className:"col-span-6 md:col-span-2",children:l?e.jsxs("span",{className:"inline-flex items-center gap-1 text-[11px] px-2 py-0.5 rounded bg-amber-500/15 text-amber-300 border border-amber-500/40",children:[e.jsx(R,{className:"h-3 w-3"}),"No post 48h+"]}):e.jsx("span",{className:"text-[11px] px-2 py-0.5 rounded bg-emerald-500/15 text-emerald-300 border border-emerald-500/40",children:"OK"})})]}),u&&e.jsxs("div",{className:"bg-black/20 px-6 py-3 space-y-1",children:[e.jsx("div",{className:"text-[10px] uppercase tracking-wide text-[#9FB3C8] mb-1",children:"Recent posts (last 30 days)"}),t.recent.length===0?e.jsx("p",{className:"text-xs text-[#9FB3C8] italic",children:"No posts in the last 30 days."}):t.recent.map(n=>e.jsxs("div",{className:"flex items-center gap-2 text-xs text-[#B8C7D9]",children:[e.jsx("span",{className:"text-[#9FB3C8] w-20 shrink-0",children:B(n.posted_at||n.started_at)}),e.jsx("span",{className:"flex-1 truncate text-[#E6EDF3]",children:ne(V[n.vehicle_id],n.listing_title)}),e.jsx("span",{className:"text-[10px] text-[#9FB3C8]",children:n.status})]},n.id))]})]},t.user_id)})})]})]}):e.jsx(z,{to:"/marketplace360",replace:!0})}export{xe 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.