New news detail json

This commit is contained in:
Yifei Hu 2024-07-09 14:17:37 +08:00
parent 858dc41ebb
commit 146a3fdd5e
15 changed files with 2336 additions and 3021 deletions

View file

@ -0,0 +1,42 @@
import React from "react";
import Image from "next/image";
interface ContextItemProps {
item: {
name: string;
url: string;
description: string;
provider: { name: string; image?: { thumbnail: { contentUrl: string } } }[];
datePublished: string;
image?: {
contentUrl: string;
thumbnail: { contentUrl: string; width: number; height: number };
};
};
}
const ContextItem: React.FC<ContextItemProps> = ({ item }) => {
return (
<div className="border p-4 rounded-lg mb-4">
<h4 className="font-bold">{item.name}</h4>
{item.image && (
<Image
src={item.image.contentUrl}
alt={item.name}
width={item.image.thumbnail.width}
height={item.image.thumbnail.height}
className="my-2 rounded"
/>
)}
<p>{item.description}</p>
<div className="text-sm text-gray-500 mt-2">
{item.provider[0].name} | {new Date(item.datePublished).toLocaleDateString()}
</div>
<a href={item.url} target="_blank" rel="noopener noreferrer" className="text-blue-500 hover:underline">
Read more
</a>
</div>
);
};
export default ContextItem;

View file

@ -0,0 +1,35 @@
import React from "react";
import ContextItem from "./ContextItem";
interface NewsDetailProps {
news: {
title: string;
sections: {
title: string;
content: string;
context: any[];
}[];
};
}
const NewsDetail: React.FC<NewsDetailProps> = ({ news }) => {
return (
<article className="prose lg:prose-xl">
<h1>{news.title}</h1>
{news.sections.map((section, index) => (
<section key={index}>
<h2>{section.title}</h2>
<p>{section.content}</p>
<div className="mt-4">
<h3>Related Context:</h3>
{section.context.map((item, i) => (
<ContextItem key={i} item={item} />
))}
</div>
</section>
))}
</article>
);
};
export default NewsDetail;

View file

@ -2,6 +2,7 @@
import { useEffect, useState } from "react";
import { Newspaper } from "lucide-react";
import Link from "next/link";
interface NewsItem {
id: string;
@ -51,7 +52,7 @@ const NewsPage = () => {
<div className="flex flex-row items-center justify-center min-h-screen">
<p className="text-black/70 dark:text-white/70 text-sm">Loading news...</p>
</div>
) : error ? (
) : (error ? (
<div className="flex flex-col items-center justify-center min-h-screen">
<p className="text-red-500 text-sm mb-2">Failed to load news.</p>
<p className="text-red-500 text-xs">{error}</p>
@ -61,15 +62,22 @@ const NewsPage = () => {
{news.length === 0 ? (
<p className="text-black/70 dark:text-white/70 text-sm text-center">No news available.</p>
) : (
news.map((item) => (
<div key={item.id} className="flex flex-col space-y-4 border-b border-white-200 dark:border-dark-200 py-6 lg:mx-4">
<h3 className="text-black dark:text-white lg:text-xl font-medium">{item.title}</h3>
news.map(item => (
<div
key={item.id}
className="flex flex-col space-y-4 border-b border-white-200 dark:border-dark-200 py-6 lg:mx-4"
>
<Link href={`/news/${item.id}`}>
<h3 className="text-black dark:text-white lg:text-xl font-medium hover:underline cursor-pointer">
{item.title}
</h3>
</Link>
<p className="text-black/70 dark:text-white/70 text-sm">{item.summary}</p>
</div>
))
)}
</div>
)}
))}
</div>
);
};