Skip to content

Commit 825ee84

Browse files
authored
feat(5340): use default editorText in script editor (#5741)
* feat(5340): use default editorText in script editor; updates to session, new script resource, and clear() methods * feat(5340): update tests for new default text, and caught edge case for certain flag+action combination * test: demonstrate that we need the default query set on both the session, and in the script resource. In order to have on-load provide the default message. * fix: add back the default message, to the default session store
1 parent c3bafb4 commit 825ee84

File tree

8 files changed

+31
-11
lines changed

8 files changed

+31
-11
lines changed

cypress/e2e/shared/editor.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Editor+LSP communication', () => {
99
cy.getByTestID(editorSelector).then(() => {
1010
cy.getByTestID('flux-editor', {timeout: 30000})
1111
.should('be.visible')
12+
.monacoType('{selectall} {backspace}')
1213
.monacoType('foo |> bar')
1314
.within(() => {
1415
cy.get('.squiggly-error', {timeout: 30000}).should('be.visible')

cypress/e2e/shared/fluxQueryBuilder.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,15 @@ describe('Script Builder', () => {
187187
})
188188

189189
describe('Schema Composition', () => {
190+
const DEFAULT_EDITOR_TEXT =
191+
'// Start by selecting data from the schema browser or typing flux here'
192+
190193
beforeEach(() => {
191194
window.sessionStorage.setItem(
192195
'dataExplorer.schema',
193196
JSON.parse(JSON.stringify(DEFAULT_SCHEMA))
194197
)
195-
window.sessionStorage.setItem('dataExplorer.query', '')
198+
window.sessionStorage.setItem('dataExplorer.query', DEFAULT_EDITOR_TEXT)
196199

197200
cy.setFeatureFlags({
198201
schemaComposition: true,
@@ -300,7 +303,7 @@ describe('Script Builder', () => {
300303

301304
cy.log('editor text is now empty')
302305
cy.getByTestID('flux-editor').within(() => {
303-
cy.get('textarea.inputarea').should('have.value', '')
306+
cy.get('textarea.inputarea').should('have.value', DEFAULT_EDITOR_TEXT)
304307
})
305308

306309
cy.log('schema browser has been cleared')
@@ -312,7 +315,7 @@ describe('Script Builder', () => {
312315
it('should not be able to modify the composition when unsynced, yet still modify the saved schema -- which updates the composition when re-synced', () => {
313316
cy.log('start with empty editor text')
314317
cy.getByTestID('flux-editor', {timeout: 30000}).within(() => {
315-
cy.get('textarea.inputarea').should('have.value', '')
318+
cy.get('textarea.inputarea').should('have.value', DEFAULT_EDITOR_TEXT)
316319
})
317320

318321
cy.log('turn off sync')
@@ -326,6 +329,7 @@ describe('Script Builder', () => {
326329

327330
cy.log('editor text is still empty')
328331
cy.getByTestID('flux-editor').within(() => {
332+
// selecting bucket will empty the editor text
329333
cy.get('textarea.inputarea').should('have.value', '')
330334
})
331335

src/dataExplorer/components/SaveAsScript.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface Props {
3434
const SaveAsScript: FC<Props> = ({onClose, type}) => {
3535
const dispatch = useDispatch()
3636
const history = useHistory()
37-
const {hasChanged, resource, setResource, save} =
37+
const {hasChanged, resource, setResource, clearSchemaSelection, save} =
3838
useContext(PersistanceContext)
3939
const {cancel} = useContext(QueryContext)
4040
const {setStatus, setResult} = useContext(ResultsContext)
@@ -79,6 +79,7 @@ const SaveAsScript: FC<Props> = ({onClose, type}) => {
7979
const clear = useCallback(() => {
8080
cancel()
8181
setStatus(RemoteDataState.NotStarted)
82+
clearSchemaSelection()
8283
setResult(null)
8384

8485
history.replace(`/orgs/${org.id}/data-explorer/from/script`)

src/dataExplorer/components/resources/TemplatePage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {RESOURCES} from 'src/dataExplorer/components/resources'
99
import {
1010
PersistanceContext,
1111
PersistanceProvider,
12+
DEFAULT_EDITOR_TEXT,
1213
} from 'src/dataExplorer/context/persistance'
1314

1415
const Template: FC = () => {
@@ -35,7 +36,7 @@ const Template: FC = () => {
3536

3637
setLoading(RemoteDataState.Loading)
3738
clearSchemaSelection()
38-
setQuery('')
39+
setQuery(DEFAULT_EDITOR_TEXT)
3940
setResource(null)
4041

4142
RESOURCES[params[0]].init.apply(this, params.slice(1)).then(data => {

src/dataExplorer/components/resources/types/script/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ResourceType} from 'src/types/resources'
22
import editor from './editor'
33
import {CLOUD} from 'src/shared/constants'
4+
import {DEFAULT_EDITOR_TEXT} from 'src/dataExplorer/context/persistance'
45

56
const {getScript, patchScript, postScript} = CLOUD
67
? require('src/client/scriptsRoutes')
@@ -18,7 +19,7 @@ export default function script(register) {
1819
if (!id || !CLOUD) {
1920
return Promise.resolve({
2021
type: ResourceType.Scripts,
21-
flux: '',
22+
flux: DEFAULT_EDITOR_TEXT,
2223
data: {},
2324
})
2425
}
@@ -36,7 +37,7 @@ export default function script(register) {
3637

3738
return {
3839
type: ResourceType.Scripts,
39-
flux: '',
40+
flux: DEFAULT_EDITOR_TEXT,
4041
data: {},
4142
}
4243
})

src/dataExplorer/context/fluxQueryBuilder.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import React, {
1111
// Context
1212
import {MeasurementsContext} from 'src/dataExplorer/context/measurements'
1313
import {FieldsContext} from 'src/dataExplorer/context/fields'
14-
import {PersistanceContext} from 'src/dataExplorer/context/persistance'
14+
import {
15+
PersistanceContext,
16+
DEFAULT_EDITOR_TEXT,
17+
} from 'src/dataExplorer/context/persistance'
1518
import {TagsContext} from 'src/dataExplorer/context/tags'
1619
import {EditorContext} from 'src/shared/contexts/editor'
1720

@@ -85,7 +88,8 @@ export const FluxQueryBuilderProvider: FC = ({children}) => {
8588
const {getFields, resetFields} = useContext(FieldsContext)
8689
const {getTagKeys, resetTags} = useContext(TagsContext)
8790
const {injectViaLsp} = useContext(EditorContext)
88-
const {selection, setSelection} = useContext(PersistanceContext)
91+
const {query, setQuery, selection, setSelection} =
92+
useContext(PersistanceContext)
8993

9094
// States
9195
// This state is a restructed PersistanceContext selection.tagValues
@@ -121,6 +125,11 @@ export const FluxQueryBuilderProvider: FC = ({children}) => {
121125
}
122126

123127
const handleSelectBucket = (bucket: Bucket): void => {
128+
// first time selecting bucket --> remove if default message
129+
if (query == DEFAULT_EDITOR_TEXT) {
130+
setQuery('')
131+
}
132+
124133
setSelection({bucket, measurement: '', fields: [], tagValues: []})
125134

126135
// Reset measurement, tags, fields, selected tag values

src/dataExplorer/context/persistance.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ export const DEFAULT_SCHEMA: SchemaSelection = {
5353
},
5454
}
5555

56+
export const DEFAULT_EDITOR_TEXT =
57+
'// Start by selecting data from the schema browser or typing flux here'
58+
5659
const DEFAULT_CONTEXT = {
5760
hasChanged: false,
5861
horizontal: [0.5],
5962
vertical: [0.25, 0.8],
6063
range: DEFAULT_TIME_RANGE,
61-
query: '',
64+
query: DEFAULT_EDITOR_TEXT,
6265
resource: null,
6366
selection: JSON.parse(JSON.stringify(DEFAULT_SCHEMA)),
6467

src/shared/components/FluxMonacoEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ const FluxEditorMonaco: FC<Props> = ({
167167
</div>
168168
</ErrorBoundary>
169169
),
170-
[onChangeScript, setEditor, useSchemaComposition]
170+
[onChangeScript, setEditor, useSchemaComposition, script]
171171
)
172172
}
173173

0 commit comments

Comments
 (0)