ShareEmbed.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { FormEvent, PureComponent } from 'react';
  2. import { AppEvents, SelectableValue } from '@grafana/data';
  3. import { ClipboardButton, Field, Modal, RadioButtonGroup, Switch, TextArea } from '@grafana/ui';
  4. import { appEvents } from 'app/core/core';
  5. import { ShareModalTabProps } from './types';
  6. import { buildIframeHtml } from './utils';
  7. const themeOptions: Array<SelectableValue<string>> = [
  8. { label: 'Current', value: 'current' },
  9. { label: 'Dark', value: 'dark' },
  10. { label: 'Light', value: 'light' },
  11. ];
  12. interface Props extends ShareModalTabProps {}
  13. interface State {
  14. useCurrentTimeRange: boolean;
  15. selectedTheme: string;
  16. iframeHtml: string;
  17. }
  18. export class ShareEmbed extends PureComponent<Props, State> {
  19. constructor(props: Props) {
  20. super(props);
  21. this.state = {
  22. useCurrentTimeRange: true,
  23. selectedTheme: 'current',
  24. iframeHtml: '',
  25. };
  26. }
  27. componentDidMount() {
  28. this.buildIframeHtml();
  29. }
  30. buildIframeHtml = () => {
  31. const { panel, dashboard } = this.props;
  32. const { useCurrentTimeRange, selectedTheme } = this.state;
  33. const iframeHtml = buildIframeHtml(useCurrentTimeRange, dashboard.uid, selectedTheme, panel);
  34. this.setState({ iframeHtml });
  35. };
  36. onIframeHtmlChange = (event: FormEvent<HTMLTextAreaElement>) => {
  37. this.setState({ iframeHtml: event.currentTarget.value });
  38. };
  39. onUseCurrentTimeRangeChange = () => {
  40. this.setState(
  41. {
  42. useCurrentTimeRange: !this.state.useCurrentTimeRange,
  43. },
  44. this.buildIframeHtml
  45. );
  46. };
  47. onThemeChange = (value: string) => {
  48. this.setState({ selectedTheme: value }, this.buildIframeHtml);
  49. };
  50. onIframeHtmlCopy = () => {
  51. appEvents.emit(AppEvents.alertSuccess, ['Content copied to clipboard']);
  52. };
  53. getIframeHtml = () => {
  54. return this.state.iframeHtml;
  55. };
  56. render() {
  57. const { useCurrentTimeRange, selectedTheme, iframeHtml } = this.state;
  58. const isRelativeTime = this.props.dashboard ? this.props.dashboard.time.to === 'now' : false;
  59. return (
  60. <>
  61. <p className="share-modal-info-text">Generate HTML for embedding an iframe with this panel.</p>
  62. <Field
  63. label="Current time range"
  64. description={isRelativeTime ? 'Transforms the current relative time range to an absolute time range' : ''}
  65. >
  66. <Switch
  67. id="share-current-time-range"
  68. value={useCurrentTimeRange}
  69. onChange={this.onUseCurrentTimeRangeChange}
  70. />
  71. </Field>
  72. <Field label="Theme">
  73. <RadioButtonGroup options={themeOptions} value={selectedTheme} onChange={this.onThemeChange} />
  74. </Field>
  75. <Field
  76. label="Embed HTML"
  77. description="The HTML code below can be pasted and included in another web page. Unless anonymous access is enabled,
  78. the user viewing that page need to be signed into Grafana for the graph to load."
  79. >
  80. <TextArea
  81. data-testid="share-embed-html"
  82. id="share-panel-embed-embed-html-textarea"
  83. rows={5}
  84. value={iframeHtml}
  85. onChange={this.onIframeHtmlChange}
  86. />
  87. </Field>
  88. <Modal.ButtonRow>
  89. <ClipboardButton variant="primary" getText={this.getIframeHtml} onClipboardCopy={this.onIframeHtmlCopy}>
  90. Copy to clipboard
  91. </ClipboardButton>
  92. </Modal.ButtonRow>
  93. </>
  94. );
  95. }
  96. }