forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAppLoadingText.react.js
More file actions
135 lines (119 loc) · 4.31 KB
/
AppLoadingText.react.js
File metadata and controls
135 lines (119 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useState, useEffect, useRef } from 'react';
import styles from 'dashboard/Data/AppOverview/AppOverview.scss';
import Icon from 'components/Icon/Icon.react';
const LOADING_TEXTS = [
{ text: 'Provisioning database', icon: 'b4a-database-icon' },
{ text: 'Setting up cloud functions', icon: 'b4a-cloud-code-icon' },
{ text: 'Configuring APIs', icon: 'b4a-api-icon' },
{ text: 'Setting auto Scaling', icon: 'b4a-auto-scaling-icon' },
{ text: 'Configuring secutiry rules', icon: 'b4a-security-icon' },
{ text: 'Setting up monitoring and logging', icon: 'b4a-monitor-icon' },
{ text: 'Final checks and optimizations', icon: 'b4a-final-checks-icon' },
{ text: 'Your app is ready!', icon: 'b4a-circle-check-icon' }
];
const MAX_POLL_DURATION = 30_000;
const POLL_INTERVAL = 2_000;
const TEXT_INTERVAL = Math.floor(MAX_POLL_DURATION / (LOADING_TEXTS.length - 1));
const AppLoadingText = ({ appName, appId, pollSchemas }) => {
const [currentTextIndex, setCurrentTextIndex] = useState(0);
const [isComplete, setIsComplete] = useState(false);
const [hasError, setHasError] = useState(false);
const shouldShow = document.cookie.includes(`newApp-${appId}=true`);
// const shouldShow = true;
const textIntervalRef = useRef(null);
const pollTimeoutRef = useRef(null);
const pollIntervalRef = useRef(null);
useEffect(() => {
document.documentElement.style.setProperty('--text-interval', `${TEXT_INTERVAL}ms`);
document.documentElement.style.setProperty('--fill-duration', `${TEXT_INTERVAL / 2}ms`);
return () => {
// console.log('deleting cookie');
try {
document.cookie = `newApp-${appId}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=back4app.com`;
} catch (error) {
console.error('Error deleting cookie:', error);
}
};
}, []);
const cleanup = () => {
if (textIntervalRef.current) {
clearInterval(textIntervalRef.current);
}
if (pollTimeoutRef.current) {
clearTimeout(pollTimeoutRef.current);
}
if (pollIntervalRef.current) {
clearInterval(pollIntervalRef.current);
}
};
useEffect(() => {
if (!shouldShow || !pollSchemas) {
return;
}
const rotateText = () => {
if (isComplete || hasError) {
return;
}
setCurrentTextIndex(prevIndex => {
const nextIndex = prevIndex + 1;
if (nextIndex >= LOADING_TEXTS.length - 1) {
return prevIndex;
}
return nextIndex;
});
};
textIntervalRef.current = setInterval(rotateText, TEXT_INTERVAL);
const pollWithTimeout = async () => {
if (hasError) {
return;
}
try {
const result = await pollSchemas();
if (result) {
setIsComplete(true);
cleanup();
setCurrentTextIndex(LOADING_TEXTS.length - 1);
}
} catch (error) {
console.error('Error polling schema:', error);
setHasError(true);
cleanup();
}
};
pollTimeoutRef.current = setTimeout(() => {
if (!isComplete) {
setHasError(true);
cleanup();
}
}, MAX_POLL_DURATION);
pollWithTimeout();
pollIntervalRef.current = setInterval(pollWithTimeout, POLL_INTERVAL);
return cleanup;
}, [appId, pollSchemas, isComplete, hasError]);
if (!shouldShow) {
return null;
}
const isLastText = currentTextIndex === LOADING_TEXTS.length - 1;
const displayText = hasError ? 'Contact support!' : LOADING_TEXTS[currentTextIndex].text;
const displayIcon = hasError ? 'b4a-cross-filled' : LOADING_TEXTS[currentTextIndex].icon;
const iconColor = hasError ? '#FF4D4D' : (!isLastText ? '#15A9FF' : '#27AE60');
return (
<>
{shouldShow && <div className={styles.appName}>Welcome to your App: <strong className={styles.appNameText}>{appName}</strong></div>}
<div className={styles.loadingText}>
<div
key={currentTextIndex}
className={`${styles.loadingTextContent} ${isLastText || hasError ? styles.lastText : ''}`}
>
<span
className={`${styles.loadingTextIcon} ${!isLastText && !hasError ? styles.loadingTextAnimate : ''}`}
>
<Icon name={displayIcon} width={18} height={18} fill={iconColor} />
</span>
{displayText}
</div>
</div>
</>
);
};
export default AppLoadingText;