tailwind-nextjs-blog/layouts/PostLayout.tsx

216 lines
7.9 KiB
TypeScript
Raw Normal View History

2022-10-17 23:37:01 +08:00
import Link from '@/components/Link';
import PageTitle from '@/components/PageTitle';
import SectionContainer from '@/components/SectionContainer';
import { BlogSEO } from '@/components/SEO';
import Image from '@/components/Image';
import Tag from '@/components/Tag';
import siteMetadata from '@/data/siteMetadata';
import Comments from '@/components/comments';
import ScrollTopAndComment from '@/components/ScrollTopAndComment';
import { ReactNode, useMemo } from 'react';
import { PostFrontMatter } from 'types/PostFrontMatter';
import { AuthorFrontMatter } from 'types/AuthorFrontMatter';
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
const editUrl = (fileName) =>
`${siteMetadata.siteRepo}/raw/master/data/blog/${fileName}`;
2022-07-17 21:40:41 +08:00
const discussUrl = (slug) =>
`https://mobile.twitter.com/search?q=${encodeURIComponent(
`${siteMetadata.siteUrl}/blog/${slug}`
2022-10-17 23:37:01 +08:00
)}`;
const Copyright = () => (
<a
rel="license"
href="http://creativecommons.org/licenses/by-sa/4.0/"
2022-10-17 23:37:01 +08:00
className="inline-flex self-center">
<Image
className="border-0"
alt="知识共享许可协议"
width={88}
height={15}
src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png"
/>
</a>
2022-10-17 23:37:01 +08:00
);
2022-07-17 21:40:41 +08:00
const postDateTemplate: Intl.DateTimeFormatOptions = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
2022-10-17 23:37:01 +08:00
};
2022-07-17 21:40:41 +08:00
interface Props {
2022-10-17 23:37:01 +08:00
frontMatter: PostFrontMatter;
authorDetails: AuthorFrontMatter[];
next?: { slug: string; title: string };
prev?: { slug: string; title: string };
children: ReactNode;
}
2022-10-17 23:37:01 +08:00
export default function PostLayout({
frontMatter,
authorDetails,
next,
prev,
children,
}: Props) {
const { slug, fileName, date, title, images, tags } = frontMatter;
2022-07-17 21:40:41 +08:00
const headerStyles = useMemo(
() =>
images?.[0]
? {
backgroundImage: `url(${images[0]})`,
}
: {},
[images]
2022-10-17 23:37:01 +08:00
);
2022-07-17 21:40:41 +08:00
return (
<SectionContainer>
<BlogSEO
url={`${siteMetadata.siteUrl}/blog/${slug}`}
authorDetails={authorDetails}
{...frontMatter}
/>
<ScrollTopAndComment />
<article>
<div className="xl:divide-y xl:divide-gray-200 xl:dark:divide-gray-700">
<header className="relative h-48 pt-6 xl:pb-6">
{images?.[0] && (
<Image
alt="background"
layout="fill"
objectFit="cover"
src={images[0]}
style={headerStyles}
className="blur-xs -z-10 opacity-50 bg-blend-soft-light"
/>
)}
<div className="space-y-5 text-center">
2022-07-17 21:40:41 +08:00
<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">
<time dateTime={date}>
2022-10-17 23:37:01 +08:00
{new Date(date).toLocaleDateString(
siteMetadata.locale,
postDateTemplate
)}
2022-07-17 21:40:41 +08:00
</time>
</dd>
</div>
</dl>
<div>
<PageTitle>{title}</PageTitle>
</div>
</div>
</header>
<div
className="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"
2022-10-17 23:37:01 +08:00
style={{ gridTemplateRows: 'auto 1fr' }}>
2022-07-17 21:40:41 +08:00
<dl className="pt-6 pb-10 xl:border-b xl:border-gray-200 xl:pt-11 xl:dark:border-gray-700">
<dt className="sr-only">Authors</dt>
<dd>
<ul className="flex justify-center space-x-8 sm:space-x-12 xl:block xl:space-x-0 xl:space-y-8">
{authorDetails.map((author) => (
2022-10-17 23:37:01 +08:00
<li
className="flex items-center space-x-2"
key={author.name}>
2022-07-17 21:40:41 +08:00
{author.avatar && (
<Image
src={author.avatar}
width={38}
height={38}
2022-07-17 21:40:41 +08:00
alt="avatar"
className="h-10 w-10 rounded-full"
/>
)}
<dl className="whitespace-nowrap text-sm font-medium leading-5">
<dt className="sr-only">Name</dt>
2022-10-17 23:37:01 +08:00
<dd className="text-gray-900 dark:text-gray-100">
{author.name}
</dd>
2022-07-17 21:40:41 +08:00
<dt className="sr-only">Twitter</dt>
<dd>
{author.twitter && (
<Link
href={author.twitter}
2022-10-17 23:37:01 +08:00
className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400">
{author.twitter.replace(
'https://twitter.com/',
'@'
)}
2022-07-17 21:40:41 +08:00
</Link>
)}
</dd>
</dl>
</li>
))}
</ul>
</dd>
</dl>
<div className="divide-y divide-gray-200 dark:divide-gray-700 xl:col-span-3 xl:row-span-2 xl:pb-0">
2022-10-17 23:37:01 +08:00
<div className="prose max-w-none pt-10 pb-8 dark:prose-dark">
{children}
</div>
<div className="flex items-center gap-4 pt-6 pb-6 text-sm text-gray-700 dark:text-gray-300">
<Copyright />
<Link href={editUrl(fileName)}>{'View source'}</Link>
2022-07-17 21:40:41 +08:00
</div>
<Comments frontMatter={frontMatter} />
</div>
<footer>
<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">
{tags && (
<div className="py-4 xl:py-8">
<h2 className="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">
Tags
</h2>
<div className="flex flex-wrap">
{tags.map((tag) => (
<Tag key={tag} text={tag} />
))}
</div>
</div>
)}
{(next || prev) && (
<div className="flex justify-between py-4 xl:block xl:space-y-8 xl:py-8">
{prev && (
<div>
<h2 className="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">
Previous Article
</h2>
<div className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400">
<Link href={`/blog/${prev.slug}`}>{prev.title}</Link>
</div>
</div>
)}
{next && (
<div>
<h2 className="text-xs uppercase tracking-wide text-gray-500 dark:text-gray-400">
Next Article
</h2>
<div className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400">
<Link href={`/blog/${next.slug}`}>{next.title}</Link>
</div>
</div>
)}
</div>
)}
</div>
<div className="pt-4 xl:pt-8">
<Link
href="/blog"
2022-10-17 23:37:01 +08:00
className="text-primary-500 hover:text-primary-600 dark:hover:text-primary-400">
2022-07-17 21:40:41 +08:00
&larr; Back to the blog
</Link>
</div>
</footer>
</div>
</div>
</article>
</SectionContainer>
2022-10-17 23:37:01 +08:00
);
2022-07-17 21:40:41 +08:00
}