Files
ipal-kit/src/modules/branding/adminGraphics.tsx
T
Radosław SmolińskiandClaude Fable 5.2 b6c91a3a11 Sign-in look: the client's logo or the site's name on a dark panel beside the form (whiteLabel.signIn)
On by default with whiteLabel. The login screen and the 2FA screens get the look through AdminLogo and
SafeTotpView, which every site already has in its import map: an upgrade needs no edit. allScreens adds one
provider (SignInLook) for forgot / reset password, first user and logout; a production start leaves it out
when the import map on disk lacks it. Logo and name come from Site settings: no new field, no migration.
Language links through GET /api/ipal/admin-language (Payload's own cookie, back to an admin path only).
Options: accent, logoMadeFor, languages, svgLogo; whiteLabel.brandAccess 'admin'. The 2FA code boxes get
names for screen readers. Reference site and starter: allScreens on. Docs, agent guide, skills, tests.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-26 20:35:33 +01:00

203 lines
8.5 KiB
TypeScript

import type { Payload } from 'payload'
import { iaplT } from '../admin-i18n/t.js'
import { settingsTexts } from '../admin-i18n/texts/settings.js'
import { secondFactorPassed } from '../security/totp/secondFactor.js'
import { publicImageDataUri, siteOrigin } from './publicImage.js'
import { type SignInLookProps, signInProviderActive } from './signIn/brand.js'
import { SignInPanel } from './signIn/SignInPanel.js'
/**
* 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 `iapl({ 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 = {
/** Injected by Payload into admin server components. */
i18n?: { language?: string; t: (key: never, vars?: Record<string, unknown>) => string }
/** Upload field to render as the image. Default 'logo' (Logo) / 'favicon' (Icon). */
imageField?: string
/** `whiteLabel.inlineImages: false` — never fetch the image on the server for the 2FA screens. */
inlineImages?: false
/** 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
/**
* `whiteLabel.signIn`, set by the plugin on `graphics.Logo`: the logo's place on the login screen then holds
* the sign-in panel (docs/white-label.md). Not set: the plain logo, as before.
*/
signIn?: SignInLookProps
/** Injected by Payload into admin server components — the account looking at the panel. */
user?: null | Record<string, unknown>
}
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
// A session that still owes its 2FA code (or has not set 2FA up) is refused `media` like every
// other collection, so the browser would show a broken image on the verify / setup screens.
// Give it the picture itself, fetched as an anonymous visitor would get it; when that fails
// (media is not public), the site name is shown instead.
if (url && props.inlineImages !== false && props.user && !secondFactorPassed({ payload, user: props.user })) {
return { name, url: await publicImageDataUri(url, siteOrigin(payload.config.serverURL)) }
}
return { name, url }
} catch (error) {
console.warn('[iapl] 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.
*
* With `signIn` (the sign-in look, on by default with `whiteLabel`) it prints the sign-in panel instead: this
* component is in the import map of every white-label site, so the look needs no new entry. When the provider
* `SignInLook` already prints the panel (`signIn.allScreens`), it prints nothing.
*/
export async function AdminLogo(props: BrandingProps = {}) {
const { imageField = 'logo', nameField = 'siteName', settingsSlug = 'site-settings' } = props
if (props.signIn) {
if (signInProviderActive(props.payload)) {return null}
return <SignInPanel i18n={props.i18n} locale={props.locale} look={props.signIn} payload={props.payload} user={props.user} />
}
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 support: Intecion.com". This is the
* PLUGIN VENDOR's credit (Intecion ships IAPL, 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 }`.
* The default text follows the admin language (`ipal:brandingCredit`, which a
* project can also override in `i18n.translations`).
*
* Wired by iapl 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: {
/** Agency name in the default text (`agency` option). Default "Intecion.com". */
agency?: string
i18n?: { t: (key: never, vars?: Record<string, unknown>) => string }
text?: string
url?: string
variant?: 'login' | 'nav'
} = {},
) {
const { agency = 'Intecion.com', i18n, url = 'https://intecion.com', variant = 'nav' } = props
// A project's own text wins; otherwise the plugin's text in the admin language.
const text =
props.text ??
(i18n ? iaplT(i18n.t, 'brandingCredit', { agency }) : settingsTexts.en.brandingCredit.replace('{{agency}}', agency))
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 className={`iapl-credit iapl-credit--${variant}`} href={url} rel="noopener noreferrer" style={style} target="_blank">
{text}
</a>
)
}