SharePDF.tsx 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React, { useState } from 'react';
  2. import { urlUtil } from '@grafana/data';
  3. import { config, featureEnabled } from '@grafana/runtime';
  4. import { ModalTabContent, Button, LinkButton, RadioButtonGroup, Field, Switch, Modal } from '@grafana/ui';
  5. import { ShareModalTabProps } from 'app/features/dashboard/components/ShareModal';
  6. import { getVariablesByKey } from 'app/features/variables/state/selectors';
  7. import { ReportOrientation, ReportLayout, reportOrientations, reportLayouts } from '../types';
  8. import { getRootPath } from '../utils/url';
  9. import { NoRendererInfoBox } from './RenderingWarnings';
  10. import { UnavailableFeatureInfoBox } from './UnavailableFeatureInfoBox';
  11. import { collectVariables, variablesToCsv } from './utils/variables';
  12. export const SharePDF = ({ dashboard, onDismiss }: ShareModalTabProps) => {
  13. const [orientation, setOrientation] = useState<ReportOrientation>('landscape');
  14. const [layout, setLayout] = useState<ReportLayout>('grid');
  15. const [useSelectedVariables, setUseSelectedVariables] = useState(false);
  16. const buildPdfLink = () => {
  17. let pdfUrl = `${getRootPath()}/api/reports/render/pdf/${dashboard.id}`;
  18. const params: Record<string, string> = { orientation, layout };
  19. if (useSelectedVariables) {
  20. const variables = collectVariables();
  21. params.variables = JSON.stringify(variables);
  22. } else {
  23. const variables = getVariablesByKey(dashboard.uid);
  24. if (variables.length) {
  25. params.variables = JSON.stringify(variablesToCsv(variables));
  26. }
  27. }
  28. pdfUrl = urlUtil.appendQueryToUrl(pdfUrl, urlUtil.toUrlParams(params));
  29. return pdfUrl;
  30. };
  31. const onOrientationChange = (orientation: ReportOrientation) => {
  32. setOrientation(orientation);
  33. };
  34. const onLayoutChange = (layout: ReportLayout) => {
  35. setLayout(layout);
  36. };
  37. if (!config.rendererAvailable) {
  38. return <NoRendererInfoBox />;
  39. }
  40. if (!featureEnabled('reports.pdf')) {
  41. return (
  42. <UnavailableFeatureInfoBox
  43. message="Rendering a dashboard as a PDF document is not available with your current license.
  44. To enable this feature, update your license."
  45. />
  46. );
  47. }
  48. if (dashboard.isSnapshot()) {
  49. return <p className="share-modal-info-text">Sharing a PDF from a snapshot is not supported.</p>;
  50. }
  51. return (
  52. <ModalTabContent icon="file-alt">
  53. <p className="share-modal-info-text">Save the dashboard as a PDF document.</p>
  54. <Field label="Orientation">
  55. <RadioButtonGroup options={reportOrientations} value={orientation} onChange={onOrientationChange} />
  56. </Field>
  57. <Field label="Layout">
  58. <RadioButtonGroup options={reportLayouts} value={layout} onChange={onLayoutChange} />
  59. </Field>
  60. {!!Object.keys(collectVariables()).length && (
  61. <Field
  62. label={'Use current variable values'}
  63. description={
  64. 'If you modify the default variable values, then your modifications are used when generating PDF documents, rather than those saved as dashboard defaults.'
  65. }
  66. >
  67. <Switch onChange={() => setUseSelectedVariables(!useSelectedVariables)} value={useSelectedVariables} />
  68. </Field>
  69. )}
  70. <Modal.ButtonRow>
  71. <Button variant="secondary" onClick={onDismiss}>
  72. Cancel
  73. </Button>
  74. <LinkButton variant="primary" href={buildPdfLink()} target="_blank" rel="noreferrer noopener">
  75. Save as PDF
  76. </LinkButton>
  77. </Modal.ButtonRow>
  78. </ModalTabContent>
  79. );
  80. };