File size: 3,452 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import merge from 'lodash/merge.js'

import { AdminJSOptions, Assets, BrandingOptions } from '../../../adminjs-options.interface.js'
import AdminJS from '../../../adminjs.js'
import { CurrentAdmin } from '../../../current-admin.interface.js'

import { ThemeInState } from '../../../frontend/store/index.js'
import { Locale, defaultLocale } from '../../../locale/index.js'
import { flat } from '../../../utils/flat/index.js'
import ViewHelpers from '../view-helpers/view-helpers.js'

const defaultBranding: AdminJSOptions['branding'] = {
  companyName: 'Company',
  withMadeWithLove: true,
}
const defaultAssets: Assets = {
  styles: [],
  scripts: [],
}

export const getAssets = async (admin: AdminJS, currentAdmin?: CurrentAdmin): Promise<Assets> => {
  const { assets } = admin.options || {}
  const computed = typeof assets === 'function' ? await assets(currentAdmin) : assets

  return merge({}, defaultAssets, computed)
}

export const getBranding = async (
  admin: AdminJS,
  currentAdmin?: CurrentAdmin,
): Promise<BrandingOptions> => {
  const { branding } = admin.options

  const h = new ViewHelpers(admin)
  const defaultLogo = h.assetPath('logo.svg')

  const computed = typeof branding === 'function' ? await branding(currentAdmin) : branding
  const merged = merge({}, defaultBranding, computed)

  // checking for undefined because logo can also be `false` or `null`
  merged.logo = merged.logo !== undefined ? merged.logo : defaultLogo

  return merged
}

export const getLocales = async (admin: AdminJS, currentAdmin?: CurrentAdmin): Promise<Locale> => {
  const { locale = {} } = admin.options || {}
  const computed = typeof locale === 'function' ? await locale(currentAdmin) : locale

  let baseLocale: Locale = merge(
    {} as Partial<Locale>,
    flat.flatten(defaultLocale) as Locale,
    flat.flatten(computed) as Locale,
  )

  if (!baseLocale.translations) {
    baseLocale.translations = {}
  }

  // Merging translations defined in resource options
  admin.resources.forEach((baseResource) => {
    const decorated = baseResource._decorated ?? baseResource.decorate()

    const { translations: resourceTranslations } = decorated.options

    if (resourceTranslations) {
      // Assure that translations object structure is consistent so we can use lodash#merge
      const resourceLocale: Omit<Locale, 'language'> = {
        translations: {},
      }

      Object.keys(resourceTranslations).forEach((language) => {
        resourceLocale.translations![language] = {
          resources: {
            [decorated.id()]: resourceTranslations[language],
          },
        }
      })

      baseLocale = merge(baseLocale, resourceLocale)
    }
  })

  return flat.unflatten(baseLocale)
}

export const getTheme = async (
  admin: AdminJS,
  currentAdmin?: CurrentAdmin,
): Promise<ThemeInState> => {
  const { availableThemes, defaultTheme } = admin.options
  let themeId = defaultTheme ?? availableThemes?.[0].id
  if (currentAdmin?.theme?.length) {
    themeId = currentAdmin?.theme
  }
  const theme = availableThemes?.find(({ id }) => id === themeId)
  return theme ? { ...theme, availableThemes } : null
}

export const getFaviconFromBranding = (branding: BrandingOptions): string => {
  if (branding.favicon) {
    const { favicon } = branding
    const type = favicon.match(/.*\.png$/) ? 'image/png' : 'image/x-icon'
    return `<link rel="shortcut icon" type="${type}" href="${favicon}" />`
  }

  return ''
}