PluginHelp.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { PureComponent } from 'react';
  2. import { renderMarkdown } from '@grafana/data';
  3. import { getBackendSrv } from '@grafana/runtime';
  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: string) => {
  34. const helpHtml = renderMarkdown(response);
  35. if (response === '' && type === 'help') {
  36. this.setState({
  37. isError: false,
  38. isLoading: false,
  39. help: this.constructPlaceholderInfo(),
  40. });
  41. } else {
  42. this.setState({
  43. isError: false,
  44. isLoading: false,
  45. help: helpHtml,
  46. });
  47. }
  48. })
  49. .catch(() => {
  50. this.setState({
  51. isError: true,
  52. isLoading: false,
  53. });
  54. });
  55. };
  56. render() {
  57. const { type } = this.props;
  58. const { isError, isLoading, help } = this.state;
  59. if (isLoading) {
  60. return <h2>Loading help...</h2>;
  61. }
  62. if (isError) {
  63. return <h3>'Error occurred when loading help'</h3>;
  64. }
  65. if (type === 'panel_help' && help === '') {
  66. }
  67. return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />;
  68. }
  69. }