AppRootPage.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Libraries
  2. import React, { Component } from 'react';
  3. import { createHtmlPortalNode, InPortal, OutPortal, HtmlPortalNode } from 'react-reverse-portal';
  4. import { AppEvents, AppPlugin, AppPluginMeta, KeyValue, NavModel, PluginType } from '@grafana/data';
  5. import { getNotFoundNav, getWarningNav, getExceptionNav } from 'app/angular/services/nav_model_srv';
  6. import Page from 'app/core/components/Page/Page';
  7. import PageLoader from 'app/core/components/PageLoader/PageLoader';
  8. import { appEvents } from 'app/core/core';
  9. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  10. import { getPluginSettings } from '../pluginSettings';
  11. import { importAppPlugin } from '../plugin_loader';
  12. interface RouteParams {
  13. pluginId: string;
  14. }
  15. interface Props extends GrafanaRouteComponentProps<RouteParams> {}
  16. interface State {
  17. loading: boolean;
  18. portalNode: HtmlPortalNode;
  19. plugin?: AppPlugin | null;
  20. nav?: NavModel;
  21. }
  22. export function getAppPluginPageError(meta: AppPluginMeta) {
  23. if (!meta) {
  24. return 'Unknown Plugin';
  25. }
  26. if (meta.type !== PluginType.app) {
  27. return 'Plugin must be an app';
  28. }
  29. if (!meta.enabled) {
  30. return 'Application Not Enabled';
  31. }
  32. return null;
  33. }
  34. class AppRootPage extends Component<Props, State> {
  35. constructor(props: Props) {
  36. super(props);
  37. this.state = {
  38. loading: true,
  39. portalNode: createHtmlPortalNode(),
  40. };
  41. }
  42. shouldComponentUpdate(nextProps: Props) {
  43. return nextProps.location.pathname.startsWith('/a/');
  44. }
  45. async loadPluginSettings() {
  46. const { params } = this.props.match;
  47. try {
  48. const app = await getPluginSettings(params.pluginId).then((info) => {
  49. const error = getAppPluginPageError(info);
  50. if (error) {
  51. appEvents.emit(AppEvents.alertError, [error]);
  52. this.setState({ nav: getWarningNav(error) });
  53. return null;
  54. }
  55. return importAppPlugin(info);
  56. });
  57. this.setState({ plugin: app, loading: false, nav: undefined });
  58. } catch (err) {
  59. this.setState({
  60. plugin: null,
  61. loading: false,
  62. nav: process.env.NODE_ENV === 'development' ? getExceptionNav(err) : getNotFoundNav(),
  63. });
  64. }
  65. }
  66. componentDidMount() {
  67. this.loadPluginSettings();
  68. }
  69. componentDidUpdate(prevProps: Props) {
  70. const { params } = this.props.match;
  71. if (prevProps.match.params.pluginId !== params.pluginId) {
  72. this.setState({
  73. loading: true,
  74. });
  75. this.loadPluginSettings();
  76. }
  77. }
  78. onNavChanged = (nav: NavModel) => {
  79. this.setState({ nav });
  80. };
  81. render() {
  82. const { loading, plugin, nav, portalNode } = this.state;
  83. if (plugin && !plugin.root) {
  84. // TODO? redirect to plugin page?
  85. return <div>No Root App</div>;
  86. }
  87. return (
  88. <>
  89. <InPortal node={portalNode}>
  90. {plugin && plugin.root && (
  91. <plugin.root
  92. meta={plugin.meta}
  93. basename={this.props.match.url}
  94. onNavChanged={this.onNavChanged}
  95. query={this.props.queryParams as KeyValue}
  96. path={this.props.location.pathname}
  97. />
  98. )}
  99. </InPortal>
  100. {nav ? (
  101. <Page navModel={nav}>
  102. <Page.Contents isLoading={loading}>
  103. <OutPortal node={portalNode} />
  104. </Page.Contents>
  105. </Page>
  106. ) : (
  107. <Page>
  108. <OutPortal node={portalNode} />
  109. {loading && <PageLoader />}
  110. </Page>
  111. )}
  112. </>
  113. );
  114. }
  115. }
  116. export default AppRootPage;