"use client"; import { getVisitorLog } from "@/services/activity-log"; import { convertDateFormat, convertDateFormatNoTime, convertDateFormatNoTimeV2, } from "@/utils/global"; import { Button, Calendar, Pagination, Popover, PopoverContent, PopoverTrigger, Select, SelectItem, Spinner, Table, TableBody, TableCell, TableColumn, TableHeader, TableRow, } from "@heroui/react"; import { Key, useCallback, useEffect, useState } from "react"; import { parseDate } from "@internationalized/date"; import Link from "next/link"; import { close, loading } from "@/config/swal"; const columns = [ { name: "No", uid: "no" }, { name: "Browser", uid: "browserName" }, { name: "Date & Time ", uid: "createdAt" }, { name: "IP Visitors", uid: "visitorIp" }, { name: "URL", uid: "url" }, { name: "City", uid: "visitorCity" }, { name: "Region", uid: "visitorRegion" }, // { name: "Country", uid: "visitorCountry" }, ]; const provinces = [ { engName: "ACEH", inName: "ACEH" }, { engName: "NORTH SUMATRA", inName: "SUMATERA UTARA" }, { engName: "WEST SUMATRA", inName: "SUMATERA BARAT" }, { engName: "RIAU", inName: "RIAU" }, { engName: "JAMBI", inName: "JAMBI" }, { engName: "SOUTH SUMATRA", inName: "SUMATERA SELATAN" }, { engName: "BENGKULU", inName: "BENGKULU" }, { engName: "LAMPUNG", inName: "LAMPUNG" }, { engName: "BANGKA BELITUNG ISLANDS", inName: "KEPULAUAN BANGKA BELITUNG" }, { engName: "RIAU ISLANDS", inName: "KEPULAUAN RIAU" }, { engName: "JAKARTA", inName: "DKI JAKARTA" }, { engName: "WEST JAVA", inName: "JAWA BARAT" }, { engName: "CENTRAL JAVA", inName: "JAWA TENGAH" }, { engName: "YOGYAKARTA", inName: "DI YOGYAKARTA" }, { engName: "EAST JAVA", inName: "JAWA TIMUR" }, { engName: "BANTEN", inName: "BANTEN" }, { engName: "BALI", inName: "BALI" }, { engName: "WEST NUSA TENGGARA", inName: "NUSA TENGGARA BARAT" }, { engName: "EAST NUSA TENGGARA", inName: "NUSA TENGGARA TIMUR" }, { engName: "WEST KALIMANTAN", inName: "KALIMANTAN BARAT" }, { engName: "CENTRAL KALIMANTAN", inName: "KALIMANTAN TENGAH" }, { engName: "SOUTH KALIMANTAN", inName: "KALIMANTAN SELATAN" }, { engName: "EAST KALIMANTAN", inName: "KALIMANTAN TIMUR" }, { engName: "NORTH KALIMANTAN", inName: "KALIMANTAN UTARA" }, { engName: "NORTH SULAWESI", inName: "SULAWESI UTARA" }, { engName: "CENTRAL SULAWESI", inName: "SULAWESI TENGAH" }, { engName: "SOUTH SULAWESI", inName: "SULAWESI SELATAN" }, { engName: "SOUTHEAST SULAWESI", inName: "SULAWESI TENGGARA" }, { engName: "GORONTALO", inName: "GORONTALO" }, { engName: "WEST SULAWESI", inName: "SULAWESI BARAT" }, { engName: "MALUKU", inName: "MALUKU" }, { engName: "NORTH MALUKU", inName: "MALUKU UTARA" }, { engName: "PAPUA", inName: "PAPUA" }, { engName: "WEST PAPUA", inName: "PAPUA BARAT" }, { engName: "SOUTH PAPUA", inName: "PAPUA SELATAN" }, { engName: "CENTRAL PAPUA", inName: "PAPUA TENGAH" }, { engName: "HIGHLAND PAPUA", inName: "PAPUA PEGUNUNGAN" }, { engName: "SOUTHWEST PAPUA", inName: "PAPUA BARAT DAYA" }, ]; const findRegion = (name: string): string => { const find = provinces.find((a) => a.engName === name); return find ? find.inName : ""; }; export default function DashboardVisitorsTable() { const [page, setPage] = useState(0); const [totalPage, setTotalPage] = useState(1); const [showData, setShowData] = useState("10"); const [tableVisitorData, setTableVisitorData] = useState([]); const [visitorBrowserDate, setVisitorBrowserDate] = useState({ startDate: parseDate( convertDateFormatNoTimeV2( new Date(new Date().setDate(new Date().getDate() - 7)) ) ), endDate: parseDate(convertDateFormatNoTimeV2(new Date())), }); useEffect(() => { initFetch(); }, [page, showData]); useEffect(() => { if (page == 0) { initFetch(); } else { setPage(0); } }, [visitorBrowserDate.startDate, visitorBrowserDate.endDate]); const initFetch = async () => { const getDate = (data: any) => { return `${data.year}-${data.month < 10 ? `0${data.month}` : data.month}-${ data.day < 10 ? `0${data.day}` : data.day }`; }; loading(); const res = await getVisitorLog({ page: page, limit: showData, startDate: getDate(visitorBrowserDate.startDate), endDate: getDate(visitorBrowserDate.endDate), }); getTableNumber(Number(showData), res?.data?.data); setTotalPage(res?.data?.meta?.totalPage); close(); }; const getTableNumber = (limit: number, data: any) => { if (data) { const startIndex = limit * page; let iterate = 0; const newData = data.map((value: any) => { iterate++; value.no = startIndex + iterate; return value; }); setTableVisitorData(newData); } else { setTableVisitorData([]); } }; const renderCell = useCallback( (visitor: any, columnKey: Key) => { const cellValue = visitor[columnKey as keyof any]; switch (columnKey) { case "url": return ( {cellValue.replace("new-", "")} ); case "browserName": return (
{cellValue}{" "} {cellValue !== null ? "Ver " + visitor.browserVersion : ""}
); case "visitorRegion": return (

{cellValue ? findRegion(cellValue.toUpperCase()).toLowerCase() : ""}

); case "createdAt": return

{convertDateFormat(visitor.createdAt)}

; default: return cellValue; } }, [tableVisitorData] ); return (

Visitors Table

Show Data

Start Date

setVisitorBrowserDate({ startDate: e, endDate: visitorBrowserDate.endDate, }) } maxValue={visitorBrowserDate.endDate} />

End Date

setVisitorBrowserDate({ startDate: visitorBrowserDate.startDate, endDate: e, }) } minValue={visitorBrowserDate.startDate} />
{(column) => ( {column.name} )} } > {(item: any) => ( {(columnKey) => ( {renderCell(item, columnKey)} )} )}
setPage(page)} />
); }