1
+ // import React, { useEffect, useRef } from "react";
2
+
3
+ // const GiscusComments: React.FC = () => {
4
+ // const ref = useRef<HTMLDivElement>(null);
5
+
6
+ // useEffect(() => {
7
+ // if (!ref.current) return;
8
+ // // Prevent duplicate script injection
9
+ // if (ref.current.querySelector("iframe")) return;
10
+
11
+ // const script = document.createElement("script");
12
+ // script.src = "https://giscus.app/client.js";
13
+ // script.setAttribute("data-repo", "recodehive/Support");
14
+ // script.setAttribute("data-repo-id", "R_kgDOL9urew");
15
+ // script.setAttribute("data-category", "General");
16
+ // script.setAttribute("data-category-id", "DIC_kwDOL9ure84Cqizj");
17
+ // script.setAttribute("data-mapping", "og:title");
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.crossOrigin = "anonymous";
25
+ // script.async = true;
26
+ // ref.current.appendChild(script);
27
+ // }, []);
28
+
29
+ // return <div ref={ref} />;
30
+ // };
31
+
32
+ // export default GiscusComments;
33
+
34
+ // import React, { useEffect, useRef } from "react";
35
+ // import { useColorMode } from "@docusaurus/theme-common"
36
+
37
+ // const GiscusComments: React.FC = () => {
38
+ // const ref = useRef<HTMLDivElement>(null);
39
+ // const { colorMode } = useColorMode();
40
+ // console.log(colorMode)
41
+ // useEffect(() => {
42
+ // if (!ref.current) return;
43
+ // // Prevent duplicate script injection
44
+ // if (ref.current.querySelector("iframe")) return;
45
+
46
+ // const script = document.createElement("script");
47
+ // script.src = "https://giscus.app/client.js";
48
+ // script.setAttribute("data-repo", "recodehive/Support");
49
+ // script.setAttribute("data-repo-id", "R_kgDOL9urew");
50
+ // script.setAttribute("data-category", "General");
51
+ // script.setAttribute("data-category-id", "DIC_kwDOL9ure84Cqizj");
52
+ // script.setAttribute("data-mapping", "og:title");
53
+ // script.setAttribute("data-strict", "0");
54
+ // script.setAttribute("data-reactions-enabled", "1");
55
+ // script.setAttribute("data-emit-metadata", "0");
56
+ // script.setAttribute("data-input-position", "top");
57
+
58
+ // // Set the default theme to "light"
59
+ // script.setAttribute("data-theme", colorMode);
60
+
61
+ // script.setAttribute("data-lang", "en");
62
+ // script.crossOrigin = "anonymous";
63
+ // script.async = true;
64
+ // ref.current.appendChild(script);
65
+ // }, [colorMode]);
66
+
67
+ // return <div ref={ref} />;
68
+ // };
69
+
70
+ // export default GiscusComments;
71
+
1
72
import React , { useEffect , useRef } from "react" ;
73
+ import { useColorMode } from "@docusaurus/theme-common" ;
2
74
3
75
const GiscusComments : React . FC = ( ) => {
4
76
const ref = useRef < HTMLDivElement > ( null ) ;
77
+ const { colorMode } = useColorMode ( ) ; // colorMode is 'light' or 'dark'
5
78
79
+ // 1. This useEffect handles the initial script loading ONCE.
6
80
useEffect ( ( ) => {
7
- if ( ! ref . current ) return ;
8
- // Prevent duplicate script injection
9
- if ( ref . current . querySelector ( "iframe" ) ) return ;
81
+ // Exit if the ref isn't set or if the script is already there
82
+ if ( ! ref . current || ref . current . hasChildNodes ( ) ) {
83
+ return ;
84
+ }
10
85
11
86
const script = document . createElement ( "script" ) ;
12
87
script . src = "https://giscus.app/client.js" ;
88
+ script . async = true ;
89
+ script . crossOrigin = "anonymous" ;
13
90
script . setAttribute ( "data-repo" , "recodehive/Support" ) ;
14
91
script . setAttribute ( "data-repo-id" , "R_kgDOL9urew" ) ;
15
92
script . setAttribute ( "data-category" , "General" ) ;
@@ -19,12 +96,30 @@ const GiscusComments: React.FC = () => {
19
96
script . setAttribute ( "data-reactions-enabled" , "1" ) ;
20
97
script . setAttribute ( "data-emit-metadata" , "0" ) ;
21
98
script . setAttribute ( "data-input-position" , "top" ) ;
22
- script . setAttribute ( "data-theme" , "preferred_color_scheme" ) ;
23
99
script . setAttribute ( "data-lang" , "en" ) ;
24
- script . crossOrigin = "anonymous" ;
25
- script . async = true ;
100
+
101
+ // Use the initial colorMode from Docusaurus for the initial theme
102
+ script . setAttribute ( "data-theme" , colorMode ) ;
103
+
26
104
ref . current . appendChild ( script ) ;
27
- } , [ ] ) ;
105
+ } , [ ] ) ; // <-- Empty dependency array ensures this runs only once on mount.
106
+
107
+ // 2. This useEffect watches for changes in colorMode and sends a message to Giscus.
108
+ useEffect ( ( ) => {
109
+ const iframe = ref . current ?. querySelector < HTMLIFrameElement > (
110
+ "iframe.giscus-frame"
111
+ ) ;
112
+
113
+ if ( ! iframe ) {
114
+ return ;
115
+ }
116
+
117
+ // Send a message to the Giscus iframe to update its theme
118
+ iframe . contentWindow . postMessage (
119
+ { giscus : { setConfig : { theme : colorMode } } } ,
120
+ "https://giscus.app"
121
+ ) ;
122
+ } , [ colorMode ] ) ; // <-- This runs every time colorMode changes.
28
123
29
124
return < div ref = { ref } /> ;
30
125
} ;
0 commit comments