Skip to content

Commit 901fb11

Browse files
committed
refactor: centralize date picker component and utilities
1 parent 9f7b317 commit 901fb11

File tree

9 files changed

+711
-481
lines changed

9 files changed

+711
-481
lines changed

dashboard/src/components/charts/all-nodes-stacked-bar-chart.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,7 @@ import { useTheme } from '@/components/common/theme-provider'
1818
import TimeSelector from './time-selector'
1919
import NodeStatsModal from '@/components/dialogs/node-stats-modal'
2020

21-
// Helper function to determine period (copied from CostumeBarChart)
22-
const getPeriodFromDateRange = (range?: DateRange): Period => {
23-
if (!range?.from || !range?.to) {
24-
return Period.hour // Default to hour if no range
25-
}
26-
const diffTime = Math.abs(range.to.getTime() - range.from.getTime())
27-
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
28-
if (diffDays <= 2) {
29-
return Period.hour
30-
}
31-
return Period.day
32-
}
21+
import { getPeriodFromDateRange } from '@/utils/datePickerUtils'
3322

3423
function CustomTooltip({ active, payload, chartConfig, dir, period }: TooltipProps<any, any> & { chartConfig?: ChartConfig; dir: string; period?: string }) {
3524
const { t, i18n } = useTranslation()

dashboard/src/components/charts/area-costume-chart.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Button } from '@/components/ui/button'
1212
import { Clock, History, Cpu, MemoryStick } from 'lucide-react'
1313
import { dateUtils } from '@/utils/dateFormatter'
1414
import { useTheme } from 'next-themes'
15+
import { getPeriodFromDateRange } from '@/utils/datePickerUtils'
1516

1617
type DataPoint = {
1718
time: string
@@ -183,18 +184,6 @@ interface AreaCostumeChartProps {
183184
realtimeStats?: SystemStats | NodeRealtimeStats
184185
}
185186

186-
const getPeriodFromDateRange = (range?: DateRange): Period => {
187-
if (!range?.from || !range?.to) {
188-
return Period.hour
189-
}
190-
const diffTime = Math.abs(range.to.getTime() - range.from.getTime())
191-
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
192-
193-
if (diffDays <= 2) {
194-
return Period.hour
195-
}
196-
return Period.day
197-
}
198187
const isSystemStats = (stats: SystemStats | NodeRealtimeStats): stats is SystemStats => {
199188
return 'total_user' in stats
200189
}

dashboard/src/components/charts/costume-bar-chart.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { TrendingUp, Upload, Download, Calendar } from 'lucide-react'
1414
import { dateUtils } from '@/utils/dateFormatter'
1515
import { TooltipProps } from 'recharts'
1616
import TimeSelector from './time-selector'
17+
import { getPeriodFromDateRange } from '@/utils/datePickerUtils'
1718

1819
type DataPoint = {
1920
time: string
@@ -32,21 +33,6 @@ interface CostumeBarChartProps {
3233
nodeId?: number
3334
}
3435

35-
// Helper function to determine period (copied from AreaCostumeChart)
36-
const getPeriodFromDateRange = (range?: DateRange): Period => {
37-
if (!range?.from || !range?.to) {
38-
return Period.hour // Default to hour if no range
39-
}
40-
const diffTime = Math.abs(range.to.getTime() - range.from.getTime())
41-
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
42-
43-
if (diffDays <= 2) {
44-
// Up to 2 days, use hourly data
45-
return Period.hour
46-
}
47-
return Period.day // More than 2 days, use daily data
48-
}
49-
5036
function CustomBarTooltip({ active, payload, period }: TooltipProps<any, any> & { period?: string }) {
5137
const { t, i18n } = useTranslation()
5238
if (!active || !payload || !payload.length) return null

0 commit comments

Comments
 (0)