|
| 1 | +/** |
| 2 | + * ArticleList Component |
| 3 | + * |
| 4 | + * This component displays a list of articles in a card format. |
| 5 | + * Each article card includes: |
| 6 | + * - Cover image (with lazy loading) |
| 7 | + * - Title |
| 8 | + * - Category tag |
| 9 | + * - Summary |
| 10 | + * - Meta information (likes, views, publish date) |
| 11 | + * |
| 12 | + * Features: |
| 13 | + * - Lazy loading for images |
| 14 | + * - Responsive design |
| 15 | + * - Category navigation |
| 16 | + * - Article statistics display |
| 17 | + */ |
| 18 | + |
1 | 19 | import { EyeOutlined, FolderOutlined, HeartOutlined, HistoryOutlined } from '@ant-design/icons';
|
2 | 20 | import { Spin, Tag } from 'antd';
|
3 | 21 | import { useTranslations } from 'next-intl';
|
4 | 22 | import Link from 'next/link';
|
5 |
| -import React, { useContext } from 'react'; |
| 23 | +import React, { useContext, useMemo } from 'react'; |
6 | 24 | import LazyLoad from 'react-lazyload';
|
7 | 25 | import LogoSvg from '../../assets/LogoSvg';
|
8 | 26 |
|
9 | 27 | import { LocaleTime } from '@/components/LocaleTime';
|
10 |
| - |
11 | 28 | import { GlobalContext } from '@/context/global';
|
12 | 29 | import { getColorFromNumber } from '@/utils';
|
13 | 30 | import style from './index.module.scss';
|
14 | 31 |
|
15 |
| -interface IProps { |
16 |
| - articles: IArticle[]; |
| 32 | +interface Article { |
| 33 | + id: string; |
| 34 | + title: string; |
| 35 | + cover?: string; |
| 36 | + summary: string; |
| 37 | + category?: { |
| 38 | + value: string; |
| 39 | + label: string; |
| 40 | + }; |
| 41 | + likes: number; |
| 42 | + views: number; |
| 43 | + publishAt: string; |
| 44 | +} |
| 45 | + |
| 46 | +interface ArticleListProps { |
| 47 | + articles: Article[]; |
17 | 48 | coverHeight?: number;
|
18 | 49 | asRecommend?: boolean;
|
19 | 50 | }
|
20 | 51 |
|
21 |
| -export const ArticleList: React.FC<IProps> = ({ articles = [] }) => { |
| 52 | +/** |
| 53 | + * ArticleCard Component |
| 54 | + * Renders a single article card with all its details |
| 55 | + */ |
| 56 | +const ArticleCard: React.FC<{ article: Article; categoryIndex: number }> = ({ article, categoryIndex }) => { |
| 57 | + return ( |
| 58 | + <div className={style.articleItem}> |
| 59 | + {/* Cover Image Section */} |
| 60 | + <div className={style.coverWrapper}> |
| 61 | + {article.cover ? ( |
| 62 | + <LazyLoad height={120} placeholder={<Spin />}> |
| 63 | + <div className={style.coverWrapper}> |
| 64 | + <Link href={`/article/[id]`} as={`/article/${article.id}`} scroll={false}> |
| 65 | + <img src={article.cover} alt={`${article.title} cover`} loading="lazy" /> |
| 66 | + </Link> |
| 67 | + </div> |
| 68 | + </LazyLoad> |
| 69 | + ) : ( |
| 70 | + <LogoSvg /> |
| 71 | + )} |
| 72 | + </div> |
| 73 | + |
| 74 | + {/* Article Content Section */} |
| 75 | + <div className={style.articleWrapper}> |
| 76 | + <Link href={`/article/[id]`} as={`/article/${article.id}`} scroll={false}> |
| 77 | + <a aria-label={article.title} className={style.link}> |
| 78 | + <header> |
| 79 | + <div className={style.title} title={article.title}> |
| 80 | + {article.title} |
| 81 | + </div> |
| 82 | + <div className={style.info}> |
| 83 | + {article.category && categoryIndex >= 0 && ( |
| 84 | + <Link |
| 85 | + href={`/category/${article?.category?.value}`} |
| 86 | + as={`/category/${article?.category?.value}`} |
| 87 | + scroll={false} |
| 88 | + > |
| 89 | + <Tag className={style.antBadge} icon={<FolderOutlined />}> |
| 90 | + <span className={style.category}>{article.category?.label}</span> |
| 91 | + </Tag> |
| 92 | + </Link> |
| 93 | + )} |
| 94 | + </div> |
| 95 | + </header> |
| 96 | + |
| 97 | + {/* Article Summary and Meta Information */} |
| 98 | + <main className={style.desc} title={article.title}> |
| 99 | + <div className={style.contentWrapper}> |
| 100 | + <div className={style.desc} title={article.summary}> |
| 101 | + <span dangerouslySetInnerHTML={{ __html: article.summary }} /> |
| 102 | + </div> |
| 103 | + <div className={style.meta}> |
| 104 | + <div> |
| 105 | + <span> |
| 106 | + <HeartOutlined /> |
| 107 | + <span className={style.number}>{article.likes}</span> |
| 108 | + </span> |
| 109 | + <span className={style.separator}>·</span> |
| 110 | + <span> |
| 111 | + <EyeOutlined /> |
| 112 | + <span className={style.number}>{article.views}</span> |
| 113 | + </span> |
| 114 | + </div> |
| 115 | + <span className={style.time}> |
| 116 | + <HistoryOutlined /> |
| 117 | + <LocaleTime date={article.publishAt} format="yyyy-MM-dd" /> |
| 118 | + </span> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </main> |
| 122 | + </a> |
| 123 | + </Link> |
| 124 | + </div> |
| 125 | + <span className={style.badge} /> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +/** |
| 131 | + * Main ArticleList Component |
| 132 | + * Renders a list of article cards with proper handling of empty state |
| 133 | + */ |
| 134 | +export const ArticleList: React.FC<ArticleListProps> = ({ articles = [] }) => { |
22 | 135 | const t = useTranslations();
|
23 | 136 | const { categories } = useContext(GlobalContext);
|
24 | 137 |
|
| 138 | + // Memoize the category indices to avoid recalculating on every render |
| 139 | + const categoryIndices = useMemo(() => { |
| 140 | + return articles.map(article => |
| 141 | + categories?.findIndex((category) => category?.value === article?.category?.value) |
| 142 | + ); |
| 143 | + }, [articles, categories]); |
| 144 | + |
25 | 145 | return (
|
26 | 146 | <div className={style.wrapper}>
|
27 | 147 | {articles && articles.length ? (
|
28 |
| - articles.map((article: IArticle) => { |
29 |
| - const categoryIndex = categories?.findIndex((category) => category?.value === article?.category?.value); |
30 |
| - return ( |
31 |
| - <div key={article.id} className={style.articleItem}> |
32 |
| - <div className={style.coverWrapper}> |
33 |
| - {article.cover ? ( |
34 |
| - <LazyLoad height={120} placeholder={<Spin />}> |
35 |
| - <div className={style.coverWrapper}> |
36 |
| - <Link href={`/article/[id]`} as={`/article/${article.id}`} scroll={false}> |
37 |
| - <img src={article.cover} alt="cover" /> |
38 |
| - </Link> |
39 |
| - </div> |
40 |
| - </LazyLoad> |
41 |
| - ) : ( |
42 |
| - <LogoSvg /> |
43 |
| - )} |
44 |
| - </div> |
45 |
| - <div className={style.articleWrapper}> |
46 |
| - <Link href={`/article/[id]`} as={`/article/${article.id}`} scroll={false}> |
47 |
| - <a aria-label={article.title} className={style.link}> |
48 |
| - <header> |
49 |
| - <div className={style.title} title={article.title}> |
50 |
| - {article.title} |
51 |
| - </div> |
52 |
| - <div className={style.info}> |
53 |
| - {article.category && categoryIndex >= 0 && ( |
54 |
| - <Link |
55 |
| - href={`/category/${article?.category?.value}`} |
56 |
| - as={`/category/${article?.category?.value}`} |
57 |
| - scroll={false} |
58 |
| - > |
59 |
| - <Tag className={style.antBadge} icon={<FolderOutlined />}> |
60 |
| - <span className={style.category}>{article.category?.label}</span> |
61 |
| - </Tag> |
62 |
| - </Link> |
63 |
| - )} |
64 |
| - </div> |
65 |
| - </header> |
66 |
| - <main className={style.desc} title={article.title}> |
67 |
| - <div className={style.contentWrapper}> |
68 |
| - <div className={style.desc} title={article.summary}> |
69 |
| - <span dangerouslySetInnerHTML={{ __html: article.summary }} /> |
70 |
| - </div> |
71 |
| - <div className={style.meta}> |
72 |
| - <div> |
73 |
| - <span> |
74 |
| - <HeartOutlined /> |
75 |
| - <span className={style.number}>{article.likes}</span> |
76 |
| - </span> |
77 |
| - <span className={style.separator}>·</span> |
78 |
| - <span> |
79 |
| - <EyeOutlined /> |
80 |
| - <span className={style.number}>{article.views}</span> |
81 |
| - </span> |
82 |
| - </div> |
83 |
| - <span className={style.time}> |
84 |
| - <HistoryOutlined /> |
85 |
| - <LocaleTime date={article.publishAt} format="yyyy-MM-dd" /> |
86 |
| - </span> |
87 |
| - </div> |
88 |
| - </div> |
89 |
| - </main> |
90 |
| - </a> |
91 |
| - </Link> |
92 |
| - </div> |
93 |
| - <span className={style.badge} /> |
94 |
| - </div> |
95 |
| - ); |
96 |
| - }) |
| 148 | + articles.map((article, index) => ( |
| 149 | + <ArticleCard |
| 150 | + key={article.id} |
| 151 | + article={article} |
| 152 | + categoryIndex={categoryIndices[index]} |
| 153 | + /> |
| 154 | + )) |
97 | 155 | ) : (
|
98 | 156 | <div className={'empty'}>{t('empty')}</div>
|
99 | 157 | )}
|
|
0 commit comments