forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use_embeddable_execution_context.ts
71 lines (63 loc) · 2.08 KB
/
use_embeddable_execution_context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import useObservable from 'react-use/lib/useObservable';
import { map } from 'rxjs';
import type { KibanaExecutionContext } from '@kbn/core/types';
import { useMemo } from 'react';
import { useExecutionContext } from '@kbn/kibana-react-plugin/public';
import type { Observable } from 'rxjs';
import type { EmbeddableInput } from '@kbn/embeddable-plugin/common';
import type { ExecutionContextStart } from '@kbn/core/public';
/**
* Use execution context for ML embeddables.
* @param executionContext
* @param embeddableInput$
* @param embeddableType
* @param id
*/
export function useEmbeddableExecutionContext<T extends EmbeddableInput>(
executionContext: ExecutionContextStart,
embeddableInput$: Observable<T>,
embeddableType: string,
id: string
) {
const parentExecutionContext = useObservable(
embeddableInput$.pipe(map((v) => v.executionContext))
);
const embeddableExecutionContext: KibanaExecutionContext = useMemo(() => {
const child: KibanaExecutionContext = {
type: 'visualization',
name: embeddableType,
id,
};
return {
...parentExecutionContext,
child,
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [parentExecutionContext, id]);
useExecutionContext(executionContext, embeddableExecutionContext);
}
export const useReactEmbeddableExecutionContext = (
executionContextStart: ExecutionContextStart,
parentExecutionContext: KibanaExecutionContext,
embeddableType: string,
id: string
) => {
const embeddableExecutionContext = useMemo(() => {
const child: KibanaExecutionContext = {
type: 'visualization',
name: embeddableType,
id,
};
return {
...parentExecutionContext,
child,
};
}, [embeddableType, id, parentExecutionContext]);
useExecutionContext(executionContextStart, embeddableExecutionContext);
};