"use client"
import { useEffect, useState } from "react"
import Link from "next/link"
import { siteConfig } from "@/config/site"
import { AnnounceInterface } from "@/lib/types"
import { convertDirtyStringToHtml } from "@/lib/utils"
import { useTranslation } from "@/app/i18n/client"
import { LocaleTypes } from "@/app/i18n/settings"
import { Icons } from "../icons"
import { AppContent } from "../ui/app-content"
import { Parser as HtmlToReactParser } from "html-to-react"
interface NewsSectionProps {
lang: LocaleTypes
}
const ContentPlaceholder = () => (
)
export const NewsSection = ({ lang }: NewsSectionProps) => {
const { t } = useTranslation(lang, "news-section")
const [newsItems, setNewsItems] = useState([])
const [loading, setLoading] = useState(true)
useEffect(() => {
const getDiscordAnnouncements = async () => {
setLoading(true)
await fetch("/api/news")
.then((res) => res.json())
.then(({ announcements }: { announcements: AnnounceInterface[] }) => {
setNewsItems(announcements ?? [])
})
.finally(() => setLoading(false))
}
getDiscordAnnouncements()
}, [])
const [news] = newsItems ?? []
// @ts-expect-error - HtmlToReactParser is not typed
const htmlToReactParser = new HtmlToReactParser()
const announcementContent = htmlToReactParser.parse(
convertDirtyStringToHtml(
news?.content || news?.message_snapshots?.[0]?.message?.content || ""
)
)
const twitterShareUrl = `https://twitter.com/intent/tweet?text=${encodeURIComponent(
news?.content
)}&url=${encodeURIComponent(siteConfig?.links?.discordAnnouncementChannel)}`
return (
{t("recentUpdates")}
{!loading ? (
news?.timestamp && (
{new Intl.DateTimeFormat(lang, {
month: "long",
day: "numeric",
year: "numeric",
}).format(new Date(news?.timestamp))}
)
) : (
)}
{t("repostOnSocial", {
socialName: "X",
})}
{!loading ? announcementContent : }
{t("seeAllUpdates")}
)
}
NewsSection.displayName = "NewsSection"