2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-07-24 16:59:35 +08:00
|
|
|
import type { FC } from 'react';
|
|
|
|
import { useContext } from 'react';
|
2023-11-29 12:32:15 +08:00
|
|
|
import { Badge, Carousel, Skeleton, Typography } from 'antd';
|
|
|
|
import { createStyles, css, useTheme } from 'antd-style';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import useLocale from '../../../hooks/useLocale';
|
2022-12-19 11:42:41 +08:00
|
|
|
import SiteContext from '../../../theme/slots/SiteContext';
|
2023-11-29 12:32:15 +08:00
|
|
|
import type { Extra, Icon } from './util';
|
2023-07-24 16:59:35 +08:00
|
|
|
import { getCarouselStyle, useSiteData } from './util';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const useStyle = createStyles(({ token }) => {
|
|
|
|
const { carousel } = getCarouselStyle();
|
2023-11-29 12:32:15 +08:00
|
|
|
const [, lang] = useLocale();
|
|
|
|
console.log('lang', lang);
|
2022-11-09 12:28:04 +08:00
|
|
|
return {
|
2022-12-11 20:39:46 +08:00
|
|
|
itemBase: css`
|
2022-11-09 12:28:04 +08:00
|
|
|
display: flex;
|
2022-12-11 20:39:46 +08:00
|
|
|
flex: 1 1 0;
|
2022-11-09 12:28:04 +08:00
|
|
|
flex-direction: column;
|
|
|
|
align-items: stretch;
|
|
|
|
text-decoration: none;
|
2023-11-29 12:32:15 +08:00
|
|
|
background-color: ${token.colorBgContainer};
|
2022-12-11 20:39:46 +08:00
|
|
|
border: ${token.lineWidth}px solid ${token.colorBorderSecondary};
|
|
|
|
border-radius: ${token.borderRadiusLG}px;
|
|
|
|
transition: all ${token.motionDurationSlow};
|
|
|
|
padding-block: ${token.paddingMD}px;
|
|
|
|
padding-inline: ${token.paddingLG}px;
|
|
|
|
`,
|
2023-11-29 12:32:15 +08:00
|
|
|
minHeight:
|
|
|
|
lang === 'cn'
|
|
|
|
? css`
|
|
|
|
min-height: calc(152px - ${token.paddingMD}px * 2);
|
|
|
|
`
|
|
|
|
: css`
|
|
|
|
min-height: calc(176px - ${token.paddingMD}px * 2);
|
|
|
|
`,
|
2022-12-11 20:39:46 +08:00
|
|
|
cardItem: css`
|
2022-11-09 12:28:04 +08:00
|
|
|
&:hover {
|
|
|
|
box-shadow: ${token.boxShadowCard};
|
|
|
|
}
|
|
|
|
`,
|
2022-12-11 20:39:46 +08:00
|
|
|
sliderItem: css`
|
|
|
|
margin: 0 ${token.margin}px;
|
|
|
|
text-align: start;
|
|
|
|
`,
|
|
|
|
container: css`
|
|
|
|
display: flex;
|
|
|
|
max-width: 1208px;
|
|
|
|
margin-inline: auto;
|
|
|
|
box-sizing: border-box;
|
|
|
|
padding-inline: ${token.marginXXL}px;
|
|
|
|
column-gap: ${token.paddingMD * 2}px;
|
|
|
|
align-items: stretch;
|
|
|
|
text-align: start;
|
2023-11-28 15:31:36 +08:00
|
|
|
> * {
|
|
|
|
width: calc((100% - ${token.marginXXL * 2}px) / 3);
|
|
|
|
}
|
2022-12-11 20:39:46 +08:00
|
|
|
`,
|
|
|
|
carousel,
|
2022-11-09 12:28:04 +08:00
|
|
|
};
|
2023-07-20 19:27:33 +08:00
|
|
|
});
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-12-11 20:39:46 +08:00
|
|
|
interface RecommendItemProps {
|
|
|
|
extra: Extra;
|
|
|
|
index: number;
|
|
|
|
icons: Icon[];
|
2023-07-20 19:27:33 +08:00
|
|
|
className?: string;
|
2022-12-11 20:39:46 +08:00
|
|
|
}
|
2023-11-29 12:32:15 +08:00
|
|
|
const RecommendItem: React.FC<RecommendItemProps> = ({ extra, index, icons, className }) => {
|
2023-07-20 19:27:33 +08:00
|
|
|
const token = useTheme();
|
|
|
|
const { styles } = useStyle();
|
2022-12-11 20:39:46 +08:00
|
|
|
|
|
|
|
if (!extra) {
|
|
|
|
return <Skeleton key={index} />;
|
|
|
|
}
|
|
|
|
const icon = icons.find((i) => i.name === extra.source);
|
|
|
|
|
2023-11-28 15:31:36 +08:00
|
|
|
const card = (
|
2022-12-19 11:42:41 +08:00
|
|
|
<a
|
|
|
|
key={extra?.title}
|
|
|
|
href={extra.href}
|
|
|
|
target="_blank"
|
2023-11-29 12:32:15 +08:00
|
|
|
className={classNames(styles.itemBase, styles.minHeight, className)}
|
2022-12-19 11:42:41 +08:00
|
|
|
rel="noreferrer"
|
|
|
|
>
|
2022-12-11 20:39:46 +08:00
|
|
|
<Typography.Title level={5}>{extra?.title}</Typography.Title>
|
|
|
|
<Typography.Paragraph type="secondary" style={{ flex: 'auto' }}>
|
|
|
|
{extra.description}
|
|
|
|
</Typography.Paragraph>
|
2023-01-04 19:37:57 +08:00
|
|
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
2022-12-11 20:39:46 +08:00
|
|
|
<Typography.Text>{extra.date}</Typography.Text>
|
2022-12-19 11:42:41 +08:00
|
|
|
{icon && <img src={icon.href} style={{ height: token.fontSize }} alt="banner" />}
|
2022-12-11 20:39:46 +08:00
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
);
|
2023-11-28 15:31:36 +08:00
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
return (
|
|
|
|
<Badge.Ribbon text="HOT" color="red">
|
|
|
|
{card}
|
|
|
|
</Badge.Ribbon>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return card;
|
2022-12-11 20:39:46 +08:00
|
|
|
};
|
|
|
|
|
2023-07-24 16:59:35 +08:00
|
|
|
export const BannerRecommendsFallback: FC = () => {
|
|
|
|
const { isMobile } = useContext(SiteContext);
|
|
|
|
const { styles } = useStyle();
|
|
|
|
|
|
|
|
const list = Array(3).fill(1);
|
|
|
|
|
|
|
|
return isMobile ? (
|
|
|
|
<Carousel className={styles.carousel}>
|
2023-11-28 15:31:36 +08:00
|
|
|
{list.map((_, index) => (
|
2023-07-24 16:59:35 +08:00
|
|
|
<div key={index}>
|
|
|
|
<Skeleton active style={{ padding: '0 24px' }} />
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</Carousel>
|
|
|
|
) : (
|
|
|
|
<div className={styles.container}>
|
|
|
|
{list.map((_, index) => (
|
|
|
|
<Skeleton key={index} active />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-11-29 12:32:15 +08:00
|
|
|
const BannerRecommends: React.FC = () => {
|
2023-07-20 19:27:33 +08:00
|
|
|
const { styles } = useStyle();
|
2023-07-24 16:59:35 +08:00
|
|
|
const [, lang] = useLocale();
|
2022-12-11 20:39:46 +08:00
|
|
|
const { isMobile } = React.useContext(SiteContext);
|
2023-07-24 16:59:35 +08:00
|
|
|
const data = useSiteData();
|
|
|
|
const extras = data?.extras?.[lang];
|
2023-11-28 15:31:36 +08:00
|
|
|
const icons = data?.icons || [];
|
|
|
|
const first3 = !extras || extras.length === 0 ? Array(3).fill(null) : extras.slice(0, 3);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
return (
|
2022-12-11 20:39:46 +08:00
|
|
|
<div>
|
|
|
|
{isMobile ? (
|
2023-07-20 19:27:33 +08:00
|
|
|
<Carousel className={styles.carousel}>
|
2022-12-11 20:39:46 +08:00
|
|
|
{first3.map((extra, index) => (
|
|
|
|
<div key={index}>
|
|
|
|
<RecommendItem
|
|
|
|
extra={extra}
|
|
|
|
index={index}
|
|
|
|
icons={icons}
|
2023-07-20 19:27:33 +08:00
|
|
|
className={styles.sliderItem}
|
2022-12-11 20:39:46 +08:00
|
|
|
/>
|
2022-11-09 12:28:04 +08:00
|
|
|
</div>
|
2022-12-11 20:39:46 +08:00
|
|
|
))}
|
|
|
|
</Carousel>
|
|
|
|
) : (
|
2023-07-20 19:27:33 +08:00
|
|
|
<div className={styles.container}>
|
2022-12-11 20:39:46 +08:00
|
|
|
{first3.map((extra, index) => (
|
|
|
|
<RecommendItem
|
|
|
|
extra={extra}
|
|
|
|
index={index}
|
|
|
|
icons={icons}
|
2023-07-20 19:27:33 +08:00
|
|
|
className={styles.cardItem}
|
2022-12-11 20:39:46 +08:00
|
|
|
key={index}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-11-09 12:28:04 +08:00
|
|
|
</div>
|
|
|
|
);
|
2023-11-29 12:32:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default BannerRecommends;
|