Skip to content

Commit f5844db

Browse files
dylanjeffersclaude
andauthored
Refactor balance history graph to use dynamic width and daily granularity (#14106)
## Summary This PR refactors the UserBalanceHistoryGraph component to dynamically measure container width instead of accepting a fixed width prop, and updates balance history queries to use daily granularity across both mobile and web platforms. ## Key Changes - **Dynamic Width Measurement**: Replaced the static `width` prop with runtime layout measurement using `onLayout` callback on mobile, allowing the graph to responsively adapt to its container - **Layout State Management**: Added `containerWidth` state and `handleLayout` callback to capture actual rendered dimensions before chart rendering - **Granularity Update**: Added `granularity: 'daily'` parameter to all `useUserBalanceHistory` hook calls in both mobile and web implementations - **Tooltip Date Formatting**: Simplified `formatTooltipDate` function to display date only (weekday short, month short, day) instead of including time information - **Prop Removal**: Removed `width` prop from `UserBalanceHistoryGraph` and `AccountBalance` components, simplifying the component API - **Render Guard**: Added early return to render empty Flex container while waiting for layout measurement to complete ## Implementation Details - Mobile implementation uses React Native's `LayoutChangeEvent` to measure container width dynamically - The chart only renders once `containerWidth > 0`, preventing layout thrashing - Web implementation maintains existing behavior while adopting the daily granularity parameter - All balance history queries now consistently use daily data granularity for improved performance and consistency https://claude.ai/code/session_01EP1m3UXR2bibGyXKUUUZqY --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4c427f2 commit f5844db

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

packages/mobile/src/components/account-balance/AccountBalance.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Flex, Text, IconArrowRight, Paper, Box } from '@audius/harmony-native'
1111
import { UserBalanceHistoryGraph } from 'app/components/user-balance-history-graph'
1212

1313
type AccountBalanceProps = {
14-
width?: number
1514
height?: number
1615
}
1716

@@ -28,16 +27,13 @@ const formatPercentage = (value: number): string => {
2827
return `${value.toFixed(2)}%`
2928
}
3029

31-
export const AccountBalance = ({
32-
width = 350,
33-
height = 204
34-
}: AccountBalanceProps) => {
30+
export const AccountBalance = ({ height = 204 }: AccountBalanceProps) => {
3531
const { data: currentUserId } = useCurrentUserId()
3632
const {
3733
data: historyData,
3834
isLoading: isHistoryLoading,
3935
isError: isHistoryError
40-
} = useUserBalanceHistory({ userId: currentUserId })
36+
} = useUserBalanceHistory({ userId: currentUserId, granularity: 'daily' })
4137

4238
const {
4339
totalBalance: currentBalance,
@@ -148,7 +144,7 @@ export const AccountBalance = ({
148144
</Flex>
149145
</Flex>
150146

151-
<UserBalanceHistoryGraph width={width} height={height} />
147+
<UserBalanceHistoryGraph height={height} />
152148
</Paper>
153149
)
154150
}

packages/mobile/src/components/user-balance-history-graph/UserBalanceHistoryGraph.tsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useMemo } from 'react'
1+
import { useCallback, useEffect, useMemo, useState } from 'react'
22

33
import {
44
useCurrentUserId,
@@ -7,7 +7,7 @@ import {
77
} from '@audius/common/api'
88
import { walletMessages } from '@audius/common/messages'
99
import { convertHexToRGBA } from '@audius/common/utils'
10-
import { View } from 'react-native'
10+
import { type LayoutChangeEvent, View } from 'react-native'
1111
import { LineChart } from 'react-native-gifted-charts'
1212
import type { lineDataItem } from 'react-native-gifted-charts'
1313

@@ -18,7 +18,6 @@ import { useNavigation } from 'app/hooks/useNavigation'
1818
const messages = walletMessages.balanceHistory
1919

2020
type UserBalanceHistoryGraphProps = {
21-
width?: number
2221
height?: number
2322
}
2423

@@ -43,22 +42,25 @@ const formatShortCurrency = (value: number): string => {
4342

4443
const formatTooltipDate = (timestamp: number): string => {
4544
const date = new Date(timestamp)
46-
const weekday = date.toLocaleDateString('en-US', { weekday: 'long' })
47-
const hour = date
48-
.toLocaleTimeString('en-US', {
49-
hour: 'numeric',
50-
hour12: true
45+
return date
46+
.toLocaleDateString('en-US', {
47+
weekday: 'short',
48+
month: 'short',
49+
day: 'numeric'
5150
})
52-
.toLowerCase()
53-
return `${weekday} ${hour}`.toUpperCase()
51+
.toUpperCase()
5452
}
5553

5654
export const UserBalanceHistoryGraph = ({
57-
width = 350,
5855
height = 191
5956
}: UserBalanceHistoryGraphProps) => {
57+
const [containerWidth, setContainerWidth] = useState(0)
6058
const { color, spacing } = useTheme()
6159
const navigation = useNavigation()
60+
61+
const handleLayout = useCallback((event: LayoutChangeEvent) => {
62+
setContainerWidth(event.nativeEvent.layout.width)
63+
}, [])
6264
useEffect(() => {
6365
navigation.setOptions({ fullScreenGestureEnabled: false })
6466
return () => {
@@ -73,7 +75,7 @@ export const UserBalanceHistoryGraph = ({
7375
data: historyDataFetched,
7476
isLoading: isHistoryLoading,
7577
isError: isHistoryError
76-
} = useUserBalanceHistory({ userId: currentUserId })
78+
} = useUserBalanceHistory({ userId: currentUserId, granularity: 'daily' })
7779

7880
const {
7981
totalBalance: currentBalance,
@@ -179,6 +181,13 @@ export const UserBalanceHistoryGraph = ({
179181
return null
180182
}
181183

184+
// Wait for layout measurement before rendering the chart
185+
if (containerWidth === 0) {
186+
return (
187+
<Flex pv='xs' onLayout={handleLayout} style={{ minHeight: height }} />
188+
)
189+
}
190+
182191
const values = chartData.map((d) => d.value as number)
183192
// Safe to assert: we know chartData.length > 0 from check above
184193
const maxValue = Math.max(...values)
@@ -188,7 +197,7 @@ export const UserBalanceHistoryGraph = ({
188197
const chartHorizontalPadding = 48
189198
const chartInitialSpacing = 10
190199
const chartEndSpacing = 10
191-
const chartWidth = Math.max(width - chartHorizontalPadding, 0)
200+
const chartWidth = Math.max(containerWidth - chartHorizontalPadding, 0)
192201
const spacingBetweenPoints =
193202
chartData.length > 1
194203
? Math.max(
@@ -206,7 +215,7 @@ export const UserBalanceHistoryGraph = ({
206215
}
207216

208217
return (
209-
<Flex pv='xs'>
218+
<Flex pv='xs' onLayout={handleLayout}>
210219
<View>
211220
<LineChart
212221
data={chartData}

packages/web/src/components/account-balance/AccountBalance.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const AccountBalanceContent = () => {
129129
data: historyData,
130130
isLoading: isHistoryLoading,
131131
isError: isHistoryError
132-
} = useUserBalanceHistory({ userId: currentUserId })
132+
} = useUserBalanceHistory({ userId: currentUserId, granularity: 'daily' })
133133

134134
const {
135135
totalBalance: currentBalance,

packages/web/src/components/user-balance-history-graph/UserBalanceHistoryGraph.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ const formatDate = (timestamp: number): string => {
4444

4545
const formatTooltipDate = (timestamp: number): string => {
4646
const date = new Date(timestamp)
47-
const weekday = date.toLocaleDateString('en-US', { weekday: 'long' })
48-
const hour = date
49-
.toLocaleTimeString('en-US', {
50-
hour: 'numeric',
51-
hour12: true
52-
})
53-
.toLowerCase()
54-
return `${weekday} ${hour}`
47+
return date.toLocaleDateString('en-US', {
48+
weekday: 'short',
49+
month: 'short',
50+
day: 'numeric'
51+
})
5552
}
5653

5754
const getChartData = (
@@ -232,7 +229,7 @@ export const UserBalanceHistoryGraph = () => {
232229
data: historyDataFetched,
233230
isLoading: isHistoryLoading,
234231
isError: isHistoryError
235-
} = useUserBalanceHistory({ userId: currentUserId })
232+
} = useUserBalanceHistory({ userId: currentUserId, granularity: 'daily' })
236233

237234
const {
238235
totalBalance: currentBalance,

0 commit comments

Comments
 (0)