Skip to content

Commit

Permalink
[Discover-next] (QueryEditorExtensions) change isEnabled to an observ…
Browse files Browse the repository at this point in the history
…able (#7183)

* (QueryEditorExtensions) change isEnabled to an observable

Signed-off-by: Joshua Li <joshuali925@gmail.com>

* rename search bar extension to query editor extension in comments

Signed-off-by: Joshua Li <joshuali925@gmail.com>

* Changeset file for PR #7183 created/updated

---------

Signed-off-by: Joshua Li <joshuali925@gmail.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
(cherry picked from commit 774f64e)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent e93688f commit 6688111
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7183.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [QueryEditorExtensions] change `isEnabled` to an observable ([#7183](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7183))
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { render, waitFor } from '@testing-library/react';
import React, { ComponentProps } from 'react';
import { of } from 'rxjs';
import { IIndexPattern } from '../../../../common';
import { QueryEditorExtension } from './query_editor_extension';

Expand Down Expand Up @@ -39,7 +40,7 @@ describe('QueryEditorExtension', () => {
config: {
id: 'test-extension',
order: 1,
isEnabled: isEnabledMock,
isEnabled$: isEnabledMock,
getComponent: getComponentMock,
getBanner: getBannerMock,
},
Expand All @@ -56,7 +57,7 @@ describe('QueryEditorExtension', () => {
});

it('renders correctly when isEnabled is true', async () => {
isEnabledMock.mockResolvedValue(true);
isEnabledMock.mockReturnValue(of(true));
getComponentMock.mockReturnValue(<div>Test Component</div>);
getBannerMock.mockReturnValue(<div>Test Banner</div>);

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

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

const { queryByText } = render(<QueryEditorExtension {...defaultProps} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { EuiErrorBoundary } from '@elastic/eui';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { Observable } from 'rxjs';
import { IIndexPattern } from '../../../../common';
import { DataSource } from '../../../data_sources/datasource';

Expand Down Expand Up @@ -33,30 +34,30 @@ export interface QueryEditorExtensionDependencies {

export interface QueryEditorExtensionConfig {
/**
* The id for the search bar extension.
* The id for the query editor extension.
*/
id: string;
/**
* Lower order indicates higher position on UI.
*/
order: number;
/**
* A function that determines if the search bar extension is enabled and should be rendered on UI.
* A function that determines if the query editor extension is enabled and should be rendered on UI.
* @returns whether the extension is enabled.
*/
isEnabled: (dependencies: QueryEditorExtensionDependencies) => Promise<boolean>;
isEnabled$: (dependencies: QueryEditorExtensionDependencies) => Observable<boolean>;
/**
* A function that returns the search bar extension component. The component
* A function that returns the query editor extension component. The component
* will be displayed on top of the query editor in the search bar.
* @param dependencies - The dependencies required for the extension.
* @returns The component the search bar extension.
* @returns The component the query editor extension.
*/
getComponent?: (dependencies: QueryEditorExtensionDependencies) => React.ReactElement | null;
/**
* A function that returns the search bar extension banner. The banner is a
* A function that returns the query editor extension banner. The banner is a
* component that will be displayed on top of the search bar.
* @param dependencies - The dependencies required for the extension.
* @returns The component the search bar extension.
* @returns The component the query editor extension.
*/
getBanner?: (dependencies: QueryEditorExtensionDependencies) => React.ReactElement | null;
}
Expand Down Expand Up @@ -92,9 +93,10 @@ export const QueryEditorExtension: React.FC<QueryEditorExtensionProps> = (props)
}, []);

useEffect(() => {
props.config.isEnabled(props.dependencies).then((enabled) => {
const subscription = props.config.isEnabled$(props.dependencies).subscribe((enabled) => {
if (isMounted.current) setIsEnabled(enabled);
});
return () => subscription.unsubscribe();
}, [props.dependencies, props.config]);

if (!isEnabled) return null;
Expand Down

0 comments on commit 6688111

Please sign in to comment.