Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[v9.3.x] Plugins: Fix plugin query help markdown (#60907)
Plugins: Fix plugin query help markdown (#60485) * refactor(pluginhelp): rewrite as functional component with useAsync * mimic old behaviour * feat(pluginhelp): display message if backend returned an empty string Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com> (cherry picked from commit 9aed364) Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
- Loading branch information
1 parent
0404d07
commit db83d5f
Showing
3 changed files
with
19 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,32 @@ | ||
| import React, { PureComponent } from 'react'; | ||
| import React from 'react'; | ||
| import { useAsync } from 'react-use'; | ||
|
|
||
| import { renderMarkdown } from '@grafana/data'; | ||
| import { getBackendSrv } from '@grafana/runtime'; | ||
| import { LoadingPlaceholder } from '@grafana/ui'; | ||
|
|
||
| interface Props { | ||
| plugin: { | ||
| name: string; | ||
| id: string; | ||
| }; | ||
| type: string; | ||
| pluginId: string; | ||
| } | ||
|
|
||
| interface State { | ||
| isError: boolean; | ||
| isLoading: boolean; | ||
| help: string; | ||
| } | ||
| export function PluginHelp({ pluginId }: Props) { | ||
| const { value, loading, error } = useAsync(async () => { | ||
| return getBackendSrv().get(`/api/plugins/${pluginId}/markdown/query_help`); | ||
| }, []); | ||
|
|
||
| export class PluginHelp extends PureComponent<Props, State> { | ||
| state = { | ||
| isError: false, | ||
| isLoading: false, | ||
| help: '', | ||
| }; | ||
| const renderedMarkdown = renderMarkdown(value); | ||
|
|
||
| componentDidMount(): void { | ||
| this.loadHelp(); | ||
| if (loading) { | ||
| return <LoadingPlaceholder text="Loading help..." />; | ||
| } | ||
|
|
||
| constructPlaceholderInfo() { | ||
| return 'No plugin help or readme markdown file was found'; | ||
| if (error) { | ||
| return <h3>An error occurred when loading help.</h3>; | ||
| } | ||
|
|
||
| loadHelp = () => { | ||
| const { plugin, type } = this.props; | ||
| this.setState({ isLoading: true }); | ||
|
|
||
| getBackendSrv() | ||
| .get(`/api/plugins/${plugin.id}/markdown/${type}`) | ||
| .then((response: string) => { | ||
| const helpHtml = renderMarkdown(response); | ||
|
|
||
| if (response === '' && type === 'help') { | ||
| 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() { | ||
| 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>; | ||
| } | ||
|
|
||
| if (type === 'panel_help' && help === '') { | ||
| } | ||
|
|
||
| return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: help }} />; | ||
| if (value === '') { | ||
| return <h3>No query help could be found.</h3>; | ||
| } | ||
|
|
||
| return <div className="markdown-html" dangerouslySetInnerHTML={{ __html: renderedMarkdown }} />; | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters