forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionDuration.tsx
More file actions
136 lines (127 loc) · 4.22 KB
/
Copy pathSessionDuration.tsx
File metadata and controls
136 lines (127 loc) · 4.22 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
'use client';
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
LineElement,
PointElement,
Tooltip,
Legend,
} from 'chart.js';
import type { TooltipItem } from 'chart.js';
import { Bar } from 'react-chartjs-2';
import { exportRowsToCsv } from '@/lib/export';
import ExportButton from '@/components/ui/ExportButton';
ChartJS.register(CategoryScale, LinearScale, BarElement, LineElement, PointElement, Tooltip, Legend);
const buckets = [
{ range: '0–1 min', users: 1240 },
{ range: '1–2 min', users: 2870 },
{ range: '2–5 min', users: 4310 },
{ range: '5–10 min', users: 3890 },
{ range: '10–20 min', users: 2540 },
{ range: '20–30 min', users: 1180 },
{ range: '30–60 min', users: 640 },
{ range: '60+ min', users: 230 },
];
// Weighted average: midpoint seconds per bucket
const midpoints = [30, 90, 210, 450, 900, 1500, 2700, 4500];
const totalUsers = buckets.reduce((s, b) => s + b.users, 0);
const avgSeconds = Math.round(
buckets.reduce((s, b, i) => s + b.users * midpoints[i], 0) / totalUsers,
);
const avgLabel = avgSeconds < 60 ? `${avgSeconds}s` : `${Math.floor(avgSeconds / 60)}m ${avgSeconds % 60}s`;
// Median bucket index (cumulative)
let cumulative = 0;
let medianIdx = 0;
for (let i = 0; i < buckets.length; i++) {
cumulative += buckets[i].users;
if (cumulative >= totalUsers / 2) { medianIdx = i; break; }
}
export default function SessionDuration() {
const data = {
labels: buckets.map((b) => b.range),
datasets: [
{
label: 'Users',
data: buckets.map((b) => b.users),
backgroundColor: buckets.map((_, i) =>
i === medianIdx ? 'rgba(251, 191, 36, 0.85)' : 'rgba(99, 102, 241, 0.75)',
),
borderColor: buckets.map((_, i) =>
i === medianIdx ? 'rgba(251, 191, 36, 1)' : 'rgba(99, 102, 241, 1)',
),
borderWidth: 1,
borderRadius: 4,
barPercentage: 0.9,
categoryPercentage: 0.85,
},
],
};
const options = {
responsive: true,
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: 'rgba(17,24,39,0.9)',
titleColor: '#f9fafb',
bodyColor: '#d1d5db',
padding: 12,
cornerRadius: 8,
callbacks: {
label: (item: TooltipItem<'bar'>) => {
const pct = ((item.raw as number) / totalUsers * 100).toFixed(1);
return ` ${(item.raw as number).toLocaleString()} users (${pct}%)`;
},
afterLabel: (_: TooltipItem<'bar'>) =>
_ .dataIndex === medianIdx ? ' ← median bucket' : '',
},
},
},
scales: {
x: {
grid: { display: false },
ticks: { color: '#9ca3af', font: { size: 11 } },
title: { display: true, text: 'Session duration', color: '#6b7280', font: { size: 12 } },
},
y: {
beginAtZero: true,
grid: { color: 'rgba(0,0,0,0.05)' },
ticks: { color: '#9ca3af', font: { size: 11 } },
border: { display: false },
title: { display: true, text: 'Users', color: '#6b7280', font: { size: 12 } },
},
},
};
return (
<div className="space-y-4">
<div className="flex items-center justify-between">
<div className="flex gap-6 text-sm">
<div>
<span className="text-gray-500">Total sessions </span>
<span className="font-semibold">{totalUsers.toLocaleString()}</span>
</div>
<div>
<span className="text-gray-500">Avg duration </span>
<span className="font-semibold">{avgLabel}</span>
</div>
<div className="flex items-center gap-1">
<span className="inline-block w-3 h-3 rounded-sm bg-yellow-400" />
<span className="text-gray-500">Median bucket: </span>
<span className="font-semibold">{buckets[medianIdx].range}</span>
</div>
</div>
<ExportButton
onExport={(onProgress) =>
exportRowsToCsv({
filenamePrefix: 'session-duration',
rows: buckets.map((b) => ({ bucket: b.range, users: b.users })),
onProgress,
})
}
/>
</div>
<Bar data={data} options={options} />
</div>
);
}