PluginHelp.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, { PureComponent } from 'react';
  2. // @ts-ignore
  3. import Remarkable from 'remarkable';
  4. import { getBackendSrv } from '@grafana/runtime';
  5. interface Props {
  6. plugin: {
  7. name: string;
  8. id: string;
  9. };
  10. type: string;
  11. }
  12. interface State {
  13. isError: boolean;
  14. isLoading: boolean;
  15. help: string;
  16. }
  17. export class PluginHelp extends PureComponent<Props, State> {
  18. state = {
  19. isError: false,
  20. isLoading: false,
  21. help: '',
  22. };
  23. componentDidMount(): void {
  24. this.loadHelp();
  25. }
  26. constructPlaceholderInfo() {
  27. return 'No plugin help or readme markdown file was found';
  28. }
  29. loadHelp = () => {
  30. const { plugin, type } = this.props;
  31. this.setState({ isLoading: true });
  32. getBackendSrv()
  33. .get(`/api/plugins/${plugin.id}/markdown/${type}`)
  34. .then((response: string) => {
  35. const markdown = new Remarkable();
  36. const helpHtml = markdown.render(response);
  37. if (response === '' && type === 'help') {
  38. this.setState({
  39. isError: false,
  40. isLoading: false,
  41. help: this.constructPlaceholderInfo(),
  42. });
  43. } else {
  44. this.setState({
  45. isError: false,
  46. isLoading: false,
  47. help: helpHtml,
  48. });
  49. }
  50. })
  51. .catch(() => {
  52. this.setState({
  53. isError: true,
  54. isLoading: false,
  55. });
  56. });
  57. };
  58. render() {
  59. const { type } = this.props;
  60. const { isError, isLoading, help } = this.state;
  61. if (isLoading) {
  62. return <h2>Loading help...</h2>;
  63. }
  64. if (isError) {
  65. return <h3>'Error occurred when loading help'</h3>;
  66. }
  67. if (type === 'panel_help' && help === '') {
  68. }
  69. return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />;
  70. }
  71. }