/* 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, Avatar, Divider, Empty } 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'; author: Author['name']; } interface Articles { cn: Article[]; en: Article[]; } type Authors = Author[]; interface ArticleListProps { name: React.ReactNode; data: Article[]; authors: Authors; } const ArticleList: React.FC = ({ name, data = [], authors = [] }) => (

{name}

); export default () => { const { locale } = useIntl(); const isZhCN = locale === 'zh-CN'; const [{ articles = { cn: [], en: [] }, authors = [] }, loading] = useSiteData<{ articles: Articles; authors: Authors }>(); // ========================== Data ========================== const mergedData = React.useMemo(() => { const yearData: Record> = {}; articles[isZhCN ? 'cn' : 'en']?.forEach(article => { const year = moment(article.date).year(); yearData[year] = yearData[year] || {}; yearData[year][article.type] = [...(yearData[year][article.type] || []), article]; }); return yearData; }, [articles]); // ========================= Render ========================= let content: React.ReactNode; if (loading) { content = ; } else { const yearList = Object.keys(mergedData).sort((a, b) => Number(b) - Number(a)); content = yearList.length ? ( {yearList.map(year => ( } data={mergedData[year].design} authors={authors} /> } data={mergedData[year].develop} authors={authors} />
))}
) : null; } return (
{content}
); };