searchIndex.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { reactive, computed } from 'vue'
  2. import {
  3. CellsIcon,
  4. DashboardSquare01Icon,
  5. LeftToRightListDashIcon,
  6. PlayIcon,
  7. Wrench01Icon
  8. } from '@hugeicons/core-free-icons'
  9. export const MAX_SEARCH_HINT_ACTIONS = 100
  10. export const MAX_SEARCH_HINT_DASHBOARDS = 100
  11. export const MAX_SEARCH_HINT_ENTITIES_PER_TYPE = 50
  12. const SOURCE_ENTITIES = 'entities'
  13. const SOURCE_ACTIONS = 'actions'
  14. const SOURCE_DASHBOARDS = 'dashboards'
  15. const SOURCE_NAVIGATION = 'navigation'
  16. // QuickSearch keeps insertion order; prefer entities over actions for equal matches.
  17. const CATEGORY_PRIORITY = {
  18. Navigation: 0,
  19. Dashboards: 1,
  20. Entities: 2,
  21. Actions: 3
  22. }
  23. const state = reactive({
  24. itemsById: {},
  25. sourceToIds: {}
  26. })
  27. export const searchIndexItems = computed(() => {
  28. return Object.values(state.itemsById).sort((a, b) => {
  29. return categoryPriority(a.category) - categoryPriority(b.category)
  30. })
  31. })
  32. function categoryPriority (category) {
  33. return CATEGORY_PRIORITY[category] ?? 50
  34. }
  35. export function clearSearchIndex () {
  36. state.itemsById = {}
  37. state.sourceToIds = {}
  38. }
  39. function replaceSource (source, items) {
  40. const previousIds = state.sourceToIds[source] || []
  41. for (const itemId of previousIds) {
  42. delete state.itemsById[itemId]
  43. }
  44. const nextIds = []
  45. for (const item of items) {
  46. if (!item?.id) {
  47. continue
  48. }
  49. state.itemsById[item.id] = item
  50. nextIds.push(item.id)
  51. }
  52. state.sourceToIds[source] = nextIds
  53. }
  54. export function dashboardRoutePath (title, entityType, entityKey) {
  55. if (!title) {
  56. return '/'
  57. }
  58. if (title === 'Actions' && !entityType && !entityKey) {
  59. return '/'
  60. }
  61. let path = `/dashboards/${encodeURIComponent(title)}`
  62. if (entityType && entityKey) {
  63. path += `/${encodeURIComponent(entityType)}/${encodeURIComponent(entityKey)}`
  64. }
  65. return path
  66. }
  67. /**
  68. * Indexes search hints from Init (entities and actions).
  69. * Dashboards are indexed separately from rootDashboardEntries.
  70. */
  71. export function indexSearchHints (searchHints) {
  72. replaceSource(SOURCE_ENTITIES, entityItemsFromHints(searchHints?.entities))
  73. replaceSource(SOURCE_ACTIONS, actionItemsFromHints(searchHints?.actions))
  74. }
  75. /**
  76. * Indexes dashboards from Init.rootDashboardEntries (already ACL-filtered).
  77. */
  78. export function indexRootDashboardEntries (entries) {
  79. replaceSource(SOURCE_DASHBOARDS, dashboardItemsFromRootEntries(entries))
  80. }
  81. function entityItemsFromHints (hints) {
  82. if (!Array.isArray(hints)) {
  83. return []
  84. }
  85. const countsByType = {}
  86. const items = []
  87. for (const hint of hints) {
  88. if (!hint?.uniqueKey || !hint?.type) {
  89. continue
  90. }
  91. const count = countsByType[hint.type] || 0
  92. if (count >= MAX_SEARCH_HINT_ENTITIES_PER_TYPE) {
  93. continue
  94. }
  95. countsByType[hint.type] = count + 1
  96. items.push({
  97. id: `entity:${hint.type}:${hint.uniqueKey}`,
  98. title: hint.title || hint.uniqueKey,
  99. description: hint.type,
  100. category: 'Entities',
  101. type: 'route',
  102. path: `/entity-details/${encodeURIComponent(hint.type)}/${encodeURIComponent(hint.uniqueKey)}`,
  103. icon: CellsIcon
  104. })
  105. }
  106. return items
  107. }
  108. function actionItemsFromHints (hints) {
  109. if (!Array.isArray(hints)) {
  110. return []
  111. }
  112. const items = []
  113. for (const hint of hints.slice(0, MAX_SEARCH_HINT_ACTIONS)) {
  114. if (!hint?.bindingId) {
  115. continue
  116. }
  117. items.push({
  118. id: `action:${hint.bindingId}`,
  119. title: hint.title || hint.bindingId,
  120. category: 'Actions',
  121. type: 'route',
  122. path: `/action/${encodeURIComponent(hint.bindingId)}`,
  123. icon: PlayIcon
  124. })
  125. }
  126. return items
  127. }
  128. function dashboardItemsFromRootEntries (entries) {
  129. if (!Array.isArray(entries)) {
  130. return []
  131. }
  132. const items = []
  133. for (const entry of entries.slice(0, MAX_SEARCH_HINT_DASHBOARDS)) {
  134. if (!entry?.title) {
  135. continue
  136. }
  137. const section = (entry.category || '').trim()
  138. items.push({
  139. id: `dashboard:${entry.title}`,
  140. title: entry.title,
  141. description: section,
  142. category: 'Dashboards',
  143. type: 'route',
  144. path: dashboardRoutePath(entry.title),
  145. icon: DashboardSquare01Icon
  146. })
  147. }
  148. return items
  149. }
  150. /**
  151. * Indexes system navigation destinations that the user can already reach from
  152. * the sidebar, respecting Init visibility flags.
  153. */
  154. export function indexSystemNavigation ({
  155. showLogs = false,
  156. showDiagnostics = false,
  157. entitiesTitle = 'Entities',
  158. logsTitle = 'Logs',
  159. diagnosticsTitle = 'Diagnostics'
  160. } = {}) {
  161. const items = [{
  162. id: 'nav:entities',
  163. title: entitiesTitle,
  164. category: 'Navigation',
  165. type: 'route',
  166. path: '/entities',
  167. icon: CellsIcon
  168. }]
  169. if (showLogs) {
  170. items.push({
  171. id: 'nav:logs',
  172. title: logsTitle,
  173. category: 'Navigation',
  174. type: 'route',
  175. path: '/logs',
  176. icon: LeftToRightListDashIcon
  177. })
  178. }
  179. if (showDiagnostics) {
  180. items.push({
  181. id: 'nav:diagnostics',
  182. title: diagnosticsTitle,
  183. category: 'Navigation',
  184. type: 'route',
  185. path: '/diagnostics',
  186. icon: Wrench01Icon
  187. })
  188. }
  189. replaceSource(SOURCE_NAVIGATION, items)
  190. }