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

121 lines
4.4 KiB
TypeScript

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.
// @ts-ignore
const mod = (await import('@payload-config')) 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>
)
}