web-humas-fe/components/ui/social-media/facebook.tsx

53 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-03-13 08:53:48 +00:00
"use client";
import { Spinner } from "@heroui/react";
import { useEffect, useRef, useState } from "react";
interface FacebookEmbedProps {
postUrl: string;
width?: number;
showText?: boolean;
}
declare global {
interface Window {
FB: any;
}
}
const FacebookEmbed: React.FC<FacebookEmbedProps> = ({ postUrl }) => {
const [loading, setLoading] = useState(true);
2024-11-05 06:15:40 +00:00
useEffect(() => {
2025-03-13 08:53:48 +00:00
if (!window.FB) {
2024-11-05 06:15:40 +00:00
const script = document.createElement("script");
2025-03-13 08:53:48 +00:00
script.src =
"https://connect.facebook.net/id_ID/sdk.js#xfbml=1&version=v22.0&appId=1290603775136204";
2024-11-05 06:15:40 +00:00
script.async = true;
2025-03-13 08:53:48 +00:00
script.defer = true;
document.body.appendChild(script);
2024-11-05 06:15:40 +00:00
2025-03-13 08:53:48 +00:00
script.onload = () => {
window.FB?.XFBML.parse();
setLoading(false);
2024-11-05 06:15:40 +00:00
};
2025-03-13 08:53:48 +00:00
} else {
window.FB.XFBML.parse();
setLoading(false);
2024-11-05 06:15:40 +00:00
}
}, []);
return (
2025-03-13 08:53:48 +00:00
<>
{loading && <Spinner />}
<div
className={`fb-post ${loading ? "hidden" : ""}`}
data-href={postUrl}
data-width="500"
data-show-text="true"
></div>
</>
2024-11-05 06:15:40 +00:00
);
};
2025-03-13 08:53:48 +00:00
export default FacebookEmbed;