Skip to content

Commit 6e3e969

Browse files
authored
Merge pull request #2884 from XRPLF/community-hydration-error
fix community page hydration error
2 parents ba907c3 + 8b927a4 commit 6e3e969

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

community/index.page.tsx

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import { useThemeHooks } from "@redocly/theme/core/hooks";
33
import moment from "moment";
44
import { Link } from "@redocly/theme/components/Link/Link";
@@ -17,29 +17,9 @@ const hackathon = require("../static/img/events/Hackathons.png");
1717
const conference = require("../static/img/events/Conference.png");
1818
const zone = require("../static/img/events/XRPLZone.png");
1919
const brazil = require("../static/img/events/event-meetup-brazil.png");
20-
const korea = require("../static/img/events/SouthKoreaMeetup.png");
2120
const infoSession2 = require("../static/img/events/xrpl-builder-office-hours-02.png");
2221
const infoSession3 = require("../static/img/events/xrpl-builder-office-hours-03.png");
2322
const infoSession4 = require("../static/img/events/xrpl-builder-office-hours-04.png");
24-
const findNearestUpcomingEvent = (events) => {
25-
let nearestEvent = null;
26-
let nearestDateDiff = Infinity;
27-
let index;
28-
events.forEach((event, i) => {
29-
const eventStartDate = moment(event.start_date, "MMMM DD, YYYY");
30-
const currentDate = moment();
31-
const diff = eventStartDate.diff(currentDate, "days");
32-
33-
if (diff >= 0 && diff < nearestDateDiff) {
34-
nearestEvent = event;
35-
nearestDateDiff = diff;
36-
index = i
37-
}
38-
});
39-
40-
return { nearestEvent, nearestDateDiff, index };
41-
};
42-
4323
const events = [
4424
{
4525
name: "New Horizon: Innovate Without Limits: New Horizons Await",
@@ -354,11 +334,46 @@ const events = [
354334
end_date: "November 22, 2024",
355335
},
356336
];
357-
const { nearestDateDiff, nearestEvent, index } = findNearestUpcomingEvent(events);
337+
338+
const findNearestUpcomingEvent = (events) => {
339+
let nearestEvent = null;
340+
let nearestDateDiff = Infinity;
341+
let index = 0;
342+
events.forEach((event, i) => {
343+
const eventStartDate = moment(event.start_date, "MMMM DD, YYYY");
344+
const currentDate = moment();
345+
const diff = eventStartDate.diff(currentDate, "days");
346+
347+
if (diff >= 0 && diff < nearestDateDiff) {
348+
nearestEvent = event;
349+
nearestDateDiff = diff;
350+
index = i;
351+
}
352+
});
353+
354+
return { nearestEvent, nearestDateDiff, index };
355+
};
356+
358357
const XrplEventsAndCarouselSection = ({ events }) => {
359358
const { useTranslate } = useThemeHooks();
360359
const { translate } = useTranslate();
361-
const [currentIndex, setCurrentIndex] = useState(index); // use nearest event's index as init state
360+
361+
// State variables for the nearest event
362+
const [nearestEventInfo, setNearestEventInfo] = useState({
363+
nearestEvent: null,
364+
nearestDateDiff: null,
365+
index: 0,
366+
});
367+
368+
// State for the current index in the carousel
369+
const [currentIndex, setCurrentIndex] = useState(0);
370+
371+
useEffect(() => {
372+
const { nearestEvent, nearestDateDiff, index } = findNearestUpcomingEvent(events);
373+
setNearestEventInfo({ nearestEvent, nearestDateDiff, index });
374+
setCurrentIndex(index);
375+
}, [events]);
376+
362377
const updateCarousel = () => {
363378
const prevEvent = events[currentIndex - 1] || null;
364379
const currentEvent = events[currentIndex];
@@ -373,17 +388,18 @@ const XrplEventsAndCarouselSection = ({ events }) => {
373388

374389
const handlePrev = () => {
375390
if (currentIndex > 0) {
376-
setCurrentIndex(currentIndex - 1);
391+
setCurrentIndex((prevIndex) => prevIndex - 1);
377392
}
378393
};
379394

380395
const handleNext = () => {
381396
if (currentIndex < events.length - 1) {
382-
setCurrentIndex(currentIndex + 1);
397+
setCurrentIndex((prevIndex) => prevIndex + 1);
383398
}
384399
};
385400

386401
const { prevEvent, currentEvent, nextEvent } = updateCarousel();
402+
387403
return (
388404
<>
389405
<section className="xrpl-events-section">
@@ -415,24 +431,24 @@ const XrplEventsAndCarouselSection = ({ events }) => {
415431
{translate("View All Events")}
416432
</Link>
417433
</div>
418-
{!!nearestEvent && (
434+
{!!nearestEventInfo.nearestEvent && (
419435
<div className="upcoming-event" id="upcoming-events-section">
420436
<p className="upcoming-label">{translate("UPCOMING EVENT")}</p>
421437
<div id="days-count" className="days-count">
422-
{nearestDateDiff}
438+
{nearestEventInfo.nearestDateDiff}
423439
</div>
424440
<div className="days-word">{translate("days")}</div>
425441
<div className="num-separator"></div>
426442
<h5 id="upcoming-event-name" className="event-name">
427-
{translate(nearestEvent?.name)}
443+
{translate(nearestEventInfo.nearestEvent?.name)}
428444
</h5>
429445
<p className="mb-2 event-details d-flex icon">
430446
<span className="icon-location"></span>
431-
<span id="upcoming-event-date">{nearestEvent?.date}</span>
447+
<span id="upcoming-event-date">{nearestEventInfo.nearestEvent?.date}</span>
432448
</p>
433449
<p className="event-location d-flex icon">
434450
<span className="icon-date" id="upcoming-event-location"></span>
435-
<span id="location-tag">{nearestEvent?.location}</span>
451+
<span id="location-tag">{nearestEventInfo.nearestEvent?.location}</span>
436452
</p>
437453
</div>
438454
)}

0 commit comments

Comments
 (0)