PluginPage.tsx 12 KB

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