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