-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated pooled-staking learn more modal with generic interactiv…
…e timespan chart component
- Loading branch information
Showing
26 changed files
with
705 additions
and
841 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ts/PoolStakingLearnMoreModal/InteractiveTimespanChart/GraphTooltip/GraphTooltip.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { StyleSheet } from 'react-native'; | ||
|
||
const styleSheet = () => | ||
StyleSheet.create({ | ||
container: { | ||
paddingVertical: 16, | ||
gap: 4, | ||
alignItems: 'center', | ||
}, | ||
}); | ||
|
||
export default styleSheet; |
31 changes: 31 additions & 0 deletions
31
...take/components/PoolStakingLearnMoreModal/InteractiveTimespanChart/GraphTooltip/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import Text, { | ||
TextVariant, | ||
TextColor, | ||
} from '../../../../../../../component-library/components/Texts/Text'; | ||
import { useStyles } from '../../../../../../hooks/useStyles'; | ||
import styleSheet from './GraphTooltip.styles'; | ||
|
||
interface GraphTooltipProps { | ||
title: string; | ||
subtitle: string; | ||
color?: string; | ||
} | ||
|
||
const GraphTooltip = ({ title, subtitle, color }: GraphTooltipProps) => { | ||
const { styles } = useStyles(styleSheet, {}); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<Text variant={TextVariant.HeadingLG} color={color ?? TextColor.Success}> | ||
{title} | ||
</Text> | ||
<Text variant={TextVariant.BodyMDMedium} color={TextColor.Alternative}> | ||
{subtitle} | ||
</Text> | ||
</View> | ||
); | ||
}; | ||
|
||
export default GraphTooltip; |
14 changes: 14 additions & 0 deletions
14
.../PoolStakingLearnMoreModal/InteractiveTimespanChart/InteractiveTimespanChart.constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { colors } from '@metamask/design-tokens'; | ||
import { CHART_BUTTONS } from './InteractiveTimespanChart'; | ||
import { GraphOptions } from './InteractiveTimespanChart.types'; | ||
|
||
const DEFAULT_INSET = 0; | ||
|
||
export const DEFAULT_GRAPH_OPTIONS: GraphOptions = { | ||
insetTop: DEFAULT_INSET, | ||
insetRight: DEFAULT_INSET, | ||
insetBottom: DEFAULT_INSET, | ||
insetLeft: DEFAULT_INSET, | ||
timespanButtons: CHART_BUTTONS, | ||
color: colors.light.success.default, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...components/PoolStakingLearnMoreModal/InteractiveTimespanChart/InteractiveTimespanChart.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { strings } from '../../../../../../../locales/i18n'; | ||
import { ChartButton } from './ChartTimespanButtonGroup/ChartTimespanButtonGroup.types'; | ||
|
||
// Small dataset ~10 points or less | ||
export const SMALL_DATASET_THRESHOLD = 10; | ||
export const SMALL_DATASET_PADDING = 16; | ||
// Large dataset ~90 points and more | ||
export const SMALL_DATASET_SNAP_RATIO = 0.5; | ||
|
||
export const CHART_BUTTONS: ChartButton[] = [ | ||
{ label: strings('stake.interactive_chart.timespan_buttons.7D'), value: 7 }, | ||
{ label: strings('stake.interactive_chart.timespan_buttons.1M'), value: 30 }, | ||
{ label: strings('stake.interactive_chart.timespan_buttons.3M'), value: 90 }, | ||
{ label: strings('stake.interactive_chart.timespan_buttons.6M'), value: 180 }, | ||
]; | ||
|
||
export enum CHART_TIMESPAN_VALUES { | ||
ONE_WEEK = 7, | ||
ONE_MONTH = 30, | ||
THREE_MONTHS = 90, | ||
SIX_MONTHS = 180, | ||
} |
14 changes: 14 additions & 0 deletions
14
...ents/PoolStakingLearnMoreModal/InteractiveTimespanChart/InteractiveTimespanChart.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ChartButton } from './ChartTimespanButtonGroup/ChartTimespanButtonGroup.types'; | ||
|
||
export type DataPoint = Record<string, unknown>; | ||
|
||
export type Accessor<T, R> = (point: T) => R; | ||
|
||
export interface GraphOptions { | ||
insetTop: number; | ||
insetBottom: number; | ||
insetLeft: number; | ||
insetRight: number; | ||
timespanButtons: ChartButton[]; | ||
color: string; | ||
} |
72 changes: 72 additions & 0 deletions
72
...ents/PoolStakingLearnMoreModal/InteractiveTimespanChart/InteractiveTimespanChart.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { VaultAprs } from '@metamask/stake-sdk'; | ||
import BigNumber from 'bignumber.js'; | ||
import { strings } from '../../../../../../../locales/i18n'; | ||
|
||
export const calculateSegmentCenters = ( | ||
dataPoints: number[] | string[], | ||
segmentWidth: number, | ||
) => | ||
dataPoints.map((_, index) => { | ||
/** | ||
* Ex. If each segment is 30px wide: | ||
* The start position of first segment (index: 0) = 0 * segmentWidth OR 0 * 30px = 0 | ||
* The center position of the first segment (index: 0) = startPosition + segmentWidth / 2 OR 0 + 30 / 2 = 15 | ||
*/ | ||
const startOfSegment = index * segmentWidth; | ||
const centerOfSegment = startOfSegment + segmentWidth / 2; | ||
return centerOfSegment; | ||
}); | ||
|
||
export const formatChartDate = (timestamp: string) => | ||
new Date(timestamp).toUTCString().split(' ').slice(0, 4).join(' '); | ||
|
||
// Example: Sun, 01 Dec 2024 | ||
export const formatDailyAprReward = (reward: { | ||
daily_apy: string; | ||
timestamp: string; | ||
}) => ({ | ||
apr: `${new BigNumber(reward.daily_apy).toFixed(2, BigNumber.ROUND_DOWN)}%`, | ||
timestamp: new Date(reward.timestamp) | ||
.toUTCString() | ||
.split(' ') | ||
.slice(0, 4) | ||
.join(' '), | ||
}); | ||
|
||
export const getGraphContentInset = (dataPoints: number[]) => { | ||
let inset = 0; | ||
|
||
if (dataPoints.length <= 10) inset = 20; | ||
|
||
if (dataPoints.length >= 30) inset = 15; | ||
|
||
if (dataPoints.length >= 90) inset = 10; | ||
|
||
if (dataPoints.length >= 180) inset = 5; | ||
|
||
return inset; | ||
}; | ||
|
||
export const parseVaultTimespanAprsResponse = ( | ||
vaultTimespanAprs: VaultAprs, | ||
) => { | ||
const numDaysMap: Record< | ||
keyof VaultAprs, | ||
{ numDays: number; label: string } | ||
> = { | ||
oneDay: { numDays: 1, label: strings('stake.today') }, | ||
oneWeek: { numDays: 7, label: strings('stake.one_week_average') }, | ||
oneMonth: { numDays: 30, label: strings('stake.one_month_average') }, | ||
threeMonths: { numDays: 90, label: strings('stake.three_month_average') }, | ||
sixMonths: { numDays: 180, label: strings('stake.six_month_average') }, | ||
oneYear: { numDays: 365, label: strings('stake.one_year_average') }, | ||
}; | ||
|
||
return Object.entries(vaultTimespanAprs).reduce< | ||
Record<number, { apr: string; numDays: number; label: string }> | ||
>((map, [key, value]) => { | ||
const numDaysMapEntry = numDaysMap[key as keyof typeof numDaysMap]; | ||
map[numDaysMapEntry.numDays] = { apr: value, ...numDaysMapEntry }; | ||
return map; | ||
}, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.