From 7e65eba01f5a43a088ad56023f10f78842c06ab5 Mon Sep 17 00:00:00 2001 From: Yoga Pratama Date: Mon, 9 Nov 2026 16:13:33 +0700 Subject: [PATCH] feat(dashboard): add bidirectional Inbound (Luar -> Agent) and Outbound (Agent -> Luar) animated lines on GlobeMap --- src/app/(dashboard)/page.tsx | 82 +++++++++++++++++-------------- src/components/ui/GlobeMap.tsx | 41 ++++++++++------ src/components/ui/useGlobeData.ts | 64 +++++++++++++++++------- 3 files changed, 117 insertions(+), 70 deletions(-) diff --git a/src/app/(dashboard)/page.tsx b/src/app/(dashboard)/page.tsx index c2a3ab3..b8f5355 100644 --- a/src/app/(dashboard)/page.tsx +++ b/src/app/(dashboard)/page.tsx @@ -125,6 +125,49 @@ export default function SummaryPage() { const arcs: any[] = []; const minShareThreshold = countries.data.length > 3 ? totalTraffic * 0.0025 : 0; + const pushBidirectionalArcs = ( + origin: GeoLocation, + dest: GeoLocation, + downloadBytes: number, + uploadBytes: number, + countryCode: string + ) => { + const dl = downloadBytes || 0; + const ul = uploadBytes || 0; + if (ul > 0) { + arcs.push({ + startLat: origin.lat, + startLng: origin.lng, + endLat: dest.lat, + endLng: dest.lng, + fromLabel: origin.label, + toLabel: dest.label, + value: ul, + download: dl, + upload: ul, + countryCode, + direction: 'outbound', + curveSign: 1, + }); + } + if (dl > 0) { + arcs.push({ + startLat: dest.lat, + startLng: dest.lng, + endLat: origin.lat, + endLng: origin.lng, + fromLabel: dest.label, + toLabel: origin.label, + value: dl, + download: dl, + upload: ul, + countryCode, + direction: 'inbound', + curveSign: -0.65, + }); + } + }; + for (const row of countries.data) { const traffic = (row.download || 0) + (row.upload || 0); if (traffic === 0 || traffic < minShareThreshold) continue; @@ -134,50 +177,17 @@ export default function SummaryPage() { if (activeAgentUuid) { const origin = resolveOriginForAgent(activeAgentUuid, activeAgentLabel); - arcs.push({ - startLat: origin.lat, - startLng: origin.lng, - endLat: dest.lat, - endLng: dest.lng, - fromLabel: origin.label, - toLabel: dest.label, - value: traffic, - download: row.download, - upload: row.upload, - countryCode: row.country_code, - }); + pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code); } else if (Array.isArray(row.agent_breakdown) && row.agent_breakdown.length > 0) { for (const ag of row.agent_breakdown) { const agTraffic = (ag.download || 0) + (ag.upload || 0); if (agTraffic === 0) continue; const origin = resolveOriginForAgent(ag.agent_uuid); - arcs.push({ - startLat: origin.lat, - startLng: origin.lng, - endLat: dest.lat, - endLng: dest.lng, - fromLabel: origin.label, - toLabel: dest.label, - value: agTraffic, - download: ag.download, - upload: ag.upload, - countryCode: row.country_code, - }); + pushBidirectionalArcs(origin, dest, ag.download, ag.upload, row.country_code); } } else { const origin = resolveOriginForAgent(null); - arcs.push({ - startLat: origin.lat, - startLng: origin.lng, - endLat: dest.lat, - endLng: dest.lng, - fromLabel: origin.label, - toLabel: dest.label, - value: traffic, - download: row.download, - upload: row.upload, - countryCode: row.country_code, - }); + pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code); } } diff --git a/src/components/ui/GlobeMap.tsx b/src/components/ui/GlobeMap.tsx index e049124..b471d59 100644 --- a/src/components/ui/GlobeMap.tsx +++ b/src/components/ui/GlobeMap.tsx @@ -1,6 +1,6 @@ "use client"; -import React, { useMemo, useState } from 'react'; +import React, { useState } from 'react'; import { ComposableMap, Geographies, Geography, Line, Marker, ZoomableGroup } from "react-simple-maps"; import { useTheme } from "next-themes"; import { useGlobeData, GlobeDataInput } from './useGlobeData'; @@ -22,7 +22,6 @@ export const GlobeMap = ({ data }: GlobeMapProps) => { const { arcsData, pointsData, labelsData } = useGlobeData(data, isLight); - const mapBgColor = "transparent"; const mapBorderColor = isLight ? "#ffffff" : "#0ea5e9"; const mapFillColor = isLight ? "#4ade80" : "#0f172a"; const mapHoverFill = isLight ? "#22c55e" : "#1e293b"; @@ -78,6 +77,7 @@ export const GlobeMap = ({ data }: GlobeMapProps) => { {arcsData.map((arc: any, i: number) => { const isLocal = arc.startLng === arc.endLng && arc.startLat === arc.endLat; const color = Array.isArray(arc.color) ? arc.color[0] : arc.color || '#0ea5e9'; + const curveSign = typeof arc.curveSign === 'number' ? arc.curveSign : 1; if (isLocal) { return ( @@ -87,23 +87,18 @@ export const GlobeMap = ({ data }: GlobeMapProps) => { ); } - // Force the line to go straight across the map (the "front") - // by interpolating multiple points. This prevents d3-geo from - // wrapping around the dateline for long distances. const coordinates: [number, number][] = []; const steps = 30; + const dist = Math.sqrt( + Math.pow(arc.endLng - arc.startLng, 2) + + Math.pow(arc.endLat - arc.startLat, 2) + ); + const height = Math.min(dist * 0.15, 25); + for (let j = 0; j <= steps; j++) { const t = j / steps; - let lng = arc.startLng + (arc.endLng - arc.startLng) * t; - - // Dynamic arc height based on distance - const dist = Math.sqrt( - Math.pow(arc.endLng - arc.startLng, 2) + - Math.pow(arc.endLat - arc.startLat, 2) - ); - const height = Math.min(dist * 0.15, 25); - let lat = arc.startLat + (arc.endLat - arc.startLat) * t + Math.sin(t * Math.PI) * height; - + const lng = arc.startLng + (arc.endLng - arc.startLng) * t; + const lat = arc.startLat + (arc.endLat - arc.startLat) * t + Math.sin(t * Math.PI) * height * curveSign; coordinates.push([lng, lat]); } @@ -147,6 +142,22 @@ export const GlobeMap = ({ data }: GlobeMapProps) => { ))} + + {/* Direction & Node Legend Overlay */} +
+ + + Agent Server + + + + Outbound (Agent → Luar) + + + + Inbound (Luar → Agent) + +
); }; diff --git a/src/components/ui/useGlobeData.ts b/src/components/ui/useGlobeData.ts index c9ab52e..96e2907 100644 --- a/src/components/ui/useGlobeData.ts +++ b/src/components/ui/useGlobeData.ts @@ -13,14 +13,22 @@ export interface GlobeDataInput { download: number; upload: number; countryCode?: string; + direction?: 'outbound' | 'inbound'; + curveSign?: number; } export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { const arcsData = useMemo(() => { - return data.map(d => ({ - ...d, - color: isLight ? ['#38bdf8', '#0284c7'] : ['#0ea5e9', '#38bdf8'], - })); + return data.map(d => { + const isInbound = d.direction === 'inbound'; + const color = isInbound + ? (isLight ? ['#10b981', '#059669'] : ['#34d399', '#10b981']) + : (isLight ? ['#38bdf8', '#0284c7'] : ['#0ea5e9', '#38bdf8']); + return { + ...d, + color, + }; + }); }, [data, isLight]); const pointsData = useMemo(() => { @@ -29,16 +37,25 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { const destSet = new Set(); arcsData.forEach(arc => { - const originKey = `${arc.startLat},${arc.startLng}`; - const destKey = `${arc.endLat},${arc.endLng}`; + const isInbound = arc.direction === 'inbound'; + const agentLat = isInbound ? arc.endLat : arc.startLat; + const agentLng = isInbound ? arc.endLng : arc.startLng; + const agentLabel = isInbound ? arc.toLabel : arc.fromLabel; + + const extLat = isInbound ? arc.startLat : arc.endLat; + const extLng = isInbound ? arc.startLng : arc.endLng; + const extLabel = isInbound ? arc.fromLabel : arc.toLabel; + + const originKey = `${agentLat},${agentLng}`; + const destKey = `${extLat},${extLng}`; if (!destSet.has(destKey)) { points.push({ - lat: arc.endLat, - lng: arc.endLng, + lat: extLat, + lng: extLng, size: 0.1, color: isLight ? '#0284c7' : '#38bdf8', - name: arc.toLabel, + name: extLabel, value: arc.value, upload: arc.upload, download: arc.download, @@ -49,11 +66,11 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { if (!originSet.has(originKey)) { points.push({ - lat: arc.startLat, - lng: arc.startLng, + lat: agentLat, + lng: agentLng, size: 0.2, color: '#f43f5e', - name: arc.fromLabel, + name: agentLabel, value: 0, upload: 0, download: 0, @@ -72,18 +89,27 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { const destSet = new Set(); arcsData.forEach(arc => { - const originKey = `${arc.startLat},${arc.startLng}`; - const destKey = `${arc.endLat},${arc.endLng}`; + const isInbound = arc.direction === 'inbound'; + const agentLat = isInbound ? arc.endLat : arc.startLat; + const agentLng = isInbound ? arc.endLng : arc.startLng; + const agentLabel = isInbound ? arc.toLabel : arc.fromLabel; + + const extLat = isInbound ? arc.startLat : arc.endLat; + const extLng = isInbound ? arc.startLng : arc.endLng; + const extLabel = isInbound ? arc.fromLabel : arc.toLabel; + + const originKey = `${agentLat},${agentLng}`; + const destKey = `${extLat},${extLng}`; if (!destSet.has(destKey)) { // Shift and stagger coordinates (Southeast Asia, Europe, Caribbean, Indian Ocean, Latin America) - const { lat, lng } = getStaggeredCoordinates(arc.countryCode || '', arc.endLat, arc.endLng); + const { lat, lng } = getStaggeredCoordinates(arc.countryCode || '', extLat, extLng); const size = SMALL_LABEL_COUNTRIES.has(arc.countryCode || '') ? 0.9 : 1.4; labels.push({ lat, lng, - name: arc.toLabel, + name: extLabel, color: isLight ? '#0f172a' : '#f8fafc', size }); @@ -92,9 +118,9 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { if (!originSet.has(originKey)) { labels.push({ - lat: arc.startLat, - lng: arc.startLng, - name: arc.fromLabel, + lat: agentLat, + lng: agentLng, + name: agentLabel, color: isLight ? '#0f172a' : '#f8fafc', size: 1.4 });