JsonEditorSettings.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { css } from '@emotion/css';
  2. import React, { useState } from 'react';
  3. import AutoSizer from 'react-virtualized-auto-sizer';
  4. import { GrafanaTheme2 } from '@grafana/data';
  5. import { Button, CodeEditor, HorizontalGroup, useStyles2 } from '@grafana/ui';
  6. import { dashboardWatcher } from 'app/features/live/dashboard/dashboardWatcher';
  7. import { getDashboardSrv } from '../../services/DashboardSrv';
  8. import { DashboardModel } from '../../state/DashboardModel';
  9. interface Props {
  10. dashboard: DashboardModel;
  11. }
  12. export const JsonEditorSettings: React.FC<Props> = ({ dashboard }) => {
  13. const [dashboardJson, setDashboardJson] = useState<string>(JSON.stringify(dashboard.getSaveModelClone(), null, 2));
  14. const onBlur = (value: string) => {
  15. setDashboardJson(value);
  16. };
  17. const onClick = () => {
  18. getDashboardSrv()
  19. .saveJSONDashboard(dashboardJson)
  20. .then(() => {
  21. dashboardWatcher.reloadPage();
  22. });
  23. };
  24. const styles = useStyles2(getStyles);
  25. return (
  26. <div>
  27. <h3 className="dashboard-settings__header">JSON Model</h3>
  28. <div className="dashboard-settings__subheader">
  29. The JSON model below is the data structure that defines the dashboard. This includes dashboard settings, panel
  30. settings, layout, queries, and so on.
  31. </div>
  32. <div className={styles.editWrapper}>
  33. <AutoSizer>
  34. {({ width, height }) => (
  35. <CodeEditor
  36. value={dashboardJson}
  37. language="json"
  38. width={width}
  39. height={height}
  40. showMiniMap={true}
  41. showLineNumbers={true}
  42. onBlur={onBlur}
  43. />
  44. )}
  45. </AutoSizer>
  46. </div>
  47. {dashboard.meta.canSave && (
  48. <HorizontalGroup>
  49. <Button type="submit" onClick={onClick}>
  50. Save changes
  51. </Button>
  52. </HorizontalGroup>
  53. )}
  54. </div>
  55. );
  56. };
  57. const getStyles = (theme: GrafanaTheme2) => ({
  58. editWrapper: css`
  59. height: calc(100vh - 250px);
  60. margin-bottom: 10px;
  61. `,
  62. });