Skip to content

Commit 92f009f

Browse files
committed
frontend: persist chart timeframe
persisting chart's timeframe using the AppContext so the timeframe doesn't reset when we navigate through the app.
1 parent b81f6ff commit 92f009f

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

frontends/web/src/contexts/AppContext.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { Dispatch, SetStateAction, createContext } from 'react';
1818

1919
export type TSidebarStatus = '' | 'forceHidden'
20+
export type TChartDisplay = 'week' | 'month' | 'year' | 'all';
2021

2122
type AppContextProps = {
2223
activeSidebar: boolean;
@@ -25,11 +26,13 @@ type AppContextProps = {
2526
hideAmounts: boolean;
2627
nativeLocale: string;
2728
sidebarStatus: string;
29+
chartDisplay: TChartDisplay;
2830
setActiveSidebar: Dispatch<SetStateAction<boolean>>;
2931
setGuideExists: Dispatch<SetStateAction<boolean>>;
3032
setGuideShown: Dispatch<SetStateAction<boolean>>;
3133
setSidebarStatus: Dispatch<SetStateAction<TSidebarStatus>>;
3234
setHideAmounts: Dispatch<SetStateAction<boolean>>;
35+
setChartDisplay: Dispatch<SetStateAction<TChartDisplay>>;
3336
toggleGuide: () => void;
3437
toggleHideAmounts: () => void;
3538
toggleSidebar: () => void;

frontends/web/src/contexts/AppProvider.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
import { ReactNode, useEffect, useState } from 'react';
1818
import { getConfig, setConfig } from '../utils/config';
19-
import { AppContext, TSidebarStatus } from './AppContext';
19+
import { AppContext } from './AppContext';
2020
import { useLoad } from '../hooks/api';
2121
import { useDefault } from '../hooks/default';
2222
import { getNativeLocale } from '../api/nativelocale';
2323
import { i18nextFormat } from '../i18n/utils';
24+
import type { TChartDisplay, TSidebarStatus } from './AppContext';
2425

2526
type TProps = {
2627
children: ReactNode;
@@ -33,6 +34,7 @@ export const AppProvider = ({ children }: TProps) => {
3334
const [hideAmounts, setHideAmounts] = useState(false);
3435
const [activeSidebar, setActiveSidebar] = useState(false);
3536
const [sidebarStatus, setSidebarStatus] = useState<TSidebarStatus>('');
37+
const [chartDisplay, setChartDisplay] = useState<TChartDisplay>('all');
3638

3739
const toggleGuide = () => {
3840
setConfig({ frontend: { guideShown: !guideShown } });
@@ -73,13 +75,15 @@ export const AppProvider = ({ children }: TProps) => {
7375
hideAmounts,
7476
nativeLocale,
7577
sidebarStatus,
78+
chartDisplay,
7679
setActiveSidebar,
7780
setGuideShown,
7881
setGuideExists,
7982
setSidebarStatus,
8083
setHideAmounts,
84+
setChartDisplay,
8185
toggleHideAmounts,
82-
toggleSidebar
86+
toggleSidebar,
8387
}}>
8488
{children}
8589
</AppContext.Provider>

frontends/web/src/routes/account/summary/chart.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import { Amount } from '../../../components/amount/amount';
2323
import { PercentageDiff } from './percentage-diff';
2424
import { Filters } from './filters';
2525
import { getDarkmode } from '../../../components/darkmode/darkmode';
26-
import { TChartDisplay, TChartFiltersProps } from './types';
26+
import { TChartFiltersProps } from './types';
2727
import { DefaultCurrencyRotator } from '../../../components/rates/rates';
28+
import { AppContext } from '../../../contexts/AppContext';
2829
import styles from './chart.module.css';
2930

3031
export interface FormattedLineData extends LineData {
@@ -40,7 +41,6 @@ type ChartProps = {
4041
};
4142

4243
type State = {
43-
display: TChartDisplay;
4444
source: 'daily' | 'hourly';
4545
difference?: number;
4646
diffSince?: string;
@@ -58,6 +58,8 @@ type FormattedData = {
5858
[key: number]: string;
5959
}
6060
class Chart extends Component<Props, State> {
61+
static contextType = AppContext;
62+
context!: React.ContextType<typeof AppContext>;
6163
private ref = createRef<HTMLDivElement>();
6264
private refToolTip = createRef<HTMLSpanElement>();
6365
private chart?: IChartApi;
@@ -81,7 +83,6 @@ class Chart extends Component<Props, State> {
8183
};
8284

8385
public readonly state: State = {
84-
display: 'all',
8586
source: 'daily',
8687
toolTipVisible: false,
8788
toolTipValue: undefined,
@@ -230,7 +231,7 @@ class Chart extends Component<Props, State> {
230231
lineColor: 'rgba(94, 148, 192, 1)',
231232
crosshairMarkerRadius: 6,
232233
});
233-
switch (this.state.display) {
234+
switch (this.context.chartDisplay) {
234235
case 'week':
235236
this.displayWeek();
236237
break;
@@ -241,8 +242,17 @@ class Chart extends Component<Props, State> {
241242
this.displayYear();
242243
break;
243244
}
244-
this.lineSeries.setData(this.props.data.chartDataDaily as ChartData);
245-
this.setFormattedData(this.props.data.chartDataDaily as ChartData);
245+
const isChartDisplayWeekly = this.context.chartDisplay === 'week';
246+
this.lineSeries.setData(
247+
(isChartDisplayWeekly ?
248+
this.props.data.chartDataHourly :
249+
this.props.data.chartDataDaily) as ChartData
250+
);
251+
this.setFormattedData(
252+
(isChartDisplayWeekly ?
253+
this.props.data.chartDataHourly :
254+
this.props.data.chartDataDaily) as ChartData
255+
);
246256
this.chart.timeScale().subscribeVisibleLogicalRangeChange(this.calculateChange);
247257
this.chart.subscribeCrosshairMove(this.handleCrosshair);
248258
this.chart.timeScale().fitContent();
@@ -312,8 +322,9 @@ class Chart extends Component<Props, State> {
312322
this.setFormattedData(this.props.data.chartDataHourly || []);
313323
this.chart.applyOptions({ timeScale: { timeVisible: true } });
314324
}
325+
this.context.setChartDisplay('week');
315326
this.setState(
316-
{ display: 'week', source: 'hourly' },
327+
{ source: 'hourly' },
317328
() => {
318329
if (!this.chart) {
319330
return;
@@ -334,8 +345,9 @@ class Chart extends Component<Props, State> {
334345
this.setFormattedData(this.props.data.chartDataDaily || []);
335346
this.chart.applyOptions({ timeScale: { timeVisible: false } });
336347
}
348+
this.context.setChartDisplay('month');
337349
this.setState(
338-
{ display: 'month', source: 'daily' },
350+
{ source: 'daily' },
339351
() => {
340352
if (!this.chart) {
341353
return;
@@ -356,8 +368,9 @@ class Chart extends Component<Props, State> {
356368
this.setFormattedData(this.props.data.chartDataDaily);
357369
this.chart.applyOptions({ timeScale: { timeVisible: false } });
358370
}
371+
this.context.setChartDisplay('year');
359372
this.setState(
360-
{ display: 'year', source: 'daily' },
373+
{ source: 'daily' },
361374
() => {
362375
if (!this.chart) {
363376
return;
@@ -378,8 +391,9 @@ class Chart extends Component<Props, State> {
378391
this.setFormattedData(this.props.data.chartDataDaily);
379392
this.chart.applyOptions({ timeScale: { timeVisible: false } });
380393
}
394+
this.context.setChartDisplay('all');
381395
this.setState(
382-
{ display: 'all', source: 'daily' },
396+
{ source: 'daily' },
383397
() => {
384398
if (!this.chart) {
385399
return;
@@ -498,7 +512,6 @@ class Chart extends Component<Props, State> {
498512
const {
499513
difference,
500514
diffSince,
501-
display,
502515
toolTipVisible,
503516
toolTipValue,
504517
toolTipTop,
@@ -513,7 +526,7 @@ class Chart extends Component<Props, State> {
513526
const disableWeeklyFilters = !hasHourlyData || chartDataMissing;
514527
const showMobileTotalValue = toolTipVisible && !!toolTipValue && isMobile;
515528
const chartFiltersProps = {
516-
display,
529+
display: this.context.chartDisplay,
517530
disableFilters,
518531
disableWeeklyFilters,
519532
onDisplayWeek: this.displayWeek,

frontends/web/src/routes/account/summary/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
export type TChartDisplay = 'week' | 'month' | 'year' | 'all';
17+
import type { TChartDisplay } from '../../../contexts/AppContext';
1818

1919
export type TChartFiltersProps = {
2020
display: TChartDisplay

0 commit comments

Comments
 (0)