-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from abdulrcs/feat/github-blog-integration
Version 2.0.0 - use github issues as posts & add comments feature!
- Loading branch information
Showing
7 changed files
with
297 additions
and
48 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,83 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
|
||
export default function useUtterances(commentNodeId, title) { | ||
const commentNodeRef = useRef() | ||
const [isCommentsLoading, setIsCommentsLoading] = useState(true) | ||
let intersectionObserver | ||
let mutationObserver | ||
let hasLoaded = false | ||
|
||
useEffect(() => { | ||
const injectScript = () => { | ||
if (hasLoaded) return | ||
|
||
const scriptParentNode = document.getElementById(commentNodeId) | ||
if (!scriptParentNode) return | ||
|
||
const script = document.createElement('script') | ||
script.src = 'https://utteranc.es/client.js' | ||
script.async = true | ||
script.setAttribute('repo', 'abdulrcs/abdulrahman.id') | ||
script.setAttribute('issue-term', title) | ||
script.setAttribute('theme', 'icy-dark') | ||
script.crossOrigin = 'anonymous' | ||
|
||
script.onload = () => setInterval(() => setIsCommentsLoading(false), 1000) | ||
|
||
scriptParentNode.appendChild(script) | ||
commentNodeRef.current = script | ||
|
||
hasLoaded = true | ||
} | ||
|
||
const observerCallback = (entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
injectScript() | ||
} | ||
}) | ||
} | ||
|
||
const mutationCallback = (mutationsList, observer) => { | ||
for (let mutation of mutationsList) { | ||
if (mutation.type === 'childList') { | ||
const scriptParentNode = document.getElementById(commentNodeId) | ||
if (scriptParentNode) { | ||
if (!intersectionObserver) { | ||
intersectionObserver = new IntersectionObserver(observerCallback) | ||
} | ||
intersectionObserver.observe(scriptParentNode) | ||
observer.disconnect() | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!mutationObserver) { | ||
mutationObserver = new MutationObserver(mutationCallback) | ||
} | ||
|
||
mutationObserver.observe(document.body, { childList: true, subtree: true }) | ||
|
||
// Cleanup on unmount | ||
return () => { | ||
if (intersectionObserver) { | ||
intersectionObserver.disconnect() | ||
} | ||
if (mutationObserver) { | ||
mutationObserver.disconnect() | ||
} | ||
const scriptParentNode = document.getElementById(commentNodeId) | ||
if ( | ||
scriptParentNode && | ||
commentNodeRef.current && | ||
commentNodeRef.current.parentNode === scriptParentNode | ||
) { | ||
scriptParentNode.removeChild(commentNodeRef.current) | ||
} | ||
hasLoaded = false | ||
} | ||
}, [commentNodeId, title]) | ||
|
||
return { isCommentsLoading } | ||
} |
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
module.exports = { | ||
images: { | ||
domains: ['imagizer.imageshack.com', 'i.imgur.com', 'images.unsplash.com', 'media.giphy.com', 'cdn-images-1.medium.com'], | ||
domains: [ | ||
'imagizer.imageshack.com', | ||
'i.imgur.com', | ||
'images.unsplash.com', | ||
'media.giphy.com', | ||
'cdn-images-1.medium.com', | ||
'github.com', | ||
], | ||
}, | ||
} |
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
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
Oops, something went wrong.