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

Markdown for landing pages #341

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"framer-motion": "^11.0.5",
"google-spreadsheet": "^3.2.0",
"jsforce": "^1.11.0",
"mdast-util-toc": "^7.0.0",
"next": "^14.1.0",
"next-mdx-remote": "^4.4.1",
"next-sitemap": "^2.5.7",
"papaparse": "^5.3.1",
"react": "^18.2.0",
Expand All @@ -38,14 +40,16 @@
"react-intersection-observer": "^8.33.1",
"react-markdown": "^8.0.0",
"redaxios": "^0.4.1",
"sharp": "^0.33.2"
"sharp": "^0.33.2",
"unist-util-visit": "^5.0.0"
},
"devDependencies": {
"@hookform/resolvers": "^3.1.0",
"@types/formidable": "^2.0.4",
"@types/google-spreadsheet": "^3.1.5",
"@types/jsforce": "^1.9.37",
"@types/mailchimp__mailchimp_marketing": "^3.0.3",
"@types/mdast": "^4.0.3",
"@types/node": "17.0.0",
"@types/papaparse": "^5.3.2",
"@types/react": "^18.2.55",
Expand Down
8 changes: 4 additions & 4 deletions src/components/Banners.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { FC } from 'react';
import { useRouter } from 'next/router';
import { Box, Link } from '@chakra-ui/react';

import { useCurrentPath } from '../hooks/useCurrentPath';
import { Banner } from './UI';

import { DATA_CHALLENGE_ROUND_URL, ZK_GRANTS_URL } from '../constants';

export const Banners: FC = () => {
const router = useRouter();
const path = useCurrentPath();

if (
!router.pathname.includes(ZK_GRANTS_URL) &&
!router.pathname.includes(DATA_CHALLENGE_ROUND_URL)
!path.includes(ZK_GRANTS_URL) &&
!path.includes(DATA_CHALLENGE_ROUND_URL)
) {
return (
<Banner>
Expand Down
12 changes: 7 additions & 5 deletions src/components/UI/common/ApplicantsSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ export const ApplicantsSidebar: FC<Props> = ({ sidebarLinks, sectionsInView }) =
/>

<Box pl={3}>
<Link as={NextLink} href={href} _hover={{ textDecoration: 'none' }}>
<PageText fontWeight={400} lineHeight='28px' color='brand.orange.100'>
{text}
</PageText>
</Link>
<NextLink href={href}>
<Link href={href} _hover={{ textDecoration: 'none' }}>
<PageText fontWeight={400} lineHeight='28px' color='brand.orange.100'>
{text}
</PageText>
</Link>
</NextLink>
</Box>
</Flex>
))}
Expand Down
5 changes: 5 additions & 0 deletions src/components/UI/md/MdLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Link, LinkProps } from '@chakra-ui/react';

export const MdLink = (props: LinkProps) => {
return <Link fontWeight={700} color='brand.orange.100' isExternal {...props} />;
};
50 changes: 50 additions & 0 deletions src/components/UI/md/MdSidebar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Box, Flex, Link } from '@chakra-ui/react';
import { FC, useMemo } from 'react';
import NextLink from 'next/link';

import { PageText } from '..';

import { SidebarLink } from '../../../types';
import { useActiveHash } from '../../../hooks/useActiveHash';

interface Props {
sidebarLinks: SidebarLink[];
}

export const MdSidebar: FC<Props> = ({ sidebarLinks }) => {
const hrefs = useMemo(() => sidebarLinks.map(({ href }) => href), [sidebarLinks]);
const activeHash = useActiveHash(hrefs, '0px');

return (
<Flex
display={{ base: 'none', lg: 'block' }}
bgGradient='linear(to-b, brand.sidebar.bgGradient.start 30%, brand.sidebar.bgGradient.end 100%)'
w='306px'
pt={12}
pl={14}
mt={-12}
top={0}
position='sticky'
alignSelf='flex-start'
h='45vh'
>
{sidebarLinks.map(({ text, href }, idx) => (
<Flex key={text} alignItems='center' mb={1}>
<Box
borderLeft='5px solid'
borderLeftColor={activeHash === href ? 'brand.accent' : 'transparent'}
h='18px'
/>

<Box pl={3}>
<Link as={NextLink} href={href} _hover={{ textDecoration: 'none' }}>
<PageText fontWeight={400} lineHeight='28px' color='brand.orange.100'>
{text}
</PageText>
</Link>
</Box>
</Flex>
))}
</Flex>
);
};
36 changes: 18 additions & 18 deletions src/components/layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Box, Container, ContainerProps } from '@chakra-ui/react';
import { FC, ReactNode } from 'react';
import { useRouter } from 'next/router';

import { Footer, FooterBackgroundImage, HomepageHero } from '../UI';
import { useCurrentPath } from '../../hooks/useCurrentPath';
import { Forms } from '../forms';

import {
Expand Down Expand Up @@ -44,10 +44,10 @@ import {
} from '../../constants';

export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
const router = useRouter();
const path = useCurrentPath();

const renderContent = (): ReactNode => {
if (router.pathname === HOME_URL) {
if (path === HOME_URL) {
return (
<>
<HomepageHero />
Expand All @@ -57,7 +57,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname.startsWith(APPLICANTS_URL)) {
if (path.startsWith(APPLICANTS_URL)) {
return (
<Box mt={-6}>
<main>
Expand All @@ -67,7 +67,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname.startsWith(ABOUT_URL)) {
if (path.startsWith(ABOUT_URL)) {
return (
<Box mt={-6}>
<main>
Expand All @@ -77,7 +77,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === DEVCON_GRANTS_URL) {
if (path === DEVCON_GRANTS_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -87,7 +87,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === ACADEMIC_GRANTS_2022_URL) {
if (path === ACADEMIC_GRANTS_2022_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -97,7 +97,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === ACADEMIC_GRANTS_2023_URL) {
if (path === ACADEMIC_GRANTS_2023_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -107,7 +107,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === ACADEMIC_GRANTS_URL) {
if (path === ACADEMIC_GRANTS_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -117,7 +117,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === MERGE_DATA_CHALLENGE_URL) {
if (path === MERGE_DATA_CHALLENGE_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -128,7 +128,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
}

// TODO: refactor these if conditions ????
if (router.pathname === SEMAPHORE_GRANT_URL) {
if (path === SEMAPHORE_GRANT_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -138,7 +138,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === LAYER_2_GRANTS_URL) {
if (path === LAYER_2_GRANTS_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -148,7 +148,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === ACCOUNT_ABSTRACTION_GRANTS_URL) {
if (path === ACCOUNT_ABSTRACTION_GRANTS_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -158,7 +158,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === RUN_A_NODE_GRANTS_URL) {
if (path === RUN_A_NODE_GRANTS_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -168,7 +168,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === DATA_COLLECTION_ROUND_URL) {
if (path === DATA_COLLECTION_ROUND_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -178,7 +178,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === ZK_GRANTS_URL) {
if (path === ZK_GRANTS_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -188,7 +188,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (router.pathname === DATA_CHALLENGE_ROUND_URL) {
if (path === DATA_CHALLENGE_ROUND_URL) {
return (
<Box mt={{ md: -10, lg: 0 }}>
<main>
Expand All @@ -198,7 +198,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
);
}

if (GRANTS_URLS.includes(router.pathname)) {
if (GRANTS_URLS.includes(path)) {
return (
<Box mt={-6}>
<main>
Expand Down
41 changes: 41 additions & 0 deletions src/components/layout/MdLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ReactNode } from 'react';
import { Box, Flex, Stack } from '@chakra-ui/react';

import { PageMetadata } from '../UI';
import { MdSidebar } from '../UI/md/MdSidebar';

import type { Frontmatter, ToCNodeEntry, TocNodeType } from '../../types';

type LayoutProps = {
children: ReactNode;
frontmatter: Frontmatter;
tocNodeItems: TocNodeType[];
};

export const MdLayout = ({ children, frontmatter, tocNodeItems }: LayoutProps) => {
const { metaTitle, metaDescription, metaImage } = frontmatter;

return (
<Stack>
<PageMetadata title={metaTitle} description={metaDescription} image={metaImage} />

<Box mx={{ md: 12 }} bg='white' position='relative' zIndex={1} mt={{ xl: 12 }}>
<Stack spacing={10} mb={8} px={{ base: 5, md: 0 }} py={{ base: 3, md: 12 }}>
<Flex>
<MdSidebar
sidebarLinks={tocNodeItems
.filter((item): item is ToCNodeEntry => !('items' in item))
.map(item => ({ text: item.title || '', href: item.url || '' }))}
/>

<Box w={{ lg: '70%' }} px={{ md: 20 }} pr={{ lg: 12 }}>
<Flex gap={6} direction='column'>
{children}
</Flex>
</Box>
</Flex>
</Stack>
</Box>
</Stack>
);
};
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,6 @@ export const DATA_CHALLENGE_GRANTS_PREVIEW_URL =

// Thank you and apply urls
export const GRANTS_URLS = [DEVCON_GRANTS_APPLY_URL, DEVCON_GRANTS_THANK_YOU_PAGE_URL];

// Markdown
export const MARKDOWN_CONTENT_PATH = 'src/content';
46 changes: 46 additions & 0 deletions src/hooks/useActiveHash.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may be a bug in this file. Noticing when I scroll past the Frequently asked questions header it jumps the sidenav active hash to the top one

Screen.Recording.2024-03-16.at.12.19.11.PM.mov

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch, will take a look!

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useEffect, useState } from 'react';

/**
* A hook to determine which section of the page is currently in the viewport.
* @param {*} itemIds Array of document ids to observe
* @param {*} rootMargin
* @returns id of the element currently in viewport
*/
export const useActiveHash = (itemIds: Array<string>, rootMargin = `0% 0% -80% 0%`): string => {
const [hashes, setHashes] = useState<Record<string, boolean>>(() =>
itemIds.reduce((acc, id, index) => ({ [id]: index === 0, ...acc }), {})
);

useEffect(() => {
const observer = new IntersectionObserver(
entries => {
entries.forEach(entry => {
const hash = `#${entry.target.id}`;
setHashes(hashes => ({ ...hashes, [hash]: entry.isIntersecting }));
});
},
{ rootMargin }
);

itemIds?.forEach(id => {
// Remove # from id. EX: #element-id -> element-id
const element = document.getElementById(id.replace('#', ''));
if (element !== null) {
observer.observe(element);
}
});

return () => {
itemIds?.forEach(id => {
const element = document.getElementById(id);
if (element !== null) {
observer.unobserve(element);
}
});
};
}, [itemIds, rootMargin]);

const firstActiveHash = itemIds.find(id => hashes[id]);

return firstActiveHash || itemIds[0];
};
9 changes: 9 additions & 0 deletions src/hooks/useCurrentPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useRouter } from 'next/router';

// Gets the current path from the slug param (if this is a dynamic route) or the
// router pathname (if this is a static route)
export const useCurrentPath = () => {
const { query, pathname } = useRouter();

return query.slug ? query.slug.toString() : pathname;
};
Loading