22
33import { useState } from 'react'
44import styles from '@/platform/app/(shell)/home/_components/activity-chart/activity-chart.module.css'
5- import type { HomeActivityState } from '@/platform/app/(shell)/home/_components/model'
5+ import type {
6+ ActivityPoint ,
7+ HomeActivityState ,
8+ } from '@/platform/app/(shell)/home/_components/model'
69import { RetryButton } from '@/platform/app/(shell)/home/_components/retry-button/retry-button'
710import { SkeletonBlock } from '@/platform/app/(shell)/home/_components/widget-skeleton/widget-skeleton'
811import { TrendPlot } from '@/platform/design-system/catalog/plots'
@@ -12,18 +15,12 @@ import { ChartTooltip } from '@/platform/design-system/components/chart/chart-to
1215import { usePlotInspection } from '@/platform/design-system/components/chart/use-plot-inspection'
1316import { RangePicker } from '@/platform/design-system/components/menu/range-picker'
1417import { useScrollEdges } from '@/platform/design-system/components/scroll-area/use-scroll-edges'
15- import { DAY_MS , formatCalendarDate } from '@/platform/lib/time'
16-
17- interface ActivityPoint {
18- date : string
19- value : number
20- }
18+ import { formatCalendarDate } from '@/platform/lib/time'
2119
2220interface HomeActivityChartProps {
2321 title : string
2422 state : HomeActivityState
25- onRetry ?: ( ) => void
26- hidden ?: boolean
23+ onRetry : ( ) => void
2724}
2825
2926interface ActivitySeriesProps {
@@ -93,33 +90,13 @@ function ActivitySeries({ title, points }: ActivitySeriesProps) {
9390const WINDOW_DAYS = [ 7 , 30 , 90 ] as const
9491const RANGE_LABELS = WINDOW_DAYS . map ( ( days ) => `Last ${ days } days` )
9592
96- function calendarWindow ( history : readonly ActivityPoint [ ] | undefined , days : number ) {
97- if ( ! history ?. length ) return [ ]
98- const end = Date . parse ( history [ history . length - 1 ] . date )
99- const start = Math . max ( Date . parse ( history [ 0 ] . date ) , end - ( days - 1 ) * DAY_MS )
100- const counts = new Map ( history . map ( ( { date, value } ) => [ date , value ] ) )
101- const points : ActivityPoint [ ] = [ ]
102- for ( let day = start ; day <= end ; day += DAY_MS ) {
103- const date = new Date ( day ) . toISOString ( ) . slice ( 0 , 10 )
104- const count = counts . get ( date ) ?? 0
105- points . push ( { date, value : Number . isFinite ( count ) && count > 0 ? count : 0 } )
106- }
107- return points
108- }
109-
110- export function HomeActivityChart ( {
111- title,
112- state,
113- onRetry,
114- hidden = false ,
115- } : HomeActivityChartProps ) {
93+ export function HomeActivityChart ( { title, state, onRetry } : HomeActivityChartProps ) {
11694 const [ range , setRange ] = useState ( RANGE_LABELS [ 0 ] )
11795 const scrollEdges = useScrollEdges < HTMLElement > ( 'vertical' , { tabStop : true } )
118- if ( hidden || state . status === 'hidden' ) return null
96+ if ( state . status === 'hidden' ) return null
11997 const ready = state . status === 'ready'
120- const history = ready ? state . points : undefined
12198 const days = WINDOW_DAYS [ RANGE_LABELS . indexOf ( range ) ]
122- const points = calendarWindow ( history , days )
99+ const points = ready ? state . points . slice ( - days ) : [ ]
123100 const total = points . reduce ( ( sum , point ) => sum + point . value , 0 )
124101 return (
125102 < section ref = { scrollEdges } className = { styles . chart } aria-label = { title } >
@@ -135,7 +112,7 @@ export function HomeActivityChart({
135112 { ready && (
136113 < RangePicker
137114 label = 'Range'
138- options = { history ? RANGE_LABELS : [ ] }
115+ options = { RANGE_LABELS }
139116 value = { range }
140117 onValueChange = { setRange }
141118 />
0 commit comments