-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add blog comments via utterances
- Loading branch information
1 parent
9438b72
commit a83a5a6
Showing
5 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React from 'react' | ||
|
||
import styles from './styles.module.css' | ||
|
||
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 ReactUtterancesProps { | ||
repo: string | ||
issueMap: MappingType | ||
issueTerm?: string | ||
issueNumber?: number | ||
label?: string | ||
theme: Theme | ||
} | ||
|
||
interface ReactUtterancesState { | ||
pending: boolean | ||
} | ||
|
||
export class ReactUtterances extends React.Component< | ||
ReactUtterancesProps, | ||
ReactUtterancesState | ||
> { | ||
reference: React.RefObject<HTMLDivElement> | ||
|
||
constructor(props: ReactUtterancesProps) { | ||
super(props) | ||
|
||
if (props.issueMap === 'issue-term' && props.issueTerm === undefined) { | ||
throw Error( | ||
"Property 'issueTerm' must be provided with issueMap 'issue-term'" | ||
) | ||
} | ||
|
||
if (props.issueMap === 'issue-number' && props.issueNumber === undefined) { | ||
throw Error( | ||
"Property 'issueNumber' must be provided with issueMap 'issue-number'" | ||
) | ||
} | ||
|
||
this.reference = React.createRef<HTMLDivElement>() | ||
this.state = { pending: true } | ||
} | ||
|
||
componentDidMount(): void { | ||
const { repo, issueMap, issueTerm, issueNumber, label, theme } = this.props | ||
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 = () => this.setState({ pending: 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) | ||
} | ||
|
||
// TODO: Check current availability | ||
this.reference.current!.appendChild(scriptElement) | ||
} | ||
|
||
render(): React.ReactElement { | ||
return ( | ||
<div className={styles.comments}> | ||
<div className={styles.utterances} ref={this.reference}> | ||
{this.state.pending && <p>Loading Comments...</p>} | ||
</div> | ||
</div> | ||
) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,7 @@ html { | |
body { | ||
overflow-y: hidden; | ||
} | ||
|
||
.utterances { | ||
max-width: 100% !important; | ||
} |
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