PluginHelp.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { PureComponent } from 'react';
  2. import Remarkable from 'remarkable';
  3. import { getBackendSrv } from '../../services/backend_srv';
  4. import { PluginMeta } from 'app/types';
  5. interface Props {
  6. plugin: PluginMeta;
  7. type: string;
  8. }
  9. interface State {
  10. isError: boolean;
  11. isLoading: boolean;
  12. help: string;
  13. }
  14. export default class PluginHelp extends PureComponent<Props, State> {
  15. state = {
  16. isError: false,
  17. isLoading: false,
  18. help: '',
  19. };
  20. componentDidMount(): void {
  21. this.loadHelp();
  22. }
  23. constructPlaceholderInfo = () => {
  24. const { plugin } = this.props;
  25. const markdown = new Remarkable();
  26. const fallBack = markdown.render(
  27. `## ${plugin.name} \n by _${plugin.info.author.name} (<${plugin.info.author.url}>)_\n\n${
  28. plugin.info.description
  29. }\n\n${
  30. plugin.info.links
  31. ? `### Links \n ${plugin.info.links.map(link => {
  32. return `${link.name}: <${link.url}>\n`;
  33. })}`
  34. : ''
  35. }`
  36. );
  37. return fallBack;
  38. };
  39. loadHelp = () => {
  40. const { plugin, type } = this.props;
  41. this.setState({ isLoading: true });
  42. getBackendSrv()
  43. .get(`/api/plugins/${plugin.id}/markdown/${type}`)
  44. .then(response => {
  45. const markdown = new Remarkable();
  46. const helpHtml = markdown.render(response);
  47. if (response === '' && type === 'help') {
  48. this.setState({
  49. isError: false,
  50. isLoading: false,
  51. help: this.constructPlaceholderInfo(),
  52. });
  53. } else {
  54. this.setState({
  55. isError: false,
  56. isLoading: false,
  57. help: helpHtml,
  58. });
  59. }
  60. })
  61. .catch(() => {
  62. this.setState({
  63. isError: true,
  64. isLoading: false,
  65. });
  66. });
  67. };
  68. render() {
  69. const { type } = this.props;
  70. const { isError, isLoading, help } = this.state;
  71. if (isLoading) {
  72. return <h2>Loading help...</h2>;
  73. }
  74. if (isError) {
  75. return <h3>'Error occurred when loading help'</h3>;
  76. }
  77. if (type === 'panel_help' && help === '') {
  78. }
  79. return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />;
  80. }
  81. }