jonbio/layouts/PostLayout.tsx

167 lines
6.9 KiB
TypeScript
Raw Normal View History

2023-07-16 18:52:51 +08:00
import { ReactNode } from 'react'
2023-07-07 11:17:22 +08:00
import { CoreContent } from 'pliny/utils/contentlayer'
import type { Blog, Authors } from 'contentlayer/generated'
2023-07-16 18:52:51 +08:00
import Comments from '@/components/Comments'
2021-01-09 17:50:45 +08:00
import Link from '@/components/Link'
import PageTitle from '@/components/PageTitle'
2021-02-25 00:04:02 +01:00
import SectionContainer from '@/components/SectionContainer'
2021-06-23 18:45:21 +08:00
import Image from '@/components/Image'
2021-01-09 17:50:45 +08:00
import Tag from '@/components/Tag'
import siteMetadata from '@/data/siteMetadata'
2023-07-08 21:03:46 +08:00
import ScrollTopAndComment from '@/components/ScrollTopAndComment'
2021-01-09 17:50:45 +08:00
2023-07-07 11:17:22 +08:00
const editUrl = (path) => `${siteMetadata.siteRepo}/blob/master/data/${path}`
const discussUrl = (path) =>
`https://mobile.twitter.com/search?q=${encodeURIComponent(`${siteMetadata.siteUrl}/${path}`)}`
2021-01-09 17:50:45 +08:00
2023-07-07 11:17:22 +08:00
const postDateTemplate: Intl.DateTimeFormatOptions = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
}
interface LayoutProps {
content: CoreContent<Blog>
authorDetails: CoreContent<Authors>[]
next?: { path: string; title: string }
prev?: { path: string; title: string }
children: ReactNode
}
2021-01-09 17:50:45 +08:00
2023-07-07 11:17:22 +08:00
export default function PostLayout({ content, authorDetails, next, prev, children }: LayoutProps) {
const { filePath, path, slug, date, title, tags } = content
const basePath = path.split('/')[0]
2021-01-09 17:50:45 +08:00
return (
<SectionContainer>
2023-07-08 21:03:46 +08:00
<ScrollTopAndComment />
2021-02-04 08:50:28 +08:00
<article>
<div className="xl:divide-y xl:divide-gray-200 xl:dark:divide-gray-700">
<header className="pt-6 xl:pb-6">
<div className="space-y-1 text-center">
<dl className="space-y-10">
<div>
<dt className="sr-only">Published on</dt>
<dd className="text-base font-medium leading-6 text-gray-500 dark:text-gray-400">
2021-02-25 00:04:02 +01:00
<time dateTime={date}>
{new Date(date).toLocaleDateString(siteMetadata.locale, postDateTemplate)}
2021-02-25 00:04:02 +01:00
</time>
2021-02-04 08:50:28 +08:00
</dd>
</div>
</dl>
2021-01-09 17:50:45 +08:00
<div>
2021-02-04 08:50:28 +08:00
<PageTitle>{title}</PageTitle>
2021-01-09 17:50:45 +08:00
</div>
</div>
2021-02-04 08:50:28 +08:00
</header>
2023-07-07 11:17:22 +08:00
<div className="grid-rows-[auto_1fr] divide-y divide-gray-200 pb-8 dark:divide-gray-700 xl:grid xl:grid-cols-4 xl:gap-x-6 xl:divide-y-0">
<dl className="pb-10 pt-6 xl:border-b xl:border-gray-200 xl:pt-11 xl:dark:border-gray-700">
2021-02-04 08:50:28 +08:00
<dt className="sr-only">Authors</dt>
<dd>
2023-07-07 11:17:22 +08:00
<ul className="flex flex-wrap justify-center gap-4 sm:space-x-12 xl:block xl:space-x-0 xl:space-y-8">
2021-05-18 23:46:30 +08:00
{authorDetails.map((author) => (
<li className="flex items-center space-x-2" key={author.name}>
{author.avatar && (
2021-06-24 00:02:00 +08:00
<Image
2021-07-04 14:47:39 +08:00
src={author.avatar}
2023-07-07 11:17:22 +08:00
width={38}
height={38}
2021-06-24 00:02:00 +08:00
alt="avatar"
2022-01-31 23:58:10 +08:00
className="h-10 w-10 rounded-full"
2021-06-24 00:02:00 +08:00
/>
2021-05-18 23:46:30 +08:00
)}
2022-01-31 23:58:10 +08:00
<dl className="whitespace-nowrap text-sm font-medium leading-5">
2021-05-18 23:46:30 +08:00
<dt className="sr-only">Name</dt>
<dd className="text-gray-900 dark:text-gray-100">{author.name}</dd>
<dt className="sr-only">Twitter</dt>
<dd>
{author.twitter && (
<Link
href={author.twitter}
className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400"
2021-05-18 23:46:30 +08:00
>
{author.twitter.replace('https://twitter.com/', '@')}
</Link>
)}
</dd>
</dl>
</li>
))}
2021-02-04 08:50:28 +08:00
</ul>
</dd>
</dl>
2022-01-31 23:58:10 +08:00
<div className="divide-y divide-gray-200 dark:divide-gray-700 xl:col-span-3 xl:row-span-2 xl:pb-0">
2023-07-07 11:17:22 +08:00
<div className="prose max-w-none pb-8 pt-10 dark:prose-dark">{children}</div>
<div className="pb-6 pt-6 text-sm text-gray-700 dark:text-gray-300">
<Link href={discussUrl(path)} rel="nofollow">
Discuss on Twitter
2021-02-04 08:50:28 +08:00
</Link>
{``}
2023-07-07 11:17:22 +08:00
<Link href={editUrl(filePath)}>View on GitHub</Link>
2021-01-09 17:50:45 +08:00
</div>
2023-07-07 11:17:22 +08:00
{siteMetadata.comments && (
<div
className="pb-6 pt-6 text-center text-gray-700 dark:text-gray-300"
id="comment"
>
2023-07-16 18:52:51 +08:00
<Comments slug={slug} />
2023-07-07 11:17:22 +08:00
</div>
)}
2021-02-04 08:50:28 +08:00
</div>
<footer>
2022-01-31 23:58:10 +08:00
<div className="divide-gray-200 text-sm font-medium leading-5 dark:divide-gray-700 xl:col-start-1 xl:row-start-2 xl:divide-y">
2021-02-04 08:50:28 +08:00
{tags && (
<div className="py-4 xl:py-8">
2022-01-31 23:58:10 +08:00
<h2 className="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">
2021-02-04 08:50:28 +08:00
Tags
2021-01-09 17:50:45 +08:00
</h2>
2021-02-12 16:40:09 +08:00
<div className="flex flex-wrap">
2021-02-04 08:50:28 +08:00
{tags.map((tag) => (
<Tag key={tag} text={tag} />
))}
2021-01-09 17:50:45 +08:00
</div>
</div>
)}
2021-02-04 08:50:28 +08:00
{(next || prev) && (
<div className="flex justify-between py-4 xl:block xl:space-y-8 xl:py-8">
{prev && (
<div>
2022-01-31 23:58:10 +08:00
<h2 className="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">
2021-02-04 08:50:28 +08:00
Previous Article
</h2>
<div className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400">
2023-07-07 11:17:22 +08:00
<Link href={`/${prev.path}`}>{prev.title}</Link>
2021-02-04 08:50:28 +08:00
</div>
</div>
)}
{next && (
<div>
2022-01-31 23:58:10 +08:00
<h2 className="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">
2021-02-04 08:50:28 +08:00
Next Article
</h2>
<div className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400">
2023-07-07 11:17:22 +08:00
<Link href={`/${next.path}`}>{next.title}</Link>
2021-02-04 08:50:28 +08:00
</div>
</div>
)}
2021-01-09 17:50:45 +08:00
</div>
)}
</div>
2021-02-04 08:50:28 +08:00
<div className="pt-4 xl:pt-8">
<Link
2023-07-07 11:17:22 +08:00
href={`/${basePath}`}
className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400"
2023-07-07 11:17:22 +08:00
aria-label="Back to the blog"
2021-02-04 08:50:28 +08:00
>
&larr; Back to the blog
</Link>
</div>
</footer>
</div>
2021-01-09 17:50:45 +08:00
</div>
</article>
</SectionContainer>
)
}