|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { format } from "date-fns"; |
| 4 | +import { GetServerSideProps } from "next"; |
| 5 | +import { MDXRemote, MDXRemoteSerializeResult } from "next-mdx-remote"; |
| 6 | +import { serialize } from "next-mdx-remote/serialize"; |
| 7 | +import Link from "next/link"; |
| 8 | +import remarkGfm from "remark-gfm"; |
| 9 | + |
| 10 | +import { Meta } from "layout/Meta"; |
| 11 | +import { isAuthedFromCookieHeader } from "lib/words-auth"; |
| 12 | +import { Main } from "templates/Main"; |
| 13 | + |
| 14 | +import Container from "components/Container"; |
| 15 | +import { ExternalLink } from "components/ExternalLink"; |
| 16 | + |
| 17 | +import { getWordBySlug } from "utils/Words"; |
| 18 | +import { WordVisibility } from "utils/words-shared"; |
| 19 | + |
| 20 | +type Props = { |
| 21 | + slug: string; |
| 22 | + date: string; |
| 23 | + time: string; |
| 24 | + wordCount: number; |
| 25 | + visibility: WordVisibility; |
| 26 | + title: string | null; |
| 27 | + mdxSource: MDXRemoteSerializeResult; |
| 28 | +}; |
| 29 | + |
| 30 | +const WordPage = (props: Props) => { |
| 31 | + const dateLabel = props.date |
| 32 | + ? format(new Date(`${props.date}T00:00:00Z`), "EEEE d MMMM yyyy") |
| 33 | + : ""; |
| 34 | + const headingTitle = props.title || dateLabel; |
| 35 | + const metaTitle = props.title |
| 36 | + ? `${props.title} · /words` |
| 37 | + : `/words/${props.slug}`; |
| 38 | + |
| 39 | + return ( |
| 40 | + <Main |
| 41 | + className="text-lg py-4 md:py-8 lg:py-16" |
| 42 | + meta={<Meta title={metaTitle} description="A daily writing entry" />} |
| 43 | + > |
| 44 | + <Container maxWidth="prose"> |
| 45 | + <p className="mb-2"> |
| 46 | + <Link href="/words">← Words</Link> |
| 47 | + </p> |
| 48 | + <div className="text-sm opacity-60 mb-1"> |
| 49 | + {dateLabel} · {props.time} · {props.wordCount} words |
| 50 | + {props.visibility === "private" && ( |
| 51 | + <span className="ml-2 uppercase tracking-wide">private</span> |
| 52 | + )} |
| 53 | + </div> |
| 54 | + <h1 className="mb-6">{headingTitle}</h1> |
| 55 | + <div className="blog-post c_blog-content"> |
| 56 | + <MDXRemote {...props.mdxSource} components={{ a: ExternalLink }} /> |
| 57 | + </div> |
| 58 | + </Container> |
| 59 | + </Main> |
| 60 | + ); |
| 61 | +}; |
| 62 | + |
| 63 | +export const getServerSideProps: GetServerSideProps<Props> = async ({ |
| 64 | + params, |
| 65 | + req, |
| 66 | + res, |
| 67 | +}) => { |
| 68 | + const slug = String(params?.slug ?? ""); |
| 69 | + const entry = getWordBySlug(slug); |
| 70 | + if (!entry) { |
| 71 | + return { notFound: true }; |
| 72 | + } |
| 73 | + |
| 74 | + if (entry.visibility === "private") { |
| 75 | + const authed = await isAuthedFromCookieHeader(req.headers.cookie); |
| 76 | + if (!authed) { |
| 77 | + return { notFound: true }; |
| 78 | + } |
| 79 | + res.setHeader("Cache-Control", "no-store"); |
| 80 | + } |
| 81 | + |
| 82 | + const mdxSource = await serialize(entry.content, { |
| 83 | + mdxOptions: { remarkPlugins: [remarkGfm] }, |
| 84 | + }); |
| 85 | + |
| 86 | + return { |
| 87 | + props: { |
| 88 | + slug: entry.slug, |
| 89 | + date: entry.date, |
| 90 | + time: entry.time, |
| 91 | + wordCount: entry.wordCount, |
| 92 | + visibility: entry.visibility, |
| 93 | + title: entry.title, |
| 94 | + mdxSource, |
| 95 | + }, |
| 96 | + }; |
| 97 | +}; |
| 98 | + |
| 99 | +export default WordPage; |
0 commit comments