forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCampaignAnalytics.js
More file actions
171 lines (157 loc) · 7.18 KB
/
Copy pathCampaignAnalytics.js
File metadata and controls
171 lines (157 loc) · 7.18 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use client';
import { useState, useEffect, useCallback, useRef } from 'react';
import api from '../lib/api';
import { formatTokenAmount } from '../lib/formatting';
const POLL_INTERVAL = 15000;
/**
* Real-time campaign analytics dashboard with participant list.
*/
export default function CampaignAnalytics({ merchantId, apiKey }) {
const [campaigns, setCampaigns] = useState([]);
const [selected, setSelected] = useState(null);
const [analytics, setAnalytics] = useState(null);
const [participants, setParticipants] = useState([]);
const [partSearch, setPartSearch] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const pollRef = useRef(null);
const loadCampaigns = useCallback(async () => {
if (!merchantId) return;
try {
const res = await api.get(`/api/campaigns/${merchantId}`);
const list = res.data.data || [];
setCampaigns(list);
if (!selected && list.length) setSelected(list[0].id);
} catch {
// ignore
}
}, [merchantId, selected]);
const loadAnalytics = useCallback(async () => {
if (!selected) return;
setLoading(true);
setError('');
try {
const [analyticsRes, participantsRes] = await Promise.allSettled([
api.get(`/api/campaigns/${selected}/analytics`, { headers: { 'x-api-key': apiKey } }),
api.get(`/api/campaigns/${selected}/participants`, { headers: { 'x-api-key': apiKey } }),
]);
if (analyticsRes.status === 'fulfilled') setAnalytics(analyticsRes.value.data.data || analyticsRes.value.data);
if (participantsRes.status === 'fulfilled') setParticipants(participantsRes.value.data.data || []);
} catch (err) {
setError('Failed to load analytics.');
} finally {
setLoading(false);
}
}, [selected, apiKey]);
useEffect(() => { loadCampaigns(); }, [loadCampaigns]);
useEffect(() => {
loadAnalytics();
clearInterval(pollRef.current);
pollRef.current = setInterval(loadAnalytics, POLL_INTERVAL);
return () => clearInterval(pollRef.current);
}, [loadAnalytics]);
const filteredParticipants = participants.filter((p) =>
!partSearch || (p.wallet_address || p.email || '').toLowerCase().includes(partSearch.toLowerCase())
);
const Metric = ({ label, value, sub }) => (
<div className="card" style={{ textAlign: 'center', flex: 1, minWidth: '120px' }}>
<p style={{ color: 'var(--muted)', fontSize: '0.8rem', marginBottom: '0.25rem' }}>{label}</p>
<p style={{ fontSize: '1.6rem', fontWeight: 800, color: 'var(--accent)' }}>{value ?? '—'}</p>
{sub && <p style={{ color: 'var(--muted)', fontSize: '0.75rem' }}>{sub}</p>}
</div>
);
return (
<div>
{/* Campaign selector */}
<div style={{ marginBottom: '1.5rem', display: 'flex', gap: '0.75rem', alignItems: 'center', flexWrap: 'wrap' }}>
<label className="label" style={{ marginBottom: 0 }}>Campaign:</label>
<select
className="input"
style={{ marginBottom: 0, width: 'auto', minWidth: '200px' }}
value={selected || ''}
onChange={(e) => setSelected(e.target.value)}
>
{campaigns.map((c) => <option key={c.id} value={c.id}>{c.name}</option>)}
</select>
<button className="btn btn-secondary" onClick={loadAnalytics} disabled={loading}>
{loading ? '…' : '↻ Refresh'}
</button>
<span style={{ color: 'var(--muted)', fontSize: '0.8rem' }}>Auto-refreshes every 15s</span>
</div>
{error && (
<div className="error" style={{ marginBottom: '1rem', padding: '0.75rem', borderRadius: '6px', background: 'rgba(220,38,38,0.1)' }}>
{error}
</div>
)}
{/* Metrics */}
{analytics && (
<div style={{ display: 'flex', gap: '1rem', flexWrap: 'wrap', marginBottom: '1.5rem' }}>
<Metric label="Total Distributed" value={analytics.totalDistributed != null ? `${formatTokenAmount(analytics.totalDistributed)} NOVA` : null} />
<Metric label="Total Redeemed" value={analytics.totalRedeemed != null ? `${formatTokenAmount(analytics.totalRedeemed)} NOVA` : null} />
<Metric label="Participants" value={analytics.participantCount ?? participants.length} />
<Metric label="Transactions" value={analytics.transactionCount} />
<Metric label="Redemption Rate" value={analytics.redemptionRate != null ? `${parseFloat(analytics.redemptionRate).toFixed(1)}%` : null} />
</div>
)}
{/* Performance bar */}
{analytics?.totalDistributed > 0 && analytics?.totalRedeemed != null && (
<div className="card" style={{ marginBottom: '1.5rem' }}>
<p style={{ marginBottom: '0.5rem', fontWeight: 600 }}>Redemption Progress</p>
<div style={{ background: 'var(--surface-2)', borderRadius: '9999px', height: '12px', overflow: 'hidden' }}>
<div style={{
height: '100%',
width: `${Math.min(100, (analytics.totalRedeemed / analytics.totalDistributed) * 100).toFixed(1)}%`,
background: 'var(--accent)',
borderRadius: '9999px',
transition: 'width 0.4s ease',
}} />
</div>
<p style={{ color: 'var(--muted)', fontSize: '0.8rem', marginTop: '0.4rem' }}>
{formatTokenAmount(analytics.totalRedeemed)} / {formatTokenAmount(analytics.totalDistributed)} NOVA redeemed
</p>
</div>
)}
{/* Participant list */}
<div className="card">
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '1rem', flexWrap: 'wrap', gap: '0.5rem' }}>
<h3 style={{ fontWeight: 700 }}>Participants ({filteredParticipants.length})</h3>
<input
className="input"
style={{ marginBottom: 0, width: '220px' }}
placeholder="Filter by wallet or email…"
value={partSearch}
onChange={(e) => setPartSearch(e.target.value)}
/>
</div>
{filteredParticipants.length === 0 ? (
<p style={{ color: 'var(--muted)', textAlign: 'center', padding: '1rem 0' }}>No participants yet.</p>
) : (
<div className="table-scroll">
<table>
<thead>
<tr>
<th>Wallet / Email</th>
<th>Earned (NOVA)</th>
<th>Redeemed (NOVA)</th>
<th>Joined</th>
</tr>
</thead>
<tbody>
{filteredParticipants.map((p, i) => (
<tr key={p.id || i}>
<td style={{ fontFamily: 'monospace', fontSize: '0.85rem' }}>
{p.wallet_address ? `${p.wallet_address.slice(0, 8)}…${p.wallet_address.slice(-4)}` : p.email || '—'}
</td>
<td style={{ color: 'var(--accent)', fontWeight: 600 }}>{formatTokenAmount(p.earned || 0)}</td>
<td>{formatTokenAmount(p.redeemed || 0)}</td>
<td>{p.joined_at ? new Date(p.joined_at).toLocaleDateString() : '—'}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
);
}