Skip to content

Commit 053b30e

Browse files
Dynamic text
1 parent 1670907 commit 053b30e

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

src/components/HomePage/Community/index.tsx

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
1-
import React, { type FC, useMemo } from "react";
1+
import React, { type FC, useEffect, useState } from "react";
22
import { useCommunityStatsContext } from "../../../context/CommunityStats";
3-
import "./LandingCommunity.css";
3+
import "./LandingCommunity.css";
44

55
type Props = {
66
className?: string;
77
};
88

99
export const LandingCommunity: FC<Props> = ({ className }) => {
10-
const { githubStarCountText } = useCommunityStatsContext();
10+
const { githubStarCountText } = useCommunityStatsContext();
11+
const [state, setState] = useState({
12+
stat0: 0,
13+
stat1: 0,
14+
stat2: 0,
15+
stat3: 0,
16+
});
1117

12-
const list = useMemo(() => {
13-
return [
14-
{
15-
stat: githubStarCountText,
16-
description:
17-
"Stars on our GitHub repository, showcasing our community's support and contribution.",
18-
href: "https://github.com/CodeHarborHub/codeharborhub.github.io",
19-
},
20-
{
21-
stat: "30+",
22-
description:
23-
"Live projects on CodeHarborHub, demonstrating the power of open-source collaboration.",
24-
},
25-
{
26-
stat: "100+",
27-
description:
28-
"Active developers engaged in our vibrant open-source community, collaborating and innovating.",
29-
},
30-
{
31-
stat: "600+",
32-
description:
33-
"Active learners in the CodeHarborHub community, continuously expanding their knowledge and skills.",
34-
},
35-
];
36-
}, [githubStarCountText]);
18+
const generateList = () => [
19+
{
20+
stat: Number(githubStarCountText)>0?githubStarCountText:83,
21+
description: "Stars on our GitHub repository, showcasing our community's support and contribution.",
22+
href: "https://github.com/CodeHarborHub/codeharborhub.github.io",
23+
},
24+
{
25+
stat: 30,
26+
description: "Live projects on CodeHarborHub, demonstrating the power of open-source collaboration.",
27+
},
28+
{
29+
stat: 100,
30+
description: "Active developers engaged in our vibrant open-source community, collaborating and innovating.",
31+
},
32+
{
33+
stat: 600,
34+
description: "Active learners in the CodeHarborHub community, continuously expanding their knowledge and skills.",
35+
},
36+
];
37+
38+
const handleDynamicChange = (target, index) => {
39+
let count = 0;
40+
const increment = target / 100;
41+
const interval = setInterval(() => {
42+
count += increment;
43+
setState(prev => ({ ...prev, [`stat${index}`]: Math.round(count) }));
44+
if (count >= target) {
45+
setState(prev => ({ ...prev, [`stat${index}`]: target }));
46+
clearInterval(interval);
47+
setTimeout(() => {
48+
handleDynamicChange(target, index);
49+
}, 3000);
50+
}
51+
}, 20);
52+
};
53+
54+
useEffect(() => {
55+
console.log('worked')
56+
const list = generateList();
57+
list.forEach((item, index) => {
58+
handleDynamicChange(item.stat, index);
59+
});
60+
},[githubStarCountText]);
3761

3862
return (
3963
<div className="landing-community">
@@ -49,19 +73,16 @@ export const LandingCommunity: FC<Props> = ({ className }) => {
4973

5074
<div className="landing-community__content">
5175
<div className="landing-community__stats">
52-
{list.map((item, index) => (
53-
<span
54-
key={index}
55-
className="landing-community__stat-item"
56-
>
76+
{generateList().map((item, index) => (
77+
<span key={index} className="landing-community__stat-item">
5778
<div className="landing-community__stat-value">
5879
<a
5980
href={item.href}
6081
target="_blank"
6182
rel="noopener noreferrer"
6283
key={index}
6384
>
64-
{item.stat}
85+
{`${state[`stat${index}`]}${index !== 0 ? "+" : ""}`}
6586
</a>
6687
</div>
6788
<div className="landing-community__stat-description">

0 commit comments

Comments
 (0)