useIcons.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useRawHTMLIcon } from './useRawHTMLIcon.ts'
  3. const renameIcons = (icons: [string, { default: string }][]) => {
  4. const iconsMap: [string, string][] = []
  5. for (const [icon, svg] of icons) {
  6. const name = icon.match(/assets\/([\w-/]+)\.svg$/)
  7. if (!name) throw new Error(`Icon name not found for ${icon}`)
  8. const iconName = name[1].replace(/\//, '-')
  9. iconsMap.push([iconName, svg.default])
  10. }
  11. return iconsMap
  12. }
  13. let iconsSymbols: [string, string][] = []
  14. let iconsContent: Record<string, string> = {}
  15. let iconsAliasesMap: Record<string, string> = {}
  16. export const provideIcons = (
  17. globImports: [string, { default: string }][],
  18. aliases: Record<string, string>,
  19. ) => {
  20. iconsSymbols = renameIcons(globImports)
  21. iconsContent = {}
  22. iconsAliasesMap = aliases
  23. for (const [name] of iconsSymbols) {
  24. const htmlIcon = useRawHTMLIcon({ name, size: 'base' })
  25. iconsContent[name] = htmlIcon
  26. }
  27. return {
  28. icons: iconsContent,
  29. symbols: iconsSymbols,
  30. }
  31. }
  32. export const useIcons = () => {
  33. return {
  34. icons: iconsContent,
  35. symbols: iconsSymbols,
  36. aliases: iconsAliasesMap,
  37. }
  38. }