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] Deangularize context_app.html #70211

Closed
kertal opened this issue Jun 29, 2020 · 1 comment
Closed

[Discover] Deangularize context_app.html #70211

kertal opened this issue Jun 29, 2020 · 1 comment
Assignees
Labels
Feature:Discover Discover Application

Comments

@kertal
Copy link
Member

kertal commented Jun 29, 2020

So the context_app template is entirely occupied by directives using React. Well not entirely ... a small directive of indomitable Angular holds out against React and won't go away until #67259 takes over.

So for further deangularisation the doc-table directive needs to be wrapped in a React component. When this is done, the whole template can be migrated to a single React component clearing the way to migrate the controller and to get rid out the Angular router.

How to proceed

  • Implement the doc_table component to render the angular template inside, there's an example how this is done in the doc_views_registry
    addDocView(docViewRaw: DocViewInput | DocViewInputFn) {
    const docView = typeof docViewRaw === 'function' ? docViewRaw() : docViewRaw;
    if (docView.directive) {
    // convert angular directive to render function for backwards compatibility
    docView.render = convertDirectiveToRenderFn(docView.directive, () => {
    if (!this.angularInjectorGetter) {
    throw new Error('Angular was not initialized');
    }
    return this.angularInjectorGetter();
    });

    It can render React or Angular under the hood, and it's used by the DocViewer
    export function DocViewer(renderProps: DocViewRenderProps) {
    const docViewsRegistry = getDocViewsRegistry();
    const tabs = docViewsRegistry
    .getDocViewsSorted(renderProps.hit)
    .map(({ title, render, component }: DocView, idx: number) => {
    return {
    id: `kbn_doc_viewer_tab_${idx}`,
    name: title,
    content: (
    <DocViewerTab
    id={idx}
    title={title}
    component={component}
    renderProps={renderProps}
    render={render}
    />
    ),
    };
    });

    export function DocViewRenderTab({ render, renderProps }: Props) {
    const ref = useRef<HTMLDivElement>(null);
    useEffect(() => {
    if (ref && ref.current) {
    return render(ref.current, renderProps);
    }
    }, [render, renderProps]);
    return <div ref={ref} />;
    }

    That's how a angular directive is registered:
    discover.docViews.addDocView({
    directive: {
    controller: function MyController($injector: any) {
    $injector.loadNewModules(['myDocView']);
    },
    template: `<my-hit hit="hit"></my-hit>`,
    },
    order: 1,
    title: 'Angular doc view',
    });
  • Deangularize context_app like it was done before in [Discover] Deangularize Skip to bottom button #69811

Here's how the React Angular Doc table could look like (in approximately)

export function DocTable({ render, renderProps }: Props) { 
   const render = convertDirectiveToRenderFn({ template: `<doc-table></doc_table>` }, () => getAngularInjector());
   const ref = useRef<HTMLDivElement>(null); 
   useEffect(() => { 
     if (ref && ref.current) { 
       return render(ref.current, renderProps); 
     } 
   }, [render, renderProps]); 
   return <div ref={ref} />; 
 } 

Alternative: Of course you could migrate doc_table directive to React, but this a potential 🐇 hole.
Note: Once this is done, we could migrate the next template 🦖, discover.html could be next

@majagrubic
Copy link
Contributor

Closed with 3 PRs above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Discover Discover Application
Projects
None yet
Development

No branches or pull requests

2 participants