11import Image from "next/image" ;
2- import * as React from "react" ;
2+ import { useEffect , useRef , useState } from "react" ;
3+
4+ import { getPageConfigByName } from "../../config/pages" ;
35
46export default function ProgressBar ( { pageName } : { pageName : string } ) {
5- const [ progressWidth , setProgressWidth ] = React . useState ( 0 ) ;
6- let progress = 0 ;
7- let titleName = "" ;
7+ const [ progressWidth , setProgressWidth ] = useState ( 0 ) ;
8+ const [ isAnimating , setIsAnimating ] = useState ( false ) ;
9+ const previousPageRef = useRef < string > ( "" ) ;
10+ const previousProgressRef = useRef < number > ( 0 ) ;
11+ const isFirstRenderRef = useRef < boolean > ( true ) ;
812
9- switch ( pageName ) {
10- case "schedule" :
11- progress = 0 ;
12- titleName = "Drone Competition Schedule" ;
13- break ;
14- case "format-rules" :
15- progress = 30 ;
16- titleName = "Format & Rules" ;
17- break ;
18- case "guests-sponsors" :
19- titleName = "Guests & Sponsors" ;
20- progress = 60 ;
21- break ;
22- case "leaderboard" :
23- titleName = "Leaderboard" ;
24- progress = 80 ;
25- break ;
26- }
13+ const pageConfig = getPageConfigByName ( pageName ) ;
14+ const progress = pageConfig ?. progress || 0 ;
15+ const titleName = pageConfig ?. title || "" ;
2716
28- React . useEffect ( ( ) => {
17+ useEffect ( ( ) => {
2918 const updateProgress = ( ) => {
19+ let targetProgress = progress ;
20+
3021 if ( pageName === "leaderboard" ) {
31- setProgressWidth ( window . innerWidth >= 768 ? 90 : 80 ) ;
22+ targetProgress = window . innerWidth >= 768 ? 90 : 80 ;
23+ }
24+
25+ if ( isFirstRenderRef . current ) {
26+ setProgressWidth ( 0 ) ;
27+ setIsAnimating ( true ) ;
28+ setTimeout ( ( ) => {
29+ setProgressWidth ( targetProgress ) ;
30+ previousProgressRef . current = targetProgress ;
31+ previousPageRef . current = pageName ;
32+ isFirstRenderRef . current = false ;
33+ } , 10 ) ;
34+ } else if ( previousPageRef . current !== pageName ) {
35+ setIsAnimating ( false ) ;
36+ setProgressWidth ( previousProgressRef . current ) ;
37+
38+ setTimeout ( ( ) => {
39+ setIsAnimating ( true ) ;
40+ setProgressWidth ( targetProgress ) ;
41+ previousProgressRef . current = targetProgress ;
42+ previousPageRef . current = pageName ;
43+ } , 10 ) ;
3244 } else {
33- setProgressWidth ( progress ) ;
45+ setProgressWidth ( targetProgress ) ;
46+ previousProgressRef . current = targetProgress ;
3447 }
3548 } ;
3649
@@ -39,20 +52,30 @@ export default function ProgressBar({ pageName }: { pageName: string }) {
3952 return ( ) => window . removeEventListener ( "resize" , updateProgress ) ;
4053 } , [ pageName , progress ] ) ;
4154
55+ useEffect ( ( ) => {
56+ if ( isAnimating ) {
57+ const timer = setTimeout ( ( ) => {
58+ setIsAnimating ( false ) ;
59+ } , 3000 ) ;
60+
61+ return ( ) => clearTimeout ( timer ) ;
62+ }
63+ } , [ isAnimating ] ) ;
64+
4265 return (
4366 < div >
4467 < div className = "w-full flex-col justify-center" >
4568 < div className = "mb-4 flex items-center justify-center" >
46- < a className = "title-large text-center text-[30px] text-dark md:text-[50px] lg:text-[60px]" >
69+ < h1 className = "title-large text-center text-[30px] text-dark md:text-[50px] lg:text-[60px]" >
4770 { titleName }
48- </ a >
71+ </ h1 >
4972 </ div >
5073 < div className = "flex flex-1 items-center" >
5174 < div
5275 className = "flex-shrink-0"
5376 style = { {
5477 width : `${ progressWidth } %` ,
55- transition : "width 3s ease-in-out" ,
78+ transition : isAnimating ? "width 3s ease-in-out" : "none ",
5679 } }
5780 />
5881 < div className = "flex-shrink-0" >
0 commit comments