|
| 1 | +'use client'; |
| 2 | +import { usePathname } from 'next/navigation'; |
| 3 | +import { useEffect, useRef } from 'react'; |
| 4 | + |
| 5 | +export default function Giscus() { |
| 6 | + const ref = useRef<HTMLDivElement>(null); |
| 7 | + const pathname = usePathname(); |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + if (!ref.current || ref.current.hasChildNodes()) return; |
| 11 | + |
| 12 | + const scriptElement = document.createElement('script'); |
| 13 | + scriptElement.src = 'https://giscus.app/client.js'; |
| 14 | + scriptElement.async = true; |
| 15 | + scriptElement.crossOrigin = 'anonymous'; |
| 16 | + |
| 17 | + scriptElement.setAttribute('data-repo', 'yuhwan-park/yuhwan-park.github.io'); |
| 18 | + scriptElement.setAttribute('data-repo-id', 'R_kgDONC_0rQ'); |
| 19 | + scriptElement.setAttribute('data-category', 'Announcements'); |
| 20 | + scriptElement.setAttribute('data-category-id', 'DIC_kwDONC_0rc4Cjwdo'); |
| 21 | + scriptElement.setAttribute('data-mapping', 'pathname'); |
| 22 | + scriptElement.setAttribute('data-strict', '0'); |
| 23 | + scriptElement.setAttribute('data-reactions-enabled', '1'); |
| 24 | + scriptElement.setAttribute('data-emit-metadata', '0'); |
| 25 | + scriptElement.setAttribute('data-input-position', 'bottom'); |
| 26 | + scriptElement.setAttribute('data-theme', 'light'); |
| 27 | + scriptElement.setAttribute('data-lang', 'en'); |
| 28 | + |
| 29 | + ref.current.appendChild(scriptElement); |
| 30 | + }, []); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const iframe = document.querySelector<HTMLIFrameElement>('iframe.giscus-frame'); |
| 34 | + iframe?.contentWindow?.postMessage( |
| 35 | + { giscus: { setConfig: { term: pathname } } }, |
| 36 | + 'https://giscus.app', |
| 37 | + ); |
| 38 | + }, [pathname]); |
| 39 | + |
| 40 | + return <section className="mt-32" ref={ref} />; |
| 41 | +} |
0 commit comments