global.static.actions.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { Action, Priority } from 'kbar';
  2. import { flatMapDeep } from 'lodash';
  3. import { NavModelItem } from '@grafana/data';
  4. import { locationService } from '@grafana/runtime';
  5. import { alertingCommandPaletteStaticActions } from './alerting.static.actions';
  6. export interface NavBarActions {
  7. url: string;
  8. actions: Action[];
  9. }
  10. export default (navBarTree: NavModelItem[]) => {
  11. const globalActions: Action[] = [
  12. {
  13. id: 'go/search',
  14. name: 'Go to dashboard search',
  15. keywords: 'navigate',
  16. perform: () => locationService.push('?search=open'),
  17. section: 'Navigation',
  18. shortcut: ['s', 'o'],
  19. },
  20. {
  21. id: 'go/dashboard',
  22. name: 'Go to dashboard...',
  23. keywords: 'navigate',
  24. section: 'Navigation',
  25. priority: Priority.NORMAL,
  26. },
  27. {
  28. id: 'preferences/theme',
  29. name: 'Change theme...',
  30. keywords: 'interface color dark light',
  31. section: 'Preferences',
  32. },
  33. {
  34. id: 'preferences/dark-theme',
  35. name: 'Dark',
  36. keywords: 'dark theme',
  37. section: '',
  38. perform: () => {
  39. locationService.push({ search: '?theme=dark' });
  40. location.reload();
  41. },
  42. parent: 'preferences/theme',
  43. },
  44. {
  45. id: 'preferences/light-theme',
  46. name: 'Light',
  47. keywords: 'light theme',
  48. section: '',
  49. perform: () => {
  50. locationService.push({ search: '?theme=light' });
  51. location.reload();
  52. },
  53. parent: 'preferences/theme',
  54. },
  55. ];
  56. // this maps actions to navbar by URL items for showing/hiding
  57. // actions is an array for multiple child actions that would be under one navbar item
  58. const navBarActionMap: NavBarActions[] = [
  59. {
  60. url: '/dashboard/new',
  61. actions: [
  62. {
  63. id: 'management/create-folder',
  64. name: 'Create folder',
  65. keywords: 'new add',
  66. perform: () => locationService.push('/dashboards/folder/new'),
  67. section: 'Management',
  68. },
  69. {
  70. id: 'management/create-dashboard',
  71. name: 'Create dashboard',
  72. keywords: 'new add',
  73. perform: () => locationService.push('/dashboard/new'),
  74. section: 'Management',
  75. },
  76. ],
  77. },
  78. {
  79. url: '/',
  80. actions: [
  81. {
  82. id: 'go/home',
  83. name: 'Go to home',
  84. keywords: 'navigate',
  85. perform: () => locationService.push('/'),
  86. section: 'Navigation',
  87. shortcut: ['g', 'h'],
  88. priority: Priority.HIGH,
  89. },
  90. ],
  91. },
  92. {
  93. url: '/explore',
  94. actions: [
  95. {
  96. id: 'go/explore',
  97. name: 'Go to explore',
  98. keywords: 'navigate',
  99. perform: () => locationService.push('/explore'),
  100. section: 'Navigation',
  101. priority: Priority.NORMAL,
  102. },
  103. ],
  104. },
  105. ...alertingCommandPaletteStaticActions,
  106. {
  107. url: '/profile',
  108. actions: [
  109. {
  110. id: 'go/profile',
  111. name: 'Go to profile',
  112. keywords: 'navigate preferences',
  113. perform: () => locationService.push('/profile'),
  114. section: 'Navigation',
  115. shortcut: ['g', 'p'],
  116. priority: Priority.LOW,
  117. },
  118. ],
  119. },
  120. {
  121. url: '/datasources',
  122. actions: [
  123. {
  124. id: 'go/configuration',
  125. name: 'Go to data sources configuration',
  126. keywords: 'navigate settings ds',
  127. perform: () => locationService.push('/datasources'),
  128. section: 'Navigation',
  129. },
  130. ],
  131. },
  132. ];
  133. const navBarActions: Action[] = [];
  134. const navBarFlatUrls = flatMapDeep(navBarTree, (nbt) => nbt.children)
  135. .map((nbf) => nbf?.url)
  136. .filter((url) => url !== undefined);
  137. navBarActionMap.forEach((navBarAction) => {
  138. const navBarItem = navBarFlatUrls.find((url) => navBarAction.url === url);
  139. if (navBarItem) {
  140. navBarActions.push(...navBarAction.actions);
  141. }
  142. });
  143. return [...globalActions, ...navBarActions];
  144. };