forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseChartPreferences.ts
More file actions
149 lines (137 loc) · 4.78 KB
/
Copy pathuseChartPreferences.ts
File metadata and controls
149 lines (137 loc) · 4.78 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
// src/components/dashboard/useChartPreferences.ts
// Manages dashboard chart visibility, collapse state, and display order in localStorage.
// Single source of truth for both inline chart controls and settings sheet.
// Order/reorder logic is dashboard-specific; shared base lives in src/hooks/useChartPreferencesBase.ts.
"use client"
import { useCallback } from "react"
import { useChartPreferencesBase } from "@/hooks/useChartPreferencesBase"
import { STORAGE_KEYS } from "@/lib/storage-keys"
interface ChartDef {
id: string
label: string
description?: string
category: "analytics" | "torrents"
}
interface ChartPrefs {
hidden: string[]
collapsed: string[]
order: string[]
}
const DASHBOARD_CHARTS: ChartDef[] = [
{ id: "daily-volume", label: "Daily Volume", category: "analytics" },
{
id: "volume-calendar",
label: "Volume Calendar",
description: "GitHub-style calendar showing daily upload or download volume over time",
category: "analytics",
},
{
id: "upload-landscape",
label: "Upload Landscape",
description: "Daily upload volume per tracker — drag to rotate, scroll to zoom",
category: "analytics",
},
{ id: "distribution", label: "Distribution", category: "analytics" },
{
id: "volume-heatmap",
label: "Volume Heatmap",
description: "Upload or download volume by day of week and hour — reveals activity patterns",
category: "analytics",
},
{ id: "comparison-uploaded", label: "Total Uploaded", category: "analytics" },
{ id: "comparison-downloaded", label: "Total Downloaded", category: "analytics" },
{ id: "comparison-ratio", label: "Ratio", category: "analytics" },
{ id: "comparison-buffer", label: "Buffer", category: "analytics" },
{ id: "comparison-seedbonus", label: "Seedbonus", category: "analytics" },
{ id: "comparison-active", label: "Active Torrents", category: "analytics" },
{
id: "ratio-stability",
label: "Ratio Stability",
description: "7-point EMA with ±1σ confidence band — wider bands indicate more volatile ratios",
category: "analytics",
},
{
id: "fleet-composition",
label: "Fleet Composition",
description: "Seeding torrent count over time per tracker — stacked to show total fleet size",
category: "analytics",
},
{
id: "rank-tenure",
label: "Rank Tenure",
description: "Time spent at each rank per tracker",
category: "analytics",
},
{
id: "buffer-velocity",
label: "Buffer Velocity",
description: "Rate of buffer change per day — above zero means gaining, below means losing",
category: "analytics",
},
{
id: "buffer-candlestick",
label: "Buffer Candlestick",
description: "Daily buffer open/high/low/close per tracker",
category: "analytics",
},
{
id: "tracker-landscape",
label: "Tracker Landscape",
description: "Position shows upload vs download, size shows fleet",
category: "analytics",
},
{
id: "seedbonus-flow",
label: "Seedbonus Flow",
description: "Seedbonus accumulation streams per tracker over time",
category: "analytics",
},
// Torrents category — placeholder entries (no render cases yet)
{ id: "torrent-overview", label: "Torrent Overview", category: "torrents" },
{ id: "torrent-categories", label: "Category Breakdown", category: "torrents" },
{ id: "torrent-activity", label: "Activity Heatmap", category: "torrents" },
{ id: "torrent-health", label: "Swarm Health", category: "torrents" },
]
const EMPTY_PREFS: ChartPrefs = { hidden: [], collapsed: [], order: [] }
function useChartPreferences() {
const base = useChartPreferencesBase<ChartPrefs>(STORAGE_KEYS.CHART_PREFERENCES, EMPTY_PREFS)
const { prefs, setPrefs, hydrated } = base
const reorder = useCallback(
(newOrder: string[]) => {
setPrefs((prev) => ({ ...prev, order: newOrder }))
},
[setPrefs]
)
const orderedCharts = useCallback(
(category?: "analytics" | "torrents") => {
const charts = category
? DASHBOARD_CHARTS.filter((c) => c.category === category)
: DASHBOARD_CHARTS
if (prefs.order.length === 0) return charts
return [...charts].sort((a, b) => {
const ai = prefs.order.indexOf(a.id)
const bi = prefs.order.indexOf(b.id)
if (ai === -1 && bi === -1) return 0
if (ai === -1) return 1
if (bi === -1) return -1
return ai - bi
})
},
[prefs.order]
)
return {
prefs,
hydrated,
isHidden: base.isHidden,
isCollapsed: base.isCollapsed,
toggleHidden: base.toggleHidden,
toggleCollapsed: base.toggleCollapsed,
setVisible: base.setVisible,
collapseAll: base.collapseAll,
allVisibleCollapsed: base.allVisibleCollapsed,
reorder,
orderedCharts,
}
}
export type { ChartDef, ChartPrefs }
export { DASHBOARD_CHARTS, useChartPreferences }