Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6f177a37d | ||
|
|
1a9c51a1fd |
+14
-7
@@ -1,3 +1,4 @@
|
||||
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,
|
||||
@@ -5,13 +6,13 @@
|
||||
* 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.
|
||||
@@ -19,8 +20,14 @@
|
||||
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;
|
||||
};
|
||||
|
||||
+33
-13
@@ -1,25 +1,45 @@
|
||||
import { jsx as _jsx } from "react/jsx-runtime";
|
||||
import { getPayload } from 'payload';
|
||||
async function loadBranding(settingsSlug, nameField, imageField) {
|
||||
/** A localized text field may arrive as a string or a { locale: value } map. */ function pickText(value, locale) {
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
if (value && typeof value === 'object') {
|
||||
const map = value;
|
||||
const preferred = locale ? map[locale] : undefined;
|
||||
if (typeof preferred === 'string') {
|
||||
return preferred;
|
||||
}
|
||||
const first = Object.values(map).find((v)=>typeof v === 'string');
|
||||
return first ?? null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
async function loadBranding(props, settingsSlug, nameField, imageField) {
|
||||
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);
|
||||
const payload = await getPayload({
|
||||
config: mod.default
|
||||
});
|
||||
const locale = typeof props.locale === 'string' ? props.locale : props.locale?.code;
|
||||
const settings = await payload.findGlobal({
|
||||
slug: settingsSlug,
|
||||
depth: 1
|
||||
depth: 1,
|
||||
...locale ? {
|
||||
locale: locale
|
||||
} : {}
|
||||
});
|
||||
const name = typeof settings[nameField] === 'string' ? settings[nameField] : null;
|
||||
const name = pickText(settings[nameField], locale);
|
||||
const image = settings[imageField];
|
||||
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
|
||||
@@ -32,7 +52,7 @@ async function loadBranding(settingsSlug, nameField, imageField) {
|
||||
* falls back to `siteName` text. Sized generously for the login screen.
|
||||
*/ export async function AdminLogo(props = {}) {
|
||||
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 /*#__PURE__*/ _jsx("img", {
|
||||
alt: name ?? '',
|
||||
@@ -59,7 +79,7 @@ async function loadBranding(settingsSlug, nameField, imageField) {
|
||||
}
|
||||
/** Admin panel icon (small nav glyph). SiteSettings `favicon`, falls back to initials. */ export async function AdminIcon(props = {}) {
|
||||
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 /*#__PURE__*/ _jsx("img", {
|
||||
alt: name ?? '',
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@intecion/ipal-kit",
|
||||
"version": "1.7.9",
|
||||
"version": "1.7.10",
|
||||
"description": "Intecion Payload Advanced Library — a Payload CMS 3 plugin: i18n, SEO, forms, consent, analytics, blog/archives.",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
|
||||
@@ -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