2022-10-17 23:37:01 +08:00
|
|
|
import React, { useState } from 'react';
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
import siteMetadata from '@/data/siteMetadata';
|
|
|
|
import { PostFrontMatter } from 'types/PostFrontMatter';
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
interface Props {
|
2022-10-17 23:37:01 +08:00
|
|
|
frontMatter: PostFrontMatter;
|
2022-10-07 13:55:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Disqus = ({ frontMatter }: Props) => {
|
2022-10-17 23:37:01 +08:00
|
|
|
const [enableLoadComments, setEnabledLoadComments] = useState(true);
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
const COMMENTS_ID = 'disqus_thread';
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
function LoadComments() {
|
2022-10-17 23:37:01 +08:00
|
|
|
setEnabledLoadComments(false);
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
// @ts-ignore
|
2022-07-17 21:40:41 +08:00
|
|
|
window.disqus_config = function () {
|
2022-10-17 23:37:01 +08:00
|
|
|
this.page.url = window.location.href;
|
|
|
|
this.page.identifier = frontMatter.slug;
|
|
|
|
};
|
2022-10-07 13:55:39 +08:00
|
|
|
// @ts-ignore
|
2022-07-17 21:40:41 +08:00
|
|
|
if (window.DISQUS === undefined) {
|
2022-10-17 23:37:01 +08:00
|
|
|
const script = document.createElement('script');
|
|
|
|
script.src =
|
|
|
|
'https://' +
|
|
|
|
siteMetadata.comment.disqusConfig.shortname +
|
|
|
|
'.disqus.com/embed.js';
|
2022-10-07 13:55:39 +08:00
|
|
|
// @ts-ignore
|
2022-10-17 23:37:01 +08:00
|
|
|
script.setAttribute('data-timestamp', +new Date());
|
|
|
|
script.setAttribute('crossorigin', 'anonymous');
|
|
|
|
script.async = true;
|
|
|
|
document.body.appendChild(script);
|
2022-07-17 21:40:41 +08:00
|
|
|
} else {
|
2022-10-07 13:55:39 +08:00
|
|
|
// @ts-ignore
|
2022-10-17 23:37:01 +08:00
|
|
|
window.DISQUS.reset({ reload: true });
|
2022-07-17 21:40:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="pt-6 pb-6 text-center text-gray-700 dark:text-gray-300">
|
2022-10-17 23:37:01 +08:00
|
|
|
{enableLoadComments && (
|
|
|
|
<button onClick={LoadComments}>Load Comments</button>
|
|
|
|
)}
|
2022-07-17 21:40:41 +08:00
|
|
|
<div className="disqus-frame" id={COMMENTS_ID} />
|
|
|
|
</div>
|
2022-10-17 23:37:01 +08:00
|
|
|
);
|
|
|
|
};
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
export default Disqus;
|