236 lines
8.3 KiB
TypeScript
236 lines
8.3 KiB
TypeScript
export type BrandLegalLinks = {
|
|
imprintUrl: string;
|
|
privacyUrl: string;
|
|
termsUrl: string;
|
|
};
|
|
|
|
export type BrandSupportContact = {
|
|
email: string;
|
|
fromName: string;
|
|
};
|
|
|
|
export type BrandAssets = {
|
|
logoUrl: string;
|
|
faviconUrl: string;
|
|
};
|
|
|
|
const brandSocialLinkKeys = ["linkedinUrl", "xUrl", "instagramUrl", "githubUrl", "youtubeUrl", "discordUrl"] as const;
|
|
|
|
type BrandSocialLinkKey = (typeof brandSocialLinkKeys)[number];
|
|
|
|
export type BrandSocialLinks = Partial<Record<BrandSocialLinkKey, string>>;
|
|
|
|
export type BrandLinkConfiguration = {
|
|
siteUrl: string;
|
|
legal: BrandLegalLinks;
|
|
support: BrandSupportContact;
|
|
assets: BrandAssets;
|
|
socials: BrandSocialLinks;
|
|
};
|
|
|
|
export type TransactionalMailColors = {
|
|
background: string;
|
|
surface: string;
|
|
primary: string;
|
|
primaryText: string;
|
|
text: string;
|
|
mutedText: string;
|
|
border: string;
|
|
link: string;
|
|
};
|
|
|
|
export type BrandTokens = {
|
|
productName: string;
|
|
siteUrl: string;
|
|
legal: BrandLegalLinks;
|
|
support: BrandSupportContact;
|
|
assets: BrandAssets;
|
|
socials: BrandSocialLinks;
|
|
mail: {
|
|
colors: TransactionalMailColors;
|
|
};
|
|
};
|
|
|
|
export type BrandTokenOverrides = Partial<{
|
|
productName: string | null;
|
|
siteUrl: string | null;
|
|
legal: Partial<Record<keyof BrandLegalLinks, string | null>> | null;
|
|
support: Partial<Record<keyof BrandSupportContact, string | null>> | null;
|
|
assets: Partial<Record<keyof BrandAssets, string | null>> | null;
|
|
socials: Partial<Record<BrandSocialLinkKey, string | null>> | null;
|
|
mail: {
|
|
colors?: Partial<Record<keyof TransactionalMailColors, string | null>> | null;
|
|
} | null;
|
|
}>;
|
|
|
|
export type AppConfigurationLike = Partial<{
|
|
productName: string | null;
|
|
siteUrl: string | null;
|
|
appName: string | null;
|
|
name: string | null;
|
|
supportEmail: string | null;
|
|
supportFromName: string | null;
|
|
emailFromName: string | null;
|
|
logoUrl: string | null;
|
|
faviconUrl: string | null;
|
|
imprintUrl: string | null;
|
|
privacyUrl: string | null;
|
|
termsUrl: string | null;
|
|
brand: BrandTokenOverrides | null;
|
|
mail: {
|
|
colors?: Partial<Record<keyof TransactionalMailColors, string | null>> | null;
|
|
} | null;
|
|
}>;
|
|
|
|
export const defaultBrandLinks: BrandLinkConfiguration = {
|
|
siteUrl: "https://hivegrid.de",
|
|
legal: {
|
|
imprintUrl: "https://hivegrid.de/impressum",
|
|
privacyUrl: "https://hivegrid.de/datenschutz",
|
|
termsUrl: "https://hivegrid.de/agb",
|
|
},
|
|
support: {
|
|
email: "support@hivegrid.de",
|
|
fromName: "HiveGrid-Team",
|
|
},
|
|
assets: {
|
|
logoUrl: "https://hivegrid.de/hivegrid-logo.png",
|
|
faviconUrl: "/favicon.ico",
|
|
},
|
|
socials: {},
|
|
};
|
|
|
|
export const defaultBrandTokens: BrandTokens = {
|
|
productName: "HiveGrid",
|
|
siteUrl: defaultBrandLinks.siteUrl,
|
|
legal: defaultBrandLinks.legal,
|
|
support: defaultBrandLinks.support,
|
|
assets: defaultBrandLinks.assets,
|
|
socials: defaultBrandLinks.socials,
|
|
mail: {
|
|
colors: {
|
|
background: "#f7f5f1",
|
|
surface: "#ffffff",
|
|
primary: "#f1b729",
|
|
primaryText: "#231f20",
|
|
text: "#231f20",
|
|
mutedText: "#655f59",
|
|
border: "#e7e1d8",
|
|
link: "#735400",
|
|
},
|
|
},
|
|
};
|
|
|
|
const nonEmpty = (value: string | null | undefined): string | undefined => {
|
|
const trimmed = value?.trim();
|
|
return trimmed ? trimmed : undefined;
|
|
};
|
|
|
|
const pickFirst = (...values: Array<string | null | undefined>): string | undefined => {
|
|
for (const value of values) {
|
|
const resolved = nonEmpty(value);
|
|
if (resolved) return resolved;
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
const resolveSocialLinks = (overrides?: Partial<Record<BrandSocialLinkKey, string | null>> | null): BrandSocialLinks => {
|
|
const socials: BrandSocialLinks = {};
|
|
|
|
for (const key of brandSocialLinkKeys) {
|
|
const value = nonEmpty(overrides?.[key]) ?? defaultBrandLinks.socials[key];
|
|
if (value) {
|
|
socials[key] = value;
|
|
}
|
|
}
|
|
|
|
return socials;
|
|
};
|
|
|
|
export function createBrandTokens(overrides: BrandTokenOverrides = {}): BrandTokens {
|
|
return {
|
|
productName: nonEmpty(overrides.productName) ?? defaultBrandTokens.productName,
|
|
siteUrl: nonEmpty(overrides.siteUrl) ?? defaultBrandTokens.siteUrl,
|
|
legal: {
|
|
imprintUrl: nonEmpty(overrides.legal?.imprintUrl) ?? defaultBrandTokens.legal.imprintUrl,
|
|
privacyUrl: nonEmpty(overrides.legal?.privacyUrl) ?? defaultBrandTokens.legal.privacyUrl,
|
|
termsUrl: nonEmpty(overrides.legal?.termsUrl) ?? defaultBrandTokens.legal.termsUrl,
|
|
},
|
|
support: {
|
|
email: nonEmpty(overrides.support?.email) ?? defaultBrandTokens.support.email,
|
|
fromName: nonEmpty(overrides.support?.fromName) ?? defaultBrandTokens.support.fromName,
|
|
},
|
|
assets: {
|
|
logoUrl: nonEmpty(overrides.assets?.logoUrl) ?? defaultBrandTokens.assets.logoUrl,
|
|
faviconUrl: nonEmpty(overrides.assets?.faviconUrl) ?? defaultBrandTokens.assets.faviconUrl,
|
|
},
|
|
socials: resolveSocialLinks(overrides.socials),
|
|
mail: {
|
|
colors: {
|
|
background: nonEmpty(overrides.mail?.colors?.background) ?? defaultBrandTokens.mail.colors.background,
|
|
surface: nonEmpty(overrides.mail?.colors?.surface) ?? defaultBrandTokens.mail.colors.surface,
|
|
primary: nonEmpty(overrides.mail?.colors?.primary) ?? defaultBrandTokens.mail.colors.primary,
|
|
primaryText: nonEmpty(overrides.mail?.colors?.primaryText) ?? defaultBrandTokens.mail.colors.primaryText,
|
|
text: nonEmpty(overrides.mail?.colors?.text) ?? defaultBrandTokens.mail.colors.text,
|
|
mutedText: nonEmpty(overrides.mail?.colors?.mutedText) ?? defaultBrandTokens.mail.colors.mutedText,
|
|
border: nonEmpty(overrides.mail?.colors?.border) ?? defaultBrandTokens.mail.colors.border,
|
|
link: nonEmpty(overrides.mail?.colors?.link) ?? defaultBrandTokens.mail.colors.link,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
export function resolveTransactionalBrandTokens(appConfigurationLike: AppConfigurationLike | null | undefined): BrandTokens {
|
|
const config = appConfigurationLike ?? {};
|
|
|
|
return createBrandTokens({
|
|
productName: pickFirst(config.brand?.productName, config.productName, config.appName, config.name),
|
|
siteUrl: pickFirst(config.brand?.siteUrl, config.siteUrl),
|
|
legal: {
|
|
imprintUrl: pickFirst(config.brand?.legal?.imprintUrl, config.imprintUrl),
|
|
privacyUrl: pickFirst(config.brand?.legal?.privacyUrl, config.privacyUrl),
|
|
termsUrl: pickFirst(config.brand?.legal?.termsUrl, config.termsUrl),
|
|
},
|
|
support: {
|
|
email: pickFirst(config.brand?.support?.email, config.supportEmail),
|
|
fromName: pickFirst(config.brand?.support?.fromName, config.supportFromName, config.emailFromName),
|
|
},
|
|
assets: {
|
|
logoUrl: pickFirst(config.brand?.assets?.logoUrl, config.logoUrl),
|
|
faviconUrl: pickFirst(config.brand?.assets?.faviconUrl, config.faviconUrl),
|
|
},
|
|
socials: config.brand?.socials,
|
|
mail: {
|
|
colors: {
|
|
background: pickFirst(config.brand?.mail?.colors?.background, config.mail?.colors?.background),
|
|
surface: pickFirst(config.brand?.mail?.colors?.surface, config.mail?.colors?.surface),
|
|
primary: pickFirst(config.brand?.mail?.colors?.primary, config.mail?.colors?.primary),
|
|
primaryText: pickFirst(config.brand?.mail?.colors?.primaryText, config.mail?.colors?.primaryText),
|
|
text: pickFirst(config.brand?.mail?.colors?.text, config.mail?.colors?.text),
|
|
mutedText: pickFirst(config.brand?.mail?.colors?.mutedText, config.mail?.colors?.mutedText),
|
|
border: pickFirst(config.brand?.mail?.colors?.border, config.mail?.colors?.border),
|
|
link: pickFirst(config.brand?.mail?.colors?.link, config.mail?.colors?.link),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export { loadBrandFromEnv, type BrandEnvSource } from "./env.js";
|
|
|
|
export function brandCssVariables(tokens: BrandTokens): Record<string, string> {
|
|
return {
|
|
"--hivegrid-product-name": tokens.productName,
|
|
"--hivegrid-site-url": tokens.siteUrl,
|
|
"--hivegrid-logo-url": `url(${tokens.assets.logoUrl})`,
|
|
"--hivegrid-favicon-url": `url(${tokens.assets.faviconUrl})`,
|
|
"--hivegrid-mail-background": tokens.mail.colors.background,
|
|
"--hivegrid-mail-surface": tokens.mail.colors.surface,
|
|
"--hivegrid-mail-primary": tokens.mail.colors.primary,
|
|
"--hivegrid-mail-primary-text": tokens.mail.colors.primaryText,
|
|
"--hivegrid-mail-text": tokens.mail.colors.text,
|
|
"--hivegrid-mail-muted-text": tokens.mail.colors.mutedText,
|
|
"--hivegrid-mail-border": tokens.mail.colors.border,
|
|
"--hivegrid-mail-link": tokens.mail.colors.link,
|
|
};
|
|
}
|