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

83 lines
1.7 KiB
TypeScript
Raw Normal View History

import React, { PureComponent } from 'react';
import { renderMarkdown } from '@grafana/data';
import { getBackendSrv } from '@grafana/runtime';
interface Props {
2018-12-19 03:53:14 -06:00
plugin: {
name: string;
id: string;
};
type: string;
}
interface State {
isError: boolean;
isLoading: boolean;
2018-12-18 07:40:54 -06:00
help: string;
}
2018-12-19 03:53:14 -06:00
export class PluginHelp extends PureComponent<Props, State> {
2018-12-18 07:40:54 -06:00
state = {
isError: false,
isLoading: false,
help: '',
};
componentDidMount(): void {
this.loadHelp();
}
2018-12-19 03:53:14 -06:00
constructPlaceholderInfo() {
return 'No plugin help or readme markdown file was found';
}
2018-12-18 07:40:54 -06:00
loadHelp = () => {
2018-12-18 07:40:54 -06:00
const { plugin, type } = this.props;
this.setState({ isLoading: true });
getBackendSrv()
2018-12-18 07:40:54 -06:00
.get(`/api/plugins/${plugin.id}/markdown/${type}`)
.then((response: string) => {
const helpHtml = renderMarkdown(response);
2018-12-18 08:30:34 -06:00
if (response === '' && type === 'help') {
2018-12-18 07:40:54 -06: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 07:40:54 -06: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 07:40:54 -06:00
if (type === 'panel_help' && help === '') {
}
return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />;
}
}