import { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import Image from 'next/image'
import { siteConfig } from '@/lib/config'
/**
* 评论插件
* @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 (
{counts
? (
{counts.type.like || 0}Likes
{counts.type.reply || 0}Replies
{(counts.type.repost || 0) + (counts.type.mention || 0)}
Mentions
)
: (
Failed to fetch Webmention counts
)
}
)
}
const Avatar = ({ author }) => (
)
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=${siteConfig('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 (
{distinctMentions.length > 0
? `Already ${distinctMentions.length} people liked, shared or talked about this article:`
: 'Be the first one to share this article!'}
{distinctMentions.map((reply) => (
))}
{replies && replies.length
? (
Replies
{replies.map((reply) => (
-
{reply.author.name}
{reply.content.text}
))}
)
: null}
)
}
const WebMentionBlock = ({ frontMatter }) => {
const router = useRouter()
const url = `https://${siteConfig('COMMENT_WEBMENTION_HOSTNAME')}${router.asPath}`
const tweet = `${frontMatter.title} by @${siteConfig('COMMENT_WEBMENTION_TWITTER_USERNAME')} ${url}`
return (
You can{' '}
{' '}
or{' '}
, the comments will show up here.
)
}
export default WebMentionBlock