Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning page-header global link items to map over defaults #739

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 66 additions & 49 deletions app/scripts/components/common/page-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@
}
`;

interface MenuLink {
title: string;
path: string;
onClick: () => void;
}

function PageHeader() {
const { isMediumDown } = useMediaQuery();

Expand All @@ -339,6 +345,65 @@
}
}, []);

const closeNavOnClick = useCallback(() => {
setGlobalNavRevealed(false);
}, []);

const defaultMenuLinks: MenuLink[] = [
{
title: 'Data Catalog',
path: DATASETS_PATH,
onClick: closeNavOnClick
},
{
title: 'Data Analysis',
path: ANALYSIS_PATH,
onClick: closeNavOnClick
},
{
title: getString('stories').other,
path: STORIES_PATH,
onClick: closeNavOnClick
},
];

function MenuLinks ({menuConfig}: {menuConfig: MenuLink[]}) {
return (
<GlobalMenu>
{
menuConfig.map((item, i) => (
<li key={`menu-item-${i}`}>

Check failure on line 375 in app/scripts/components/common/page-header.tsx

View workflow job for this annotation

GitHub Actions / lint

Do not use Array index in keys
<GlobalMenuLink
to={item.path}
onClick={item.onClick}
>
{item.title}
</GlobalMenuLink>
</li>
))
}
{/*
Temporarily add hub link through env variables.
This does not scale for the different instances, but it's a
quick fix for the GHG app.
*/}
{!!process.env.HUB_URL && !!process.env.HUB_NAME && (
<li>
<GlobalMenuLink
as='a'
target='_blank'
rel='noopener'
href={process.env.HUB_URL}
onClick={closeNavOnClick}
>
{process.env.HUB_NAME}
</GlobalMenuLink>
</li>
)}
</GlobalMenu>
);
}

useEffect(() => {
// Close global nav when media query changes.
// NOTE: isMediumDown is returning document.body's width, not the whole window width
Expand All @@ -348,10 +413,6 @@
if (!isMediumDown) setGlobalNavRevealed(false);
}, [isMediumDown]);

const closeNavOnClick = useCallback(() => {
setGlobalNavRevealed(false);
}, []);

return (
<PageHeaderSelf id={HEADER_ID}>
{globalNavRevealed && isMediumDown && <UnscrollableBody />}
Expand Down Expand Up @@ -402,51 +463,7 @@
<GlobalNavBodyInner>
<SectionsNavBlock>
<GlobalNavBlockTitle>Global</GlobalNavBlockTitle>
<GlobalMenu>
<li>
<GlobalMenuLink
to={DATASETS_PATH}
onClick={closeNavOnClick}
>
Data Catalog
</GlobalMenuLink>
</li>
<li>
<GlobalMenuLink
to={ANALYSIS_PATH}
onClick={closeNavOnClick}
>
Data Analysis
</GlobalMenuLink>
</li>
<li>
<GlobalMenuLink
to={STORIES_PATH}
onClick={closeNavOnClick}
>
{getString('stories').other}
</GlobalMenuLink>
</li>

{/*
Temporarily add hub link through env variables.
This does not scale for the different instances, but it's a
quick fix for the GHG app.
*/}
{!!process.env.HUB_URL && !!process.env.HUB_NAME && (
<li>
<GlobalMenuLink
as='a'
target='_blank'
rel='noopener'
href={process.env.HUB_URL}
onClick={closeNavOnClick}
>
{process.env.HUB_NAME}
</GlobalMenuLink>
</li>
)}
</GlobalMenu>
<MenuLinks menuConfig={defaultMenuLinks} />
</SectionsNavBlock>
<SectionsNavBlock>
<GlobalNavBlockTitle>Meta</GlobalNavBlockTitle>
Expand Down
Loading