PluginPage.tsx 11 KB

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