"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 { LABELS } from "@/app/labels"
import { Icons } from "../icons"
import { AppContent } from "../ui/app-content"
import { Parser as HtmlToReactParser } from "html-to-react"
const ContentPlaceholder = () => (
)
export const NewsSection = () => {
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 (
{LABELS.NEWS_SECTION.RECENT_UPDATES}
{!loading ? (
news?.timestamp && (
{new Intl.DateTimeFormat("en-US", {
month: "long",
day: "numeric",
year: "numeric",
}).format(new Date(news?.timestamp))}
)
) : (
)}
{LABELS.NEWS_SECTION.REPOST_ON_SOCIAL.replace(
"{{socialName}}",
"Twitter"
)}
{!loading ? announcementContent : }
{LABELS.NEWS_SECTION.SEE_ALL_UPDATES}
)
}
NewsSection.displayName = "NewsSection"