95 lines
2.5 KiB
TypeScript
95 lines
2.5 KiB
TypeScript
import type { ContactContent } from "@/content/types";
|
|
|
|
const FALLBACK_SITE_URL = "https://example.com";
|
|
const SITE_MODES = ["coming-soon", "full"] as const;
|
|
|
|
export type SiteMode = (typeof SITE_MODES)[number];
|
|
|
|
function normalizeSiteUrl(value: string | undefined): string {
|
|
const raw = value?.trim();
|
|
|
|
if (!raw) {
|
|
return FALLBACK_SITE_URL;
|
|
}
|
|
|
|
try {
|
|
const url = new URL(raw);
|
|
return url.origin;
|
|
} catch {
|
|
return FALLBACK_SITE_URL;
|
|
}
|
|
}
|
|
|
|
function normalizeExternalUrl(value: string | undefined): string | undefined {
|
|
const raw = value?.trim();
|
|
|
|
if (!raw) {
|
|
return undefined;
|
|
}
|
|
|
|
try {
|
|
return new URL(raw).toString();
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
function formatDisplayValue(key: "email" | "linkedin" | "github", href: string): string {
|
|
if (key === "email") {
|
|
return href.replace("mailto:", "");
|
|
}
|
|
|
|
const url = new URL(href);
|
|
return `${url.hostname}${url.pathname === "/" ? "" : url.pathname.replace(/\/$/, "")}`;
|
|
}
|
|
|
|
export const siteConfig = {
|
|
ownerName: "Diyaa",
|
|
siteUrl: normalizeSiteUrl(process.env.NEXT_PUBLIC_SITE_URL),
|
|
email: process.env.NEXT_PUBLIC_CONTACT_EMAIL?.trim() || "",
|
|
linkedinUrl: normalizeExternalUrl(process.env.NEXT_PUBLIC_LINKEDIN_URL),
|
|
githubUrl: normalizeExternalUrl(process.env.NEXT_PUBLIC_GITHUB_URL),
|
|
};
|
|
|
|
export function getSiteMode(): SiteMode {
|
|
const mode = process.env.NEXT_PUBLIC_SITE_MODE?.trim();
|
|
return SITE_MODES.includes(mode as SiteMode) ? (mode as SiteMode) : "coming-soon";
|
|
}
|
|
|
|
export function isComingSoonMode(): boolean {
|
|
return getSiteMode() === "coming-soon";
|
|
}
|
|
|
|
export function getLocalizedPath(pathname: string, locale: "ar" | "en"): string {
|
|
const normalizedPath = pathname === "/" ? "" : pathname;
|
|
return `/${locale}${normalizedPath}`;
|
|
}
|
|
|
|
export function getLocalizedUrl(pathname: string, locale: "ar" | "en"): string {
|
|
return new URL(getLocalizedPath(pathname, locale), siteConfig.siteUrl).toString();
|
|
}
|
|
|
|
export function getEmailHref(): string | undefined {
|
|
return siteConfig.email ? `mailto:${siteConfig.email}` : undefined;
|
|
}
|
|
|
|
export function getContactChannels(contact: ContactContent) {
|
|
const valueMap = {
|
|
email: getEmailHref(),
|
|
linkedin: siteConfig.linkedinUrl,
|
|
github: siteConfig.githubUrl,
|
|
} as const;
|
|
|
|
return contact.channels.map((channel) => {
|
|
const href = valueMap[channel.key];
|
|
const external = channel.key !== "email";
|
|
|
|
return {
|
|
...channel,
|
|
href,
|
|
external,
|
|
value: href ? formatDisplayValue(channel.key, href) : contact.channelFallback,
|
|
};
|
|
});
|
|
}
|