Skip to content

Commit d8da5b2

Browse files
committed
chore: use top loading bar only for page changes
1 parent 366b4ec commit d8da5b2

File tree

1 file changed

+17
-53
lines changed

1 file changed

+17
-53
lines changed

dashboard/src/components/TopLoadingBar.tsx

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { useEffect, useRef, useCallback, useMemo, useState, memo } from 'react'
1+
import { useEffect, useRef, useMemo, useState, memo } from 'react'
22
import { useLocation } from 'react-router'
33
import { useTheme } from '@/components/theme-provider'
4-
import { useTopLoadingBar } from '@/hooks/use-top-loading-bar'
54
import LoadingBar from 'react-top-loading-bar'
65

76
const shouldIgnoreRoute = (pathname: string): boolean => {
@@ -34,7 +33,6 @@ function TopLoadingBar({ height = 3, color, shadow = true, className = '' }: Top
3433
const ref = useRef<any>(null)
3534
const maxTimeoutRef = useRef<NodeJS.Timeout>()
3635
const { resolvedTheme } = useTheme()
37-
const { isLoading, progress } = useTopLoadingBar()
3836
const location = useLocation()
3937

4038
const [themeKey, setThemeKey] = useState(resolvedTheme)
@@ -124,74 +122,40 @@ function TopLoadingBar({ height = 3, color, shadow = true, className = '' }: Top
124122
return resolvedTheme === 'dark' ? '#3b82f6' : '#2563eb'
125123
}, [color, resolvedTheme, themeKey, colorThemeKey])
126124

127-
const handleLoadingStart = useCallback(() => {
128-
if (isLoading && ref.current) {
129-
ref.current.continuousStart()
130-
}
131-
}, [isLoading])
125+
useEffect(() => {
126+
const currentPath = location.pathname + location.search
132127

133-
const handleLoadingComplete = useCallback(() => {
134-
if (!isLoading && ref.current) {
135-
ref.current.complete()
128+
if (currentPath !== lastLocation && lastLocation !== '') {
129+
if ((window as any).resetLoadingBarInitialState) {
130+
;(window as any).resetLoadingBarInitialState()
131+
}
136132
}
137-
}, [isLoading])
138133

139-
const handleProgressUpdate = useCallback(() => {
140-
if (progress !== undefined && ref.current) {
141-
ref.current.complete(progress)
142-
}
143-
}, [progress])
134+
lastLocation = currentPath
144135

145-
useEffect(() => {
146-
if (isLoading && ref.current) {
136+
if (!shouldIgnoreRoute(pathname) && ref.current) {
137+
// Start loading bar on route change with continuous animation
138+
ref.current.continuousStart()
139+
140+
// Set timeout to complete the loading bar after a longer delay
141+
// This gives time for the animation to progress and the page to render
147142
if (maxTimeoutRef.current) {
148143
clearTimeout(maxTimeoutRef.current)
149144
}
150-
145+
151146
maxTimeoutRef.current = setTimeout(() => {
152147
if (ref.current) {
153148
ref.current.complete()
154149
}
155150
maxTimeoutRef.current = undefined
156-
}, 3000)
157-
} else if (!isLoading && maxTimeoutRef.current) {
158-
clearTimeout(maxTimeoutRef.current)
159-
maxTimeoutRef.current = undefined
151+
}, 800)
160152
}
161153

162154
return () => {
163155
if (maxTimeoutRef.current) {
164156
clearTimeout(maxTimeoutRef.current)
165157
}
166158
}
167-
}, [isLoading])
168-
169-
useEffect(() => {
170-
handleLoadingStart()
171-
}, [handleLoadingStart])
172-
173-
useEffect(() => {
174-
handleLoadingComplete()
175-
}, [handleLoadingComplete])
176-
177-
useEffect(() => {
178-
handleProgressUpdate()
179-
}, [handleProgressUpdate])
180-
181-
useEffect(() => {
182-
const currentPath = location.pathname + location.search
183-
184-
if (currentPath !== lastLocation && lastLocation !== '') {
185-
if ((window as any).resetLoadingBarInitialState) {
186-
;(window as any).resetLoadingBarInitialState()
187-
}
188-
}
189-
190-
lastLocation = currentPath
191-
192-
if (!shouldIgnoreRoute(pathname) && ref.current) {
193-
ref.current.continuousStart()
194-
}
195159
}, [pathname, location.search])
196160

197161
useEffect(() => {
@@ -210,7 +174,7 @@ function TopLoadingBar({ height = 3, color, shadow = true, className = '' }: Top
210174
shadow,
211175
className: `${className} [direction:ltr]`,
212176
waitingTime: 0,
213-
transitionTime: 50,
177+
transitionTime: 200,
214178
}),
215179
[primaryColor, height, shadow, className],
216180
)

0 commit comments

Comments
 (0)