ShareModal.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React from 'react';
  2. import { Modal, ModalTabsHeader, TabContent } from '@grafana/ui';
  3. import { config } from 'app/core/config';
  4. import { contextSrv } from 'app/core/core';
  5. import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
  6. import { isPanelModelLibraryPanel } from 'app/features/library-panels/guard';
  7. import { ShareEmbed } from './ShareEmbed';
  8. import { ShareExport } from './ShareExport';
  9. import { ShareLibraryPanel } from './ShareLibraryPanel';
  10. import { ShareLink } from './ShareLink';
  11. import { SharePublicDashboard } from './SharePublicDashboard';
  12. import { ShareSnapshot } from './ShareSnapshot';
  13. import { ShareModalTabModel } from './types';
  14. const customDashboardTabs: ShareModalTabModel[] = [];
  15. const customPanelTabs: ShareModalTabModel[] = [];
  16. export function addDashboardShareTab(tab: ShareModalTabModel) {
  17. customDashboardTabs.push(tab);
  18. }
  19. export function addPanelShareTab(tab: ShareModalTabModel) {
  20. customPanelTabs.push(tab);
  21. }
  22. function getInitialState(props: Props): State {
  23. const tabs = getTabs(props);
  24. return {
  25. tabs,
  26. activeTab: tabs[0].value,
  27. };
  28. }
  29. function getTabs(props: Props) {
  30. const { panel } = props;
  31. const tabs: ShareModalTabModel[] = [{ label: 'Link', value: 'link', component: ShareLink }];
  32. if (contextSrv.isSignedIn) {
  33. tabs.push({ label: 'Snapshot', value: 'snapshot', component: ShareSnapshot });
  34. }
  35. if (panel) {
  36. tabs.push({ label: 'Embed', value: 'embed', component: ShareEmbed });
  37. if (!isPanelModelLibraryPanel(panel)) {
  38. tabs.push({ label: 'Library panel', value: 'library_panel', component: ShareLibraryPanel });
  39. }
  40. tabs.push(...customPanelTabs);
  41. } else {
  42. tabs.push({ label: 'Export', value: 'export', component: ShareExport });
  43. tabs.push(...customDashboardTabs);
  44. }
  45. if (Boolean(config.featureToggles['publicDashboards'])) {
  46. tabs.push({ label: 'Public Dashboard', value: 'share', component: SharePublicDashboard });
  47. }
  48. return tabs;
  49. }
  50. interface Props {
  51. dashboard: DashboardModel;
  52. panel?: PanelModel;
  53. onDismiss(): void;
  54. }
  55. interface State {
  56. tabs: ShareModalTabModel[];
  57. activeTab: string;
  58. }
  59. export class ShareModal extends React.Component<Props, State> {
  60. constructor(props: Props) {
  61. super(props);
  62. this.state = getInitialState(props);
  63. }
  64. // onDismiss = () => {
  65. // //this.setState(getInitialState(this.props));
  66. // this.props.onDismiss();
  67. // };
  68. onSelectTab = (t: any) => {
  69. this.setState({ activeTab: t.value });
  70. };
  71. getTabs() {
  72. return getTabs(this.props);
  73. }
  74. getActiveTab() {
  75. const { tabs, activeTab } = this.state;
  76. return tabs.find((t) => t.value === activeTab)!;
  77. }
  78. renderTitle() {
  79. const { panel } = this.props;
  80. const { activeTab } = this.state;
  81. const title = panel ? 'Share Panel' : 'Share';
  82. const tabs = this.getTabs();
  83. return (
  84. <ModalTabsHeader
  85. title={title}
  86. icon="share-alt"
  87. tabs={tabs}
  88. activeTab={activeTab}
  89. onChangeTab={this.onSelectTab}
  90. />
  91. );
  92. }
  93. render() {
  94. const { dashboard, panel } = this.props;
  95. const activeTabModel = this.getActiveTab();
  96. const ActiveTab = activeTabModel.component;
  97. return (
  98. <Modal isOpen={true} title={this.renderTitle()} onDismiss={this.props.onDismiss}>
  99. <TabContent>
  100. <ActiveTab dashboard={dashboard} panel={panel} onDismiss={this.props.onDismiss} />
  101. </TabContent>
  102. </Modal>
  103. );
  104. }
  105. }