PluginPage.tsx 11 KB

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