forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mount views dynamically with services (opensearch-project#4519)
Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>
- Loading branch information
Showing
14 changed files
with
206 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/plugins/data_explorer/public/components/app_container.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { AppContainer } from './app_container'; | ||
import { render } from '@testing-library/react'; | ||
import { View } from '../services/view_service/view'; | ||
import { ViewMountParameters } from '../services/view_service'; | ||
|
||
describe('DataExplorerApp', () => { | ||
const createView = () => { | ||
return new View({ | ||
id: 'test-view', | ||
title: 'Test View', | ||
defaultPath: '/test-path', | ||
appExtentions: {} as any, | ||
mount: async ({ canvasElement, panelElement }: ViewMountParameters) => { | ||
const canvasContent = document.createElement('div'); | ||
const panelContent = document.createElement('div'); | ||
canvasContent.innerHTML = 'canvas-content'; | ||
panelContent.innerHTML = 'panel-content'; | ||
canvasElement.appendChild(canvasContent); | ||
panelElement.appendChild(panelContent); | ||
return () => { | ||
canvasContent.remove(); | ||
panelContent.remove(); | ||
}; | ||
}, | ||
}); | ||
}; | ||
|
||
it('should render NoView when a non existent view is selected', () => { | ||
const { container } = render(<AppContainer />); | ||
|
||
expect(container).toContainHTML('View not found'); | ||
}); | ||
|
||
// TODO: Complete once state management is in place | ||
// it('should render the canvas and panel when selected', () => { | ||
// const view = createView(); | ||
// const { container } = render(<AppContainer view={view} />); | ||
|
||
// expect(container).toMatchSnapshot(); | ||
// }); | ||
}); |
83 changes: 83 additions & 0 deletions
83
src/plugins/data_explorer/public/components/app_container.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useLayoutEffect, useRef, useState } from 'react'; | ||
import { EuiPageTemplate } from '@elastic/eui'; | ||
|
||
import { Sidebar } from './sidebar'; | ||
import { NoView } from './no_view'; | ||
import { View } from '../services/view_service/view'; | ||
|
||
export const AppContainer = ({ view }: { view?: View }) => { | ||
const [showSpinner, setShowSpinner] = useState(false); | ||
const canvasRef = useRef<HTMLDivElement>(null); | ||
const panelRef = useRef<HTMLDivElement>(null); | ||
const unmountRef = useRef<any>(null); | ||
|
||
useLayoutEffect(() => { | ||
const unmount = () => { | ||
if (unmountRef.current) { | ||
unmountRef.current(); | ||
unmountRef.current = null; | ||
} | ||
}; | ||
|
||
if (!view) { | ||
return; | ||
} | ||
|
||
// unmount the previous view | ||
unmount(); | ||
|
||
const mount = async () => { | ||
setShowSpinner(true); | ||
try { | ||
unmountRef.current = | ||
(await view.mount({ | ||
canvasElement: canvasRef.current!, | ||
panelElement: panelRef.current!, | ||
})) || null; | ||
} catch (e) { | ||
// TODO: add error UI | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
} finally { | ||
// if (canvasRef.current && panelRef.current) { | ||
if (canvasRef.current) { | ||
setShowSpinner(false); | ||
} | ||
} | ||
}; | ||
|
||
mount(); | ||
|
||
return unmount; | ||
}, [view]); | ||
|
||
// TODO: Make this more robust. | ||
if (!view) { | ||
return <NoView />; | ||
} | ||
|
||
// Render the application DOM. | ||
// Note that `navigation.ui.TopNavMenu` is a stateful component exported on the `navigation` plugin's start contract. | ||
return ( | ||
<EuiPageTemplate | ||
pageSideBar={ | ||
<Sidebar> | ||
<div ref={panelRef} /> | ||
</Sidebar> | ||
} | ||
className="dePageTemplate" | ||
template="default" | ||
restrictWidth={false} | ||
paddingSize="none" | ||
> | ||
{/* TODO: improve loading state */} | ||
{showSpinner && <div>Loading...</div>} | ||
<div key={view.id} ref={canvasRef} /> | ||
</EuiPageTemplate> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/plugins/discover/public/application/view_components/canvas/create_canvas.tsx
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
src/plugins/discover/public/application/view_components/canvas/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { ViewMountParameters } from '../../../../../data_explorer/public'; | ||
import { OpenSearchDashboardsContextProvider } from '../../../../../opensearch_dashboards_react/public'; | ||
import { DiscoverServices } from '../../../build_services'; | ||
|
||
export const renderCanvas = ( | ||
{ canvasElement }: ViewMountParameters, | ||
services: DiscoverServices | ||
) => { | ||
ReactDOM.render( | ||
<OpenSearchDashboardsContextProvider services={services}> | ||
{/* This is dummy code, inline styles will not be added in production */} | ||
<div style={{ whiteSpace: 'pre-wrap' }}> | ||
{JSON.stringify(services.capabilities.navLinks, null, 2)} | ||
</div> | ||
</OpenSearchDashboardsContextProvider>, | ||
canvasElement | ||
); | ||
|
||
return () => ReactDOM.unmountComponentAtNode(canvasElement); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/plugins/discover/public/application/view_components/panel/create_panel.tsx
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/plugins/discover/public/application/view_components/panel/index.ts
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/plugins/discover/public/application/view_components/panel/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { ViewMountParameters } from '../../../../../data_explorer/public'; | ||
import { OpenSearchDashboardsContextProvider } from '../../../../../opensearch_dashboards_react/public'; | ||
import { DiscoverServices } from '../../../build_services'; | ||
|
||
export const renderPanel = ({ panelElement }: ViewMountParameters, services: DiscoverServices) => { | ||
ReactDOM.render( | ||
<OpenSearchDashboardsContextProvider services={services}> | ||
<div>Side panel</div> | ||
</OpenSearchDashboardsContextProvider>, | ||
panelElement | ||
); | ||
|
||
return () => ReactDOM.unmountComponentAtNode(panelElement); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters