Skip to content

Commit dff988f

Browse files
committed
feat: time range selector
1 parent b82d3d2 commit dff988f

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

src/hooks/useSGhoApyHistory.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import dayjs from 'dayjs';
12
import { sghoConfig } from 'pages/api/SGhoService';
23
import { useCallback, useEffect, useMemo, useState } from 'react';
34
import { MeritApyDataItem } from 'src/modules/reserve-overview/graphs/MeritApyGraph';
5+
import { ESupportedTimeRanges } from 'src/modules/reserve-overview/TimeRangeSelector';
6+
7+
export const sghoTimeRangeOptions = [
8+
ESupportedTimeRanges.OneWeek,
9+
ESupportedTimeRanges.OneMonth,
10+
ESupportedTimeRanges.SixMonths,
11+
];
12+
13+
export type SGhoTimeRange = (typeof sghoTimeRangeOptions)[number];
414

515
type SGhoApyApiResponse = {
616
data?: MeritApyDataItem[];
@@ -18,6 +28,31 @@ type UseSGhoApyHistoryOptions = {
1828
limit?: number;
1929
startDate?: string;
2030
endDate?: string;
31+
timeRange?: SGhoTimeRange;
32+
};
33+
34+
/**
35+
* Convert time range to start/end dates
36+
*/
37+
const timeRangeToDateRange = (timeRange: SGhoTimeRange): { startDate: string; endDate: string } => {
38+
const endDate = dayjs().format('YYYY-MM-DD');
39+
let startDate: string;
40+
41+
switch (timeRange) {
42+
case ESupportedTimeRanges.OneWeek:
43+
startDate = dayjs().subtract(7, 'day').format('YYYY-MM-DD');
44+
break;
45+
case ESupportedTimeRanges.OneMonth:
46+
startDate = dayjs().subtract(1, 'month').format('YYYY-MM-DD');
47+
break;
48+
case ESupportedTimeRanges.SixMonths:
49+
startDate = dayjs().subtract(6, 'month').format('YYYY-MM-DD');
50+
break;
51+
default:
52+
startDate = dayjs().subtract(7, 'day').format('YYYY-MM-DD');
53+
}
54+
55+
return { startDate, endDate };
2156
};
2257

2358
/**
@@ -37,9 +72,23 @@ export const useSGhoApyHistory = (
3772
const [error, setError] = useState<boolean>(false);
3873

3974
// Stabilize the options to prevent unnecessary re-fetches
40-
const stableOptions = useMemo(() => options, [options.limit, options.startDate, options.endDate]);
75+
const stableOptions = useMemo(
76+
() => options,
77+
[options.limit, options.startDate, options.endDate, options.timeRange]
78+
);
4179

42-
const { limit = sghoConfig.defaultLimit, startDate, endDate } = stableOptions;
80+
const { limit = sghoConfig.defaultLimit, timeRange } = stableOptions;
81+
82+
// Determine dates: use timeRange if provided, otherwise use explicit startDate/endDate
83+
const { startDate, endDate } = useMemo(() => {
84+
if (timeRange) {
85+
return timeRangeToDateRange(timeRange);
86+
}
87+
return {
88+
startDate: stableOptions.startDate,
89+
endDate: stableOptions.endDate,
90+
};
91+
}, [timeRange, stableOptions.startDate, stableOptions.endDate]);
4392

4493
const fetchData = useCallback(async () => {
4594
try {

src/modules/reserve-overview/TimeRangeSelector.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { SxProps, Theme, ToggleButton, ToggleButtonGroup, Typography } from '@mu
33
export const supportedTimeRangeOptions = ['1m', '3m', '6m', '1y'] as const;
44

55
export enum ESupportedTimeRanges {
6+
OneWeek = '1w',
67
OneMonth = '1m',
78
ThreeMonths = '3m',
89
SixMonths = '6m',

src/modules/reserve-overview/graphs/MeritApyGraphContainer.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export type MeritApyGraphContainerProps = {
6161
showAverage?: boolean;
6262
title?: string;
6363
height?: number;
64+
timeRangeSelector?: React.ReactNode;
6465
};
6566

6667
/**
@@ -76,6 +77,7 @@ export const MeritApyGraphContainer = ({
7677
showAverage = true,
7778
title = 'Merit APY',
7879
height = 155,
80+
timeRangeSelector,
7981
}: MeritApyGraphContainerProps): JSX.Element => {
8082
const CHART_HEIGHT_LOADING_FIX = 3;
8183

@@ -137,6 +139,7 @@ export const MeritApyGraphContainer = ({
137139
}}
138140
>
139141
<GraphLegend labels={legendFields} />
142+
{timeRangeSelector}
140143
</Box>
141144

142145
<Box sx={{ flex: 1, minHeight: height, width: '100%' }}>

src/modules/sGho/SGhoDepositPanel.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from '@mui/material';
1717
import { BigNumber } from 'ethers';
1818
import { formatEther, formatUnits } from 'ethers/lib/utils';
19-
import React from 'react';
19+
import React, { useState } from 'react';
2020
import { DarkTooltip } from 'src/components/infoTooltips/DarkTooltip';
2121
import { FormattedNumber } from 'src/components/primitives/FormattedNumber';
2222
import { TokenIcon } from 'src/components/primitives/TokenIcon';
@@ -25,12 +25,17 @@ import { TextWithTooltip } from 'src/components/TextWithTooltip';
2525
import { StakeTokenFormatted } from 'src/hooks/stake/useGeneralStakeUiData';
2626
import { useCurrentTimestamp } from 'src/hooks/useCurrentTimestamp';
2727
import { useModalContext } from 'src/hooks/useModal';
28-
import { useSGhoApyHistory } from 'src/hooks/useSGhoApyHistory';
28+
import {
29+
SGhoTimeRange,
30+
sghoTimeRangeOptions,
31+
useSGhoApyHistory,
32+
} from 'src/hooks/useSGhoApyHistory';
2933
import { useStakeTokenAPR } from 'src/hooks/useStakeTokenAPR';
3034
import { useWeb3Context } from 'src/libs/hooks/useWeb3Context';
3135
import { GENERAL } from 'src/utils/events';
3236

3337
import { MeritApyGraphContainer } from '../reserve-overview/graphs/MeritApyGraphContainer';
38+
import { ESupportedTimeRanges, TimeRangeSelector } from '../reserve-overview/TimeRangeSelector';
3439
import { StakeActionBox } from '../staking/StakeActionBox';
3540

3641
export interface SGHODepositPanelProps {
@@ -63,14 +68,16 @@ export const SGHODepositPanel: React.FC<SGHODepositPanelProps> = ({
6368
const { openSwitch } = useModalContext();
6469
const { currentAccount } = useWeb3Context();
6570

66-
// const { data: stakeAPR, isLoading: isLoadingStakeAPR, error: errorStakeAPR } = useStakeTokenAPR();
67-
// Stable options object to prevent unnecessary re-fetches
71+
const [selectedTimeRange, setSelectedTimeRange] = useState<SGhoTimeRange>(
72+
ESupportedTimeRanges.OneWeek
73+
);
74+
6875
const {
6976
data: meritApyHistory,
7077
loading: loadingMeritApy,
7178
error: errorMeritApyHistory,
7279
refetch: refetchMeritApyHistory,
73-
} = useSGhoApyHistory();
80+
} = useSGhoApyHistory({ timeRange: selectedTimeRange });
7481
const { data: stakeAPR } = useStakeTokenAPR();
7582

7683
// Handle different states properly
@@ -503,6 +510,14 @@ export const SGHODepositPanel: React.FC<SGHODepositPanelProps> = ({
503510
lineColor="#2EBAC6"
504511
showAverage={true}
505512
height={155}
513+
timeRangeSelector={
514+
<TimeRangeSelector
515+
disabled={loadingMeritApy || errorMeritApyHistory}
516+
timeRanges={sghoTimeRangeOptions}
517+
selectedTimeRange={selectedTimeRange}
518+
onTimeRangeChanged={setSelectedTimeRange}
519+
/>
520+
}
506521
/>
507522
</Grid>
508523
</Grid>

0 commit comments

Comments
 (0)