PluginHelp.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. return markdown.render(
  27. `## ${plugin.name} \n by _${plugin.info.author.name} (<${plugin.info.author.url}>)_\n\n${
  28. plugin.info.description
  29. }\n\n### Links \n ${plugin.info.links.map(link => {
  30. return `${link.name}: <${link.url}>\n`;
  31. })}`
  32. );
  33. }
  34. loadHelp = () => {
  35. const { plugin, type } = this.props;
  36. this.setState({ isLoading: true });
  37. getBackendSrv()
  38. .get(`/api/plugins/${plugin.id}/markdown/${type}`)
  39. .then(response => {
  40. const markdown = new Remarkable();
  41. const helpHtml = markdown.render(response);
  42. if (response === '' && this.props.type === 'help') {
  43. this.setState({
  44. isError: false,
  45. isLoading: false,
  46. help: this.constructPlaceholderInfo(),
  47. });
  48. } else {
  49. this.setState({
  50. isError: false,
  51. isLoading: false,
  52. help: helpHtml,
  53. });
  54. }
  55. })
  56. .catch(() => {
  57. this.setState({
  58. isError: true,
  59. isLoading: false,
  60. });
  61. });
  62. };
  63. render() {
  64. const { type } = this.props;
  65. const { isError, isLoading, help } = this.state;
  66. if (isLoading) {
  67. return <h2>Loading help...</h2>;
  68. }
  69. if (isError) {
  70. return <h3>'Error occurred when loading help'</h3>;
  71. }
  72. if (type === 'panel_help' && help === '') {
  73. }
  74. return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />;
  75. }
  76. }