diff --git a/src/components/CommunitySection/index.js b/src/components/CommunitySection/index.js index c3e8d06..93dba76 100644 --- a/src/components/CommunitySection/index.js +++ b/src/components/CommunitySection/index.js @@ -1,22 +1,80 @@ -import React, { useRef, useEffect } from 'react'; +import React, { useRef, useEffect, useState } from 'react'; import { useColorMode } from '@docusaurus/theme-common'; import clsx from 'clsx'; import styles from './styles.module.scss'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faDiscord } from '@fortawesome/free-brands-svg-icons'; -import { faCloudUpload } from '@fortawesome/free-solid-svg-icons' +import { faCloudUpload } from '@fortawesome/free-solid-svg-icons'; export default function CommunitySection() { const statsContainerRef = useRef(null); const adoptersListRef = useRef(null); + const [adopterCount, setAdopterCount] = useState(0); // New state for adopter count const { colorMode } = useColorMode(); + useEffect(() => { + /** Function to load adopters list and update count **/ + async function loadAdopters() { + const adoptersList = adoptersListRef.current; + + // Clear existing adopters + while (adoptersList.lastElementChild) { + adoptersList.removeChild(adoptersList.lastElementChild); + } + + // Load new adopters list based on color mode + try { + await eclipseFdnAdopters.getList({ + project_id: "iot.thingweb", + selector: ".scroller", + ul_classes: "adopters", + logo_white: colorMode === 'dark' ? true : false, + }); + } catch (error) { + console.error('Error loading adopters list', error); + } + + const adoptersContainer = document.querySelector('.adopters-container'); + const config = { childList: true }; + + /** + * Check for changes in the adopters list container to update the adopter count + * as well as duplicating the adopters list for the scroller effect + */ + const observer = new MutationObserver(() => { + const adopters = document.querySelector('.adopters'); + const adoptersList = Array.from(document.querySelectorAll('.adopters li')); + + setAdopterCount(adoptersList.length); // Update the adopter count + + adoptersList.forEach(adopter => { + const duplicatedAdopter = adopter.cloneNode(true); + duplicatedAdopter.setAttribute('aria-hidden', 'true'); + adopters.appendChild(duplicatedAdopter); + }); + + observer.disconnect(); + }) + + if (adoptersContainer) { + observer.observe(adoptersContainer, config); + } + } + + loadAdopters(); // Call function to load adopters on color mode change + }, [colorMode]); + useEffect(() => { /** Stats counter animation **/ const statsContainer = statsContainerRef.current; if (statsContainer) { const statsItems = document.querySelectorAll('#stats-number'); + //Making sure the animation will run again when the adopter count updates + statsItems.forEach(item => { + item.dataset.count = 'true'; + }); + let interval = 2000; const appearOptions = { threshold: 0, @@ -33,22 +91,23 @@ export default function CommunitySection() { let count = target.dataset.count; if (count === 'true') { - target.dataset.count = 'false' + target.dataset.count = 'false'; let startValue = 0; let endValue = parseInt(target.getAttribute('data-val')); let duration = Math.floor(interval / endValue); let counter = setInterval(() => { - startValue++; target.textContent = startValue; if (startValue === endValue) { - if (endValue >= 30) { target.textContent = endValue + "+"; } clearInterval(counter); } + + startValue++; + }, duration); } } @@ -59,66 +118,7 @@ export default function CommunitySection() { startCounter.observe(item); }); } - - /** Adopter scroller */ - const adoptersList = adoptersListRef.current; - - - if (colorMode === 'dark') { - while (adoptersList.lastElementChild) { - adoptersList.removeChild(adoptersList.lastElementChild); - } - try { - eclipseFdnAdopters.getList({ - project_id: "iot.thingweb", - selector: ".scroller", - ul_classes: "adopters", - logo_white: true, - }); - } - catch (error) { - console.error('Error loading adopters list', error); - } - } - else { - while (adoptersList.lastElementChild) { - adoptersList.removeChild(adoptersList.lastElementChild); - } - try { - eclipseFdnAdopters.getList({ - project_id: "iot.thingweb", - selector: ".scroller", - ul_classes: "adopters", - logo_white: false, - }); - } - catch (error) { - console.error('Error loading adopters list', error); - } - } - - const adoptersContainer = document.querySelector('.adopters-container'); - const config = { childList: true }; - - const observer = new MutationObserver(() => { - const adopters = document.querySelector('.adopters'); - const adoptersList = Array.from(document.querySelectorAll('.adopters li')); - - - adoptersList.forEach(adopter => { - const duplicatedAdopter = adopter.cloneNode(true); - duplicatedAdopter.setAttribute('aria-hidden', 'true'); - adopters.appendChild(duplicatedAdopter); - }); - - observer.disconnect(); - }) - - if (adoptersContainer) { - observer.observe(adoptersContainer, config); - } - }, [colorMode]); - + }, [adopterCount]); // Run this effect when adopter count updates return (
@@ -137,7 +137,7 @@ export default function CommunitySection() {
-

0

+

0

Adopters

@@ -162,8 +162,6 @@ export default function CommunitySection() { - -
); -} +} \ No newline at end of file