Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(announcement): 公告判空不展示 (#327)
Browse files Browse the repository at this point in the history
* fix(announcement): 公告判空不展示

* fix: 优化顶部公告展示逻辑,example 演示页不展示头部公告

* feat: 优化图表上新公告展示逻辑:大于4个新增 demo 或全部新增,则直接使用 category 作为代替
  • Loading branch information
visiky authored May 25, 2021
1 parent 7adddbb commit 13ff212
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 48 deletions.
12 changes: 5 additions & 7 deletions @antv/gatsby-theme-antv/site/components/TopBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ const TopBanner: React.FC<Props> = ({ announcement }) => {
return announcement ? announcement.en : '';
}, [announcement]);

return (
const content = get(announcement, i18n.language);

return content ? (
<Announcement
message={
<span className={styles.topBannerAnnouncements}>
{get(announcement, i18n.language)}
</span>
}
message={<span className={styles.topBannerAnnouncements}>{content}</span>}
bannerId={bannerId}
localStorageId={BANNER_LOCALSTORAGE_KEY}
style={{ borderRadius: 0, borderWidth: '1px 0' }}
/>
);
) : null;
};

export default TopBanner;
15 changes: 9 additions & 6 deletions @antv/gatsby-theme-antv/site/layouts/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ const Layout: React.FC<LayoutProps> = ({ children, location, footerProps }) => {
}
: {};

const isExamplePage =
location.pathname.includes('/examples/') &&
!location.pathname.endsWith('/gallery');

return (
<>
{rediectUrl && (
Expand Down Expand Up @@ -225,13 +229,12 @@ const Layout: React.FC<LayoutProps> = ({ children, location, footerProps }) => {
ecosystems={ecosystems}
{...logoProps}
/>
{/* 首页不展示 头部 banner */}
{!isHomePage && <TopBanner announcement={announcement} />}
{/* 首页和 example 演示页 不展示 头部 banner */}
{!isHomePage && !isExamplePage && (
<TopBanner announcement={announcement} />
)}
<main className={styles.main}>{children}</main>
{!(
location.pathname.includes('/examples/') &&
!location.pathname.endsWith('/gallery')
) && (
{!isExamplePage && (
<Footer
githubUrl={githubUrl}
rootDomain="https://antv.vision"
Expand Down
88 changes: 54 additions & 34 deletions @antv/gatsby-theme-antv/site/templates/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
MenuUnfoldOutlined,
VerticalAlignTopOutlined,
} from '@ant-design/icons';
import { groupBy, debounce } from 'lodash-es';
import { groupBy, debounce, each, filter, find, size } from 'lodash-es';
import { useTranslation } from 'react-i18next';
import Drawer from 'rc-drawer';
import { useMedia } from 'react-use';
Expand Down Expand Up @@ -183,11 +183,11 @@ export default function Template({
const [prev, next] = usePrevAndNext();

// 获取 demo 的 Category 分类
const getDemoCategory = (demo: any) => {
if (!demo.postFrontmatter || !demo.postFrontmatter[i18n.language]) {
const getDemoCategory = (demo: any, lang = i18n.language) => {
if (!demo.postFrontmatter || !demo.postFrontmatter[lang]) {
return 'OTHER';
}
return demo.postFrontmatter[i18n.language].title;
return demo.postFrontmatter[lang].title;
};

const allDemosInCategory = groupBy(allDemos || [], getDemoCategory);
Expand Down Expand Up @@ -332,22 +332,40 @@ export default function Template({
</Affix>
);

/** 获取上新的 demo. 直接用英文 title 作为 id */
const demosOnTheNew = useMemo((): Array<{
type NewDemo = {
title: string;
id: string;
category: string;
}> => {
return allDemos
.filter((demo) => demo.new)
.map((demo) => {
return {
title: demo.title[i18n.language],
id: demo.title.en,
category: getDemoCategory(demo),
};
});
}, [allDemos, i18n.language]);
};

/** 获取上新的 demo. 直接用英文 title 作为 id */
const demosOnTheNew = useMemo((): Array<NewDemo> => {
const result: NewDemo[] = [];

each(allDemosInCategory, (categoryDemos, category) => {
const newDemos = filter(categoryDemos, (d) => d.new);
// 大于4个新增 demo 或全部新增,则直接使用 category 作为代替
if (
size(newDemos) > 6 ||
(size(newDemos) && size(newDemos) === size(categoryDemos))
) {
result.push({
title: category,
id: getDemoCategory(newDemos[0], 'en'),
category,
});
} else {
each(newDemos, (demo) =>
result.push({
title: demo.title[i18n.language],
id: demo.title.en,
category: getDemoCategory(demo),
}),
);
}
});
return result;
}, [allDemosInCategory, allDemos, i18n.language]);

/** 公告 id */
const bannerId = useMemo(() => {
Expand All @@ -364,23 +382,25 @@ export default function Template({
}}
/>
{/* 是否展示上新公告 */}
<Announcement
message={
<div>
{t('上新啦,点击直达:')}
{demosOnTheNew.map((demo, idx) => (
<span key={demo.title}>
{idx !== 0 && ','}
<a href={`#category-${demo.category.replace(/\s/g, '')}`}>
{demo.title}
</a>
</span>
))}
</div>
}
localStorageId={BANNER_LOCALSTORAGE_KEY}
bannerId={bannerId}
/>
{demosOnTheNew.length && (
<Announcement
message={
<div>
{t('上新啦,点击直达:')}
{demosOnTheNew.map((demo, idx) => (
<span key={demo.title}>
{idx !== 0 && ','}
<a href={`#category-${demo.category.replace(/\s/g, '')}`}>
{demo.title}
</a>
</span>
))}
</div>
}
localStorageId={BANNER_LOCALSTORAGE_KEY}
bannerId={bannerId}
/>
)}
{Categories.map((category: string, i) => (
<div key={i}>
{category !== 'OTHER' && (
Expand Down
3 changes: 2 additions & 1 deletion example/examples/radar/basic/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"zh": "雷达图-tooltip 添加辅助线",
"en": "Rardar chart with tooltip crosshairs"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*r46-T43zmzwAAAAAAAAAAAAAARQnAQ"
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*r46-T43zmzwAAAAAAAAAAAAAARQnAQ",
"new": true
}
]
}

0 comments on commit 13ff212

Please sign in to comment.