forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTorrentActivityHeatmap.tsx
More file actions
139 lines (126 loc) · 4.39 KB
/
Copy pathTorrentActivityHeatmap.tsx
File metadata and controls
139 lines (126 loc) · 4.39 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
// src/components/charts/TorrentActivityHeatmap.tsx
"use client"
import type { EChartsOption } from "echarts"
import { useMemo } from "react"
import { hexToRgba } from "@/lib/color-utils"
import { formatCount } from "@/lib/formatters"
import { ChartECharts } from "./lib/ChartECharts"
import { ChartEmptyState } from "./lib/ChartEmptyState"
import { DAY_LABELS, HOUR_LABELS } from "./lib/chart-helpers"
import { buildActivityMatrix } from "./lib/chart-transforms"
import { CHART_THEME, chartAxisLabel, chartTooltip, escHtml } from "./lib/theme"
// Pre-aggregated props (fleet dashboard path)
interface PreAggregatedProps {
grid: { data: [number, number, number][]; maxCount: number }
accentColor?: string
height?: number
}
// Raw torrents props (per-tracker page path)
interface RawTorrentsProps {
torrents: { addedAt: number }[]
accentColor?: string
height?: number
}
type TorrentActivityHeatmapProps = PreAggregatedProps | RawTorrentsProps
function buildHeatmapOption(
data: [number, number, number][],
maxCount: number,
accentColor: string
): EChartsOption {
return {
backgroundColor: "transparent",
tooltip: chartTooltip("item", {
borderColor: accentColor,
formatter: (params: unknown) => {
const p = params as { value: [number, number, number] }
const [hour, day, count] = p.value
const dayLabel = DAY_LABELS[day] ?? ""
const hourLabel = HOUR_LABELS[hour] ?? ""
return (
`<span style="color:${CHART_THEME.textPrimary};font-weight:600;">${escHtml(dayLabel)} at ${escHtml(hourLabel)}</span><br/>` +
`<span style="color:${CHART_THEME.textSecondary};">${formatCount(count)} torrent${count !== 1 ? "s" : ""} added</span>`
)
},
}),
grid: { left: 48, right: 24, top: 16, bottom: 40 },
xAxis: {
type: "category",
data: HOUR_LABELS,
splitArea: { show: false },
axisLine: { lineStyle: { color: CHART_THEME.gridLine } },
axisTick: { show: false },
axisLabel: chartAxisLabel({
interval: 1,
formatter: (_val: string, index: number) => (index % 2 === 0 ? _val : ""),
}),
},
yAxis: {
type: "category",
data: DAY_LABELS,
splitArea: { show: false },
axisLine: { show: false },
axisTick: { show: false },
axisLabel: chartAxisLabel(),
},
visualMap: {
min: 0,
max: Math.max(maxCount, 1),
show: false,
inRange: {
color: [
CHART_THEME.gridLine,
hexToRgba(accentColor, 0.3),
hexToRgba(accentColor, 0.6),
accentColor,
],
},
},
series: [
{
type: "heatmap",
data,
label: { show: false },
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowColor: hexToRgba(accentColor, 0.4),
},
},
itemStyle: {
borderRadius: 3,
borderColor: CHART_THEME.surface,
borderWidth: 2,
},
},
],
}
}
function TorrentActivityHeatmap(props: TorrentActivityHeatmapProps) {
const { accentColor = CHART_THEME.accent, height = 240 } = props
const isFleet = "grid" in props
const fleetGrid = isFleet ? (props as PreAggregatedProps).grid : null
const rawTorrents = !isFleet ? (props as RawTorrentsProps).torrents : []
const fleetOption = useMemo<EChartsOption | null>(() => {
if (!fleetGrid || fleetGrid.maxCount === 0) return null
return buildHeatmapOption(fleetGrid.data, fleetGrid.maxCount, accentColor)
}, [fleetGrid, accentColor])
const perTrackerOption = useMemo<EChartsOption | null>(() => {
if (isFleet) return null
const validTimestamps = rawTorrents.map((t) => t.addedAt).filter((ts) => ts > 0)
const { data, maxCount } = buildActivityMatrix(validTimestamps)
if (maxCount === 0) return null
return buildHeatmapOption(data, maxCount, accentColor)
}, [isFleet, rawTorrents, accentColor])
if (isFleet) {
if (!fleetGrid || fleetGrid.maxCount === 0 || !fleetOption) {
return <ChartEmptyState height={height} message="No activity data" />
}
return <ChartECharts option={fleetOption} style={{ height, width: "100%" }} />
}
if (!perTrackerOption) {
return <ChartEmptyState height={height} message="No activity data" />
}
return <ChartECharts option={perTrackerOption} style={{ height, width: "100%" }} />
}
export type { TorrentActivityHeatmapProps }
export { TorrentActivityHeatmap }