Added white-label support

This commit is contained in:
2026-10-12 12:17:45 +02:00
parent 10d1806130
commit 1f55c2406e
23 changed files with 610 additions and 38 deletions
+8
View File
@@ -0,0 +1,8 @@
/**
* Entry point: @intecion/ipal-kit/admin
*
* Admin-panel UI components (white-label branding). Separate export so Payload's
* importMap can resolve them by a stable path when ipalKit injects
* admin.components.graphics.Logo/Icon.
*/
export { AdminIcon, AdminLogo, IntecionCredit } from '../modules/branding/index.js'
+120
View File
@@ -0,0 +1,120 @@
import { getPayload } 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. 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.
*
* A0: no client brand in code. The logo/name come from the panel; if nothing is
* set, nothing is forced.
*/
type BrandingProps = {
/** SiteSettings global slug. Default 'site-settings'. */
settingsSlug?: string
/** Field holding the site name. Default 'siteName'. */
nameField?: string
/** Upload field to render as the image. Default 'logo' (Logo) / 'favicon' (Icon). */
imageField?: string
}
type MediaLike = { url?: string | null } | null | undefined
async function loadBranding(
settingsSlug: string,
nameField: string,
imageField: string,
): Promise<{ name: string | null; url: string | 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 settings = (await payload.findGlobal({
slug: settingsSlug as never,
depth: 1,
})) as Record<string, unknown>
const name = typeof settings[nameField] === 'string' ? (settings[nameField] as string) : null
const image = settings[imageField] as MediaLike
const url =
image && typeof image === 'object' && typeof image.url === 'string' ? image.url : null
return { name, url }
} catch {
return { name: null, url: null }
}
}
/** Admin panel logo (login screen + nav). SiteSettings `logo`, falls back to `siteName`. */
export async function AdminLogo(props: BrandingProps = {}) {
const { settingsSlug = 'site-settings', nameField = 'siteName', imageField = 'logo' } = props
const { name, url } = await loadBranding(settingsSlug, nameField, imageField)
if (url) {
return (
<img
src={url}
alt={name ?? ''}
style={{ maxHeight: 60, maxWidth: 240, width: 'auto', height: 'auto' }}
/>
)
}
if (name) return <div style={{ fontSize: '1.5rem', 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 { settingsSlug = 'site-settings', nameField = 'siteName', imageField = 'favicon' } = props
const { name, url } = await loadBranding(settingsSlug, nameField, imageField)
if (url) {
return (
<img
src={url}
alt={name ?? ''}
style={{ maxHeight: 28, maxWidth: 28, width: 'auto', height: 'auto' }}
/>
)
}
if (name) return <div style={{ fontWeight: 700 }}>{name.slice(0, 2).toUpperCase()}</div>
return null
}
/**
* Discreet vendor credit shown at the bottom of the admin nav: "Technology by
* Intecion.com". This is the PLUGIN VENDOR's credit (Intecion ships ipal-kit),
* not client data — so a sensible default here is fine and A0-safe. The client's
* own brand still owns the logo/name above (AdminLogo); this is a small line
* stating who provides the technology. Text/URL overridable; turn it off with
* `whiteLabel: { credit: false }`.
*
* Wired by ipalKit into admin.components.afterNavLinks.
*/
export function IntecionCredit(props: { text?: string; url?: string } = {}) {
const { text = 'Technology by Intecion.com', url = 'https://intecion.com' } = props
return (
<a
href={url}
target="_blank"
rel="noopener noreferrer"
style={{
display: 'block',
padding: '0.75rem 1rem',
fontSize: '0.75rem',
opacity: 0.6,
textDecoration: 'none',
}}
>
{text}
</a>
)
}
+1
View File
@@ -0,0 +1 @@
export { AdminIcon, AdminLogo, IntecionCredit } from './adminGraphics.js'
+50 -2
View File
@@ -76,8 +76,8 @@ export const ipalKit = (options: IpalOptions): Plugin => {
// optional peer dependency at build time (it isn't in the plugin's own
// node_modules). Avoids TS2307 without @ts-expect-error; the module
// exists at runtime in projects that installed it.
// @ts-ignore
const mod = (await import('@clocklimited/payload-2fa')) as {
const pkg = '@clocklimited/payload-2fa'
const mod = (await import(pkg)) as {
payloadTotp?: (opts: Record<string, unknown>) => Plugin
totpPlugin?: (opts: Record<string, unknown>) => Plugin
}
@@ -104,6 +104,54 @@ export const ipalKit = (options: IpalOptions): Plugin => {
}
}
// --- white-label admin branding ---
// Inject admin.components.graphics.Logo/Icon pointing at the plugin's
// /admin export. The components read SiteSettings at render, so each client
// sees their own brand from the panel (no brand in code). Project must run
// `pnpm generate:importmap` so Payload resolves the component paths.
if (options.whiteLabel) {
const wl = options.whiteLabel === true ? {} : options.whiteLabel
const serverProps: Record<string, unknown> = {}
if (wl.settingsSlug) {serverProps.settingsSlug = wl.settingsSlug}
if (wl.nameField) {serverProps.nameField = wl.nameField}
const logoProps = { ...serverProps, ...(wl.logoField ? { imageField: wl.logoField } : {}) }
const iconProps = { ...serverProps, ...(wl.iconField ? { imageField: wl.iconField } : {}) }
// Vendor credit ("Technology by Intecion.com") under the nav — on by
// default, hidden with credit:false, text/url overridable.
const creditOn = wl.credit !== false
const creditProps =
wl.credit && typeof wl.credit === 'object'
? {
...(wl.credit.text ? { text: wl.credit.text } : {}),
...(wl.credit.url ? { url: wl.credit.url } : {}),
}
: {}
config.admin = {
...(config.admin ?? {}),
components: {
...(config.admin?.components ?? {}),
graphics: {
...(config.admin?.components?.graphics ?? {}),
Icon: { path: '@intecion/ipal-kit/admin#AdminIcon', serverProps: iconProps },
Logo: { path: '@intecion/ipal-kit/admin#AdminLogo', serverProps: logoProps },
},
...(creditOn
? {
afterNavLinks: [
...(config.admin?.components?.afterNavLinks ?? []),
{ path: '@intecion/ipal-kit/admin#IntecionCredit', serverProps: creditProps },
],
}
: {}),
},
...(wl.titleSuffix
? { meta: { ...(config.admin?.meta ?? {}), titleSuffix: wl.titleSuffix } }
: {}),
}
}
// --- i18n ---
config.localization = buildLocalizationConfig(options.i18n)
+36
View File
@@ -86,4 +86,40 @@ export type IpalOptions = {
issuer: string
}
| false
/**
* White-label the admin panel: show the CLIENT's brand (logo + name) in the
* admin — login screen and nav — pulled from SiteSettings, set by the editor
* in the panel (no brand hardcoded in code). `true` wires it with defaults
* (SiteSettings `logo`/`favicon`/`siteName`); the object form overrides the
* global slug, field names, or the browser-tab title suffix.
*
* Injects admin.components.graphics.Logo/Icon pointing at
* `@intecion/ipal-kit/admin`. After enabling, run `pnpm generate:importmap`
* so Payload can resolve the components. See docs/white-label.md.
*/
whiteLabel?:
| {
/**
* Vendor credit ("Technology by Intecion.com") at the bottom of the
* admin nav. Default on; `false` hides it, or override text/url.
*/
credit?:
| {
text?: string
url?: string
}
| boolean
/** Upload field for the small nav icon. Default 'favicon'. */
iconField?: string
/** Upload field for the login/nav logo. Default 'logo'. */
logoField?: string
/** Field with the site name. Default 'siteName'. */
nameField?: string
/** SiteSettings global slug. Default 'site-settings'. */
settingsSlug?: string
/** Static browser-tab title suffix (e.g. ' · Acme Admin'). */
titleSuffix?: string
}
| boolean
}