feat: AcfSocial / parseAcfSocial
This commit is contained in:
32
wp-content/themes/moonshine/app/utils/acf-social.ts
Normal file
32
wp-content/themes/moonshine/app/utils/acf-social.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
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";
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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",
|
||||
};
|
||||
|
||||
export function mapSocialIcon(url: string) {
|
||||
try {
|
||||
const domain = new URL(url).hostname.toLowerCase().replace(/^www\./, "");
|
||||
const icon = socialIconMap[domain as keyof typeof socialIconMap];
|
||||
if (!icon) throw new Error(`Média social introuvable ${url}`);
|
||||
return icon;
|
||||
}
|
||||
catch {
|
||||
return "i-lucide-globe";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user