Skip to content

Commit 7767a40

Browse files
committed
♻️ Add comment component.
1 parent 1b1cf19 commit 7767a40

File tree

2 files changed

+83
-65
lines changed

2 files changed

+83
-65
lines changed

components/Comments.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { fetchCusdisLang } from '@/lib/cusdisLang'
2+
import BLOG from '@/blog.config'
3+
import dynamic from 'next/dynamic'
4+
import { useRouter } from 'next/router'
5+
import 'gitalk/dist/gitalk.css'
6+
7+
const GitalkComponent = dynamic(
8+
() => {
9+
return import('gitalk/dist/gitalk-component')
10+
},
11+
{ ssr: false }
12+
)
13+
const UtterancesComponent = dynamic(
14+
() => {
15+
return import('@/components/Utterances')
16+
},
17+
{ ssr: false }
18+
)
19+
const CusdisComponent = dynamic(
20+
() => {
21+
return import('react-cusdis').then(m => m.ReactCusdis)
22+
},
23+
{ ssr: false }
24+
)
25+
26+
const Comments = ({ frontMatter }) => {
27+
const router = useRouter()
28+
return (
29+
<div>
30+
{BLOG.comment && BLOG.comment.provider === 'gitalk' && (
31+
<GitalkComponent
32+
options={{
33+
id: frontMatter.id,
34+
title: frontMatter.title,
35+
clientID: BLOG.comment.gitalkConfig.clientID,
36+
clientSecret: BLOG.comment.gitalkConfig.clientSecret,
37+
repo: BLOG.comment.gitalkConfig.repo,
38+
owner: BLOG.comment.gitalkConfig.owner,
39+
admin: BLOG.comment.gitalkConfig.admin,
40+
distractionFreeMode: BLOG.comment.gitalkConfig.distractionFreeMode
41+
}}
42+
/>
43+
)}
44+
{BLOG.comment && BLOG.comment.provider === 'utterances' && (
45+
<UtterancesComponent issueTerm={frontMatter.id} />
46+
)}
47+
{BLOG.comment && BLOG.comment.provider === 'cusdis' && (
48+
<CusdisComponent
49+
lang={fetchCusdisLang()}
50+
attrs={{
51+
host: BLOG.comment.cusdisConfig.host,
52+
appId: BLOG.comment.cusdisConfig.appId,
53+
pageId: frontMatter.id,
54+
pageTitle: frontMatter.title,
55+
pageUrl: BLOG.link + router.asPath,
56+
theme: BLOG.appearance
57+
}}
58+
/>
59+
)}
60+
</div>
61+
)
62+
}
63+
64+
export default Comments

layouts/layout.js

+19-65
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
11
import Image from 'next/image'
22
import Container from '@/components/Container'
33
import TagItem from '@/components/TagItem'
4-
import { useRouter } from 'next/router'
54
import { NotionRenderer, Equation, Code, CollectionRow } from 'react-notion-x'
65
import BLOG from '@/blog.config'
76
import formatDate from '@/lib/formatDate'
8-
import { fetchCusdisLang } from '@/lib/cusdisLang'
9-
import dynamic from 'next/dynamic'
10-
import 'gitalk/dist/gitalk.css'
117
import { useLocale } from '@/lib/locale'
12-
13-
const GitalkComponent = dynamic(
14-
() => {
15-
return import('gitalk/dist/gitalk-component')
16-
},
17-
{ ssr: false }
18-
)
19-
const UtterancesComponent = dynamic(
20-
() => {
21-
return import('@/components/Utterances')
22-
},
23-
{ ssr: false }
24-
)
25-
const CusdisComponent = dynamic(
26-
() => {
27-
return import('react-cusdis').then(m => m.ReactCusdis)
28-
},
29-
{ ssr: false }
30-
)
8+
import { useRouter } from 'next/router'
9+
import Comments from '@/components/Comments'
3110

3211
const mapPageUrl = id => {
3312
return 'https://www.notion.so/' + id.replace(/-/g, '')
@@ -101,49 +80,24 @@ const Layout = ({
10180
)}
10281
</article>
10382
<div className="flex justify-between font-medium text-gray-500 dark:text-gray-400">
104-
<button
105-
onClick={() => router.push(BLOG.path || '/')}
106-
className="mt-2 cursor-pointer hover:text-black dark:hover:text-gray-100"
107-
>
108-
{locale.POST.BACK}
109-
</button>
110-
<button
111-
onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })}
112-
className="mt-2 cursor-pointer hover:text-black dark:hover:text-gray-100"
113-
>
114-
{locale.POST.TOP}
115-
</button>
83+
<a>
84+
<button
85+
onClick={() => router.push(BLOG.path || '/')}
86+
className="mt-2 cursor-pointer hover:text-black dark:hover:text-gray-100"
87+
>
88+
{locale.POST.BACK}
89+
</button>
90+
</a>
91+
<a>
92+
<button
93+
onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })}
94+
className="mt-2 cursor-pointer hover:text-black dark:hover:text-gray-100"
95+
>
96+
{locale.POST.TOP}
97+
</button>
98+
</a>
11699
</div>
117-
{BLOG.comment && BLOG.comment.provider === 'gitalk' && (
118-
<GitalkComponent
119-
options={{
120-
id: frontMatter.id,
121-
title: frontMatter.title,
122-
clientID: BLOG.comment.gitalkConfig.clientID,
123-
clientSecret: BLOG.comment.gitalkConfig.clientSecret,
124-
repo: BLOG.comment.gitalkConfig.repo,
125-
owner: BLOG.comment.gitalkConfig.owner,
126-
admin: BLOG.comment.gitalkConfig.admin,
127-
distractionFreeMode: BLOG.comment.gitalkConfig.distractionFreeMode
128-
}}
129-
/>
130-
)}
131-
{BLOG.comment && BLOG.comment.provider === 'utterances' && (
132-
<UtterancesComponent issueTerm={frontMatter.id} />
133-
)}
134-
{BLOG.comment && BLOG.comment.provider === 'cusdis' && (
135-
<CusdisComponent
136-
attrs={{
137-
host: BLOG.comment.cusdisConfig.host,
138-
appId: BLOG.comment.cusdisConfig.appId,
139-
pageId: frontMatter.id,
140-
pageTitle: frontMatter.title,
141-
pageUrl: BLOG.link + router.asPath,
142-
theme: BLOG.appearance
143-
}}
144-
lang={fetchCusdisLang()}
145-
/>
146-
)}
100+
<Comments frontMatter={frontMatter} />
147101
</Container>
148102
)
149103
}

0 commit comments

Comments
 (0)