File size: 4,961 Bytes
6d9f36a
 
 
 
 
 
 
 
 
 
 
 
 
e84b48e
6d9f36a
 
 
 
 
f7d65f7
6d9f36a
 
 
f7d65f7
6d9f36a
 
 
 
 
 
 
 
 
 
 
 
 
f7d65f7
 
 
 
 
 
 
6d9f36a
 
 
 
 
 
f7d65f7
 
6d9f36a
 
f7d65f7
6d9f36a
 
 
 
f7d65f7
6d9f36a
 
 
 
 
 
 
 
 
f7d65f7
 
6d9f36a
 
f7d65f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d9f36a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { useEffect, useState } from "react";
import { Card } from "@/components/ui/card";
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from "recharts";
import { Users, Loader2 } from "lucide-react";

interface VisitorData {
  timestamp: number;
  count: number;
}

export function VisitorChart() {
  const [data, setData] = useState<VisitorData[]>([]);
  const [loading, setLoading] = useState(true);
  const [days, setDays] = useState(7);

  useEffect(() => {
    fetchVisitorData();
    const interval = setInterval(fetchVisitorData, 5 * 60 * 1000);
    return () => clearInterval(interval);
  }, [days]);

  const fetchVisitorData = async () => {
    try {
      const res = await fetch(`/api/stats/visitors?days=${days}`);
      const json = await res.json();
      if (json.success) {
        setData(json.data);
      }
    } catch (error) {
      console.error("Failed to fetch visitor data:", error);
    } finally {
      setLoading(false);
    }
  };

  const formatXAxis = (timestamp: number) => {
    const date = new Date(timestamp);
    if (days <= 7) {
      return date.toLocaleDateString('id-ID', { month: 'short', day: 'numeric' });
    } else if (days <= 30) {
      return date.toLocaleDateString('id-ID', { month: 'short', day: 'numeric' });
    } else {
      return date.toLocaleDateString('id-ID', { month: 'short', day: 'numeric' });
    }
  };

  const CustomTooltip = ({ active, payload }: any) => {
    if (active && payload && payload.length) {
      const data = payload[0].payload;
      const date = new Date(data.timestamp);
      const dateStr = date.toLocaleDateString('id-ID', {
        weekday: 'short',
        month: 'short',
        day: 'numeric',
        year: 'numeric',
      });

      return (
        <div className="bg-black/90 border border-white/20 rounded-lg p-3 backdrop-blur-sm">
          <p className="text-xs text-gray-400 mb-1">{dateStr}</p>
          <p className="text-sm font-semibold text-purple-400">
            {data.count} visitor{data.count !== 1 ? 's' : ''}
          </p>
        </div>
      );
    }
    return null;
  };

  const totalVisitors = data.reduce((sum, d) => sum + d.count, 0);

  return (
    <Card className="p-6 bg-white/[0.02] border-white/10">
      <div className="flex items-center justify-between mb-4">
        <div className="flex items-center gap-2">
          <Users className="w-5 h-5 text-purple-400" />
          <h3 className="text-lg font-semibold text-white">Visitor Activity</h3>
        </div>
        
        <div className="flex gap-2">
          <button
            onClick={() => setDays(7)}
            className={`px-3 py-1 rounded text-sm transition-colors ${
              days === 7
                ? 'bg-purple-500 text-white'
                : 'bg-white/5 text-gray-400 hover:bg-white/10'
            }`}
          >
            7D
          </button>
          <button
            onClick={() => setDays(30)}
            className={`px-3 py-1 rounded text-sm transition-colors ${
              days === 30
                ? 'bg-purple-500 text-white'
                : 'bg-white/5 text-gray-400 hover:bg-white/10'
            }`}
          >
            30D
          </button>
          <button
            onClick={() => setDays(90)}
            className={`px-3 py-1 rounded text-sm transition-colors ${
              days === 90
                ? 'bg-purple-500 text-white'
                : 'bg-white/5 text-gray-400 hover:bg-white/10'
            }`}
          >
            90D
          </button>
        </div>
      </div>

      <div className="mb-4">
        <p className="text-2xl font-bold text-white">{totalVisitors}</p>
        <p className="text-sm text-gray-400">Total visitors in last {days} days</p>
      </div>

      {loading ? (
        <div className="h-64 flex items-center justify-center">
          <Loader2 className="w-6 h-6 text-purple-400 animate-spin" />
        </div>
      ) : (
        <ResponsiveContainer width="100%" height={300}>
          <LineChart data={data} margin={{ top: 5, right: 30, left: 0, bottom: 5 }}>
            <CartesianGrid strokeDasharray="3 3" stroke="rgba(255,255,255,0.1)" />
            <XAxis
              dataKey="timestamp"
              tickFormatter={formatXAxis}
              stroke="rgba(255,255,255,0.5)"
              style={{ fontSize: '12px' }}
              interval="preserveStartEnd"
            />
            <YAxis
              stroke="rgba(255,255,255,0.5)"
              style={{ fontSize: '12px' }}
              allowDecimals={false}
            />
            <Tooltip content={<CustomTooltip />} />
            <Line
              type="monotone"
              dataKey="count"
              stroke="#a855f7"
              strokeWidth={2}
              dot={{ fill: '#a855f7', r: 3 }}
              activeDot={{ r: 5 }}
            />
          </LineChart>
        </ResponsiveContainer>
      )}
    </Card>
  );
}