usePluginDetailsTabs.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { useMemo } from 'react';
  2. import { useLocation } from 'react-router-dom';
  3. import { PluginIncludeType, PluginType } from '@grafana/data';
  4. import { usePluginConfig } from '../hooks/usePluginConfig';
  5. import { isOrgAdmin } from '../permissions';
  6. import { CatalogPlugin, PluginDetailsTab, PluginTabIds, PluginTabLabels } from '../types';
  7. type ReturnType = {
  8. error: Error | undefined;
  9. loading: boolean;
  10. tabs: PluginDetailsTab[];
  11. defaultTab: string;
  12. };
  13. export const usePluginDetailsTabs = (plugin?: CatalogPlugin, defaultTabs: PluginDetailsTab[] = []): ReturnType => {
  14. const { loading, error, value: pluginConfig } = usePluginConfig(plugin);
  15. const isPublished = Boolean(plugin?.isPublished);
  16. const { pathname } = useLocation();
  17. const [tabs, defaultTab] = useMemo(() => {
  18. const canConfigurePlugins = isOrgAdmin();
  19. const tabs: PluginDetailsTab[] = [...defaultTabs];
  20. let defaultTab;
  21. if (isPublished) {
  22. tabs.push({
  23. label: PluginTabLabels.VERSIONS,
  24. icon: 'history',
  25. id: PluginTabIds.VERSIONS,
  26. href: `${pathname}?page=${PluginTabIds.VERSIONS}`,
  27. });
  28. }
  29. // Not extending the tabs with the config pages if the plugin is not installed
  30. if (!pluginConfig) {
  31. defaultTab = PluginTabIds.OVERVIEW;
  32. return [tabs, defaultTab];
  33. }
  34. if (canConfigurePlugins) {
  35. if (pluginConfig.meta.type === PluginType.app) {
  36. if (pluginConfig.angularConfigCtrl) {
  37. tabs.push({
  38. label: 'Config',
  39. icon: 'cog',
  40. id: PluginTabIds.CONFIG,
  41. href: `${pathname}?page=${PluginTabIds.CONFIG}`,
  42. });
  43. defaultTab = PluginTabIds.CONFIG;
  44. }
  45. if (pluginConfig.configPages) {
  46. for (const page of pluginConfig.configPages) {
  47. tabs.push({
  48. label: page.title,
  49. icon: page.icon,
  50. id: page.id,
  51. href: `${pathname}?page=${page.id}`,
  52. });
  53. if (!defaultTab) {
  54. defaultTab = page.id;
  55. }
  56. }
  57. }
  58. if (pluginConfig.meta.includes?.find((include) => include.type === PluginIncludeType.dashboard)) {
  59. tabs.push({
  60. label: 'Dashboards',
  61. icon: 'apps',
  62. id: PluginTabIds.DASHBOARDS,
  63. href: `${pathname}?page=${PluginTabIds.DASHBOARDS}`,
  64. });
  65. }
  66. }
  67. }
  68. if (!defaultTab) {
  69. defaultTab = PluginTabIds.OVERVIEW;
  70. }
  71. return [tabs, defaultTab];
  72. }, [pluginConfig, defaultTabs, pathname, isPublished]);
  73. return {
  74. error,
  75. loading,
  76. tabs,
  77. defaultTab,
  78. };
  79. };