1import{r as w,j as e}from"./vendor-react-DQQWdOKu.js";import{m as E,o as L,p as B,q as U,s as d,u as F,f as k,j as _,J as H,g as A,h as C,i as G,G as Q,B as J}from"./index-C8IO8FdB.js";import{u as K}from"./useMutation-DuVW3JiO.js";import{B as I}from"./badge-ixBf9BUj.js";import{T as R,a as W,b as D,c as h,d as $,e as f}from"./table-CT-l--Yl.js";import{S as z}from"./skeleton-BPope85i.js";import{S as V}from"./shield-xspC4o9P.js";import{C as X}from"./circle-check-Da0-kJV2.js";import{M as T}from"./monitor-u57nUuVr.js";import{S as Y}from"./smartphone-uALbxT4g.js";import{C as Z}from"./clock-7xFdzbGz.js";import{L as ee}from"./log-out-BW4fYPqm.js";import"./vendor-ui-QYDqtm38.js";import"./vendor-i18n-CULzOv1i.js";import"./vendor-charts-C8Nx4tSC.js";import"./vendor-motion-CDSjW_qk.js";function j(n){if(!n)return{browser:"Unknown",os:"Unknown",device:"Desktop"};let i="Unknown",a="Unknown",g="Desktop";return n.includes("Firefox")?i="Firefox":n.includes("Chrome")?i="Chrome":n.includes("Safari")?i="Safari":n.includes("Edge")?i="Edge":n.includes("Opera")&&(i="Opera"),n.includes("Windows")?a="Windows":n.includes("Mac")?a="macOS":n.includes("Linux")?a="Linux":n.includes("Android")?a="Android":(n.includes("iOS")||n.includes("iPhone")||n.includes("iPad"))&&(a="iOS"),n.includes("Mobile")||n.includes("Android")||n.includes("iPhone")?g="Mobile":(n.includes("Tablet")||n.includes("iPad"))&&(g="Tablet"),{browser:i,os:a,device:g}}function pe(){const{isSuperAdmin:n,user:i,session:a,role:g}=E();L("sessions");const{toast:b}=B(),O=U(),[o,P]=w.useState(null);w.useEffect(()=>{(async()=>{if(!i||!a)return;const r=j(navigator.userAgent),{data:c,error:p}=await d.from("admin_sessions").upsert({user_id:i.id,session_token:a.access_token.substring(0,50),ip_address:"client",user_agent:navigator.userAgent,device_info:r,is_current:!0,last_activity:new Date().toISOString(),expires_at:new Date(Date.now()+30*60*1e3).toISOString()},{onConflict:"session_token"}).select().single();c&&P(c.id)})();const t=setInterval(async()=>{o&&await d.from("admin_sessions").update({last_activity:new Date().toISOString(),expires_at:new Date(Date.now()+30*60*1e3).toISOString()}).eq("id",o);const r=new Date(Date.now()-30*60*1e3).toISOString();await d.from("admin_sessions").delete().lt("last_activity",r)},6e4);return()=>{clearInterval(t)}},[i,a,o]),w.useEffect(()=>{const s=async()=>{var t;o&&((t=navigator.sendBeacon)==null||t.call(navigator,`https://atfrjhydmhpdwfepbkij.supabase.co/rest/v1/admin_sessions?id=eq.${o}`,""))};return window.addEventListener("beforeunload",s),()=>window.removeEventListener("beforeunload",s)},[o]);const{data:l,isLoading:M,refetch:S}=F({queryKey:["admin-sessions",n,g],queryFn:async()=>{const s=new Date(Date.now()-18e5).toISOString();await d.from("admin_sessions").delete().lt("last_activity",s);let t=d.from("admin_sessions").select("*").order("last_activity",{ascending:!1});n||(t=t.eq("user_id",i==null?void 0:i.id));const{data:r,error:c}=await t;if(c)throw c;const p=[...new Set((r==null?void 0:r.map(m=>m.user_id))||[])],{data:u}=await d.from("profiles").select("id, username, full_name").in("id",p),x=u==null?void 0:u.reduce((m,v)=>(m[v.id]={username:v.username,full_name:v.full_name},m),{});return r==null?void 0:r.map(m=>({...m,profiles:x==null?void 0:x[m.user_id]}))},enabled:!!i,refetchInterval:1e4});w.useEffect(()=>{if(!i)return;const s=d.channel("admin-sessions-realtime").on("postgres_changes",{event:"*",schema:"public",table:"admin_sessions"},()=>{S()}).subscribe();return()=>{d.removeChannel(s)}},[i,S]);const y=K({mutationFn:async s=>{const{error:t}=await d.from("admin_sessions").delete().eq("id",s);if(t)throw t;await d.rpc("log_audit_event",{p_action:"terminate_session",p_resource_type:"admin_session",p_resource_id:s})},onSuccess:()=>{O.invalidateQueries({queryKey:["admin-sessions"]}),b({title:"Sesi berhasil dihentikan"})},onError:s=>{b({title:"Gagal menghentikan sesi",description:s.message,variant:"destructive"})}});l==null||l.reduce((s,t)=>{const r=t.user_id;return s[r]||(s[r]=[]),s[r].push(t),s},{});const N=(l==null?void 0:l.filter(s=>s.user_id===(i==null?void 0:i.id)))||[],q=N.length>1;return i?e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold",children:"Sesi Aktif"}),e.jsx("p",{className:"text-muted-foreground",children:n?"Pantau semua sesi login aktif di sistem":"Lihat sesi login aktif akun Anda"})]}),q&&e.jsx(k,{className:"border-yellow-200 bg-yellow-50 dark:border-yellow-900 dark:bg-yellow-950",children:e.jsx(_,{className:"pt-6",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(H,{className:"h-5 w-5 text-yellow-600 dark:text-yellow-500 mt-0.5"}),e.jsxs("div",{children:[e.jsxs("p",{className:"font-medium text-yellow-800 dark:text-yellow-200",children:["Akun And
1a sedang digunakan di ",N.length," perangkat"]}),e.jsx("p",{className:"text-sm text-yellow-700 dark:text-yellow-300 mt-1",children:"Jika Anda tidak mengenali salah satu sesi, segera hentikan sesi tersebut dan ubah password Anda."})]})]})})}),e.jsxs(k,{children:[e.jsxs(A,{children:[e.jsxs(C,{className:"flex items-center gap-2",children:[e.jsx(X,{className:"h-5 w-5 text-green-500"}),"Sesi Saat Ini"]}),e.jsx(G,{children:"Perangkat yang sedang Anda gunakan"})]}),e.jsx(_,{children:e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("div",{className:"flex-shrink-0",children:e.jsx(T,{className:"h-10 w-10 text-muted-foreground"})}),e.jsxs("div",{className:"flex-1",children:[e.jsxs("p",{className:"font-medium",children:[j(navigator.userAgent).browser," di ",j(navigator.userAgent).os]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[j(navigator.userAgent).device," ⢠Aktif sekarang"]})]}),e.jsx(I,{variant:"outline",className:"bg-green-50 text-green-700 border-green-200",children:"Perangkat Ini"})]})})]}),e.jsxs(k,{children:[e.jsx(A,{children:e.jsx(C,{children:n?"Semua Sesi Aktif":"Sesi Anda"})}),e.jsx(_,{children:M?e.jsx("div",{className:"space-y-3",children:[...Array(3)].map((s,t)=>e.jsx(z,{className:"h-16"},t))}):l!=null&&l.length?e.jsxs(R,{children:[e.jsx(W,{children:e.jsxs(D,{children:[n&&e.jsx(h,{children:"Pengguna"}),e.jsx(h,{children:"Perangkat"}),e.jsx(h,{children:"Browser"}),e.jsx(h,{children:"IP Address"}),e.jsx(h,{children:"Terakhir Aktif"}),e.jsx(h,{children:"Aksi"})]})}),e.jsx($,{children:l.map(s=>{var p,u,x;const t=s.device_info||j(s.user_agent),r=s.id===o,c=t.device==="Mobile"?Y:T;return e.jsxs(D,{className:r?"bg-green-50 dark:bg-green-950":"",children:[n&&e.jsx(f,{children:e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:((p=s.profiles)==null?void 0:p.full_name)||((u=s.profiles)==null?void 0:u.username)}),e.jsx("p",{className:"text-sm text-muted-foreground",children:(x=s.profiles)==null?void 0:x.username})]})}),e.jsx(f,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(c,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{children:t.os}),r&&e.jsx(I,{variant:"outline",className:"text-xs",children:"Saat ini"})]})}),e.jsx(f,{children:t.browser}),e.jsx(f,{children:e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(Q,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{className:"text-sm",children:s.ip_address||"-"})]})}),e.jsx(f,{children:e.jsxs("div",{className:"flex items-center gap-1 text-sm text-muted-foreground",children:[e.jsx(Z,{className:"h-4 w-4"}),new Date(s.last_activity).toLocaleString("id-ID")]})}),e.jsx(f,{children:!r&&e.jsxs(J,{size:"sm",variant:"destructive",onClick:()=>y.mutate(s.id),disabled:y.isPending,children:[e.jsx(ee,{className:"h-4 w-4 mr-1"}),"Hentikan"]})})]},s.id)})})]}):e.jsx("p",{className:"text-center py-8 text-muted-foreground",children:"Tidak ada sesi aktif"})})]})]}):e.jsxs("div",{className:"text-center py-12",children:[e.jsx(V,{className:"h-12 w-12 text-muted-foreground mx-auto mb-4"}),e.jsx("h2",{className:"text-xl font-bold mb-2",children:"Akses Ditolak"}),e.jsx("p",{className:"text-muted-foreground",children:"Silakan login terlebih dahulu"})]})}export{pe as default}; 2//# sourceMappingURL=SessionsManagement-B-nprX2w.js.map
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.