2021-04-04 07:25:21 +03:00
|
|
|
/* eslint-disable camelcase */
|
|
|
|
/* eslint-disable react/no-danger */
|
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-04-05 04:43:55 +03:00
|
|
|
import { Typography, Skeleton } from 'antd';
|
2021-04-04 07:25:21 +03:00
|
|
|
import format from 'date-fns/format';
|
|
|
|
|
|
|
|
import { fetchExternalData } from '../utils/apis';
|
|
|
|
|
|
|
|
const { Title, Link } = Typography;
|
|
|
|
|
|
|
|
const OWNCAST_FEED_URL = 'https://owncast.online/news/index.json';
|
|
|
|
const OWNCAST_BASE_URL = 'https://owncast.online';
|
|
|
|
|
|
|
|
interface Article {
|
|
|
|
title: string;
|
|
|
|
url: string;
|
|
|
|
content_html: string;
|
|
|
|
date_published: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ArticleItem({ title, url, content_html: content, date_published: date }: Article) {
|
|
|
|
const dateObject = new Date(date);
|
|
|
|
const dateString = format(dateObject, 'MMM dd, yyyy, HH:mm');
|
|
|
|
return (
|
|
|
|
<article>
|
|
|
|
<p className="timestamp">{dateString}</p>
|
|
|
|
<Title level={3}>
|
|
|
|
<Link href={`${OWNCAST_BASE_URL}${url}`} target="_blank" rel="noopener noreferrer">
|
|
|
|
{title}
|
|
|
|
</Link>
|
|
|
|
</Title>
|
|
|
|
<div dangerouslySetInnerHTML={{ __html: content }} />
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function NewsFeed() {
|
|
|
|
const [feed, setFeed] = useState<Article[]>([]);
|
2021-04-05 04:43:55 +03:00
|
|
|
const [loading, setLoading] = useState<Boolean>(true);
|
|
|
|
|
2021-04-04 07:25:21 +03:00
|
|
|
const getFeed = async () => {
|
2021-04-05 04:43:55 +03:00
|
|
|
setLoading(false);
|
|
|
|
|
2021-04-04 07:25:21 +03:00
|
|
|
try {
|
|
|
|
const result = await fetchExternalData(OWNCAST_FEED_URL);
|
2021-04-12 09:56:41 +03:00
|
|
|
if (result?.items.length > 0) {
|
|
|
|
setFeed(result.items);
|
|
|
|
}
|
2021-04-04 07:25:21 +03:00
|
|
|
} catch (error) {
|
|
|
|
console.log('==== error', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
getFeed();
|
|
|
|
}, []);
|
|
|
|
|
2021-04-05 04:43:55 +03:00
|
|
|
const loadingSpinner = loading ? (<Skeleton loading={true} active />) : null;
|
|
|
|
const noNews = !loading && feed.length === 0 ? (<div>No news.</div>) : null;
|
2021-04-04 07:25:21 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<section className="news-feed form-module">
|
|
|
|
<Title level={2}>News & Updates from Owncast</Title>
|
2021-04-05 04:43:55 +03:00
|
|
|
{loadingSpinner}
|
2021-04-04 07:25:21 +03:00
|
|
|
{feed.map(item => (
|
2021-04-05 04:30:38 +03:00
|
|
|
<ArticleItem {...item} key={item.url} />
|
2021-04-04 07:25:21 +03:00
|
|
|
))}
|
2021-04-05 04:43:55 +03:00
|
|
|
|
|
|
|
{noNews}
|
2021-04-04 07:25:21 +03:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|