PluginPage.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import { connect } from 'react-redux';
  5. import find from 'lodash/find';
  6. // Types
  7. import { UrlQueryMap } from '@grafana/runtime';
  8. import { StoreState } from 'app/types';
  9. import {
  10. NavModel,
  11. NavModelItem,
  12. PluginType,
  13. GrafanaPlugin,
  14. PluginInclude,
  15. PluginDependencies,
  16. PluginMeta,
  17. PluginMetaInfo,
  18. Tooltip,
  19. AppPlugin,
  20. PluginIncludeType,
  21. } from '@grafana/ui';
  22. import Page from 'app/core/components/Page/Page';
  23. import { getPluginSettings } from './PluginSettingsCache';
  24. import { importAppPlugin, importDataSourcePlugin, importPanelPlugin } from './plugin_loader';
  25. import { getNotFoundNav } from 'app/core/nav_model_srv';
  26. import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp';
  27. import { AppConfigCtrlWrapper } from './wrappers/AppConfigWrapper';
  28. import { PluginDashboards } from './PluginDashboards';
  29. import { appEvents } from 'app/core/core';
  30. import { config } from 'app/core/config';
  31. export function getLoadingNav(): NavModel {
  32. const node = {
  33. text: 'Loading...',
  34. icon: 'icon-gf icon-gf-panel',
  35. };
  36. return {
  37. node: node,
  38. main: node,
  39. };
  40. }
  41. function loadPlugin(pluginId: string): Promise<GrafanaPlugin> {
  42. return getPluginSettings(pluginId).then(info => {
  43. if (info.type === PluginType.app) {
  44. return importAppPlugin(info);
  45. }
  46. if (info.type === PluginType.datasource) {
  47. return importDataSourcePlugin(info);
  48. }
  49. if (info.type === PluginType.panel) {
  50. return importPanelPlugin(pluginId).then(plugin => {
  51. // Panel Meta does not have the *full* settings meta
  52. return getPluginSettings(pluginId).then(meta => {
  53. plugin.meta = {
  54. ...meta, // Set any fields that do not exist
  55. ...plugin.meta,
  56. };
  57. return plugin;
  58. });
  59. });
  60. }
  61. return Promise.reject('Unknown Plugin type: ' + info.type);
  62. });
  63. }
  64. interface Props {
  65. pluginId: string;
  66. query: UrlQueryMap;
  67. path: string; // the URL path
  68. }
  69. interface State {
  70. loading: boolean;
  71. plugin?: GrafanaPlugin;
  72. nav: NavModel;
  73. defaultPage: string; // The first configured one or readme
  74. }
  75. const PAGE_ID_README = 'readme';
  76. const PAGE_ID_DASHBOARDS = 'dashboards';
  77. const PAGE_ID_CONFIG_CTRL = 'config';
  78. class PluginPage extends PureComponent<Props, State> {
  79. constructor(props: Props) {
  80. super(props);
  81. this.state = {
  82. loading: true,
  83. nav: getLoadingNav(),
  84. defaultPage: PAGE_ID_README,
  85. };
  86. }
  87. async componentDidMount() {
  88. const { pluginId, path, query } = this.props;
  89. const { appSubUrl } = config;
  90. const plugin = await loadPlugin(pluginId);
  91. if (!plugin) {
  92. this.setState({
  93. loading: false,
  94. nav: getNotFoundNav(),
  95. });
  96. return; // 404
  97. }
  98. const { meta } = plugin;
  99. let defaultPage: string;
  100. const pages: NavModelItem[] = [];
  101. if (true) {
  102. pages.push({
  103. text: 'Readme',
  104. icon: 'fa fa-fw fa-file-text-o',
  105. url: `${appSubUrl}${path}?page=${PAGE_ID_README}`,
  106. id: PAGE_ID_README,
  107. });
  108. }
  109. // Only show Config/Pages for app
  110. if (meta.type === PluginType.app) {
  111. // Legacy App Config
  112. if (plugin.angularConfigCtrl) {
  113. pages.push({
  114. text: 'Config',
  115. icon: 'gicon gicon-cog',
  116. url: `${appSubUrl}${path}?page=${PAGE_ID_CONFIG_CTRL}`,
  117. id: PAGE_ID_CONFIG_CTRL,
  118. });
  119. defaultPage = PAGE_ID_CONFIG_CTRL;
  120. }
  121. if (plugin.configPages) {
  122. for (const page of plugin.configPages) {
  123. pages.push({
  124. text: page.title,
  125. icon: page.icon,
  126. url: path + '?page=' + page.id,
  127. id: page.id,
  128. });
  129. if (!defaultPage) {
  130. defaultPage = page.id;
  131. }
  132. }
  133. }
  134. // Check for the dashboard pages
  135. if (find(meta.includes, { type: 'dashboard' })) {
  136. pages.push({
  137. text: 'Dashboards',
  138. icon: 'gicon gicon-dashboard',
  139. url: `${appSubUrl}${path}?page=${PAGE_ID_DASHBOARDS}`,
  140. id: PAGE_ID_DASHBOARDS,
  141. });
  142. }
  143. }
  144. if (!defaultPage) {
  145. defaultPage = pages[0].id; // the first tab
  146. }
  147. const node = {
  148. text: meta.name,
  149. img: meta.info.logos.large,
  150. subTitle: meta.info.author.name,
  151. breadcrumbs: [{ title: 'Plugins', url: '/plugins' }],
  152. url: `${appSubUrl}${path}`,
  153. children: this.setActivePage(query.page as string, pages, defaultPage),
  154. };
  155. this.setState({
  156. loading: false,
  157. plugin,
  158. defaultPage,
  159. nav: {
  160. node: node,
  161. main: node,
  162. },
  163. });
  164. }
  165. setActivePage(pageId: string, pages: NavModelItem[], defaultPageId: string): NavModelItem[] {
  166. let found = false;
  167. const selected = pageId || defaultPageId;
  168. const changed = pages.map(p => {
  169. const active = !found && selected === p.id;
  170. if (active) {
  171. found = true;
  172. }
  173. return { ...p, active };
  174. });
  175. if (!found) {
  176. changed[0].active = true;
  177. }
  178. return changed;
  179. }
  180. componentDidUpdate(prevProps: Props) {
  181. const prevPage = prevProps.query.page as string;
  182. const page = this.props.query.page as string;
  183. if (prevPage !== page) {
  184. const { nav, defaultPage } = this.state;
  185. const node = {
  186. ...nav.node,
  187. children: this.setActivePage(page, nav.node.children, defaultPage),
  188. };
  189. this.setState({
  190. nav: {
  191. node: node,
  192. main: node,
  193. },
  194. });
  195. }
  196. }
  197. renderBody() {
  198. const { query } = this.props;
  199. const { plugin, nav } = this.state;
  200. if (!plugin) {
  201. return <div>Plugin not found.</div>;
  202. }
  203. const active = nav.main.children.find(tab => tab.active);
  204. if (active) {
  205. // Find the current config tab
  206. if (plugin.configPages) {
  207. for (const tab of plugin.configPages) {
  208. if (tab.id === active.id) {
  209. return <tab.body plugin={plugin} query={query} />;
  210. }
  211. }
  212. }
  213. // Apps have some special behavior
  214. if (plugin.meta.type === PluginType.app) {
  215. if (active.id === PAGE_ID_DASHBOARDS) {
  216. return <PluginDashboards plugin={plugin.meta} />;
  217. }
  218. if (active.id === PAGE_ID_CONFIG_CTRL && plugin.angularConfigCtrl) {
  219. return <AppConfigCtrlWrapper app={plugin as AppPlugin} />;
  220. }
  221. }
  222. }
  223. return <PluginHelp plugin={plugin.meta} type="help" />;
  224. }
  225. showUpdateInfo = () => {
  226. appEvents.emit('show-modal', {
  227. src: 'public/app/features/plugins/partials/update_instructions.html',
  228. model: this.state.plugin.meta,
  229. });
  230. };
  231. renderVersionInfo(meta: PluginMeta) {
  232. if (!meta.info.version) {
  233. return null;
  234. }
  235. return (
  236. <section className="page-sidebar-section">
  237. <h4>Version</h4>
  238. <span>{meta.info.version}</span>
  239. {meta.hasUpdate && (
  240. <div>
  241. <Tooltip content={meta.latestVersion} theme="info" placement="top">
  242. <a href="#" onClick={this.showUpdateInfo}>
  243. Update Available!
  244. </a>
  245. </Tooltip>
  246. </div>
  247. )}
  248. </section>
  249. );
  250. }
  251. renderSidebarIncludeBody(item: PluginInclude) {
  252. if (item.type === PluginIncludeType.page) {
  253. const pluginId = this.state.plugin.meta.id;
  254. const page = item.name.toLowerCase().replace(' ', '-');
  255. return (
  256. <a href={`plugins/${pluginId}/page/${page}`}>
  257. <i className={getPluginIcon(item.type)} />
  258. {item.name}
  259. </a>
  260. );
  261. }
  262. return (
  263. <>
  264. <i className={getPluginIcon(item.type)} />
  265. {item.name}
  266. </>
  267. );
  268. }
  269. renderSidebarIncludes(includes: PluginInclude[]) {
  270. if (!includes || !includes.length) {
  271. return null;
  272. }
  273. return (
  274. <section className="page-sidebar-section">
  275. <h4>Includes</h4>
  276. <ul className="ui-list plugin-info-list">
  277. {includes.map(include => {
  278. return (
  279. <li className="plugin-info-list-item" key={include.name}>
  280. {this.renderSidebarIncludeBody(include)}
  281. </li>
  282. );
  283. })}
  284. </ul>
  285. </section>
  286. );
  287. }
  288. renderSidebarDependencies(dependencies: PluginDependencies) {
  289. if (!dependencies) {
  290. return null;
  291. }
  292. return (
  293. <section className="page-sidebar-section">
  294. <h4>Dependencies</h4>
  295. <ul className="ui-list plugin-info-list">
  296. <li className="plugin-info-list-item">
  297. <img src="public/img/grafana_icon.svg" />
  298. Grafana {dependencies.grafanaVersion}
  299. </li>
  300. {dependencies.plugins &&
  301. dependencies.plugins.map(plug => {
  302. return (
  303. <li className="plugin-info-list-item" key={plug.name}>
  304. <i className={getPluginIcon(plug.type)} />
  305. {plug.name} {plug.version}
  306. </li>
  307. );
  308. })}
  309. </ul>
  310. </section>
  311. );
  312. }
  313. renderSidebarLinks(info: PluginMetaInfo) {
  314. if (!info.links || !info.links.length) {
  315. return null;
  316. }
  317. return (
  318. <section className="page-sidebar-section">
  319. <h4>Links</h4>
  320. <ul className="ui-list">
  321. {info.links.map(link => {
  322. return (
  323. <li key={link.url}>
  324. <a href={link.url} className="external-link" target="_blank">
  325. {link.name}
  326. </a>
  327. </li>
  328. );
  329. })}
  330. </ul>
  331. </section>
  332. );
  333. }
  334. render() {
  335. const { loading, nav, plugin } = this.state;
  336. return (
  337. <Page navModel={nav}>
  338. <Page.Contents isLoading={loading}>
  339. {!loading && (
  340. <div className="sidebar-container">
  341. <div className="sidebar-content">{this.renderBody()}</div>
  342. <aside className="page-sidebar">
  343. {plugin && (
  344. <section className="page-sidebar-section">
  345. {this.renderVersionInfo(plugin.meta)}
  346. {this.renderSidebarIncludes(plugin.meta.includes)}
  347. {this.renderSidebarDependencies(plugin.meta.dependencies)}
  348. {this.renderSidebarLinks(plugin.meta.info)}
  349. </section>
  350. )}
  351. </aside>
  352. </div>
  353. )}
  354. </Page.Contents>
  355. </Page>
  356. );
  357. }
  358. }
  359. function getPluginIcon(type: string) {
  360. switch (type) {
  361. case 'datasource':
  362. return 'gicon gicon-datasources';
  363. case 'panel':
  364. return 'icon-gf icon-gf-panel';
  365. case 'app':
  366. return 'icon-gf icon-gf-apps';
  367. case 'page':
  368. return 'icon-gf icon-gf-endpoint-tiny';
  369. case 'dashboard':
  370. return 'gicon gicon-dashboard';
  371. default:
  372. return 'icon-gf icon-gf-apps';
  373. }
  374. }
  375. const mapStateToProps = (state: StoreState) => ({
  376. pluginId: state.location.routeParams.pluginId,
  377. query: state.location.query,
  378. path: state.location.path,
  379. });
  380. export default hot(module)(connect(mapStateToProps)(PluginPage));