1
- import React , { type FC , useMemo } from "react" ;
1
+ import React , { type FC , useEffect , useState } from "react" ;
2
2
import { useCommunityStatsContext } from "../../../context/CommunityStats" ;
3
- import "./LandingCommunity.css" ;
3
+ import "./LandingCommunity.css" ;
4
4
5
5
type Props = {
6
6
className ?: string ;
7
7
} ;
8
8
9
9
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
+ } ) ;
11
17
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 ] ) ;
37
61
38
62
return (
39
63
< div className = "landing-community" >
@@ -49,19 +73,16 @@ export const LandingCommunity: FC<Props> = ({ className }) => {
49
73
50
74
< div className = "landing-community__content" >
51
75
< 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" >
57
78
< div className = "landing-community__stat-value" >
58
79
< a
59
80
href = { item . href }
60
81
target = "_blank"
61
82
rel = "noopener noreferrer"
62
83
key = { index }
63
84
>
64
- { item . stat }
85
+ { ` ${ state [ ` stat${ index } ` ] } ${ index !== 0 ? "+" : "" } ` }
65
86
</ a >
66
87
</ div >
67
88
< div className = "landing-community__stat-description" >
0 commit comments