@@ -3,7 +3,7 @@ import React, { CSSProperties, FC, useEffect, useRef } from 'react';
33// eslint-disable-next-line no-restricted-imports
44import { useDispatch , useSelector } from 'react-redux' ;
55
6- import { TimeRange , isDateTime , rangeUtil , TimeZone , toUtc } from '@grafana/data' ;
6+ import { TimeRange , isDateTime , rangeUtil } from '@grafana/data' ;
77import { TimeRangePickerProps , TimeRangePicker , useTheme2 } from '@grafana/ui' ;
88import { FnGlobalState , updatePartialFnStates } from 'app/core/reducers/fn-slice' ;
99import { StoreState } from 'app/types' ;
@@ -14,6 +14,15 @@ const LOCAL_STORAGE_KEY = 'grafana.dashboard.timepicker.history';
1414
1515interface Props extends Omit < TimeRangePickerProps , 'history' | 'theme' > { }
1616
17+ // Simplified object to store in local storage
18+ interface TimePickerHistoryItem {
19+ from : string ;
20+ to : string ;
21+ }
22+
23+ // We should only be storing TimePickerHistoryItem, but in the past we also stored TimeRange
24+ type LSTimePickerHistoryItem = TimePickerHistoryItem | TimeRange ;
25+
1726const FnText : React . FC = ( ) => {
1827 const { FNDashboard } = useSelector < StoreState , FnGlobalState > ( ( { fnGlobalState } ) => fnGlobalState ) ;
1928 const theme = useTheme2 ( ) ;
@@ -24,23 +33,26 @@ const FnText: React.FC = () => {
2433} ;
2534
2635export const TimePickerWithHistory : FC < Props > = ( props ) => (
27- < LocalStorageValueProvider < TimeRange [ ] > storageKey = { LOCAL_STORAGE_KEY } defaultValue = { [ ] } >
28- { ( values , onSaveToStore ) => {
29- return < Picker values = { values } onSaveToStore = { onSaveToStore } pickerProps = { props } /> ;
36+ < LocalStorageValueProvider < LSTimePickerHistoryItem [ ] > storageKey = { LOCAL_STORAGE_KEY } defaultValue = { [ ] } >
37+ { ( rawValues , onSaveToStore ) => {
38+ return < Picker rawValues = { rawValues } onSaveToStore = { onSaveToStore } pickerProps = { props } /> ;
3039 } }
3140 </ LocalStorageValueProvider >
3241) ;
3342
3443export interface PickerProps {
35- values : TimeRange [ ] ;
36- onSaveToStore : ( value : TimeRange [ ] ) => void ;
44+ rawValues : LSTimePickerHistoryItem [ ] ;
45+ onSaveToStore : ( value : LSTimePickerHistoryItem [ ] ) => void ;
3746 pickerProps : Props ;
3847}
3948
40- export const Picker : FC < PickerProps > = ( { values , onSaveToStore, pickerProps } ) => {
49+ export const Picker : FC < PickerProps > = ( { rawValues , onSaveToStore, pickerProps } ) => {
4150 const { fnGlobalTimeRange } = useSelector < StoreState , FnGlobalState > ( ( { fnGlobalState } ) => fnGlobalState ) ;
4251 const dispatch = useDispatch ( ) ;
4352
53+ const values = migrateHistory ( rawValues ) ;
54+ const history = deserializeHistory ( values ) ;
55+
4456 const didMountRef = useRef ( false ) ;
4557 useEffect ( ( ) => {
4658 /* The condition below skips the first run of useeffect that happens when this component gets mounted */
@@ -62,7 +74,7 @@ export const Picker: FC<PickerProps> = ({ values, onSaveToStore, pickerProps })
6274 < TimeRangePicker
6375 { ...pickerProps }
6476 value = { fnGlobalTimeRange || pickerProps . value }
65- history = { convertIfJson ( values ) }
77+ history = { history }
6678 onChange = { ( value ) => {
6779 onAppendToHistory ( value , values , onSaveToStore ) ;
6880 pickerProps . onChange ( value ) ;
@@ -72,16 +84,9 @@ export const Picker: FC<PickerProps> = ({ values, onSaveToStore, pickerProps })
7284 ) ;
7385} ;
7486
75- function convertIfJson ( history : TimeRange [ ] ) : TimeRange [ ] {
76- return history . map ( ( time ) => {
77- if ( isDateTime ( time . from ) ) {
78- return time ;
79- }
80- } ) ;
81- }
82-
83- function deserializeHistory ( values : TimePickerHistoryItem [ ] , timeZone : TimeZone | undefined ) : TimeRange [ ] {
84- return values . map ( ( item ) => rangeUtil . convertRawToRange ( item , timeZone ) ) ;
87+ function deserializeHistory ( values : TimePickerHistoryItem [ ] ) : TimeRange [ ] {
88+ // The history is saved in UTC and with the default date format, so we need to pass those values to the convertRawToRange
89+ return values . map ( ( item ) => rangeUtil . convertRawToRange ( item , 'utc' , undefined , 'YYYY-MM-DD HH:mm:ss' ) ) ;
8590}
8691
8792function migrateHistory ( values : LSTimePickerHistoryItem [ ] ) : TimePickerHistoryItem [ ] {
0 commit comments