2026-10-05 18:10:18 -04:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
import { setHttpProgress, useHttpProgress } from '@/state/httpProgress';
|
2020-04-10 12:41:08 -07:00
|
|
|
import { randomInt } from '@/helpers';
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
export default function ProgressBar() {
|
|
|
|
|
const { progress, continuous } = useHttpProgress();
|
|
|
|
|
const visible = (progress || 0) > 0;
|
2020-04-10 12:41:08 -07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-10-05 18:10:18 -04:00
|
|
|
if (progress !== 100) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2020-04-10 12:41:08 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
const timeout = setTimeout(() => setHttpProgress(undefined), 500);
|
2020-04-10 12:41:08 -07:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return () => clearTimeout(timeout);
|
2022-06-26 15:13:52 -04:00
|
|
|
}, [progress]);
|
2020-04-10 12:41:08 -07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!continuous) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!progress || progress === 0) {
|
2026-10-05 18:10:18 -04:00
|
|
|
setHttpProgress(randomInt(20, 30));
|
|
|
|
|
return;
|
2020-04-10 12:41:08 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if (progress >= 90) {
|
|
|
|
|
setHttpProgress(90);
|
|
|
|
|
return;
|
2020-04-10 12:41:08 -07:00
|
|
|
}
|
2026-10-05 18:10:18 -04:00
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => setHttpProgress(progress + randomInt(1, 5)), 500);
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timeout);
|
|
|
|
|
}, [continuous, progress]);
|
2020-04-10 12:41:08 -07:00
|
|
|
|
|
|
|
|
return (
|
2026-10-05 18:10:18 -04:00
|
|
|
<div className={'fixed h-0.5 w-full'}>
|
|
|
|
|
{visible && (
|
|
|
|
|
<div
|
|
|
|
|
className={'h-full bg-accent shadow-progress transition-[width] duration-300 ease-in-out'}
|
|
|
|
|
style={{ width: progress === undefined ? '100%' : `${progress}%` }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2020-04-10 12:41:08 -07:00
|
|
|
</div>
|
|
|
|
|
);
|
2026-10-05 18:10:18 -04:00
|
|
|
}
|