Files
ipal-kit/src/modules/branding/adminGraphics.tsx
T

161 lines
5.9 KiB
TypeScript

import type { Payload } from 'payload'
/**
* White-label admin branding: the Payload admin panel's logo (login + nav) and
* icon, pulled from the project's SiteSettings so each client sees THEIR brand,
* set by the editor in the panel — not hardcoded per project. Wired
* automatically by `ipalKit({ whiteLabel: true })` (it injects
* admin.components.graphics.Logo/Icon pointing here).
*
* 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.
*/
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 {
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 = 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 (error) {
console.warn('[ipal] admin branding: falling back to Payload default:', error)
return { name: null, url: null }
}
}
/**
* Admin panel logo — this is the LARGE logo on the login / unauthorized screens
* (Payload uses `graphics.Icon` for the small nav glyph). SiteSettings `logo`,
* falls back to `siteName` text. Sized generously for the login screen.
*/
export async function AdminLogo(props: BrandingProps = {}) {
const { imageField = 'logo', nameField = 'siteName', settingsSlug = 'site-settings' } = props
const { name, url } = await loadBranding(props, settingsSlug, nameField, imageField)
if (url) {
return (
<img
alt={name ?? ''}
src={url}
style={{ height: 'auto', maxHeight: 120, maxWidth: 420, width: 'auto' }}
/>
)
}
if (name) {return <div style={{ fontSize: '2rem', fontWeight: 600 }}>{name}</div>}
return null // nothing configured → Payload's default
}
/** 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(props, settingsSlug, nameField, imageField)
if (url) {
return (
<img
alt={name ?? ''}
src={url}
style={{ height: 'auto', maxHeight: 28, maxWidth: 28, width: 'auto' }}
/>
)
}
if (name) {return <div style={{ fontWeight: 700 }}>{name.slice(0, 2).toUpperCase()}</div>}
return null
}
/**
* Discreet vendor credit: "Technology supported by Intecion.com". This is the
* PLUGIN VENDOR's credit (Intecion ships ipal-kit, the framework), not client
* data — a sensible default here is fine and A0-safe. The client's own brand
* still owns the logo/name; this is a small line stating who supports the tech.
* Text/URL overridable; turn it off with `whiteLabel: { credit: false }`.
*
* Wired by ipalKit into admin.components.afterNavLinks (nav) AND afterLogin
* (login screen footer). `variant` adjusts placement:
* - 'nav' → left-aligned line under the nav links
* - 'login' → centered line below the login form
*/
export function IntecionCredit(
props: { text?: string; url?: string; variant?: 'login' | 'nav' } = {},
) {
const {
text = 'Technology supported by Intecion.com',
url = 'https://intecion.com',
variant = 'nav',
} = props
const style =
variant === 'login'
? {
color: 'inherit' as const,
display: 'block',
fontSize: '0.75rem',
marginTop: '2rem',
opacity: 0.6,
padding: '0.5rem',
textAlign: 'center' as const,
textDecoration: 'none',
}
: {
color: 'inherit' as const,
display: 'block',
fontSize: '0.75rem',
opacity: 0.6,
padding: '0.75rem 1rem',
textDecoration: 'none',
}
return (
<a href={url} rel="noopener noreferrer" style={style} target="_blank">
{text}
</a>
)
}