Skip to content

Commit 5572ed7

Browse files
authored
fix: allow view options to update (#5024)
1 parent ffdaf3e commit 5572ed7

File tree

6 files changed

+100
-11
lines changed

6 files changed

+100
-11
lines changed

src/dataExplorer/components/FluxQueryBuilder.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {FC} from 'react'
1+
import React, {FC, useEffect} from 'react'
22
import {createLocalStorageStateHook} from 'use-local-storage-state'
33

44
// Components
@@ -10,6 +10,7 @@ import {SidebarProvider} from 'src/dataExplorer/context/sidebar'
1010
import ResultsPane from 'src/dataExplorer/components/ResultsPane'
1111
import Sidebar from 'src/dataExplorer/components/Sidebar'
1212
import Schema from 'src/dataExplorer/components/Schema'
13+
import {useSessionStorage} from 'src/dataExplorer/shared/utils'
1314

1415
// Styles
1516
import './FluxQueryBuilder.scss'
@@ -19,7 +20,17 @@ const useResizeState = createLocalStorageStateHook(
1920
[0.25, 0.8]
2021
)
2122
const FluxQueryBuilder: FC = () => {
22-
const [vertDragPosition, setVertDragPosition] = useResizeState()
23+
const [oldVertDragPosition, oldSetVertDragPosition] = useResizeState()
24+
const [vertDragPosition, setVertDragPosition] = useSessionStorage(
25+
'dataExplorer.resize.vertical',
26+
oldVertDragPosition
27+
)
28+
29+
// migration to allow people to keep their last used settings
30+
// immediately after rollout
31+
useEffect(() => {
32+
oldSetVertDragPosition([0.25, 0.8])
33+
}, [])
2334

2435
return (
2536
<QueryProvider>

src/dataExplorer/components/Results.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ const WrappedOptions: FC = () => {
7171
properties={view.properties}
7272
results={result.parsed}
7373
update={update => {
74-
Object.keys(update).forEach(k => (view.properties[k] = update[k]))
75-
76-
setView({...view})
74+
setView({
75+
...view,
76+
properties: {
77+
...view.properties,
78+
...update,
79+
},
80+
})
7781
}}
7882
/>
7983
)

src/dataExplorer/components/ResultsContext.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {FC, createContext, useState, useRef, useEffect} from 'react'
22
import {createLocalStorageStateHook} from 'use-local-storage-state'
33

4+
import {useSessionStorage} from 'src/dataExplorer/shared/utils'
45
import {FluxResult} from 'src/types/flows'
56
import {
67
RemoteDataState,
@@ -61,7 +62,20 @@ export const ResultsProvider: FC = ({children}) => {
6162
)
6263

6364
// for display, should be moved
64-
const [view, setView] = useLocalStorageState()
65+
const [oldView, setOldView] = useLocalStorageState()
66+
const [view, setView] = useSessionStorage('dataExplorer.results', oldView)
67+
68+
// migration to allow people to keep their last used settings
69+
// immediately after rollout
70+
useEffect(() => {
71+
setOldView({
72+
state: 'table',
73+
properties: {
74+
type: 'simple-table',
75+
showAll: false,
76+
} as SimpleTableViewProperties,
77+
})
78+
}, [])
6579

6680
useEffect(() => {
6781
let running = false

src/dataExplorer/components/ResultsPane.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Libraries
2-
import React, {FC, lazy, Suspense, useContext} from 'react'
2+
import React, {FC, lazy, Suspense, useContext, useEffect} from 'react'
33
import {
44
DraggableResizer,
55
Orientation,
@@ -40,6 +40,7 @@ import {getWindowPeriodVariableFromVariables} from 'src/variables/utils/getWindo
4040
// Constants
4141
import {TIME_RANGE_START, TIME_RANGE_STOP} from 'src/variables/constants'
4242
import {DEFAULT_TIME_RANGE} from 'src/shared/constants/timeRanges'
43+
import {useSessionStorage} from '../shared/utils'
4344

4445
const FluxMonacoEditor = lazy(() =>
4546
import('src/shared/components/FluxMonacoEditor')
@@ -94,9 +95,26 @@ const ResultsPane: FC = () => {
9495
const {basic, query} = useContext(QueryContext)
9596
const {status, setStatus, setResult} = useContext(ResultsContext)
9697

97-
const [horizDragPosition, setHorizDragPosition] = useResizeState()
98-
const [text, setText] = useQueryState()
99-
const [timeRange, setTimeRange] = useRangeState()
98+
const [oldHorizDragPosition, setOldHorizDragPosition] = useResizeState()
99+
const [horizDragPosition, setHorizDragPosition] = useSessionStorage(
100+
'dataExplorer.resize.horizontal',
101+
oldHorizDragPosition
102+
)
103+
const [oldText, setOldText] = useQueryState()
104+
const [text, setText] = useSessionStorage('dataExplorer.query', oldText)
105+
const [oldTimeRange, setOldTimeRange] = useRangeState()
106+
const [timeRange, setTimeRange] = useSessionStorage(
107+
'dataExplorer.range',
108+
oldTimeRange
109+
)
110+
111+
// migration to allow people to keep their last used settings
112+
// immediately after rollout
113+
useEffect(() => {
114+
setOldHorizDragPosition([0.2])
115+
setOldText('')
116+
setOldTimeRange(DEFAULT_TIME_RANGE)
117+
}, [])
100118

101119
const download = () => {
102120
event('CSV Download Initiated')

src/dataExplorer/context/fluxQueryBuilder.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, {
55
useMemo,
66
useContext,
77
useCallback,
8+
useEffect,
89
} from 'react'
910
import {createLocalStorageStateHook} from 'use-local-storage-state'
1011

@@ -24,6 +25,7 @@ import {
2425
ExecuteCommandInjectTagValue,
2526
ExecuteCommandInjectField,
2627
} from 'src/languageSupport/languages/flux/lsp/utils'
28+
import {useSessionStorage} from 'src/dataExplorer/shared/utils'
2729

2830
const useLocalStorageState = createLocalStorageStateHook(
2931
'dataExplorer.schema',
@@ -80,9 +82,22 @@ export const FluxQueryBuilderProvider: FC = ({children}) => {
8082
const {injectViaLsp} = useContext(EditorContext)
8183

8284
// States
83-
const [selection, setSelection] = useLocalStorageState()
85+
const [oldSelection, setOldSelection] = useLocalStorageState()
86+
const [selection, setSelection] = useSessionStorage(
87+
'dataExplorer.schema',
88+
oldSelection
89+
)
8490
const [searchTerm, setSearchTerm] = useState('')
8591

92+
// migration to allow people to keep their last used settings
93+
// immediately after rollout
94+
useEffect(() => {
95+
setOldSelection({
96+
bucket: null,
97+
measurement: null,
98+
})
99+
}, [])
100+
86101
const handleSelectBucket = (bucket: Bucket): void => {
87102
selection.bucket = bucket
88103
selection.measurement = ''

src/dataExplorer/shared/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {useState} from 'react'
12
export const LOAD_MORE_LIMIT_INITIAL = 8
23
export const LOAD_MORE_LIMIT = 25
34
export const IMPORT_REGEXP = 'import "regexp"\n'
@@ -14,3 +15,29 @@ export const FROM_BUCKET = (bucketName: string) =>
1415

1516
export const SEARCH_STRING = (searchTerm: string): string =>
1617
`|> filter(fn: (r) => r._value =~ regexp.compile(v: "(?i:" + regexp.quoteMeta(v: "${searchTerm}") + ")"))`
18+
19+
export const useSessionStorage = (keyName: string, defaultValue: any) => {
20+
const [storedValue, setStoredValue] = useState(() => {
21+
try {
22+
const value = window.sessionStorage.getItem(keyName)
23+
24+
if (value) {
25+
return JSON.parse(value)
26+
} else {
27+
window.sessionStorage.setItem(keyName, JSON.stringify(defaultValue))
28+
return defaultValue
29+
}
30+
} catch (err) {
31+
return defaultValue
32+
}
33+
})
34+
35+
const setValue = (newValue: any) => {
36+
try {
37+
window.sessionStorage.setItem(keyName, JSON.stringify(newValue))
38+
} catch (err) {}
39+
setStoredValue(newValue)
40+
}
41+
42+
return [storedValue, setValue]
43+
}

0 commit comments

Comments
 (0)