index.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useSessionStore } from '#shared/stores/session.ts'
  3. import type { TicketSidebarScreenType } from '#desktop/pages/ticket/types/sidebar.ts'
  4. import type { TicketSidebarPlugin } from './types.ts'
  5. const pluginModules = import.meta.glob<TicketSidebarPlugin>(
  6. ['./**/*.ts', '!./**/index.ts', '!./types.ts', '!./__tests__/**/*.ts'],
  7. {
  8. eager: true,
  9. import: 'default',
  10. },
  11. )
  12. export const pluginFiles = Object.entries(pluginModules)
  13. .map<[string, TicketSidebarPlugin]>(([file, plugin]) => {
  14. const name = file.replace(/^.*\/([^/]+)\.ts$/, '$1')
  15. return [name, plugin]
  16. })
  17. .sort(([, p1], [, p2]) => p1.order - p2.order)
  18. export const useTicketSidebarPlugins = (screen: TicketSidebarScreenType) => {
  19. const { hasPermission } = useSessionStore()
  20. return pluginFiles
  21. .filter(
  22. ([, plugin]) =>
  23. hasPermission(plugin.permissions) && plugin.screens.includes(screen),
  24. )
  25. .reduce<Record<string, TicketSidebarPlugin>>((acc, [name, plugin]) => {
  26. acc[name] = plugin
  27. return acc
  28. }, {})
  29. }