import { useState } from "react"; import { getGameTitle } from "../utils.ts"; export interface NewsData { date: string; identifier: string; type: string | null; timestamp: number; headline: string | null; content: string; url: string | null; images: Array<{ image: string; link: string | null; }>; en_headline: string | null; en_content: string | null; } interface NewsFeedProps { newsItems: NewsData[]; } export const NewsFeed: React.FC = ({ newsItems }) => { // Track which items are showing English content const [showEnglish, setShowEnglish] = useState>({}); // Toggle language for a specific news item const toggleLanguage = (itemId: string) => { setShowEnglish(prev => ({ ...prev, [itemId]: !prev[itemId] })); }; return (
{newsItems.map((news) => { const formattedDate = new Date(news.timestamp * 1000).toLocaleDateString("ja-JP", { year: "numeric", month: "2-digit", day: "2-digit", }); const gameId = news.identifier; const newsId = `${news.identifier}-${news.timestamp}-${news.content.substring(0, 20)}`; const isEnglish = showEnglish[newsId] || false; const hasTranslation = news.en_headline || news.en_content; // Choose content based on language selection const displayHeadline = isEnglish && news.en_headline ? news.en_headline : news.headline; const displayContent = isEnglish && news.en_content ? news.en_content : news.content; return (
{/* Header (Game Icon + Info) */}
{/* Game Icon */}
{gameId.substring(0, 1)}
{getGameTitle(news.identifier)} {formattedDate} {/* Display News Type */} {news.type && ( {news.type} )}
{/* Language Toggle Button */} {hasTranslation && ( )}
{/* Content Area */}
{/* Headline */} {displayHeadline && (

{displayHeadline}

)} {/* Content */}

{displayContent.split(/(\[.*?\]\(.*?\)|https?:\/\/[^\s]+)/g).map((part, index) => { const match = part.match(/\[(.*?)\]\((.*?)\)/); const urlMatch = part.match(/https?:\/\/[^\s]+/); if (match) { return ( {match[1]} ); } else if (urlMatch) { return ( {urlMatch[0]} ); } return part; })}

{/* Post Image(s) */}
{news.images.map((img, i) => ( img.link ? ( news visual ) : (
news visual
) ))}
{/* Footer with Read More Link */} {news.url && (
READ MORE
)}
); })}
); };