website/src/templates/notes.tsx

97 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-11-27 20:25:22 +00:00
/*
* This file handles rendering the pages that let users select and find notes.
* See note.tsx for rendering a single note.
*/
2020-11-28 00:39:21 +00:00
import React from 'react';
import Navbar from '../components/navbar';
import { graphql } from 'gatsby';
import style from './notes.module.css';
2020-11-27 19:07:44 +00:00
2020-11-27 21:12:09 +00:00
export default ({ data, pageContext }): JSX.Element => {
2020-11-27 19:07:44 +00:00
const posts = data.allMdx.edges;
const { currentPage, numPages, rootPath } = pageContext;
2020-11-27 19:19:33 +00:00
let prevPageLink = null;
2020-11-27 19:07:44 +00:00
if (currentPage === 2) {
2020-11-28 00:39:21 +00:00
prevPageLink = (
<a href={rootPath} className={style.prevPageLink}>
&lt;&lt;
</a>
);
2020-11-27 19:07:44 +00:00
} else if (currentPage !== 1) {
2020-11-28 00:39:21 +00:00
prevPageLink = (
<a
href={rootPath + '/' + (currentPage - 1)}
className={style.prevPageLink}
>
&lt;&lt;
</a>
);
2020-11-27 19:07:44 +00:00
}
2020-11-27 20:25:22 +00:00
const shouldDisplayNav = numPages !== 1;
2020-11-27 19:07:44 +00:00
return (
<>
<Navbar />
{posts.map(({ node }) => {
2020-11-28 00:39:21 +00:00
const frontmatter = node.frontmatter;
2020-11-27 19:07:44 +00:00
return (
<article key={frontmatter.title}>
<div className={style.noteTitle}>
<h3>
2020-11-28 00:39:21 +00:00
<a href={`${rootPath + '/' + frontmatter.path}`}>
{frontmatter.title}
</a>
2020-11-27 19:07:44 +00:00
</h3>
<time dateTime={frontmatter.date}>{frontmatter.date}</time>
</div>
<p>{node.excerpt}</p>
</article>
2020-11-28 00:39:21 +00:00
);
2020-11-27 19:07:44 +00:00
})}
2020-11-28 00:39:21 +00:00
{shouldDisplayNav && (
2020-11-27 20:25:22 +00:00
<p className={style.paginationNav}>
{prevPageLink}
{currentPage}
2020-11-28 00:39:21 +00:00
{currentPage !== numPages && (
2020-11-27 20:25:22 +00:00
<a
href={rootPath + '/' + (currentPage + 1)}
2020-11-28 00:39:21 +00:00
className={style.nextPageLink}
>
2020-11-27 20:25:22 +00:00
&gt;&gt;
</a>
2020-11-28 00:39:21 +00:00
)}
2020-11-27 20:25:22 +00:00
</p>
2020-11-28 00:39:21 +00:00
)}
2020-11-27 19:07:44 +00:00
</>
2020-11-28 00:39:21 +00:00
);
};
2020-11-27 19:07:44 +00:00
export const query = graphql`
query IndexQuery($skip: Int!, $limit: Int!) {
allMdx(
sort: { order: DESC, fields: [frontmatter___date] }
2020-11-28 00:39:21 +00:00
filter: {
fileAbsolutePath: { glob: "**/src/notes/*" }
frontmatter: { hidden: { ne: true } }
}
2020-11-27 19:07:44 +00:00
limit: $limit
skip: $skip
) {
edges {
node {
excerpt(pruneLength: 250)
frontmatter {
title
date(formatString: "YYYY-MM-DD")
path
}
id
}
}
}
}
2020-11-28 00:39:21 +00:00
`;