diff --git a/changelogs/fragments/8334.yml b/changelogs/fragments/8334.yml new file mode 100644 index 000000000000..f2ff968f472f --- /dev/null +++ b/changelogs/fragments/8334.yml @@ -0,0 +1,2 @@ +refactor: +- Move the render of query extension control buttons to extension component ([#8334](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8334)) \ No newline at end of file diff --git a/src/plugins/data/public/ui/query_editor/_query_editor.scss b/src/plugins/data/public/ui/query_editor/_query_editor.scss index e9655d245fde..bb6d1c57f95e 100644 --- a/src/plugins/data/public/ui/query_editor/_query_editor.scss +++ b/src/plugins/data/public/ui/query_editor/_query_editor.scss @@ -169,6 +169,12 @@ .osdQueryEditor__querycontrols { float: right; margin: $euiSizeS $euiSizeS; + + .osdQueryEditor__extensionQueryControls { + display: flex; + padding: 0 $euiSizeS 0 $euiSizeXS; + border-right: $euiBorderThin; + } } .osdQueryEditor__dataSetPicker { diff --git a/src/plugins/data/public/ui/query_editor/query_editor.tsx b/src/plugins/data/public/ui/query_editor/query_editor.tsx index 6932cec906f8..c2d79fb6d470 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor.tsx @@ -106,6 +106,7 @@ export default class QueryEditorUI extends Component { private services = this.props.opensearchDashboards.services; private headerRef: RefObject = createRef(); private bannerRef: RefObject = createRef(); + private queryControlsContainer: RefObject = createRef(); private extensionMap = this.languageManager.getQueryEditorExtensionMap(); private getQueryString = () => { @@ -121,6 +122,7 @@ export default class QueryEditorUI extends Component { !( this.headerRef.current && this.bannerRef.current && + this.queryControlsContainer.current && this.props.query.language && this.extensionMap && Object.keys(this.extensionMap).length > 0 @@ -137,6 +139,7 @@ export default class QueryEditorUI extends Component { configMap={this.extensionMap} componentContainer={this.headerRef.current} bannerContainer={this.bannerRef.current} + queryControlsContainer={this.queryControlsContainer.current} /> ); } @@ -336,25 +339,6 @@ export default class QueryEditorUI extends Component { return ; }; - private renderExtensionSearchBarButton = () => { - if (!this.extensionMap || Object.keys(this.extensionMap).length === 0) return null; - const sortedConfigs = Object.values(this.extensionMap).sort((a, b) => a.order - b.order); - return ( - <> - {sortedConfigs.map((config) => { - return config.getSearchBarButton - ? config.getSearchBarButton({ - language: this.props.query.language, - onSelectLanguage: this.onSelectLanguage, - isCollapsed: this.state.isCollapsed, - setIsCollapsed: this.setIsCollapsed, - }) - : null; - })} - - ); - }; - public render() { const className = classNames(this.props.className); @@ -471,9 +455,12 @@ export default class QueryEditorUI extends Component { {languageSelector}
+
{this.renderQueryControls(languageEditor.TopBar.Controls)} {!languageEditor.TopBar.Expanded && this.renderToggleIcon()} - {!languageEditor.TopBar.Expanded && this.renderExtensionSearchBarButton()} {this.props.savedQueryManagement}
diff --git a/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extension.tsx b/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extension.tsx index 7a8fddfe7ee8..95af159c785f 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extension.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extension.tsx @@ -14,6 +14,7 @@ interface QueryEditorExtensionProps { dependencies: QueryEditorExtensionDependencies; componentContainer: Element; bannerContainer: Element; + queryControlsContainer: Element; } export interface QueryEditorExtensionDependencies { @@ -97,6 +98,11 @@ export const QueryEditorExtension: React.FC = (props) props.dependencies, ]); + const queryControlButtons = useMemo(() => props.config.getSearchBarButton?.(props.dependencies), [ + props.config, + props.dependencies, + ]); + useEffect(() => { isMounted.current = true; return () => { @@ -121,6 +127,9 @@ export const QueryEditorExtension: React.FC = (props) {component} + + {queryControlButtons} + ); }; diff --git a/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.test.tsx b/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.test.tsx index 37a8afe91e6e..ec67a3a52dfb 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.test.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.test.tsx @@ -23,6 +23,7 @@ describe('QueryEditorExtensions', () => { const defaultProps: QueryEditorExtensionsProps = { componentContainer: document.createElement('div'), bannerContainer: document.createElement('div'), + queryControlsContainer: document.createElement('div'), language: 'Test-lang', onSelectLanguage: jest.fn(), isCollapsed: false, diff --git a/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.tsx b/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.tsx index e3de25ac5198..90c7fbf51666 100644 --- a/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.tsx +++ b/src/plugins/data/public/ui/query_editor/query_editor_extensions/query_editor_extensions.tsx @@ -14,10 +14,17 @@ interface QueryEditorExtensionsProps extends QueryEditorExtensionDependencies { configMap?: Record; componentContainer: Element; bannerContainer: Element; + queryControlsContainer: Element; } const QueryEditorExtensions: React.FC = React.memo((props) => { - const { configMap, componentContainer, bannerContainer, ...dependencies } = props; + const { + configMap, + componentContainer, + bannerContainer, + queryControlsContainer, + ...dependencies + } = props; const sortedConfigs = useMemo(() => { if (!configMap || Object.keys(configMap).length === 0) return []; @@ -27,14 +34,25 @@ const QueryEditorExtensions: React.FC = React.memo(( return ( <> {sortedConfigs.map((config) => { - const id = `osdQueryEditorExtensionComponent-${config.id}`; - - let container = document.getElementById(id); - if (!container) { - container = document.createElement('div'); - container.className = `osdQueryEditorExtensionComponent osdQueryEditorExtensionComponent__${config.id}`; - container.id = id; - componentContainer.appendChild(container); + const extensionComponentId = `osdQueryEditorExtensionComponent-${config.id}`; + const extensionQueryControlsId = `osdQueryEditorExtensionQueryControls-${config.id}`; + + // Make sure extension components are rendered in order + let extensionComponentContainer = document.getElementById(extensionComponentId); + if (!extensionComponentContainer) { + extensionComponentContainer = document.createElement('div'); + extensionComponentContainer.className = `osdQueryEditorExtensionComponent osdQueryEditorExtensionComponent__${config.id}`; + extensionComponentContainer.id = extensionComponentId; + componentContainer.appendChild(extensionComponentContainer); + } + + // Make sure extension query controls are rendered in order + let extensionQueryControlsContainer = document.getElementById(extensionQueryControlsId); + if (!extensionQueryControlsContainer) { + extensionQueryControlsContainer = document.createElement('div'); + extensionQueryControlsContainer.className = `osdQueryEditorExtensionQueryControls osdQueryEditorExtensionQueryControls__${config.id}`; + extensionQueryControlsContainer.id = extensionQueryControlsId; + queryControlsContainer.appendChild(extensionQueryControlsContainer); } return ( @@ -42,8 +60,9 @@ const QueryEditorExtensions: React.FC = React.memo(( key={config.id} config={config} dependencies={dependencies} - componentContainer={container} + componentContainer={extensionComponentContainer} bannerContainer={bannerContainer} + queryControlsContainer={extensionQueryControlsContainer} /> ); })} diff --git a/src/plugins/query_enhancements/public/query_assist/components/query_assist_button.tsx b/src/plugins/query_enhancements/public/query_assist/components/query_assist_button.tsx index 25a198288442..32c20b3a33ce 100644 --- a/src/plugins/query_enhancements/public/query_assist/components/query_assist_button.tsx +++ b/src/plugins/query_enhancements/public/query_assist/components/query_assist_button.tsx @@ -32,7 +32,7 @@ export const QueryAssistButton: React.FC = (props) => { iconType={ !props.dependencies.isCollapsed && !isQueryAssistCollapsed ? expandIcon : collapsedIcon } - aria-label={i18n.translate('queryEnhancements.queryAssist.button.ariaLable', { + aria-label={i18n.translate('queryEnhancements.queryAssist.button.ariaLabel', { defaultMessage: `Query Assist Toggle`, })} onClick={onClick}