-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
124 additions
and
38 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,117 @@ | ||
import React, { useEffect } from 'react' | ||
import { useTheme } from 'next-themes' | ||
import { Fragment, useEffect, useState } from 'react' | ||
|
||
interface CommentBoxProps { | ||
projectId: string | ||
} | ||
|
||
// Extend the window interface directly in this file | ||
declare global { | ||
interface Window { | ||
commentBox: (projectId: string) => void | ||
REMARK42: any | ||
remark_config: any | ||
} | ||
} | ||
|
||
const insertScript = ( | ||
id: string, | ||
parentElement: HTMLElement, | ||
theme: string, | ||
) => { | ||
const script = window.document.createElement('script') | ||
script.type = 'text/javascript' | ||
script.async = true | ||
script.id = id | ||
let url = window.location.origin + window.location.pathname | ||
if (url.endsWith('/')) { | ||
url = url.slice(0, -1) | ||
} | ||
script.innerHTML = ` | ||
var remark_config = { | ||
host: "${process.env.NEXT_PUBLIC_REMARK42_HOST}", | ||
site_id: "${process.env.NEXT_PUBLIC_REMARK42_SITE_ID}", | ||
url: "${url}", | ||
theme: "${theme}", | ||
no_footer: true, | ||
}; | ||
!function(e,n){for(var o=0;o<e.length;o++){var r=n.createElement("script"),c=".js",d=n.head||n.body;"noModule"in r?(r.type="module",c=".mjs"):r.async=!0,r.defer=!0,r.src=remark_config.host+"/web/"+e[o]+c,d.appendChild(r)}}(remark_config.components||["embed"],document);` | ||
parentElement.appendChild(script) | ||
} | ||
|
||
const removeScript = (id: string, parentElement: HTMLElement) => { | ||
const script = window.document.getElementById(id) | ||
if (script) { | ||
parentElement.removeChild(script) | ||
} | ||
} | ||
|
||
const CommentBox: React.FC<CommentBoxProps> = ({ projectId }) => { | ||
const manageScript = (theme: string) => { | ||
if (!window) { | ||
return () => {} | ||
} | ||
const { document } = window | ||
if (document.getElementById('remark42')) { | ||
insertScript('comments-script', document.body, theme) | ||
} | ||
return () => removeScript('comments-script', document.body) | ||
} | ||
|
||
const recreateRemark42Instance = () => { | ||
if (!window) { | ||
return | ||
} | ||
const remark42 = window.REMARK42 | ||
if (remark42) { | ||
remark42.destroy() | ||
remark42.createInstance(window.remark_config) | ||
} | ||
} | ||
|
||
const getPreferredTheme = (theme: string) => { | ||
if (theme === 'system') { | ||
const prefersDark = | ||
window.matchMedia && | ||
window.matchMedia('(prefers-color-scheme: dark)').matches | ||
return prefersDark ? 'dark' : 'light' | ||
} | ||
return theme | ||
} | ||
|
||
type CommentBoxProps = { | ||
location: string | ||
} | ||
|
||
export default function CommentBox({ location }: CommentBoxProps) { | ||
const { theme } = useTheme() | ||
const [preferredTheme, setPreferredTheme] = useState( | ||
getPreferredTheme(theme ?? 'system'), | ||
) | ||
|
||
useEffect(() => { | ||
// Create a script element | ||
const script = document.createElement('script') | ||
script.src = 'https://unpkg.com/commentbox.io/dist/commentBox.min.js' | ||
script.async = true | ||
|
||
// Append the script to the document body | ||
document.body.appendChild(script) | ||
|
||
// Initialize CommentBox.io after the script has loaded | ||
script.onload = () => { | ||
if (window.commentBox) { | ||
window.commentBox(projectId) | ||
const handleSystemThemeChange = (e: MediaQueryListEvent) => { | ||
if (theme === 'system') { | ||
setPreferredTheme(e.matches ? 'dark' : 'light') | ||
} | ||
} | ||
|
||
// Cleanup function to remove the script when the component unmounts | ||
return () => { | ||
if (script.parentNode) { | ||
script.parentNode.removeChild(script) | ||
if (theme === 'system') { | ||
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)') | ||
mediaQueryList.addEventListener('change', handleSystemThemeChange) | ||
setPreferredTheme(mediaQueryList.matches ? 'dark' : 'light') | ||
|
||
return () => { | ||
mediaQueryList.removeEventListener('change', handleSystemThemeChange) | ||
} | ||
} else { | ||
setPreferredTheme(theme ?? 'system') | ||
} | ||
}, [projectId]) | ||
}, [theme]) | ||
|
||
return <div className="commentbox"></div> | ||
} | ||
useEffect(() => { | ||
return manageScript(preferredTheme) | ||
}, [location, preferredTheme]) | ||
|
||
useEffect(recreateRemark42Instance, [location, preferredTheme]) | ||
|
||
export default CommentBox | ||
return ( | ||
<Fragment> | ||
<h2>Comments</h2> | ||
<div id="remark42" /> | ||
</Fragment> | ||
) | ||
} |