-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAN-15: Share blog articles on Discours with comment section (#128)
* add comments from the Community * prettier formatting * remove unused style * create component for Discourse comments * remove debug logs
- Loading branch information
1 parent
72dc754
commit f5d0103
Showing
3 changed files
with
87 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import { useColorMode } from "@docusaurus/theme-common"; | ||
import BrowserOnly from "@docusaurus/BrowserOnly"; | ||
|
||
const DiscourseComments = ({ embedUrl }) => { | ||
const { colorMode } = useColorMode(); | ||
const scriptAdded = useRef(false); | ||
|
||
// Function to send the theme to the iframe | ||
const sendThemeToIframe = () => { | ||
const discourseUrl = "https://community.reduct.store/"; | ||
const iframe = document.querySelector( | ||
'iframe[src*="community.reduct.store"]', | ||
); | ||
if (iframe && iframe.contentWindow) { | ||
iframe.contentWindow.postMessage({ theme: colorMode }, discourseUrl); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const discourseUrl = "https://community.reduct.store/"; | ||
|
||
if (!scriptAdded.current) { | ||
window.DiscourseEmbed = { | ||
discourseUrl, | ||
discourseEmbedUrl: embedUrl, | ||
}; | ||
|
||
// Add Discourse embed script if not already there | ||
const script = document.createElement("script"); | ||
script.type = "text/javascript"; | ||
script.async = true; | ||
script.src = `${discourseUrl}/javascripts/embed.js`; | ||
document.body.appendChild(script); | ||
|
||
// Mark the script as added (avoid duplicate script) | ||
scriptAdded.current = true; | ||
} | ||
|
||
// Send the theme on colorMode change | ||
sendThemeToIframe(); | ||
|
||
// Send the theme to the iframe when it loads | ||
const handleIframeLoad = (event) => { | ||
if (event.data === "iframe loaded") { | ||
sendThemeToIframe(); | ||
} | ||
}; | ||
window.addEventListener("message", handleIframeLoad); | ||
return () => { | ||
window.removeEventListener("message", handleIframeLoad); | ||
}; | ||
}, [embedUrl, colorMode]); | ||
|
||
return <div id="discourse-comments"></div>; | ||
}; | ||
|
||
const DiscourseCommentsWrapper = (props) => ( | ||
<BrowserOnly>{() => <DiscourseComments {...props} />}</BrowserOnly> | ||
); | ||
|
||
export default DiscourseCommentsWrapper; |
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,15 @@ | ||
.commentsContainer { | ||
text-align: center; | ||
margin-top: 2rem; | ||
padding: 1rem; | ||
background-color: var(--reduct--background-white); | ||
border: 1px solid #ddd; | ||
border-radius: 8px; | ||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); | ||
} | ||
|
||
.discourseTitle { | ||
font-size: 24px; | ||
font-weight: 600; | ||
margin-bottom: 20px; | ||
} |