Skip to content

Commit 582e53c

Browse files
authored
feat(4615): generalize editor context (#4629)
Make a single, generalized context which can be used anywhere we utilize the monaco-editor in the UI. This editor context can handle incoming UI interactions/inputs into the monaco-editor, in a consistent single approach. Right now it only handles the injection of text blocks (e.g. functions or variables), but we can also expand later to additional functionality.
1 parent 735e831 commit 582e53c

File tree

10 files changed

+362
-416
lines changed

10 files changed

+362
-416
lines changed

src/flows/context/editor.tsx

Lines changed: 0 additions & 131 deletions
This file was deleted.

src/flows/pipes/RawFluxEditor/SecretsList.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ import {showOverlay, dismissOverlay} from 'src/overlays/actions/overlays'
1818
import {getSecrets} from 'src/secrets/actions/thunks'
1919
import {getAllSecrets} from 'src/resources/selectors'
2020

21-
// Context
22-
import {InjectionOptions, InjectionType} from 'src/flows/context/editor'
21+
// Injection
22+
import {InjectionOptions, InjectionType} from 'src/shared/contexts/editor'
2323

2424
// Utils
2525
import {event} from 'src/cloud/utils/reporting'
2626
import {Secret} from 'src/types'
2727

2828
interface Props {
2929
inject: (options: InjectionOptions) => void
30+
cbOnInject: (queryText: string) => void
3031
}
3132

32-
const SecretsList: FC<Props> = ({inject}) => {
33+
const SecretsList: FC<Props> = ({inject, cbOnInject}) => {
3334
const dispatch = useDispatch()
3435
const secrets = useSelector(getAllSecrets)
3536

@@ -46,11 +47,12 @@ const SecretsList: FC<Props> = ({inject}) => {
4647
text: `secrets.get(key: "${secret.id}") `,
4748
type: InjectionType.SameLine,
4849
header: `import "influxdata/influxdb/secrets"`,
50+
cbParentOnUpdateText: cbOnInject,
4951
}
5052
inject(options)
5153
event('Inject secret into Flux Script', {secret: secret.id})
5254
},
53-
[inject]
55+
[inject, cbOnInject]
5456
)
5557

5658
useEffect(() => {

src/flows/pipes/RawFluxEditor/view.tsx

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ import {PipeProp} from 'src/types/flows'
2323
// Context
2424
import {PipeContext} from 'src/flows/context/pipe'
2525
import {SidebarContext} from 'src/flows/context/sidebar'
26-
import {
27-
EditorContext,
28-
EditorProvider,
29-
InjectionType,
30-
} from 'src/flows/context/editor'
26+
import {EditorContext, EditorProvider} from 'src/shared/contexts/editor'
3127
import {VariablesContext} from 'src/flows/context/variables'
3228

3329
// Components
@@ -39,10 +35,6 @@ import DynamicFunctions from 'src/flows/pipes/RawFluxEditor/FunctionsList/Dynami
3935
import 'src/flows/pipes/RawFluxEditor/style.scss'
4036

4137
// Utils
42-
import {
43-
isPipeTransformation,
44-
functionRequiresNewLine,
45-
} from 'src/shared/utils/fluxFunctions'
4638
import {event} from 'src/cloud/utils/reporting'
4739
import {CLOUD} from 'src/shared/constants'
4840
import {isFlagEnabled} from 'src/shared/utils/featureFlag'
@@ -52,12 +44,12 @@ const FluxMonacoEditor = lazy(() =>
5244
)
5345

5446
const Query: FC<PipeProp> = ({Context}) => {
55-
const {id, data} = useContext(PipeContext)
47+
const {id, data, update} = useContext(PipeContext)
5648
const {hideSub, id: showId, show, showSub, register} = useContext(
5749
SidebarContext
5850
)
5951
const editorContext = useContext(EditorContext)
60-
const {setEditor, inject, updateText} = editorContext
52+
const {setEditor, inject, injectFunction} = editorContext
6153
const {queries, activeQuery} = data
6254
const query = queries[activeQuery]
6355
const {variables} = useContext(VariablesContext)
@@ -71,54 +63,35 @@ const Query: FC<PipeProp> = ({Context}) => {
7163
{
7264
title: 'Inject Secret',
7365
disable: () => false,
74-
menu: <SecretsList inject={inject} />,
66+
menu: <SecretsList inject={inject} cbOnInject={updateText} />,
7567
},
7668
],
7769
},
7870
])
7971
}
8072
}, [id, inject])
8173

82-
const injectIntoEditor = useCallback(
83-
(fn): void => {
84-
const text = isPipeTransformation(fn)
85-
? ` |> ${fn.example}`
86-
: `${fn.example}`
87-
88-
const getHeader = fn => {
89-
let importStatement = null
90-
91-
// universe packages are loaded by deafult. Don't need import statement
92-
if (fn.package && fn.package !== 'universe') {
93-
importStatement = `import "${fn.package}"`
94-
if (
95-
CLOUD &&
96-
isFlagEnabled('fluxDynamicDocs') &&
97-
fn.path.includes('/')
98-
) {
99-
importStatement = `import "${fn.path}"`
100-
}
101-
}
102-
return importStatement
74+
const updateText = useCallback(
75+
text => {
76+
const _queries = [...queries]
77+
_queries[activeQuery] = {
78+
...queries[activeQuery],
79+
text,
10380
}
10481

105-
const type =
106-
isPipeTransformation(fn) || functionRequiresNewLine(fn)
107-
? InjectionType.OnOwnLine
108-
: InjectionType.SameLine
82+
update({queries: _queries})
83+
},
84+
[queries, activeQuery]
85+
)
10986

110-
const options = {
111-
text,
112-
type,
113-
header: getHeader(fn),
114-
triggerSuggest: true,
115-
}
116-
inject(options)
87+
const injectIntoEditor = useCallback(
88+
(fn): void => {
89+
injectFunction(fn, updateText)
11790
},
118-
[inject]
91+
[injectFunction, updateText]
11992
)
12093

121-
const launcher = () => {
94+
const launcher = useCallback(() => {
12295
if (showId === id) {
12396
event('Flux Panel (Notebooks) - Toggle Functions - Off')
12497
hideSub()
@@ -131,7 +104,7 @@ const Query: FC<PipeProp> = ({Context}) => {
131104
showSub(<Functions onSelect={injectIntoEditor} />)
132105
}
133106
}
134-
}
107+
}, [injectIntoEditor, showId])
135108

136109
const controls = (
137110
<Button
@@ -172,6 +145,7 @@ const Query: FC<PipeProp> = ({Context}) => {
172145
updateText,
173146
editorContext.editor,
174147
variables,
148+
launcher,
175149
]
176150
)
177151
}

0 commit comments

Comments
 (0)