-
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.
Merge pull request #254 from RyoJerryYu/feat/add-utterances-as-comment
feat: add utterances comment for articles
- Loading branch information
Showing
8 changed files
with
115 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import useScript from "@/hooks/use-script"; | ||
import { useRef } from "react"; | ||
|
||
export type CommentsProps = { | ||
theme?: string; | ||
"issue-term"?: string; | ||
repo?: string; | ||
label?: string; | ||
}; | ||
|
||
const Comments = (props?: CommentsProps) => { | ||
const comment = useRef(null); | ||
|
||
const status = useScript({ | ||
url: "https://utteranc.es/client.js", | ||
attributes: { | ||
theme: props?.theme ?? "github-light", | ||
"issue-term": props?.["issue-term"] ?? "pathname", | ||
repo: props?.repo ?? "RyoJerryYu/blog-next", | ||
label: props?.label ?? "comment", | ||
}, | ||
ref: comment, | ||
}); | ||
|
||
return ( | ||
<div className="w-full"> | ||
<div ref={comment} /> | ||
{status === "loading" && ( | ||
<div className="text-center">Loading comments...</div> | ||
)} | ||
{status === "error" && ( | ||
<div className="text-center text-red-500"> | ||
Error loading comments. Please try again later. | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { MutableRefObject, useEffect, useState } from "react"; | ||
|
||
export type UseScriptParams = { | ||
url: string; | ||
attributes?: Record<string, string>; | ||
ref: MutableRefObject<HTMLElement | null>; | ||
}; | ||
|
||
type ScriptStatus = "idle" | "loading" | "ready" | "error"; | ||
|
||
const useScript = (params: UseScriptParams) => { | ||
const { url, attributes, ref } = params; | ||
|
||
const [status, setStatus] = useState<ScriptStatus>(url ? "loading" : "idle"); | ||
const setAttributeStatus = (e: Event) => { | ||
setStatus(e.type === "load" ? "ready" : "error"); | ||
}; | ||
|
||
useEffect( | ||
() => { | ||
if (!url) { | ||
setStatus("idle"); | ||
return; | ||
} | ||
|
||
// prevent adding script tag if it already exists | ||
// now it's only for using utterances, | ||
// replacing or only not appending script tag won't work well | ||
let script: HTMLScriptElement; | ||
if (ref.current?.children?.length) { | ||
script = ref.current.children[0] as HTMLScriptElement; | ||
} else { | ||
script = document.createElement("script"); | ||
|
||
script.src = url; | ||
script.async = true; | ||
script.crossOrigin = "anonymous"; | ||
if (attributes) { | ||
Object.keys(attributes).forEach((key) => { | ||
script.setAttribute(key, attributes[key]); | ||
}); | ||
} | ||
ref.current?.appendChild(script); | ||
} | ||
|
||
script.addEventListener("load", setAttributeStatus); | ||
script.addEventListener("error", setAttributeStatus); | ||
|
||
return () => { | ||
if (script) { | ||
script.removeEventListener("load", setAttributeStatus); | ||
script.removeEventListener("error", setAttributeStatus); | ||
} | ||
}; | ||
}, | ||
// create script tag only once | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[url, attributes] | ||
); | ||
|
||
return status; | ||
}; | ||
|
||
export default useScript; |
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 was deleted.
Oops, something went wrong.
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