/* eslint-disable react/no-array-index-key */
import * as React from 'react';
import moment from 'moment';
import { FormattedMessage, useIntl } from 'react-intl';
import { Tabs, Skeleton } from 'antd';
import { useSiteData } from '../../Home/util';
import './index.less';
interface Author {
avatar: string;
href: string;
type: 'design' | 'develop';
name: string;
}
interface Article {
title: string;
href: string;
date: string;
type: 'design' | 'develop';
}
interface Articles {
author: Author[];
cn: Article[];
en: Article[];
}
interface ArticleListProps {
name: React.ReactNode;
data: Article[];
}
const ArticleList = ({ name, data }: ArticleListProps) => (
{name}
|
);
interface AuthorsProps {
data: Author[];
}
const Authors = ({ data }: AuthorsProps) => (
{data.map(author => (
-
))}
|
);
export default () => {
const { locale } = useIntl();
const isZhCN = locale === 'zh-CN';
const data = useSiteData('articles');
// ========================== Data ==========================
const mergedData = React.useMemo(() => {
if (!data) {
return data;
}
const articles = data[isZhCN ? 'cn' : 'en'];
const yearData: Record> = {};
articles.forEach(article => {
const year = moment(article.date).year();
yearData[year] = yearData[year] || {};
yearData[year][article.type] = [...(yearData[year][article.type] || []), article];
});
return yearData;
}, [data]);
const mergedAuthors = React.useMemo(() => {
if (!data) {
return data;
}
const authors: Record = {};
(data.author || []).forEach(author => {
authors[author.type] = [...(authors[author.type] || []), author];
});
return authors;
}, [data]);
// ========================= Render =========================
let content: React.ReactNode;
if (!data) {
content = ;
} else {
const yearList = Object.keys(mergedData).sort((a, b) => Number(b) - Number(a));
content = yearList.length ? (
{yearList.map(year => {
const showAuthors = String(moment().year()) === year;
return (
}
data={mergedData[year].design}
/>
}
data={mergedData[year].develop}
/>
{showAuthors && (
)}
);
})}
) : null;
}
return (
{content}
);
};