Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Add support to remotely open Embeddable Console #176017

4 changes: 4 additions & 0 deletions packages/kbn-search-api-panels/components/code_box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import type { ApplicationStart } from '@kbn/core-application-browser';
import type { ConsolePluginStart } from '@kbn/console-plugin/public';
import type { SharePluginStart } from '@kbn/share-plugin/public';

import { LanguageDefinition } from '../types';
Expand All @@ -38,13 +39,15 @@ interface CodeBoxProps {
setSelectedLanguage: (language: LanguageDefinition) => void;
assetBasePath: string;
application?: ApplicationStart;
consolePlugin?: ConsolePluginStart;
sharePlugin: SharePluginStart;
consoleRequest?: string;
}

export const CodeBox: React.FC<CodeBoxProps> = ({
application,
codeSnippet,
consolePlugin,
languageType,
languages,
assetBasePath,
Expand Down Expand Up @@ -117,6 +120,7 @@ export const CodeBox: React.FC<CodeBoxProps> = ({
<TryInConsoleButton
request={consoleRequest}
application={application}
consolePlugin={consolePlugin}
sharePlugin={sharePlugin}
/>
</EuiFlexItem>
Expand Down
4 changes: 4 additions & 0 deletions packages/kbn-search-api-panels/components/ingest_data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React from 'react';
import { EuiSpacer, EuiTitle } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import type { ApplicationStart } from '@kbn/core-application-browser';
import type { ConsolePluginStart } from '@kbn/console-plugin/public';
import type { SharePluginStart } from '@kbn/share-plugin/public';
import { CodeBox } from './code_box';
import { LanguageDefinition } from '../types';
Expand All @@ -26,6 +27,7 @@ interface IngestDataProps {
};
assetBasePath: string;
application?: ApplicationStart;
consolePlugin?: ConsolePluginStart;
sharePlugin: SharePluginStart;
languages: LanguageDefinition[];
consoleRequest?: string;
Expand All @@ -39,6 +41,7 @@ export const IngestData: React.FC<IngestDataProps> = ({
docLinks,
assetBasePath,
application,
consolePlugin,
sharePlugin,
languages,
consoleRequest,
Expand All @@ -58,6 +61,7 @@ export const IngestData: React.FC<IngestDataProps> = ({
setSelectedLanguage={setSelectedLanguage}
assetBasePath={assetBasePath}
application={application}
consolePlugin={consolePlugin}
sharePlugin={sharePlugin}
/>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import React from 'react';
import { EuiButtonEmpty } from '@elastic/eui';
import type { ApplicationStart } from '@kbn/core-application-browser';
import type { SharePluginStart } from '@kbn/share-plugin/public';
import type { ConsolePluginStart } from '@kbn/console-plugin/public';

import { FormattedMessage } from '@kbn/i18n-react';
import { compressToEncodedURIComponent } from 'lz-string';

export interface TryInConsoleButtonProps {
request: string;
application?: ApplicationStart;
consolePlugin?: ConsolePluginStart;
sharePlugin: SharePluginStart;
}
export const TryInConsoleButton = ({
request,
application,
consolePlugin,
sharePlugin,
}: TryInConsoleButtonProps) => {
const { url } = sharePlugin;
Expand All @@ -39,8 +42,20 @@ export const TryInConsoleButton = ({
);
if (!consolePreviewLink) return null;

const onClick = () => {
const embeddedConsoleAvailable =
(consolePlugin?.openEmbeddedConsole !== undefined &&
consolePlugin?.isEmbeddedConsoleAvailable?.()) ??
false;
if (embeddedConsoleAvailable) {
consolePlugin!.openEmbeddedConsole!(request);
} else {
window.open(consolePreviewLink, '_blank', 'noreferrer');
}
};

return (
<EuiButtonEmpty href={consolePreviewLink} iconType="popout" target="_blank" size="s">
<EuiButtonEmpty onClick={onClick} iconType="popout" size="s">
<FormattedMessage
id="searchApiPanels.welcomeBanner.tryInConsoleButton"
defaultMessage="Try in Console"
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-search-api-panels/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@kbn/core-application-browser",
"@kbn/share-plugin",
"@kbn/i18n-react",
"@kbn/security-plugin"
"@kbn/security-plugin",
"@kbn/console-plugin"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function EditorUI({ initialTextValue, setEditorInstance }: EditorProps) {
}

const readQueryParams = () => {
const [, queryString] = (window.location.hash || '').split('?');
const [, queryString] = (window.location.hash || window.location.search || '').split('?');

return parse(queryString || '', { sort: false }) as Required<QueryParams>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
I18nStart,
CoreTheme,
DocLinksStart,
CoreStart,
} from '@kbn/core/public';
import { KibanaThemeProvider } from '@kbn/react-kibana-context-theme';
import { UsageCollectionStart } from '@kbn/usage-collection-plugin/public';

import { ObjectStorageClient } from '../../../../common/types';

Expand Down Expand Up @@ -62,10 +64,10 @@ interface ConsoleDependencies {
trackUiMetric: MetricsTracker;
}

const loadDependencies = async ({
core,
usageCollection,
}: EmbeddableConsoleDependencies): Promise<ConsoleDependencies> => {
const loadDependencies = async (
core: CoreStart,
usageCollection?: UsageCollectionStart
): Promise<ConsoleDependencies> => {
const {
docLinks: { DOC_LINK_VERSION, links },
http,
Expand Down Expand Up @@ -107,10 +109,12 @@ const loadDependencies = async ({
};
};

export const ConsoleWrapper = (props: EmbeddableConsoleDependencies): React.ReactElement => {
type ConsoleWrapperProps = Omit<EmbeddableConsoleDependencies, 'setDispatch'>;

export const ConsoleWrapper: React.FunctionComponent<ConsoleWrapperProps> = (props) => {
const [dependencies, setDependencies] = useState<ConsoleDependencies | null>(null);
useEffect(() => {
loadDependencies(props).then(setDependencies);
loadDependencies(props.core, props.usageCollection).then(setDependencies);
}, [setDependencies, props]);
useEffect(() => {
return () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import React, { useState } from 'react';
import React, { useReducer, useEffect } from 'react';
import classNames from 'classnames';
import useObservable from 'react-use/lib/useObservable';
import {
Expand All @@ -25,6 +25,9 @@ import {
EmbeddableConsoleDependencies,
} from '../../../types/embeddable_console';

import * as store from '../../stores/embeddable_console';
import { setLoadFromParameter, removeLoadFromParameter } from '../../lib/load_from';

import { ConsoleWrapper } from './console_wrapper';
import './_index.scss';

Expand All @@ -36,10 +39,31 @@ export const EmbeddableConsole = ({
size = 'm',
core,
usageCollection,
setDispatch,
}: EmbeddableConsoleProps & EmbeddableConsoleDependencies) => {
const [isConsoleOpen, setIsConsoleOpen] = useState<boolean>(false);
const toggleConsole = () => setIsConsoleOpen(!isConsoleOpen);
const [consoleState, consoleDispatch] = useReducer(
store.reducer,
store.initialValue,
(value) => ({ ...value })
);
const chromeStyle = useObservable(core.chrome.getChromeStyle$());
useEffect(() => {
setDispatch(consoleDispatch);
return () => setDispatch(null);
}, [setDispatch, consoleDispatch]);
useEffect(() => {
if (consoleState.isOpen && consoleState.loadFromContent) {
setLoadFromParameter(consoleState.loadFromContent);
} else if (!consoleState.isOpen) {
removeLoadFromParameter();
}
}, [consoleState.isOpen, consoleState.loadFromContent]);

const isConsoleOpen = consoleState.isOpen;
const setIsConsoleOpen = (value: boolean) => {
consoleDispatch(value ? { type: 'open' } : { type: 'close' });
};
const toggleConsole = () => setIsConsoleOpen(!isConsoleOpen);

const onKeyDown = (event: any) => {
if (event.key === keys.ESCAPE) {
Expand Down
Loading