Created
March 31, 2021 13:50
-
-
Save Daniel-Vinicius/f57a9eca29a6db4146df72fea8f9cdbd to your computer and use it in GitHub Desktop.
Comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useRef, useEffect } from 'react'; | |
import styles from './styles.module.scss'; | |
export type MappingType = | |
| 'pathname' | |
| 'url' | |
| 'title' | |
| 'og:title' | |
| 'issue-number' | |
| 'issue-term'; | |
export type Theme = | |
| 'github-light' | |
| 'github-dark' | |
| 'preferred-color-scheme' | |
| 'github-dark-orange' | |
| 'icy-dark' | |
| 'dark-blue' | |
| 'photon-dark'; | |
interface CommentProps { | |
repo: string; | |
issueMap: MappingType; | |
issueTerm?: string; | |
issueNumber?: number; | |
label?: string; | |
theme: Theme; | |
} | |
export default function Comments({ | |
issueTerm, | |
issueMap, | |
repo, | |
theme, | |
issueNumber, | |
label, | |
}: CommentProps): JSX.Element { | |
const [pending, setPending] = useState(true); | |
const reference = useRef<HTMLDivElement>(null); | |
if (issueMap === 'issue-term' && issueTerm === undefined) { | |
throw Error( | |
"Property 'issueTerm' must be provided with issueMap 'issue-term'" | |
); | |
} | |
if (issueMap === 'issue-number' && issueNumber === undefined) { | |
throw Error( | |
"Property 'issueNumber' must be provided with issueMap 'issue-number'" | |
); | |
} | |
useEffect(() => { | |
const scriptElement = document.createElement('script'); | |
scriptElement.src = 'https://utteranc.es/client.js'; | |
scriptElement.async = true; | |
scriptElement.defer = true; | |
scriptElement.setAttribute('repo', repo); | |
scriptElement.setAttribute('crossorigin', 'annonymous'); | |
scriptElement.setAttribute('theme', theme); | |
scriptElement.onload = () => setPending(false); | |
if (label) { | |
scriptElement.setAttribute('label', label); | |
} | |
if (issueMap === 'issue-number') { | |
scriptElement.setAttribute('issue-number', issueNumber.toString()); | |
} else if (issueMap === 'issue-term') { | |
scriptElement.setAttribute('issue-term', issueTerm); | |
} else { | |
scriptElement.setAttribute('issue-term', issueMap); | |
} | |
reference.current.appendChild(scriptElement); | |
}, [issueMap, issueNumber, issueTerm, label, repo, theme]); | |
return ( | |
<div className={styles.comments}> | |
<div className={styles.utterances} ref={reference}> | |
{pending && <p>Loading Comments...</p>} | |
</div> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment