@axa-fr/slimfaas-planet-saver 0.30.5 → 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,31 +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.',
51
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.'
52
57
  });
53
58
 
54
- // Start polling
55
- environmentStarter.startPolling();
59
+ environmentStarterRef.current = instance;
60
+
61
+ // Initialiser les effets de bord
62
+ instance.initialize();
63
+ instance.startPolling();
56
64
 
57
- // Cleanup
58
- return () => environmentStarter.cleanup();
59
- }, [baseUrl, isFirstStart]);
65
+ return () => {
66
+ instance.cleanup();
67
+ environmentStarterRef.current = null;
68
+ };
69
+ }, [baseUrl]);
60
70
 
61
- // During the first start, display a loading message
62
71
  if (isFirstStart) {
63
72
  return null;
64
73
  }
65
74
 
66
- // Once the environment is started, display the children
67
75
  return <>{children}</>;
68
-
69
76
  };
70
77
 
71
78
  export default PlanetSaver;
72
79
 
80
+
73
81
  ```
74
82
 
75
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.5",
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,25 +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 the environment.... 🌳';
15
+ this.overlayStartingMessage = options.overlayStartingMessage || '🌳 Starting the environment.... 🌳';
14
16
  this.overlayNoActivityMessage = options.overlayNoActivityMessage || 'Waiting activity to start environment...';
15
- this.overlayErrorMessage = options.overlayErrorMessage || 'Une erreur est survenue lors du démarrage de l\'environnement. Veuillez contacter un administrateur.';
17
+ this.overlayErrorMessage = options.overlayErrorMessage || 'An error occurred while starting the environment.';
16
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 || '🌍';
17
21
  this.fetch = options.fetch || fetch;
18
22
  this.intervalId = null;
19
23
  this.isDocumentVisible = !document.hidden;
20
24
  this.overlayElement = null;
21
- this.spanElement = null; // Nouvel élément pour le <span>
25
+ this.spanElement = null;
22
26
  this.styleElement = null;
23
27
  this.isReady = false;
24
28
 
25
- // Initialisation du temps du dernier mouvement de souris et liaison du gestionnaire
29
+ this.events = document.createElement('div');
30
+ }
31
+
32
+ initialize() {
26
33
  this.lastMouseMoveTime = Date.now();
27
34
  this.handleMouseMove = this.handleMouseMove.bind(this);
28
35
  this.handleVisibilityChange = this.handleVisibilityChange.bind(this);
@@ -32,10 +39,7 @@ export default class SlimFaasPlanetSaver {
32
39
 
33
40
  this.createOverlay();
34
41
  this.injectStyles();
35
-
36
- this.events = document.createElement('div');
37
42
  }
38
-
39
43
  handleMouseMove() {
40
44
  this.lastMouseMoveTime = Date.now();
41
45
  }
@@ -52,24 +56,29 @@ export default class SlimFaasPlanetSaver {
52
56
  async wakeUpPods(data) {
53
57
  const wakePromises = data
54
58
  .filter((item) => item.NumberReady === 0)
55
- .map((item) =>
56
- this.fetch(`${this.baseUrl}/wake-function/${item.Name}`, {
59
+ .map(async (item) => {
60
+ const response = await this.fetch(`${this.baseUrl}/wake-function/${item.Name}`, {
57
61
  method: 'POST',
58
- })
59
- );
62
+ });
63
+ if (response.status >= 400) {
64
+ throw new Error(`HTTP Error! status: ${response.status} for function ${item.Name}`);
65
+ }
66
+ return response;
67
+ });
60
68
 
61
69
  try {
62
70
  await Promise.all(wakePromises);
63
71
  } catch (error) {
64
- console.error("Erreur lors du réveil des pods :", error);
72
+ console.error("Error waking up pods:", error);
73
+ throw error;
65
74
  }
66
75
  }
67
76
 
68
77
  async fetchStatus() {
69
78
  try {
70
79
  const response = await this.fetch(`${this.baseUrl}/status-functions`);
71
- if (!response.ok) {
72
- throw new Error(`Erreur HTTP ! statut : ${response.status}`);
80
+ if (response.status >= 400) {
81
+ throw new Error(`HTTP Error! status: ${response.status}`);
73
82
  }
74
83
  const data = await response.json();
75
84
 
@@ -79,7 +88,7 @@ export default class SlimFaasPlanetSaver {
79
88
  this.updateCallback(data);
80
89
 
81
90
  const now = Date.now();
82
- const mouseMovedRecently = now - this.lastMouseMoveTime <= 60000; // 1 minute en millisecondes
91
+ const mouseMovedRecently = now - this.lastMouseMoveTime <= 60000; // 1 minute
83
92
 
84
93
  if (!allReady && this.isDocumentVisible && !mouseMovedRecently) {
85
94
  this.updateOverlayMessage(this.overlayNoActivityMessage, 'waiting-action');
@@ -89,10 +98,10 @@ export default class SlimFaasPlanetSaver {
89
98
  }
90
99
  } catch (error) {
91
100
  const errorMessage = error.message;
92
- this.updateOverlayMessage(this.overlayErrorMessage, 'error');
101
+ this.updateOverlayMessage(this.overlayErrorMessage, 'error', this.overlayErrorSecondaryMessage);
93
102
  this.errorCallback(errorMessage);
94
103
  this.triggerEvent('error', { message: errorMessage });
95
- console.error('Erreur lors de la récupération des données slimfaas :', errorMessage);
104
+ console.error('Error fetching slimfaas data:', errorMessage);
96
105
  } finally {
97
106
  this.intervalId = setTimeout(() => {
98
107
  this.fetchStatus();
@@ -128,61 +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.8);
139
- display: flex;
140
- flex-direction: column;
141
- justify-content: center;
142
- align-items: center;
143
- color: white;
144
- font-size: 2rem;
145
- font-weight: bold;
146
- z-index: 1000;
147
- visibility: hidden;
148
- text-align: center;
149
- }
150
-
151
- .environment-overlay.visible {
152
- visibility: visible;
153
- }
154
-
155
- .environment-overlay .main-message {
156
- display: flex;
157
- align-items: center;
158
- gap: 0.5rem;
159
- color: white;
160
- }
161
-
162
- .environment-overlay .secondary-message {
163
- font-size: 1.2rem;
164
- font-weight: normal;
165
- margin-top: 1rem;
166
- }
167
-
168
- .environment-overlay--waiting{
169
- color: white;
170
- }
171
-
172
- .environment-overlay--waiting-action {
173
- color: lightyellow;
174
- }
175
- .environment-overlay--waiting-action .secondary-message {
176
- visibility: hidden;
177
- }
178
-
179
- .environment-overlay--error {
180
- color: lightred;
181
- }
182
- .environment-overlay--error .secondary-message {
183
- visibility: hidden;
184
- }
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
+ }
185
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
+ }
186
201
  `;
187
202
 
188
203
  this.styleElement = document.createElement('style');
@@ -192,43 +207,60 @@ export default class SlimFaasPlanetSaver {
192
207
 
193
208
  createOverlay() {
194
209
  this.overlayElement = document.createElement('div');
195
- this.overlayElement.className = 'environment-overlay';
210
+ this.overlayElement.className = 'slimfaas-environment-overlay';
196
211
 
197
- // Créer un élément <span> pour le texte et les icônes
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
198
218
  this.spanElement = document.createElement('span');
219
+ this.spanElement.className = 'slimfaas-environment-overlay__main-message';
199
220
  this.spanElement.innerHTML = `${this.overlayStartingMessage}`;
200
221
 
201
- // Créer un élément <span> pour le second message
222
+ // Créer l'élément du message secondaire
202
223
  this.secondarySpanElement = document.createElement('span');
203
- this.secondarySpanElement.className = 'secondary-message';
224
+ this.secondarySpanElement.className = 'slimfaas-environment-overlay__secondary-message';
204
225
  this.secondarySpanElement.innerText = this.overlaySecondaryMessage;
205
226
 
206
- // Ajouter le <span> à l'overlay
227
+ // Ajouter les éléments à l'overlay
228
+ this.overlayElement.appendChild(this.iconElement);
207
229
  this.overlayElement.appendChild(this.spanElement);
208
230
  this.overlayElement.appendChild(this.secondarySpanElement);
209
231
 
210
- document.body.appendChild(this.overlayElement);
232
+ // Ne pas ajouter l'overlay au DOM ici
233
+ // document.body.appendChild(this.overlayElement);
211
234
  }
212
235
 
213
236
  showOverlay() {
214
- if (this.overlayElement) {
215
- this.overlayElement.classList.add('visible');
237
+ if (this.overlayElement && !document.body.contains(this.overlayElement)) {
238
+ document.body.appendChild(this.overlayElement);
216
239
  }
217
240
  }
218
241
 
219
242
  hideOverlay() {
220
- if (this.overlayElement) {
221
- this.overlayElement.classList.remove('visible');
243
+ if (this.overlayElement && document.body.contains(this.overlayElement)) {
244
+ document.body.removeChild(this.overlayElement);
222
245
  }
223
246
  }
224
247
 
225
- updateOverlayMessage(newMessage, status = 'waiting') {
248
+ updateOverlayMessage(newMessage, status = 'waiting', secondaryMessage = null) {
226
249
  if (this.spanElement) {
227
250
  this.spanElement.innerHTML = `${newMessage}`;
228
251
  }
252
+ if (this.secondarySpanElement && secondaryMessage !== null) {
253
+ this.secondarySpanElement.innerText = secondaryMessage;
254
+ } else {
255
+ this.secondarySpanElement.innerText = this.overlaySecondaryMessage;
256
+ }
229
257
  if (this.overlayElement) {
230
- this.overlayElement.classList.remove('environment-overlay--error', 'environment--overlay-waiting', 'environment-overlay--waiting-action');
231
- this.overlayElement.classList.add("environment-overlay--"+status);
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);
232
264
  }
233
265
  }
234
266
 
@@ -241,10 +273,10 @@ export default class SlimFaasPlanetSaver {
241
273
  this.stopPolling();
242
274
  document.removeEventListener('visibilitychange', this.handleVisibilityChange);
243
275
  document.removeEventListener('mousemove', this.handleMouseMove);
244
- if (this.overlayElement) {
276
+ if (this.overlayElement && document.body.contains(this.overlayElement)) {
245
277
  document.body.removeChild(this.overlayElement);
246
278
  }
247
- if (this.styleElement) {
279
+ if (this.styleElement && document.head.contains(this.styleElement)) {
248
280
  document.head.removeChild(this.styleElement);
249
281
  }
250
282
  }