blog-fs/pages/articles/[id].tsx

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-05-02 20:04:54 +08:00
import { FC } from "react";
import styles from "./article.module.css";
2021-07-03 09:55:27 +08:00
import { GetServerSideProps } from "next";
2021-05-02 20:04:54 +08:00
import { addApolloState, initializeApollo } from "../../commons/graphql/client";
import { ARTICLE } from "../../commons/graphql/queries";
import { Article } from "../../commons/graphql/generated";
2021-07-04 12:25:57 +08:00
import { formatRelative } from "date-fns";
2021-07-03 22:36:38 +08:00
import { zhCN } from "date-fns/locale";
2021-07-25 21:52:09 +08:00
import Head from "next/head";
2021-05-02 20:04:54 +08:00
interface Props {
article: Article;
}
const ArticleDetails: FC<Props> = ({ article }) => {
// const router = useRouter()
// const { data } = useQuery<{ article: Article }>(ARTICLE, {
// variables: router.query,
// });
2021-07-03 22:36:38 +08:00
2021-05-02 20:04:54 +08:00
return (
<main className={styles.articleDetails}>
2021-07-25 21:52:09 +08:00
<Head>
<title>{article.title} - Ivans Blog - IvanLi.cc</title>
</Head>
2021-05-02 20:25:25 +08:00
<article className={styles.article}>
<header>
<h1>{article.title}</h1>
2021-07-03 22:36:38 +08:00
<div className={styles.headerInfo}>
<address> Ivan Li </address>
<time>
{formatRelative(article.publishedAt, new Date(), {
locale: zhCN,
})}
</time>
</div>
2021-05-02 20:25:25 +08:00
</header>
2021-07-03 09:55:27 +08:00
<div dangerouslySetInnerHTML={{ __html: article.html }}></div>
<footer>
<div className={styles.articleEnd}>The End</div>
</footer>
2021-05-02 20:25:25 +08:00
</article>
2021-05-02 20:04:54 +08:00
</main>
);
};
export const getServerSideProps: GetServerSideProps = async ({ params }) => {
const apolloClient = initializeApollo();
const { data } = await apolloClient.query<{ article: Article }>({
query: ARTICLE,
variables: params,
});
return addApolloState(apolloClient, {
props: { article: data.article },
});
};
export default ArticleDetails;