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

[Discover] Deangularization of search embeddable #100552

Merged
merged 29 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 50 additions & 53 deletions api_docs/deprecations.mdx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ export function createDiscoverGridDirective(reactDirective: any) {
['settings', { watchDepth: 'reference' }],
['showTimeCol', { watchDepth: 'value' }],
['sort', { watchDepth: 'value' }],
['className', { watchDepth: 'value' }],
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { render } from 'react-dom';
import React, { useRef, useEffect } from 'react';
import { I18nProvider } from '@kbn/i18n/react';
import { IScope } from 'angular';
import { getServices } from '../../../kibana_services';
import { DocTableLegacyProps, injectAngularElement } from './create_doc_table_react';

type AngularEmbeddableScope = IScope & { renderProps?: DocTableEmbeddableProps };

export interface DocTableEmbeddableProps extends Partial<DocTableLegacyProps> {
refs: HTMLElement;
}

function getRenderFn(domNode: Element, props: DocTableEmbeddableProps) {
const directive = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 Since this object has only one property, and doesn't seem to be used anywhere except for accessing that single property, maybe we could just make this a const template instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other angular directives and overall readability, I'd like to keep it as is

template: `<doc-table
class="panel-content"
columns="renderProps.columns"
data-description="{{renderProps.searchDescription}}"
data-shared-item
data-test-subj="embeddedSavedSearchDocTable"
data-title="{{renderProps.sharedItemTitle}}"
filter="renderProps.onFilter"
hits="renderProps.rows"
index-pattern="renderProps.indexPattern"
is-loading="renderProps.isLoading"
on-add-column="renderProps.onAddColumn"
on-change-sort-order="renderProps.onSort"
on-move-column="renderProps.onMoveColumn"
on-remove-column="renderProps.onRemoveColumn"
render-complete
sorting="renderProps.sort"
total-hit-count="renderProps.totalHitCount"
use-new-fields-api="renderProps.useNewFieldsApi"></doc-table>`,
};

return async () => {
try {
const injector = await getServices().getEmbeddableInjector();
return await injectAngularElement(domNode, directive.template, props, injector);
} catch (e) {
render(<div>error</div>, domNode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume we expect this to never happen. Since we still catch it, I think we should render a more meaningful message, that will help us trace this in case it would ever happen. If we expect it to "NEVER" happen, I think alternatively we might just not catch the error at all, and instead let it be handled by the overall embeddable error infrastructure.

}
};
}

export function DiscoverDocTableEmbeddable(props: DocTableEmbeddableProps) {
return (
<I18nProvider>
<DocTableLegacyInner {...props} />
</I18nProvider>
);
}

function DocTableLegacyInner(renderProps: DocTableEmbeddableProps) {
const scope = useRef<AngularEmbeddableScope | undefined>();

useEffect(() => {
if (renderProps.refs && !scope.current) {
const fn = getRenderFn(renderProps.refs, renderProps);
fn().then((newScope) => {
scope.current = newScope;
});
} else if (scope?.current) {
scope.current.renderProps = { ...renderProps };
scope.current.$applyAsync();
}
}, [renderProps]);

useEffect(() => {
return () => {
if (scope.current) {
scope.current.$destroy();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (scope.current) {
scope.current.$destroy();
}
scope.current?.$destroy();

};
}, []);
return <React.Fragment />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
export { createDocTableDirective } from './doc_table';
export { getSort, getSortArray } from './lib/get_sort';
export { getSortForSearchSource } from './lib/get_sort_for_search_source';
export { getDefaultSort } from './lib/get_default_sort';
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export interface DiscoverGridProps {
* Determines which element labels the grid for ARIA
*/
ariaLabelledBy: string;
/**
* Optional class name to apply
*/
className?: string;
/**
* Determines which columns are displayed
*/
Expand Down Expand Up @@ -175,6 +179,7 @@ export const DiscoverGrid = ({
isSortEnabled = true,
isPaginationEnabled = true,
controlColumnIds = ['openDetails', 'select'],
className,
}: DiscoverGridProps) => {
const [selectedDocs, setSelectedDocs] = useState<string[]>([]);
const [isFilterActive, setIsFilterActive] = useState(false);
Expand Down Expand Up @@ -284,6 +289,7 @@ export const DiscoverGrid = ({
),
[displayedColumns, indexPattern, showTimeCol, settings, defaultColumns, isSortEnabled]
);

const schemaDetectors = useMemo(() => getSchemaDetectors(), []);
const columnsVisibility = useMemo(
() => ({
Expand Down Expand Up @@ -368,6 +374,7 @@ export const DiscoverGrid = ({
data-title={searchTitle}
data-description={searchDescription}
data-document-number={displayedRows.length}
className={className}
>
<KibanaContextProvider services={{ uiSettings: services.uiSettings }}>
<EuiDataGridMemoized
Expand Down
Loading