@@ -3,7 +3,7 @@ import React, { CSSProperties, FC, useEffect, useRef } from 'react';
3
3
// eslint-disable-next-line no-restricted-imports
4
4
import { useDispatch , useSelector } from 'react-redux' ;
5
5
6
- import { TimeRange , isDateTime , rangeUtil , TimeZone , toUtc } from '@grafana/data' ;
6
+ import { TimeRange , isDateTime , rangeUtil } from '@grafana/data' ;
7
7
import { TimeRangePickerProps , TimeRangePicker , useTheme2 } from '@grafana/ui' ;
8
8
import { FnGlobalState , updatePartialFnStates } from 'app/core/reducers/fn-slice' ;
9
9
import { StoreState } from 'app/types' ;
@@ -14,6 +14,15 @@ const LOCAL_STORAGE_KEY = 'grafana.dashboard.timepicker.history';
14
14
15
15
interface Props extends Omit < TimeRangePickerProps , 'history' | 'theme' > { }
16
16
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
+
17
26
const FnText : React . FC = ( ) => {
18
27
const { FNDashboard } = useSelector < StoreState , FnGlobalState > ( ( { fnGlobalState } ) => fnGlobalState ) ;
19
28
const theme = useTheme2 ( ) ;
@@ -24,23 +33,26 @@ const FnText: React.FC = () => {
24
33
} ;
25
34
26
35
export 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 } /> ;
30
39
} }
31
40
</ LocalStorageValueProvider >
32
41
) ;
33
42
34
43
export interface PickerProps {
35
- values : TimeRange [ ] ;
36
- onSaveToStore : ( value : TimeRange [ ] ) => void ;
44
+ rawValues : LSTimePickerHistoryItem [ ] ;
45
+ onSaveToStore : ( value : LSTimePickerHistoryItem [ ] ) => void ;
37
46
pickerProps : Props ;
38
47
}
39
48
40
- export const Picker : FC < PickerProps > = ( { values , onSaveToStore, pickerProps } ) => {
49
+ export const Picker : FC < PickerProps > = ( { rawValues , onSaveToStore, pickerProps } ) => {
41
50
const { fnGlobalTimeRange } = useSelector < StoreState , FnGlobalState > ( ( { fnGlobalState } ) => fnGlobalState ) ;
42
51
const dispatch = useDispatch ( ) ;
43
52
53
+ const values = migrateHistory ( rawValues ) ;
54
+ const history = deserializeHistory ( values ) ;
55
+
44
56
const didMountRef = useRef ( false ) ;
45
57
useEffect ( ( ) => {
46
58
/* 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 })
62
74
< TimeRangePicker
63
75
{ ...pickerProps }
64
76
value = { fnGlobalTimeRange || pickerProps . value }
65
- history = { convertIfJson ( values ) }
77
+ history = { history }
66
78
onChange = { ( value ) => {
67
79
onAppendToHistory ( value , values , onSaveToStore ) ;
68
80
pickerProps . onChange ( value ) ;
@@ -72,16 +84,9 @@ export const Picker: FC<PickerProps> = ({ values, onSaveToStore, pickerProps })
72
84
) ;
73
85
} ;
74
86
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' ) ) ;
85
90
}
86
91
87
92
function migrateHistory ( values : LSTimePickerHistoryItem [ ] ) : TimePickerHistoryItem [ ] {
0 commit comments