navModel.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { NavModelItem, NavModel } from '@grafana/data';
  2. import { featureEnabled } from '@grafana/runtime';
  3. import { ProBadge } from 'app/core/components/Upgrade/ProBadge';
  4. import config from 'app/core/config';
  5. import { contextSrv } from 'app/core/services/context_srv';
  6. import { highlightTrial } from 'app/features/admin/utils';
  7. import { AccessControlAction, Team, TeamPermissionLevel } from 'app/types';
  8. const loadingTeam = {
  9. avatarUrl: 'public/img/user_profile.png',
  10. id: 1,
  11. name: 'Loading',
  12. email: 'loading',
  13. memberCount: 0,
  14. permission: TeamPermissionLevel.Member,
  15. };
  16. export function buildNavModel(team: Team): NavModelItem {
  17. const navModel: NavModelItem = {
  18. img: team.avatarUrl,
  19. id: 'team-' + team.id,
  20. subTitle: 'Manage members and settings',
  21. url: '',
  22. text: team.name,
  23. breadcrumbs: [{ title: 'Teams', url: 'org/teams' }],
  24. children: [
  25. // With RBAC this tab will always be available (but not always editable)
  26. // With Legacy it will be hidden by hideTabsFromNonTeamAdmin should the user not be allowed to see it
  27. {
  28. active: false,
  29. icon: 'sliders-v-alt',
  30. id: `team-settings-${team.id}`,
  31. text: 'Settings',
  32. url: `org/teams/edit/${team.id}/settings`,
  33. },
  34. ],
  35. };
  36. // While team is loading we leave the members tab
  37. // With RBAC the Members tab is available when user has ActionTeamsPermissionsRead for this team
  38. // With Legacy it will always be present
  39. if (
  40. team === loadingTeam ||
  41. contextSrv.hasPermissionInMetadata(AccessControlAction.ActionTeamsPermissionsRead, team)
  42. ) {
  43. navModel.children!.unshift({
  44. active: false,
  45. icon: 'users-alt',
  46. id: `team-members-${team.id}`,
  47. text: 'Members',
  48. url: `org/teams/edit/${team.id}/members`,
  49. });
  50. }
  51. const teamGroupSync: NavModelItem = {
  52. active: false,
  53. icon: 'sync',
  54. id: `team-groupsync-${team.id}`,
  55. text: 'External group sync',
  56. url: `org/teams/edit/${team.id}/groupsync`,
  57. };
  58. const isLoadingTeam = team === loadingTeam;
  59. if (highlightTrial()) {
  60. teamGroupSync.tabSuffix = () =>
  61. ProBadge({ experimentId: isLoadingTeam ? '' : 'feature-highlights-team-sync-badge', eventVariant: 'trial' });
  62. }
  63. // With both Legacy and RBAC the tab is protected being featureEnabled
  64. // While team is loading we leave the teamsync tab
  65. // With RBAC the External Group Sync tab is available when user has ActionTeamsPermissionsRead for this team
  66. if (featureEnabled('teamsync')) {
  67. if (isLoadingTeam || contextSrv.hasPermissionInMetadata(AccessControlAction.ActionTeamsPermissionsRead, team)) {
  68. navModel.children!.push(teamGroupSync);
  69. }
  70. } else if (config.featureToggles.featureHighlights) {
  71. navModel.children!.push({
  72. ...teamGroupSync,
  73. tabSuffix: () => ProBadge({ experimentId: isLoadingTeam ? '' : 'feature-highlights-team-sync-badge' }),
  74. });
  75. }
  76. return navModel;
  77. }
  78. export function getTeamLoadingNav(pageName: string): NavModel {
  79. const main = buildNavModel(loadingTeam);
  80. let node: NavModelItem;
  81. // find active page
  82. for (const child of main.children!) {
  83. if (child.id!.indexOf(pageName) > 0) {
  84. child.active = true;
  85. node = child;
  86. break;
  87. }
  88. }
  89. return {
  90. main: main,
  91. node: node!,
  92. };
  93. }