Skip to content

Commit f5068a1

Browse files
committed
feat: add category tag for article list
1 parent 6002c2d commit f5068a1

File tree

8 files changed

+40
-28
lines changed

8 files changed

+40
-28
lines changed

client/pages/article/index.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
display: inline-block;
1212
width: 100%;
1313
height: auto;
14+
max-height: 480px;
1415
border-radius: var(--border-radius);
1516
}
1617
}

client/src/components/ArticleCarousel/index.module.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
position: relative;
1212
display: flex;
1313
width: 100%;
14-
height: 380px;
14+
height: 460px;
1515
background-position: center;
1616
background-size: cover;
1717
background-repeat: no-repeat;
@@ -54,7 +54,7 @@
5454

5555
@media (max-width: 768px) {
5656
.articleItem {
57-
height: 170px;
57+
height: 180px;
5858
}
5959
}
6060
}

client/src/components/ArticleList/index.module.scss

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
&:hover {
2323
img {
2424
transform: scale(1.1);
25+
transition: all .2s ease-in;
26+
}
27+
}
28+
29+
.antBadge {
30+
margin-left: 4px;
31+
> * {
32+
margin-left: 4px;
2533
}
2634
}
2735

@@ -106,7 +114,7 @@
106114

107115
.time,
108116
.category {
109-
color: #8590a6;
117+
color: #fff;
110118
}
111119
}
112120

client/src/components/ArticleList/index.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import { EyeOutlined, HeartOutlined, HistoryOutlined } from '@ant-design/icons';
2-
import { Divider } from 'antd';
1+
import { EyeOutlined, FolderOutlined, HeartOutlined, HistoryOutlined } from '@ant-design/icons';
2+
import { Spin, Tag } from 'antd';
33
import { useTranslations } from 'next-intl';
44
import Link from 'next/link';
5-
import React from 'react';
5+
import React, { useContext } from 'react';
66
import LazyLoad from 'react-lazyload';
77
import LogoSvg from '../../assets/LogoSvg';
88

99
import { LocaleTime } from '@/components/LocaleTime';
1010

11+
import { GlobalContext } from '@/context/global';
12+
import { getColorFromNumber } from '@/utils';
1113
import style from './index.module.scss';
1214

1315
interface IProps {
@@ -18,16 +20,18 @@ interface IProps {
1820

1921
export const ArticleList: React.FC<IProps> = ({ articles = [] }) => {
2022
const t = useTranslations();
23+
const { categories } = useContext(GlobalContext);
2124

2225
return (
2326
<div className={style.wrapper}>
2427
{articles && articles.length ? (
25-
articles.map((article) => {
28+
articles.map((article: IArticle) => {
29+
const categoryIndex = categories?.findIndex((category) => category?.value === article?.category?.value);
2630
return (
2731
<div key={article.id} className={style.articleItem}>
2832
<div className={style.coverWrapper}>
2933
{article.cover ? (
30-
<LazyLoad height={120}>
34+
<LazyLoad height={120} placeholder={<Spin />}>
3135
<div className={style.coverWrapper}>
3236
<Link href={`/article/[id]`} as={`/article/${article.id}`} scroll={false}>
3337
<img src={article.cover} alt="cover" />
@@ -44,11 +48,11 @@ export const ArticleList: React.FC<IProps> = ({ articles = [] }) => {
4448
<header>
4549
<div className={style.title}>{article.title}</div>
4650
<div className={style.info}>
47-
{article.category && (
48-
<>
49-
<Divider type="vertical" />
51+
{article.category && categoryIndex >= 0 && (
52+
<Tag className={style.antBadge} color={getColorFromNumber(categoryIndex)}>
53+
<FolderOutlined />
5054
<span className={style.category}>{article.category?.label}</span>
51-
</>
55+
</Tag>
5256
)}
5357
</div>
5458
</header>

client/src/components/Header/index.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ $height: 64px;
138138
background: transparent;
139139
border: none;
140140
width: fit-content;
141+
display: flex;
141142
}
142143

143144
nav {

client/src/components/KnowledgeList/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { EyeOutlined, ShareAltOutlined } from '@ant-design/icons';
2-
import { Divider } from 'antd';
1+
import { EyeOutlined } from '@ant-design/icons';
2+
import { Divider, Spin } from 'antd';
33
import cls from 'classnames';
4-
import Link from 'next/link';
54
import { useTranslations } from 'next-intl';
5+
import Link from 'next/link';
66
import React from 'react';
77
import LazyLoad from 'react-lazyload';
88

@@ -46,7 +46,7 @@ export const KnowledgeList: React.FC<IProps> = ({ knowledges = [], small = false
4646
</div>
4747

4848
{knowledge.cover && (
49-
<LazyLoad height={120}>
49+
<LazyLoad height={120} placeholder={<Spin />}>
5050
<div className={style.coverWrapper}>
5151
<img src={knowledge.cover} alt="cover" />
5252
</div>

client/src/components/Tags/index.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
import { Tag } from 'antd';
2-
import cls from 'classnames';
3-
import Link from 'next/link';
4-
import { useRouter } from 'next/router';
52
import { useTranslations } from 'next-intl';
6-
import React from 'react';
3+
import Link from 'next/link';
74

5+
import { getColorFromNumber } from '@/utils';
86
import style from './index.module.scss';
97

108
export const Tags = ({ tags = [], needTitle = true, style: cssStyle = {} }) => {
11-
const router = useRouter();
129
const t = useTranslations();
13-
const { tag: routerTag } = router.query;
14-
15-
function getColorFromNumber(num) {
16-
const colors = ['#dc3545', '#17a2b8', '#00b74a', '#fc651f', '#6c757d', '#f5c800', '#808695'];
17-
const index = num % colors.length;
18-
return colors[index];
19-
}
2010

2111
return (
2212
<div className={style.wrapper} style={cssStyle}>

client/src/utils/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,11 @@ export const scrollToBottom = (el: HTMLElement) => {
146146

147147
el.scrollTo(0, currentScrollTop + (scrollHeight - currentScrollTop - clientHeight));
148148
};
149+
150+
151+
152+
export function getColorFromNumber(num) {
153+
const colors = ['#dc3545', '#17a2b8', '#00b74a', '#fc651f', '#6c757d', '#f5c800', '#808695'];
154+
const index = num % colors.length;
155+
return colors[index];
156+
}

0 commit comments

Comments
 (0)