import React, { useEffect } from 'react';
import { Card, Row, Col, ProgressBar } from 'react-bootstrap';
//
const CharacterSet = [
'📁',
'🗄️',
'🚕',
'⛽',
'✈️',
'🗿',
'🪐',
'🛸',
'🚀',
'🌈',
];
let Count = [];
let hasUnmounted = false;
let timeout;
const AddEmoji = (settings) => {
// Keep icons below the max icons limit
if (Count.length >= (settings?.maxIcons || 246)) {
Count[0].remove();
Count = Count.slice(1);
}
const id = document.querySelector('#loadingIcons');
if (id === null) {
return 2;
}
const elm = document.createElement('p');
elm.innerHTML =
Object.values(CharacterSet)[
Math.floor(Math.random() * CharacterSet.length)
];
if (Math.floor(Math.random() * 100) < 25) {
elm.innerHTML = `${'☂️'}${
elm.innerHTML
}`;
}
if (Math.floor(Math.random() * 100) < 10) {
elm.innerHTML = `${'🔫'}${
elm.innerHTML
}`;
}
if (Math.floor(Math.random() * 100) < 5) {
elm.innerHTML = `${'✏️'}${
elm.innerHTML
}`;
}
elm.className = 'loadingIcon';
elm.style.paddingTop =
Math.floor(Math.random() * (settings?.range || 100)).toString() + 'px';
elm.style.animationDuration =
Math.max(
settings?.minSpeed || 15,
Math.floor(Math.random() * (settings?.speed || 30))
).toString() + 's';
try {
id.append(elm);
Count.push(elm);
} catch {}
return Math.random() * (settings?.maxWaitTime || 64);
};
const startLoop = (SaveSettings) => {
timeout = (seconds = 1) => {
seconds = Math.max(1, seconds);
setTimeout(() => {
if (!hasUnmounted) {
timeout(AddEmoji(SaveSettings));
}
}, seconds * 1000);
};
timeout();
};
const Loading = ({
settings = {},
loadingReason = '',
showLoadingBar = false,
loadingPercentage = 0,
disableMargin = false,
}: {
settings?: {
range?: number;
speed?: number;
minSpeed?: number;
maxWaitTime?: number;
smallFont?: true;
smallerFont?: true;
top?: number;
maxIcons?: number;
};
loadingReason?: string;
showLoadingBar?: boolean;
loadingPercentage?: number;
disableMargin?: boolean;
}) => {
useEffect(() => {
startLoop(settings);
return () => {
hasUnmounted = true;
for (const value of Count) {
try {
value.remove();
} catch {}
}
Count = [];
};
}, [settings]);
if (hasUnmounted) {
return <>>;
}
return (
{/*
*/}
{loadingReason !== undefined &&
loadingReason !== null ? (
{loadingReason}
) : (
<>>
)}
Please Wait
🔨
{showLoadingBar &&
loadingPercentage !== undefined &&
!isNaN(loadingPercentage) ? (
) : (
<>>
)}
);
};
export default Loading;