33 lines
962 B
TypeScript
33 lines
962 B
TypeScript
import * as z from "zod";
|
|
import type { AcfSocialFragment } from "#graphql/operations";
|
|
|
|
const socialProfile = z.object({ url: z.url() }).transform(({ url }) => ({ url, icon: getSocialIcon(url) }));
|
|
const acfSocialSchema = z.object({
|
|
profiles: z.array(socialProfile),
|
|
});
|
|
export type AcfSocialOutput = z.infer<typeof acfSocialSchema>;
|
|
|
|
export function parseAcfSocial(data?: AcfSocialFragment) {
|
|
try {
|
|
return acfSocialSchema.parse(data);
|
|
}
|
|
catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
const socialIconMap = {
|
|
"facebook.com": "i-cib-facebook-f",
|
|
"twitter.com": "i-cib-twitter",
|
|
"x.com": "i-cib-twitter",
|
|
"instagram.com": "i-cib-instagram",
|
|
"youtube.com": "i-cib-youtube",
|
|
"linkedin.com": "i-cib-linkedin",
|
|
"tiktok.com": "i-cib-tiktok",
|
|
};
|
|
|
|
function getSocialIcon(url: string): string {
|
|
const domain = new URL(url).hostname.toLowerCase().replace(/^www\./, "");
|
|
return socialIconMap[domain as keyof typeof socialIconMap] ?? "i-lucide-globe";
|
|
}
|