forked from tangly1024/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebMention.js
173 lines (162 loc) · 4.97 KB
/
WebMention.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import BLOG from '@/blog.config'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import Image from 'next/image'
/**
* 评论插件
* @param issueTerm
* @param layout
* @returns {JSX.Element}
* @constructor
*/
const WebmentionCount = ({ target }) => {
const initialCounts = {
count: 0,
type: {
like: 0,
mention: 0,
reply: 0,
repost: 0
}
}
const [counts, setCounts] = useState(initialCounts)
const fetchCounts = async (target) => {
const responseData = await fetch(`https://webmention.io/api/count.json?target=${encodeURIComponent(target)}`)
return (responseData.json) ? await responseData.json() : responseData
}
useEffect(() => {
async function getCounts() {
const responseCounts = await fetchCounts(target)
setCounts(responseCounts)
}
getCounts()
}, [target])
return (
<div className='webmention-counts'>
{counts
? (
<div className='counts'>
<span>
<span className='count'>{counts.type.like || 0}</span>Likes
</span>
<span>
<span className='count'>{counts.type.reply || 0}</span>Replies
</span>
<span>
<span className='count'>
{(counts.type.repost || 0) + (counts.type.mention || 0)}
</span>
Mentions
</span>
</div>
)
: (
<p>Failed to fetch Webmention counts</p>
)
}
</div>
)
}
const Avatar = ({ author }) => (
<a className='avatar-wrapper' href={author.url} key={author.name}>
<Image
className="avatar"
src={author.photo}
alt={author.name}
fill
sizes="(max-width: 768px) 100vw,
(max-width: 1200px) 50vw,
33vw"
/>
</a>
)
const WebmentionReplies = ({ target }) => {
const [mentions, setMentions] = useState([])
const fetchMentions = async (target) =>
fetch(
`https://webmention.io/api/mentions.jf2?per-page=500&target=${encodeURIComponent(target)}&token=${BLOG.COMMENT_WEBMENTION.TOKEN}`
).then((response) => (response.json ? response.json() : response))
useEffect(() => {
async function getMentions() {
const responseMentions = await fetchMentions(target)
if (responseMentions.children) {
setMentions(responseMentions.children)
}
}
getMentions()
}, [target])
const distinctMentions = [
...new Map(mentions.map((item) => [item.author.url, item])).values()
].sort((a, b) => new Date(a['wm-received']) - new Date(b['wm-received']))
const replies = mentions.filter(
(mention) => 'in-reply-to' in mention && 'content' in mention
)
return (
<div>
<p>
{distinctMentions.length > 0
? `Already ${distinctMentions.length} people liked, shared or talked about this article:`
: 'Be the first one to share this article!'}
</p>
<div className='webmention-avatars'>
{distinctMentions.map((reply) => (
<Avatar key={reply.author.name} author={reply.author} />
))}
</div>
{replies && replies.length
? (
<div className='webmention-replies'>
<h4>Replies</h4>
<ul className='replies'>
{replies.map((reply) => (
<li className='reply' key={reply.content.text}>
<div>
<Avatar key={reply.author.name} author={reply.author} />
</div>
<div className='text'>
<p className='reply-author-name'>{reply.author.name}</p>
<p className='reply-content'>{reply.content.text}</p>
</div>
</li>
))}
</ul>
</div>
)
: null}
</div>
)
}
const WebMentionBlock = ({ frontMatter }) => {
const router = useRouter()
const url = `https://${BLOG.COMMENT_WEBMENTION.HOSTNAME}${router.asPath}`
const tweet = `${frontMatter.title} by @${BLOG.COMMENT_WEBMENTION.TWITTER_USERNAME} ${url}`
return (
<div className='webmention-block'>
<h1 className='webmention-header'>
powered by <a href="https://webmention.io" target='_blank' rel='noreferrer'>WebMention.io</a>
</h1>
<div className='webmention-block-intro'>
You can{' '}
<a
target="_blank"
id='tweet-post-url'
href={`https://twitter.com/intent/tweet?text=${encodeURIComponent(tweet)}`}
rel="noopener noreferrer"
>tweet this post</a>{' '}
or{' '}
<a
target='_blank'
id='tweet-discuss-url'
href={`https://www.twitter.com/search?q=${url}`}
rel='noopener noreferrer'
>discuss it on Twitter</a>
, the comments will show up here.
</div>
<div className='webmention-info'>
<WebmentionCount target={url} />
<WebmentionReplies target={url} />
</div>
</div>
)
}
export default WebMentionBlock