Skip to content

Commit 599efc5

Browse files
committed
refactor: simplify GiscusComment component by removing theme handling and dynamic loading logic
1 parent 1854231 commit 599efc5

1 file changed

Lines changed: 25 additions & 119 deletions

File tree

src/components/GiscusComment.tsx

Lines changed: 25 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,32 @@
11
'use client'
22

3-
import { useTheme } from 'next-themes'
4-
import { usePathname } from 'next/navigation'
5-
import { useEffect, useState } from 'react'
3+
import { useEffect, useRef } from 'react'
64

75
export default function GiscusComment() {
8-
const { resolvedTheme } = useTheme()
9-
const pathname = usePathname()
10-
const [isGiscusLoaded, setIsGiscusLoaded] = useState(false)
6+
const ref = useRef<HTMLDivElement>(null)
117

12-
// 每当路径变化时,完全重载Giscus
138
useEffect(() => {
14-
// 如果已加载,则先移除现有的Giscus元素
15-
if (isGiscusLoaded) {
16-
const giscusContainer = document.querySelector('.giscus')
17-
if (giscusContainer) {
18-
// 清空容器内容
19-
giscusContainer.innerHTML = ''
20-
// 重新设置标志位
21-
setIsGiscusLoaded(false)
22-
23-
// 延迟重新加载Giscus脚本
24-
setTimeout(() => {
25-
loadGiscus()
26-
}, 300)
27-
}
28-
} else if (document.readyState === 'complete') {
29-
// 首次加载
30-
loadGiscus()
31-
}
32-
33-
// 加载Giscus脚本的函数
34-
function loadGiscus() {
35-
const script = document.createElement('script')
36-
script.src = 'https://giscus.app/client.js'
37-
script.setAttribute('data-repo', 'ScienceOL/docs')
38-
script.setAttribute('data-repo-id', 'R_kgDOOI9EFg')
39-
script.setAttribute('data-category', 'Announcements')
40-
script.setAttribute('data-category-id', 'DIC_kwDOOI9EFs4Cr9K0')
41-
script.setAttribute('data-mapping', 'pathname')
42-
script.setAttribute('data-strict', '0')
43-
script.setAttribute('data-reactions-enabled', '1')
44-
script.setAttribute('data-emit-metadata', '0')
45-
script.setAttribute('data-input-position', 'top')
46-
script.setAttribute(
47-
'data-theme',
48-
resolvedTheme === 'dark' ? 'transparent_dark' : 'light',
49-
)
50-
script.setAttribute('data-loading', 'lazy')
51-
script.setAttribute('data-lang', 'en')
52-
script.crossOrigin = 'anonymous'
53-
script.async = true
54-
55-
// 错误处理
56-
script.onerror = () => {
57-
console.error('Failed to load Giscus script')
58-
}
59-
60-
// 找到Giscus容器并添加脚本
61-
const giscusContainer = document.querySelector('.giscus')
62-
if (giscusContainer) {
63-
giscusContainer.appendChild(script)
64-
setIsGiscusLoaded(true)
65-
}
66-
}
67-
68-
// 监听DOM加载完成事件
69-
const handleDOMContentLoaded = () => {
70-
loadGiscus()
71-
}
72-
73-
if (document.readyState === 'loading') {
74-
document.addEventListener('DOMContentLoaded', handleDOMContentLoaded)
75-
}
76-
77-
return () => {
78-
document.removeEventListener('DOMContentLoaded', handleDOMContentLoaded)
79-
} // 依赖 resolvedTheme 是正确的
80-
}, [pathname, resolvedTheme]) // eslint-disable-line react-hooks/exhaustive-deps
81-
82-
// 监听主题变化,实时更新Giscus主题
83-
useEffect(() => {
84-
if (!isGiscusLoaded) return
85-
86-
const updateGiscusTheme = () => {
87-
const iframe = document.querySelector(
88-
'iframe.giscus-frame',
89-
) as HTMLIFrameElement
90-
if (iframe && iframe.contentWindow) {
91-
const theme = resolvedTheme === 'dark' ? 'transparent_dark' : 'light'
92-
iframe.contentWindow.postMessage(
93-
{ giscus: { setConfig: { theme } } },
94-
'https://giscus.app',
95-
)
96-
}
97-
}
98-
99-
// 监听Giscus消息
100-
const handleMessage = (event: MessageEvent) => {
101-
if (
102-
event.origin === 'https://giscus.app' &&
103-
event.data?.giscus?.discussion
104-
) {
105-
updateGiscusTheme()
106-
}
107-
}
108-
109-
window.addEventListener('message', handleMessage)
110-
111-
// 轮询尝试更新主题,确保iframe加载后应用正确主题
112-
let attempts = 0
113-
const interval = setInterval(() => {
114-
updateGiscusTheme()
115-
attempts++
116-
if (attempts >= 10) clearInterval(interval)
117-
}, 500)
118-
119-
return () => {
120-
window.removeEventListener('message', handleMessage)
121-
clearInterval(interval)
122-
}
123-
}, [resolvedTheme, isGiscusLoaded])
124-
125-
return <div className="giscus mt-8">{/* Giscus将在这里动态加载 */}</div>
9+
if (!ref.current || ref.current.hasChildNodes()) return
10+
11+
const script = document.createElement('script')
12+
script.src = 'https://giscus.app/client.js'
13+
script.setAttribute('data-repo', 'ScienceOL/docs')
14+
script.setAttribute('data-repo-id', 'R_kgDOOI9EFg')
15+
script.setAttribute('data-category', 'Announcements')
16+
script.setAttribute('data-category-id', 'DIC_kwDOOI9EFs4Cr9K0')
17+
script.setAttribute('data-mapping', 'pathname')
18+
script.setAttribute('data-strict', '0')
19+
script.setAttribute('data-reactions-enabled', '1')
20+
script.setAttribute('data-emit-metadata', '0')
21+
script.setAttribute('data-input-position', 'top')
22+
script.setAttribute('data-theme', 'preferred_color_scheme')
23+
script.setAttribute('data-lang', 'en')
24+
script.setAttribute('data-loading', 'lazy')
25+
script.crossOrigin = 'anonymous'
26+
script.async = true
27+
28+
ref.current.appendChild(script)
29+
}, [])
30+
31+
return <div ref={ref} className="giscus mt-8" />
12632
}

0 commit comments

Comments
 (0)