@axa-fr/slimfaas-planet-saver 0.30.4 → 0.30.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,16 +24,19 @@ npm install @axa-fr/slimfaas-planet-saver
24
24
 
25
25
  Example usage with react :
26
26
  ```javascript
27
- import React, { useState, useEffect } from 'react';
28
- import SlimFaasPlanetSaver from "@axa-fr/slimfaas-planet-saver";
27
+ import React, { useState, useEffect, useRef } from 'react';
28
+ import SlimFaasPlanetSaver from "./SlimFaasPlanetSaver.js";
29
29
 
30
30
  const PlanetSaver = ({ children, baseUrl, fetch }) => {
31
- const [isFirstStart, setIsFirstStart] = useState(true); // State for first start
31
+ const [isFirstStart, setIsFirstStart] = useState(true);
32
+ const environmentStarterRef = useRef(null);
32
33
 
33
34
  useEffect(() => {
34
35
  if (!baseUrl) return;
35
36
 
36
- const environmentStarter = new SlimFaasPlanetSaver(baseUrl, {
37
+ if (environmentStarterRef.current) return;
38
+
39
+ const instance = new SlimFaasPlanetSaver(baseUrl, {
37
40
  interval: 2000,
38
41
  fetch,
39
42
  updateCallback: (data) => {
@@ -45,30 +48,36 @@ const PlanetSaver = ({ children, baseUrl, fetch }) => {
45
48
  errorCallback: (error) => {
46
49
  console.error('Error detected :', error);
47
50
  },
48
- overlayStartingMessage: 'Starting the environment...',
51
+ overlayStartingMessage: '🌳 Starting the environment.... 🌳',
49
52
  overlayNoActivityMessage: 'Waiting activity to start environment...',
50
- overlayErrorMessage: 'An error occured when starting environment. Please contact an administrator.',
53
+ overlayErrorMessage: 'An error occurred when starting environment. Please contact an administrator.',
54
+ overlaySecondaryMessage: 'Startup should be fast, but if no machines are available it can take several minutes.',
55
+ overlayLoadingIcon: '🌍',
56
+ overlayErrorSecondaryMessage: 'If the error persists, please contact an administrator.'
51
57
  });
52
58
 
53
- // Start polling
54
- environmentStarter.startPolling();
59
+ environmentStarterRef.current = instance;
60
+
61
+ // Initialiser les effets de bord
62
+ instance.initialize();
63
+ instance.startPolling();
55
64
 
56
- // Cleanup
57
- return () => environmentStarter.cleanup();
58
- }, [baseUrl, isFirstStart]);
65
+ return () => {
66
+ instance.cleanup();
67
+ environmentStarterRef.current = null;
68
+ };
69
+ }, [baseUrl]);
59
70
 
60
- // During the first start, display a loading message
61
71
  if (isFirstStart) {
62
72
  return null;
63
73
  }
64
74
 
65
- // Once the environment is started, display the children
66
75
  return <>{children}</>;
67
-
68
76
  };
69
77
 
70
78
  export default PlanetSaver;
71
79
 
80
+
72
81
  ```
73
82
 
74
83
  ## Run the demo
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@axa-fr/slimfaas-planet-saver",
3
3
  "private": false,
4
- "version": "0.30.4",
4
+ "version": "0.30.6",
5
5
  "type": "module",
6
6
  "module": "./src/SlimFaasPlanetSaver.js",
7
7
  "description": "Pure vanilla javascript which call SlimFaas API to convert infrastructures resilience to an UX resilience and help to save the planet",
@@ -4,23 +4,32 @@
4
4
  return tempUrl;
5
5
  }
6
6
 
7
+
8
+
7
9
  export default class SlimFaasPlanetSaver {
8
10
  constructor(baseUrl, options = {}) {
9
11
  this.baseUrl = normalizeBaseUrl(baseUrl);
10
12
  this.updateCallback = options.updateCallback || (() => {});
11
13
  this.errorCallback = options.errorCallback || (() => {});
12
14
  this.interval = options.interval || 5000;
13
- this.overlayStartingMessage = options.overlayStartingMessage || 'Starting in progress...';
15
+ this.overlayStartingMessage = options.overlayStartingMessage || '🌳 Starting the environment.... 🌳';
14
16
  this.overlayNoActivityMessage = options.overlayNoActivityMessage || 'Waiting activity to start environment...';
15
- this.overlayErrorMessage = options.overlayErrorMessage || 'An error occured when starting environment. Please contact an administrator.';
17
+ this.overlayErrorMessage = options.overlayErrorMessage || 'An error occurred while starting the environment.';
18
+ this.overlaySecondaryMessage = options.overlaySecondaryMessage || 'Startup should be fast, but if no machines are available it can take several minutes.';
19
+ this.overlayErrorSecondaryMessage = options.overlayErrorSecondaryMessage || 'If the error persists, please contact an administrator.';
20
+ this.overlayLoadingIcon = options.overlayLoadingIcon || '🌍';
16
21
  this.fetch = options.fetch || fetch;
17
22
  this.intervalId = null;
18
23
  this.isDocumentVisible = !document.hidden;
19
24
  this.overlayElement = null;
25
+ this.spanElement = null;
20
26
  this.styleElement = null;
21
27
  this.isReady = false;
22
28
 
23
- // Initialize lastMouseMoveTime and bind the handler
29
+ this.events = document.createElement('div');
30
+ }
31
+
32
+ initialize() {
24
33
  this.lastMouseMoveTime = Date.now();
25
34
  this.handleMouseMove = this.handleMouseMove.bind(this);
26
35
  this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
@@ -30,10 +39,7 @@ export default class SlimFaasPlanetSaver {
30
39
 
31
40
  this.createOverlay();
32
41
  this.injectStyles();
33
-
34
- this.events = document.createElement('div');
35
42
  }
36
-
37
43
  handleMouseMove() {
38
44
  this.lastMouseMoveTime = Date.now();
39
45
  }
@@ -50,24 +56,29 @@ export default class SlimFaasPlanetSaver {
50
56
  async wakeUpPods(data) {
51
57
  const wakePromises = data
52
58
  .filter((item) => item.NumberReady === 0)
53
- .map((item) =>
54
- this.fetch(`${this.baseUrl}/wake-function/${item.Name}`, {
59
+ .map(async (item) => {
60
+ const response = await this.fetch(`${this.baseUrl}/wake-function/${item.Name}`, {
55
61
  method: 'POST',
56
- })
57
- );
62
+ });
63
+ if (response.status >= 400) {
64
+ throw new Error(`HTTP Error! status: ${response.status} for function ${item.Name}`);
65
+ }
66
+ return response;
67
+ });
58
68
 
59
69
  try {
60
70
  await Promise.all(wakePromises);
61
71
  } catch (error) {
62
72
  console.error("Error waking up pods:", error);
73
+ throw error;
63
74
  }
64
75
  }
65
76
 
66
77
  async fetchStatus() {
67
78
  try {
68
79
  const response = await this.fetch(`${this.baseUrl}/status-functions`);
69
- if (!response.ok) {
70
- throw new Error(`HTTP error! status: ${response.status}`);
80
+ if (response.status >= 400) {
81
+ throw new Error(`HTTP Error! status: ${response.status}`);
71
82
  }
72
83
  const data = await response.json();
73
84
 
@@ -77,22 +88,20 @@ export default class SlimFaasPlanetSaver {
77
88
  this.updateCallback(data);
78
89
 
79
90
  const now = Date.now();
80
- const mouseMovedRecently = now - this.lastMouseMoveTime <= 60000; // 1 minute in milliseconds
91
+ const mouseMovedRecently = now - this.lastMouseMoveTime <= 60000; // 1 minute
81
92
 
82
93
  if (!allReady && this.isDocumentVisible && !mouseMovedRecently) {
83
- this.updateOverlayMessage(this.overlayNoActivityMessage);
84
- }
85
-
86
- if (!this.isDocumentVisible || mouseMovedRecently) {
87
- this.updateOverlayMessage(this.overlayStartingMessage);
94
+ this.updateOverlayMessage(this.overlayNoActivityMessage, 'waiting-action');
95
+ } else if (!this.isDocumentVisible || mouseMovedRecently) {
96
+ this.updateOverlayMessage(this.overlayStartingMessage, 'waiting');
88
97
  await this.wakeUpPods(data);
89
98
  }
90
99
  } catch (error) {
91
100
  const errorMessage = error.message;
92
- this.updateOverlayMessage(this.overlayErrorMessage);
101
+ this.updateOverlayMessage(this.overlayErrorMessage, 'error', this.overlayErrorSecondaryMessage);
93
102
  this.errorCallback(errorMessage);
94
103
  this.triggerEvent('error', { message: errorMessage });
95
- console.error('Error getting slimfaas data:', errorMessage);
104
+ console.error('Error fetching slimfaas data:', errorMessage);
96
105
  } finally {
97
106
  this.intervalId = setTimeout(() => {
98
107
  this.fetchStatus();
@@ -128,27 +137,67 @@ export default class SlimFaasPlanetSaver {
128
137
 
129
138
  injectStyles() {
130
139
  const cssString = `
131
- .environment-overlay {
132
- position: fixed;
133
- top: 0;
134
- left: 0;
135
- width: 100%;
136
- cursor: not-allowed;
137
- height: 100%;
138
- background-color: rgba(0, 0, 0, 0.5);
139
- display: flex;
140
- justify-content: center;
141
- align-items: center;
142
- color: white;
143
- font-size: 1.5rem;
144
- font-weight: bold;
145
- z-index: 1000;
146
- visibility: hidden;
147
- }
148
-
149
- .environment-overlay.visible {
150
- visibility: visible;
151
- }
140
+ .slimfaas-environment-overlay {
141
+ position: fixed;
142
+ top: 0;
143
+ left: 0;
144
+ width: 100%;
145
+ cursor: not-allowed;
146
+ height: 100%;
147
+ background-color: rgba(0, 0, 0, 0.8);
148
+ display: flex;
149
+ flex-direction: column;
150
+ justify-content: center;
151
+ align-items: center;
152
+ font-size: 2rem;
153
+ font-weight: bold;
154
+ z-index: 1000;
155
+ text-align: center;
156
+ }
157
+
158
+ .slimfaas-environment-overlay__icon {
159
+ font-size: 4rem;
160
+ animation: slimfaas-environment-overlay__icon-spin 0.5s linear infinite;
161
+ }
162
+
163
+ @keyframes slimfaas-environment-overlay__icon-spin {
164
+ from {
165
+ transform: rotate(0deg);
166
+ }
167
+ to {
168
+ transform: rotate(360deg);
169
+ }
170
+ }
171
+
172
+ .slimfaas-environment-overlay__main-message {
173
+ display: flex;
174
+ align-items: center;
175
+ gap: 0.5rem;
176
+ }
177
+
178
+ .slimfaas-environment-overlay__secondary-message {
179
+ font-size: 1.2rem;
180
+ font-weight: normal;
181
+ margin-top: 1rem;
182
+ }
183
+
184
+ .slimfaas-environment-overlay--waiting {
185
+ color: white;
186
+ }
187
+
188
+ .slimfaas-environment-overlay--waiting-action {
189
+ color: lightyellow;
190
+ }
191
+ .slimfaas-environment-overlay--waiting-action .slimfaas-environment-overlay__secondary-message {
192
+ visibility: hidden;
193
+ }
194
+ .slimfaas-environment-overlay--waiting-action .slimfaas-environment-overlay__icon {
195
+ animation: none;
196
+ }
197
+
198
+ .slimfaas-environment-overlay--error {
199
+ color: lightcoral;
200
+ }
152
201
  `;
153
202
 
154
203
  this.styleElement = document.createElement('style');
@@ -158,26 +207,60 @@ export default class SlimFaasPlanetSaver {
158
207
 
159
208
  createOverlay() {
160
209
  this.overlayElement = document.createElement('div');
161
- this.overlayElement.className = 'environment-overlay';
162
- this.overlayElement.innerText = this.overlayMessage;
163
- document.body.appendChild(this.overlayElement);
210
+ this.overlayElement.className = 'slimfaas-environment-overlay';
211
+
212
+ // Créer l'élément icône
213
+ this.iconElement = document.createElement('div');
214
+ this.iconElement.className = 'slimfaas-environment-overlay__icon';
215
+ this.iconElement.innerText = this.overlayLoadingIcon;
216
+
217
+ // Créer l'élément du message principal
218
+ this.spanElement = document.createElement('span');
219
+ this.spanElement.className = 'slimfaas-environment-overlay__main-message';
220
+ this.spanElement.innerHTML = `${this.overlayStartingMessage}`;
221
+
222
+ // Créer l'élément du message secondaire
223
+ this.secondarySpanElement = document.createElement('span');
224
+ this.secondarySpanElement.className = 'slimfaas-environment-overlay__secondary-message';
225
+ this.secondarySpanElement.innerText = this.overlaySecondaryMessage;
226
+
227
+ // Ajouter les éléments à l'overlay
228
+ this.overlayElement.appendChild(this.iconElement);
229
+ this.overlayElement.appendChild(this.spanElement);
230
+ this.overlayElement.appendChild(this.secondarySpanElement);
231
+
232
+ // Ne pas ajouter l'overlay au DOM ici
233
+ // document.body.appendChild(this.overlayElement);
164
234
  }
165
235
 
166
236
  showOverlay() {
167
- if (this.overlayElement) {
168
- this.overlayElement.classList.add('visible');
237
+ if (this.overlayElement && !document.body.contains(this.overlayElement)) {
238
+ document.body.appendChild(this.overlayElement);
169
239
  }
170
240
  }
171
241
 
172
242
  hideOverlay() {
173
- if (this.overlayElement) {
174
- this.overlayElement.classList.remove('visible');
243
+ if (this.overlayElement && document.body.contains(this.overlayElement)) {
244
+ document.body.removeChild(this.overlayElement);
175
245
  }
176
246
  }
177
247
 
178
- updateOverlayMessage(newMessage) {
248
+ updateOverlayMessage(newMessage, status = 'waiting', secondaryMessage = null) {
249
+ if (this.spanElement) {
250
+ this.spanElement.innerHTML = `${newMessage}`;
251
+ }
252
+ if (this.secondarySpanElement && secondaryMessage !== null) {
253
+ this.secondarySpanElement.innerText = secondaryMessage;
254
+ } else {
255
+ this.secondarySpanElement.innerText = this.overlaySecondaryMessage;
256
+ }
179
257
  if (this.overlayElement) {
180
- this.overlayElement.innerText = newMessage;
258
+ this.overlayElement.classList.remove(
259
+ 'slimfaas-environment-overlay--error',
260
+ 'slimfaas-environment-overlay--waiting',
261
+ 'slimfaas-environment-overlay--waiting-action'
262
+ );
263
+ this.overlayElement.classList.add('slimfaas-environment-overlay--' + status);
181
264
  }
182
265
  }
183
266
 
@@ -189,11 +272,11 @@ export default class SlimFaasPlanetSaver {
189
272
  cleanup() {
190
273
  this.stopPolling();
191
274
  document.removeEventListener('visibilitychange', this.handleVisibilityChange);
192
- document.removeEventListener('mousemove', this.handleMouseMove); // Remove mousemove listener
193
- if (this.overlayElement) {
275
+ document.removeEventListener('mousemove', this.handleMouseMove);
276
+ if (this.overlayElement && document.body.contains(this.overlayElement)) {
194
277
  document.body.removeChild(this.overlayElement);
195
278
  }
196
- if (this.styleElement) {
279
+ if (this.styleElement && document.head.contains(this.styleElement)) {
197
280
  document.head.removeChild(this.styleElement);
198
281
  }
199
282
  }