2023-02-09 22:17:31 +08:00
|
|
|
import React, { memo, useContext, useMemo, useRef, useState } from 'react';
|
2023-01-11 21:37:12 +08:00
|
|
|
import type { CSSProperties } from 'react';
|
2023-08-30 09:53:18 +08:00
|
|
|
import { SearchOutlined } from '@ant-design/icons';
|
|
|
|
import { Affix, Card, Col, Divider, Input, Row, Space, Tag, Typography } from 'antd';
|
2023-07-20 19:27:33 +08:00
|
|
|
import { createStyles, useTheme } from 'antd-style';
|
2023-10-08 13:44:02 +08:00
|
|
|
import { useIntl, useLocation, useSidebarData } from 'dumi';
|
2022-11-09 12:28:04 +08:00
|
|
|
import debounce from 'lodash/debounce';
|
2023-10-08 15:41:50 +08:00
|
|
|
import scrollIntoView from 'scroll-into-view-if-needed';
|
2023-08-30 09:53:18 +08:00
|
|
|
|
2023-10-08 15:41:50 +08:00
|
|
|
import Link from '../../common/Link';
|
2023-08-30 09:53:18 +08:00
|
|
|
import SiteContext from '../../slots/SiteContext';
|
2022-11-09 12:28:04 +08:00
|
|
|
import type { Component } from './ProComponentsList';
|
2022-11-30 11:05:41 +08:00
|
|
|
import proComponentsList from './ProComponentsList';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const useStyle = createStyles(({ token, css }) => ({
|
|
|
|
componentsOverviewGroupTitle: css`
|
2023-08-30 09:53:18 +08:00
|
|
|
margin-bottom: 24px !important;
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
componentsOverviewTitle: css`
|
2023-08-30 09:53:18 +08:00
|
|
|
overflow: hidden;
|
|
|
|
color: ${token.colorTextHeading};
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
componentsOverviewImg: css`
|
2023-08-30 09:53:18 +08:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
height: 152px;
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
componentsOverviewCard: css`
|
2023-08-30 09:53:18 +08:00
|
|
|
cursor: pointer;
|
|
|
|
transition: all 0.5s;
|
|
|
|
&:hover {
|
|
|
|
box-shadow:
|
|
|
|
0 6px 16px -8px #00000014,
|
|
|
|
0 9px 28px #0000000d,
|
|
|
|
0 12px 48px 16px #00000008;
|
|
|
|
}
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
componentsOverviewAffix: css`
|
2023-08-30 09:53:18 +08:00
|
|
|
display: flex;
|
|
|
|
transition: all 0.3s;
|
|
|
|
justify-content: space-between;
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
componentsOverviewSearch: css`
|
2023-08-30 09:53:18 +08:00
|
|
|
padding: 0;
|
|
|
|
.anticon-search {
|
|
|
|
color: ${token.colorTextDisabled};
|
|
|
|
}
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
componentsOverviewContent: css`
|
2023-08-30 09:53:18 +08:00
|
|
|
&:empty:after {
|
|
|
|
display: block;
|
|
|
|
padding: 16px 0 40px;
|
|
|
|
color: ${token.colorTextDisabled};
|
|
|
|
text-align: center;
|
|
|
|
border-bottom: 1px solid ${token.colorSplit};
|
|
|
|
content: 'Not Found';
|
|
|
|
}
|
|
|
|
`,
|
2023-07-20 19:27:33 +08:00
|
|
|
}));
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const onClickCard = (pathname: string) => {
|
|
|
|
if (window.gtag) {
|
|
|
|
window.gtag('event', '点击', {
|
|
|
|
event_category: '组件总览卡片',
|
|
|
|
event_label: pathname,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const reportSearch = debounce<(value: string) => void>((value) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
if (window.gtag) {
|
|
|
|
window.gtag('event', '搜索', {
|
|
|
|
event_category: '组件总览卡片',
|
|
|
|
event_label: value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
const { Title } = Typography;
|
|
|
|
|
|
|
|
const Overview: React.FC = () => {
|
2023-07-20 19:27:33 +08:00
|
|
|
const { styles } = useStyle();
|
2023-09-25 16:08:42 +08:00
|
|
|
const { theme } = useContext(SiteContext);
|
2023-01-11 21:37:12 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
const data = useSidebarData();
|
2023-01-11 21:37:12 +08:00
|
|
|
const [searchBarAffixed, setSearchBarAffixed] = useState<boolean>(false);
|
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const token = useTheme();
|
2023-09-25 16:08:42 +08:00
|
|
|
const { borderRadius, colorBgContainer, fontSizeXL, anchorTop } = token;
|
2023-01-11 21:37:12 +08:00
|
|
|
|
|
|
|
const affixedStyle: CSSProperties = {
|
|
|
|
boxShadow: 'rgba(50, 50, 93, 0.25) 0 6px 12px -2px, rgba(0, 0, 0, 0.3) 0 3px 7px -3px',
|
|
|
|
padding: 8,
|
|
|
|
margin: -8,
|
|
|
|
borderRadius,
|
|
|
|
backgroundColor: colorBgContainer,
|
|
|
|
};
|
|
|
|
|
2022-12-19 11:42:41 +08:00
|
|
|
const { search: urlSearch } = useLocation();
|
2022-11-09 12:28:04 +08:00
|
|
|
const { locale, formatMessage } = useIntl();
|
|
|
|
|
2023-03-07 14:39:55 +08:00
|
|
|
const [search, setSearch] = useState<string>(() => {
|
|
|
|
const params = new URLSearchParams(urlSearch);
|
|
|
|
if (params.has('s')) {
|
2023-10-08 13:44:02 +08:00
|
|
|
return params.get('s') || '';
|
2023-03-07 14:39:55 +08:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
});
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-01-11 21:37:12 +08:00
|
|
|
const sectionRef = useRef<HTMLElement>(null);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-11-19 13:47:33 +08:00
|
|
|
const onKeyDown: React.KeyboardEventHandler<HTMLInputElement> = (event) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
if (event.keyCode === 13 && search.trim().length) {
|
2023-08-30 09:53:18 +08:00
|
|
|
sectionRef.current?.querySelector<HTMLElement>(`.${styles.componentsOverviewCard}`)?.click();
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-30 11:05:41 +08:00
|
|
|
const groups = useMemo<{ title: string; children: Component[] }[]>(
|
|
|
|
() =>
|
|
|
|
data
|
2022-12-01 12:21:42 +08:00
|
|
|
.filter((item) => item?.title)
|
2022-11-30 11:05:41 +08:00
|
|
|
.map<{ title: string; children: Component[] }>((item) => ({
|
2023-10-08 13:44:02 +08:00
|
|
|
title: item?.title || '',
|
2022-11-19 13:47:33 +08:00
|
|
|
children: item.children.map((child) => ({
|
2023-10-08 13:44:02 +08:00
|
|
|
title: child.frontmatter?.title || '',
|
|
|
|
subtitle: child.frontmatter?.subtitle,
|
|
|
|
cover: child.frontmatter?.cover,
|
|
|
|
coverDark: child.frontmatter?.coverDark,
|
2022-11-09 12:28:04 +08:00
|
|
|
link: child.link,
|
|
|
|
})),
|
2022-11-30 11:05:41 +08:00
|
|
|
}))
|
|
|
|
.concat([
|
|
|
|
{
|
|
|
|
title: locale === 'zh-CN' ? '重型组件' : 'Others',
|
|
|
|
children:
|
|
|
|
locale === 'zh-CN'
|
|
|
|
? proComponentsList
|
|
|
|
: proComponentsList.map((component) => ({ ...component, subtitle: '' })),
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
[data, locale],
|
|
|
|
);
|
2022-11-09 12:28:04 +08:00
|
|
|
return (
|
|
|
|
<section className="markdown" ref={sectionRef}>
|
|
|
|
<Divider />
|
2023-10-08 13:44:02 +08:00
|
|
|
<Affix offsetTop={anchorTop} onChange={(affixed) => setSearchBarAffixed(!!affixed)}>
|
2023-07-20 19:27:33 +08:00
|
|
|
<div
|
|
|
|
className={styles.componentsOverviewAffix}
|
|
|
|
style={searchBarAffixed ? affixedStyle : {}}
|
|
|
|
>
|
2023-01-11 21:37:12 +08:00
|
|
|
<Input
|
|
|
|
autoFocus
|
|
|
|
value={search}
|
|
|
|
placeholder={formatMessage({ id: 'app.components.overview.search' })}
|
2023-07-20 19:27:33 +08:00
|
|
|
className={styles.componentsOverviewSearch}
|
2023-01-11 21:37:12 +08:00
|
|
|
onChange={(e) => {
|
|
|
|
setSearch(e.target.value);
|
|
|
|
reportSearch(e.target.value);
|
2023-10-08 15:41:50 +08:00
|
|
|
if (sectionRef.current) {
|
|
|
|
scrollIntoView(sectionRef.current, {
|
|
|
|
scrollMode: 'if-needed',
|
|
|
|
block: 'start',
|
|
|
|
behavior: (actions) =>
|
|
|
|
actions.forEach(({ el, top }) => {
|
|
|
|
el.scrollTop = top - 64;
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
2023-01-11 21:37:12 +08:00
|
|
|
}}
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
bordered={false}
|
|
|
|
suffix={<SearchOutlined />}
|
|
|
|
style={{ fontSize: searchBarAffixed ? fontSizeXL - 2 : fontSizeXL }}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Affix>
|
2022-11-09 12:28:04 +08:00
|
|
|
<Divider />
|
2023-07-20 19:27:33 +08:00
|
|
|
<div className={styles.componentsOverviewContent}>
|
2023-01-06 14:52:08 +08:00
|
|
|
{groups
|
|
|
|
.filter((i) => i?.title)
|
|
|
|
.map((group) => {
|
|
|
|
const components = group?.children?.filter(
|
|
|
|
(component) =>
|
|
|
|
!search.trim() ||
|
|
|
|
component?.title?.toLowerCase()?.includes(search.trim().toLowerCase()) ||
|
|
|
|
(component?.subtitle || '').toLowerCase().includes(search.trim().toLowerCase()),
|
|
|
|
);
|
|
|
|
return components?.length ? (
|
|
|
|
<div key={group?.title}>
|
2023-07-20 19:27:33 +08:00
|
|
|
<Title level={2} className={styles.componentsOverviewGroupTitle}>
|
2023-01-06 14:52:08 +08:00
|
|
|
<Space align="center">
|
|
|
|
<span style={{ fontSize: 24 }}>{group?.title}</span>
|
|
|
|
<Tag style={{ display: 'block' }}>{components.length}</Tag>
|
|
|
|
</Space>
|
|
|
|
</Title>
|
|
|
|
<Row gutter={[24, 24]}>
|
|
|
|
{components.map((component) => {
|
|
|
|
/** 是否是外链 */
|
|
|
|
const isExternalLink = component.link.startsWith('http');
|
|
|
|
let url = `${component.link}`;
|
|
|
|
|
|
|
|
if (!isExternalLink) {
|
|
|
|
url += urlSearch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col xs={24} sm={12} lg={8} xl={6} key={component?.title}>
|
2023-10-08 13:44:02 +08:00
|
|
|
<Link to={url}>
|
2023-01-06 14:52:08 +08:00
|
|
|
<Card
|
2023-10-08 13:44:02 +08:00
|
|
|
onClick={() => onClickCard(url)}
|
2023-01-06 14:52:08 +08:00
|
|
|
bodyStyle={{
|
|
|
|
backgroundRepeat: 'no-repeat',
|
|
|
|
backgroundPosition: 'bottom right',
|
|
|
|
backgroundImage: `url(${component?.tag || ''})`,
|
|
|
|
}}
|
|
|
|
size="small"
|
2023-07-20 19:27:33 +08:00
|
|
|
className={styles.componentsOverviewCard}
|
2023-01-06 14:52:08 +08:00
|
|
|
title={
|
2023-07-20 19:27:33 +08:00
|
|
|
<div className={styles.componentsOverviewTitle}>
|
2023-01-06 14:52:08 +08:00
|
|
|
{component?.title} {component.subtitle}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
2023-07-20 19:27:33 +08:00
|
|
|
<div className={styles.componentsOverviewImg}>
|
2023-02-09 22:17:31 +08:00
|
|
|
<img
|
|
|
|
src={
|
|
|
|
theme.includes('dark') && component.coverDark
|
|
|
|
? component.coverDark
|
|
|
|
: component.cover
|
|
|
|
}
|
|
|
|
alt={component?.title}
|
|
|
|
/>
|
2022-11-09 12:28:04 +08:00
|
|
|
</div>
|
2023-01-06 14:52:08 +08:00
|
|
|
</Card>
|
2023-10-08 13:44:02 +08:00
|
|
|
</Link>
|
2023-01-06 14:52:08 +08:00
|
|
|
</Col>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Row>
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
})}
|
|
|
|
</div>
|
2022-11-09 12:28:04 +08:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default memo(Overview);
|