Add fix for branding
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { getPayload } from 'payload'
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
/**
|
||||
* White-label admin branding: the Payload admin panel's logo (login + nav) and
|
||||
@@ -7,13 +7,13 @@ import { getPayload } from 'payload'
|
||||
* automatically by `ipalKit({ whiteLabel: true })` (it injects
|
||||
* admin.components.graphics.Logo/Icon pointing here).
|
||||
*
|
||||
* Server Components — they read the SiteSettings global at render. Data access
|
||||
* uses the project's `@payload-config` alias (universal in Payload 3 Next apps),
|
||||
* loaded via a variable specifier so the PLUGIN's own typecheck doesn't try to
|
||||
* resolve it (same pattern as the optional 2FA import). Everything is wrapped in
|
||||
* try/catch: on the login screen before the DB is ready, or a build with no DB,
|
||||
* the component falls back to the site name text, then to null (Payload's own
|
||||
* default) — never throws, never blocks the panel.
|
||||
* Server Components — they read the SiteSettings global at render, using the
|
||||
* `payload` instance Payload itself passes to admin server components. NO
|
||||
* `import('@payload-config')`: a variable-specifier import isn't traced by the
|
||||
* Next bundler ("Cannot find module '@payload-config'") — same bug class as the
|
||||
* old 2FA import. Everything is wrapped in try/catch: no payload prop, DB not
|
||||
* ready, etc. → site name text, then null (Payload's own default) — never
|
||||
* throws, never blocks the panel.
|
||||
*
|
||||
* A0: no client brand in code. The logo/name come from the panel; if nothing is
|
||||
* set, nothing is forced.
|
||||
@@ -22,35 +22,54 @@ import { getPayload } from 'payload'
|
||||
type BrandingProps = {
|
||||
/** Upload field to render as the image. Default 'logo' (Logo) / 'favicon' (Icon). */
|
||||
imageField?: string
|
||||
/** Injected by Payload — current admin locale (object or code). */
|
||||
locale?: { code?: string } | string
|
||||
/** Field holding the site name. Default 'siteName'. */
|
||||
nameField?: string
|
||||
/** Injected by Payload into admin server components. */
|
||||
payload?: Payload
|
||||
/** SiteSettings global slug. Default 'site-settings'. */
|
||||
settingsSlug?: string
|
||||
}
|
||||
|
||||
type MediaLike = { url?: null | string } | null | undefined
|
||||
|
||||
/** A localized text field may arrive as a string or a { locale: value } map. */
|
||||
function pickText(value: unknown, locale: string | undefined): null | string {
|
||||
if (typeof value === 'string') {return value}
|
||||
if (value && typeof value === 'object') {
|
||||
const map = value as Record<string, unknown>
|
||||
const preferred = locale ? map[locale] : undefined
|
||||
if (typeof preferred === 'string') {return preferred}
|
||||
const first = Object.values(map).find((v): v is string => typeof v === 'string')
|
||||
return first ?? null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
async function loadBranding(
|
||||
props: BrandingProps,
|
||||
settingsSlug: string,
|
||||
nameField: string,
|
||||
imageField: string,
|
||||
): Promise<{ name: null | string; url: null | string }> {
|
||||
const { payload } = props
|
||||
if (!payload) {return { name: null, url: null }}
|
||||
try {
|
||||
// Consumer alias — resolved in the project's build, hidden from plugin tsc.
|
||||
const spec = '@payload-config'
|
||||
const mod = (await import(spec)) as { default: unknown }
|
||||
const payload = await getPayload({ config: mod.default as never })
|
||||
const locale = typeof props.locale === 'string' ? props.locale : props.locale?.code
|
||||
const settings = (await payload.findGlobal({
|
||||
slug: settingsSlug as never,
|
||||
depth: 1,
|
||||
...(locale ? { locale: locale as never } : {}),
|
||||
})) as Record<string, unknown>
|
||||
|
||||
const name = typeof settings[nameField] === 'string' ? (settings[nameField]) : null
|
||||
const name = pickText(settings[nameField], locale)
|
||||
const image = settings[imageField] as MediaLike
|
||||
const url =
|
||||
image && typeof image === 'object' && typeof image.url === 'string' ? image.url : null
|
||||
return { name, url }
|
||||
} catch {
|
||||
} catch (error) {
|
||||
console.warn('[ipal] admin branding: falling back to Payload default:', error)
|
||||
return { name: null, url: null }
|
||||
}
|
||||
}
|
||||
@@ -62,7 +81,7 @@ async function loadBranding(
|
||||
*/
|
||||
export async function AdminLogo(props: BrandingProps = {}) {
|
||||
const { imageField = 'logo', nameField = 'siteName', settingsSlug = 'site-settings' } = props
|
||||
const { name, url } = await loadBranding(settingsSlug, nameField, imageField)
|
||||
const { name, url } = await loadBranding(props, settingsSlug, nameField, imageField)
|
||||
if (url) {
|
||||
return (
|
||||
<img
|
||||
@@ -79,7 +98,7 @@ export async function AdminLogo(props: BrandingProps = {}) {
|
||||
/** Admin panel icon (small nav glyph). SiteSettings `favicon`, falls back to initials. */
|
||||
export async function AdminIcon(props: BrandingProps = {}) {
|
||||
const { imageField = 'favicon', nameField = 'siteName', settingsSlug = 'site-settings' } = props
|
||||
const { name, url } = await loadBranding(settingsSlug, nameField, imageField)
|
||||
const { name, url } = await loadBranding(props, settingsSlug, nameField, imageField)
|
||||
if (url) {
|
||||
return (
|
||||
<img
|
||||
|
||||
Reference in New Issue
Block a user