PluginPage.tsx 11 KB

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