PanelHelp.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, { PureComponent } from 'react';
  2. import Remarkable from 'remarkable';
  3. import { getBackendSrv } from '../../services/backend_srv';
  4. import { DataSource } from 'app/types';
  5. interface Props {
  6. dataSource: DataSource;
  7. type: string;
  8. }
  9. interface State {
  10. isError: boolean;
  11. isLoading: boolean;
  12. help: any;
  13. }
  14. export default class PanelHelp extends PureComponent<Props, State> {
  15. componentDidMount(): void {
  16. this.loadHelp();
  17. }
  18. loadHelp = () => {
  19. const { dataSource, type } = this.props;
  20. this.setState({ isLoading: true });
  21. getBackendSrv()
  22. .get(`/api/plugins/${dataSource.meta.id}/markdown/${type}`)
  23. .then(response => {
  24. const markdown = new Remarkable();
  25. const helpHtml = markdown.render(response);
  26. this.setState({
  27. isError: false,
  28. isLoading: false,
  29. help: helpHtml,
  30. });
  31. })
  32. .catch(() => {
  33. this.setState({
  34. isError: true,
  35. isLoading: false,
  36. });
  37. });
  38. };
  39. render() {
  40. const { isError, isLoading, help } = this.state;
  41. if (isLoading) {
  42. return <h2>Loading help...</h2>;
  43. }
  44. if (isError) {
  45. return <h3>'Error occurred when loading help'</h3>;
  46. }
  47. return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />;
  48. }
  49. }