Skip to content

Commit

Permalink
change query editor extension configs from map to record
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <joshuali925@gmail.com>
  • Loading branch information
joshuali925 committed Jun 17, 2024
1 parent 886ace5 commit 7f0e39e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe('QueryEditorExtensions', () => {
});

it('renders without any items in map', () => {
const { container } = render(<QueryEditorExtensions {...defaultProps} configMap={new Map()} />);
const { container } = render(<QueryEditorExtensions {...defaultProps} configMap={{}} />);
expect(container).toBeEmptyDOMElement();
});

it('correctly orders configurations based on order property', () => {
const configMap = new Map([
['1', { id: '1', order: 2, isEnabled: jest.fn(), getComponent: jest.fn() }],
['2', { id: '2', order: 1, isEnabled: jest.fn(), getComponent: jest.fn() }],
]);
const configMap = {
'1': { id: '1', order: 2, isEnabled: jest.fn(), getComponent: jest.fn() },
'2': { id: '2', order: 1, isEnabled: jest.fn(), getComponent: jest.fn() },
};

const { getAllByText } = render(
<QueryEditorExtensions {...defaultProps} configMap={configMap} />
Expand All @@ -74,9 +74,9 @@ describe('QueryEditorExtensions', () => {
});

it('passes dependencies correctly to QueryEditorExtension', async () => {
const configMap = new Map([
['1', { id: '1', order: 1, isEnabled: jest.fn(), getComponent: jest.fn() }],
]);
const configMap = {
'1': { id: '1', order: 1, isEnabled: jest.fn(), getComponent: jest.fn() },
};

const { getByText } = render(<QueryEditorExtensions {...defaultProps} configMap={configMap} />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './query_editor_extension';

interface QueryEditorExtensionsProps extends QueryEditorExtensionDependencies {
configMap?: Map<string, QueryEditorExtensionConfig>;
configMap?: Record<string, QueryEditorExtensionConfig>;
componentContainer: Element;
bannerContainer: Element;
}
Expand All @@ -20,7 +20,7 @@ const QueryEditorExtensions: React.FC<QueryEditorExtensionsProps> = React.memo((
const { configMap, componentContainer, bannerContainer, ...dependencies } = props;

const sortedConfigs = useMemo(() => {
if (!configMap?.size) return [];
if (!configMap || !Object.keys(configMap)) return [];
return Object.values(configMap).sort((a, b) => a.order - b.order);
}, [configMap]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function QueryEditorTopRow(props: QueryEditorTopRowProps) {
props.settings &&
props.settings.getQueryEnhancements(queryLanguage)?.searchBar) ||
null;
const queryEditorExtensions = props.settings?.getQueryEditorExtensions();
const queryEditorExtensionMap = props.settings?.getQueryEditorExtensionMap();
const parsedQuery =
!queryUiEnhancement || isValidQuery(props.query)
? props.query!
Expand Down Expand Up @@ -264,7 +264,7 @@ export default function QueryEditorTopRow(props: QueryEditorTopRowProps) {
return (
<QueryEditorExtensions
language={queryLanguage}
configMap={queryEditorExtensions}
configMap={queryEditorExtensionMap}
componentContainer={queryEditorHeaderRef.current}
bannerContainer={queryEditorBannerRef.current}
indexPatterns={props.indexPatterns}
Expand Down Expand Up @@ -305,7 +305,7 @@ export default function QueryEditorTopRow(props: QueryEditorTopRowProps) {
}

function shouldRenderQueryEditorExtensions(): boolean {
return Boolean(queryEditorExtensions?.size);
return Boolean(queryEditorExtensionMap && Object.keys(queryEditorExtensionMap).length);
}

function renderUpdateButton() {
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/data/public/ui/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Settings {
private readonly search: ISearchStart,
private readonly storage: IStorageWrapper,
private readonly queryEnhancements: Map<string, QueryEnhancement>,
private readonly queryEditorExtensions: Map<string, QueryEditorExtensionConfig>
private readonly queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig>
) {
this.isEnabled = this.config.enabled;
this.setUserQueryEnhancementsEnabled(this.isEnabled);
Expand Down Expand Up @@ -67,8 +67,8 @@ export class Settings {
return this.queryEnhancements.get(language);
}

getQueryEditorExtensions() {
return this.queryEditorExtensions;
getQueryEditorExtensionMap() {
return this.queryEditorExtensionMap;

Check warning on line 71 in src/plugins/data/public/ui/settings/settings.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/settings/settings.ts#L71

Added line #L71 was not covered by tests
}

getUserQueryLanguageBlocklist() {
Expand Down Expand Up @@ -155,15 +155,15 @@ interface Deps {
search: ISearchStart;
storage: IStorageWrapper;
queryEnhancements: Map<string, QueryEnhancement>;
queryEditorExtensions: Map<string, QueryEditorExtensionConfig>;
queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig>;
}

export function createSettings({
config,
search,
storage,
queryEnhancements,
queryEditorExtensions,
queryEditorExtensionMap,
}: Deps) {
return new Settings(config, search, storage, queryEnhancements, queryEditorExtensions);
return new Settings(config, search, storage, queryEnhancements, queryEditorExtensionMap);

Check warning on line 168 in src/plugins/data/public/ui/settings/settings.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/settings/settings.ts#L168

Added line #L168 was not covered by tests
}
12 changes: 5 additions & 7 deletions src/plugins/data/public/ui/ui_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { IStorageWrapper } from '../../../opensearch_dashboards_utils/public';
import { ConfigSchema } from '../../config';
import { DataPublicPluginStart } from '../types';
import { createIndexPatternSelect } from './index_pattern_select';
import { createSearchBar } from './search_bar/create_search_bar';
import { QueryEditorExtensionConfig } from './query_editor';
import { createSearchBar } from './search_bar/create_search_bar';
import { createSettings } from './settings';
import { SuggestionsComponent } from './typeahead';
import { IUiSetup, IUiStart, QueryEnhancement, UiEnhancements } from './types';
Expand All @@ -28,7 +28,7 @@ export interface UiServiceStartDependencies {
export class UiService implements Plugin<IUiSetup, IUiStart> {
enhancementsConfig: ConfigSchema['enhancements'];
private queryEnhancements: Map<string, QueryEnhancement> = new Map();
private queryEditorExtensions: Map<string, QueryEditorExtensionConfig> = new Map();
private queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig> = {};

Check warning on line 31 in src/plugins/data/public/ui/ui_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/ui_service.ts#L31

Added line #L31 was not covered by tests
private container$ = new BehaviorSubject<HTMLDivElement | null>(null);

constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
Expand All @@ -46,10 +46,8 @@ export class UiService implements Plugin<IUiSetup, IUiStart> {
this.queryEnhancements.set(enhancements.query.language, enhancements.query);
}
if (enhancements.queryEditorExtension) {
this.queryEditorExtensions.set(
enhancements.queryEditorExtension.id,
enhancements.queryEditorExtension
);
this.queryEditorExtensionMap[enhancements.queryEditorExtension.id] =

Check warning on line 49 in src/plugins/data/public/ui/ui_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/ui_service.ts#L49

Added line #L49 was not covered by tests
enhancements.queryEditorExtension;
}
},
};
Expand All @@ -61,7 +59,7 @@ export class UiService implements Plugin<IUiSetup, IUiStart> {
search: dataServices.search,
storage,
queryEnhancements: this.queryEnhancements,
queryEditorExtensions: this.queryEditorExtensions,
queryEditorExtensionMap: this.queryEditorExtensionMap,
});

const setContainerRef = (ref: HTMLDivElement | null) => {
Expand Down

0 comments on commit 7f0e39e

Please sign in to comment.