Skip to content

Commit 450ec4b

Browse files
opensearch-trigger-bot[bot]github-actions[bot]opensearch-changeset-bot[bot]
authored
[Discover-next] (QueryEditorExtensions) change isEnabled to an observable (#7183) (#7193)
* (QueryEditorExtensions) change isEnabled to an observable * rename search bar extension to query editor extension in comments * Changeset file for PR #7183 created/updated --------- (cherry picked from commit 774f64e) Signed-off-by: Joshua Li <joshuali925@gmail.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
1 parent e2bb1e4 commit 450ec4b

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

changelogs/fragments/7183.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
feat:
2+
- [QueryEditorExtensions] change `isEnabled` to an observable ([#7183](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7183))

src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extension.test.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { render, waitFor } from '@testing-library/react';
77
import React, { ComponentProps } from 'react';
8+
import { of } from 'rxjs';
89
import { IIndexPattern } from '../../../../common';
910
import { QueryEditorExtension } from './query_editor_extension';
1011

@@ -39,7 +40,7 @@ describe('QueryEditorExtension', () => {
3940
config: {
4041
id: 'test-extension',
4142
order: 1,
42-
isEnabled: isEnabledMock,
43+
isEnabled$: isEnabledMock,
4344
getComponent: getComponentMock,
4445
getBanner: getBannerMock,
4546
},
@@ -56,7 +57,7 @@ describe('QueryEditorExtension', () => {
5657
});
5758

5859
it('renders correctly when isEnabled is true', async () => {
59-
isEnabledMock.mockResolvedValue(true);
60+
isEnabledMock.mockReturnValue(of(true));
6061
getComponentMock.mockReturnValue(<div>Test Component</div>);
6162
getBannerMock.mockReturnValue(<div>Test Banner</div>);
6263

@@ -72,7 +73,7 @@ describe('QueryEditorExtension', () => {
7273
});
7374

7475
it('does not render when isEnabled is false', async () => {
75-
isEnabledMock.mockResolvedValue(false);
76+
isEnabledMock.mockReturnValue(of(false));
7677
getComponentMock.mockReturnValue(<div>Test Component</div>);
7778

7879
const { queryByText } = render(<QueryEditorExtension {...defaultProps} />);

src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extension.tsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { EuiErrorBoundary } from '@elastic/eui';
77
import React, { useEffect, useMemo, useRef, useState } from 'react';
88
import ReactDOM from 'react-dom';
9+
import { Observable } from 'rxjs';
910
import { IIndexPattern } from '../../../../common';
1011
import { DataSource } from '../../../data_sources/datasource';
1112

@@ -33,30 +34,30 @@ export interface QueryEditorExtensionDependencies {
3334

3435
export interface QueryEditorExtensionConfig {
3536
/**
36-
* The id for the search bar extension.
37+
* The id for the query editor extension.
3738
*/
3839
id: string;
3940
/**
4041
* Lower order indicates higher position on UI.
4142
*/
4243
order: number;
4344
/**
44-
* A function that determines if the search bar extension is enabled and should be rendered on UI.
45+
* A function that determines if the query editor extension is enabled and should be rendered on UI.
4546
* @returns whether the extension is enabled.
4647
*/
47-
isEnabled: (dependencies: QueryEditorExtensionDependencies) => Promise<boolean>;
48+
isEnabled$: (dependencies: QueryEditorExtensionDependencies) => Observable<boolean>;
4849
/**
49-
* A function that returns the search bar extension component. The component
50+
* A function that returns the query editor extension component. The component
5051
* will be displayed on top of the query editor in the search bar.
5152
* @param dependencies - The dependencies required for the extension.
52-
* @returns The component the search bar extension.
53+
* @returns The component the query editor extension.
5354
*/
5455
getComponent?: (dependencies: QueryEditorExtensionDependencies) => React.ReactElement | null;
5556
/**
56-
* A function that returns the search bar extension banner. The banner is a
57+
* A function that returns the query editor extension banner. The banner is a
5758
* component that will be displayed on top of the search bar.
5859
* @param dependencies - The dependencies required for the extension.
59-
* @returns The component the search bar extension.
60+
* @returns The component the query editor extension.
6061
*/
6162
getBanner?: (dependencies: QueryEditorExtensionDependencies) => React.ReactElement | null;
6263
}
@@ -92,9 +93,10 @@ export const QueryEditorExtension: React.FC<QueryEditorExtensionProps> = (props)
9293
}, []);
9394

9495
useEffect(() => {
95-
props.config.isEnabled(props.dependencies).then((enabled) => {
96+
const subscription = props.config.isEnabled$(props.dependencies).subscribe((enabled) => {
9697
if (isMounted.current) setIsEnabled(enabled);
9798
});
99+
return () => subscription.unsubscribe();
98100
}, [props.dependencies, props.config]);
99101

100102
if (!isEnabled) return null;

0 commit comments

Comments
 (0)