grafana/public/app/core/components/PluginHelp/PluginHelp.tsx

85 lines
1.8 KiB
TypeScript
Raw Normal View History

import React, { PureComponent } from 'react';
// @ts-ignore
import Remarkable from 'remarkable';
import { getBackendSrv } from '@grafana/runtime';
interface Props {
2018-12-19 17:53:14 +08:00
plugin: {
name: string;
id: string;
};
type: string;
}
interface State {
isError: boolean;
isLoading: boolean;
2018-12-18 21:40:54 +08:00
help: string;
}
2018-12-19 17:53:14 +08:00
export class PluginHelp extends PureComponent<Props, State> {
2018-12-18 21:40:54 +08:00
state = {
isError: false,
isLoading: false,
help: '',
};
componentDidMount(): void {
this.loadHelp();
}
2018-12-19 17:53:14 +08:00
constructPlaceholderInfo() {
return 'No plugin help or readme markdown file was found';
}
2018-12-18 21:40:54 +08:00
loadHelp = () => {
2018-12-18 21:40:54 +08:00
const { plugin, type } = this.props;
this.setState({ isLoading: true });
getBackendSrv()
2018-12-18 21:40:54 +08:00
.get(`/api/plugins/${plugin.id}/markdown/${type}`)
.then((response: string) => {
const markdown = new Remarkable();
const helpHtml = markdown.render(response);
2018-12-18 22:30:34 +08:00
if (response === '' && type === 'help') {
2018-12-18 21:40:54 +08:00
this.setState({
isError: false,
isLoading: false,
help: this.constructPlaceholderInfo(),
});
} else {
this.setState({
isError: false,
isLoading: false,
help: helpHtml,
});
}
})
.catch(() => {
this.setState({
isError: true,
isLoading: false,
});
});
};
render() {
2018-12-18 21:40:54 +08:00
const { type } = this.props;
const { isError, isLoading, help } = this.state;
if (isLoading) {
return <h2>Loading help...</h2>;
}
if (isError) {
return <h3>'Error occurred when loading help'</h3>;
}
2018-12-18 21:40:54 +08:00
if (type === 'panel_help' && help === '') {
}
return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />;
}
}