PluginHelp.tsx 1.8 KB

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