Skip to content

Commit

Permalink
fix(SlimFaasPlanetSaver): better UI
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Dec 1, 2024
1 parent 3178440 commit 649d7f9
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 115 deletions.
36 changes: 22 additions & 14 deletions src/SlimFaasPlanetSaver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ npm install @axa-fr/slimfaas-planet-saver

Example usage with react :
```javascript
import React, { useState, useEffect } from 'react';
import SlimFaasPlanetSaver from "@axa-fr/slimfaas-planet-saver";
import React, { useState, useEffect, useRef } from 'react';
import SlimFaasPlanetSaver from "./SlimFaasPlanetSaver.js";

const PlanetSaver = ({ children, baseUrl, fetch }) => {
const [isFirstStart, setIsFirstStart] = useState(true); // State for first start
const [isFirstStart, setIsFirstStart] = useState(true);
const environmentStarterRef = useRef(null);

useEffect(() => {
if (!baseUrl) return;

const environmentStarter = new SlimFaasPlanetSaver(baseUrl, {
if (environmentStarterRef.current) return;

const instance = new SlimFaasPlanetSaver(baseUrl, {
interval: 2000,
fetch,
updateCallback: (data) => {
Expand All @@ -45,31 +48,36 @@ const PlanetSaver = ({ children, baseUrl, fetch }) => {
errorCallback: (error) => {
console.error('Error detected :', error);
},
overlayStartingMessage: '🌍 Starting the environment.... 🌳',
overlayStartingMessage: '🌳 Starting the environment.... 🌳',
overlayNoActivityMessage: 'Waiting activity to start environment...',
overlayErrorMessage: 'An error occured when starting environment. Please contact an administrator.',
overlayErrorMessage: 'An error occurred when starting environment. Please contact an administrator.',
overlaySecondaryMessage: 'Startup should be fast, but if no machines are available it can take several minutes.',
overlayLoadingIcon: '🌍',
overlayErrorSecondaryMessage: 'If the error persists, please contact an administrator.'
});

// Start polling
environmentStarter.startPolling();
environmentStarterRef.current = instance;

// Initialiser les effets de bord
instance.initialize();
instance.startPolling();

// Cleanup
return () => environmentStarter.cleanup();
}, [baseUrl, isFirstStart]);
return () => {
instance.cleanup();
environmentStarterRef.current = null;
};
}, [baseUrl]);

// During the first start, display a loading message
if (isFirstStart) {
return null;
}

// Once the environment is started, display the children
return <>{children}</>;

};

export default PlanetSaver;


```

## Run the demo
Expand Down
35 changes: 21 additions & 14 deletions src/SlimFaasPlanetSaver/src/PlanetSaver.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useRef } from 'react';
import SlimFaasPlanetSaver from "./SlimFaasPlanetSaver.js";

const PlanetSaver = ({ children, baseUrl, fetch }) => {
const [isFirstStart, setIsFirstStart] = useState(true); // State for first start
const [isFirstStart, setIsFirstStart] = useState(true);
const environmentStarterRef = useRef(null);

useEffect(() => {
if (!baseUrl) return;

const environmentStarter = new SlimFaasPlanetSaver(baseUrl, {
if (environmentStarterRef.current) return;

const instance = new SlimFaasPlanetSaver(baseUrl, {
interval: 2000,
fetch,
updateCallback: (data) => {
Expand All @@ -19,27 +22,31 @@ const PlanetSaver = ({ children, baseUrl, fetch }) => {
errorCallback: (error) => {
console.error('Error detected :', error);
},
overlayStartingMessage: '🌍 Starting the environment.... 🌳',
overlayStartingMessage: '🌳 Starting the environment.... 🌳',
overlayNoActivityMessage: 'Waiting activity to start environment...',
overlayErrorMessage: 'An error occured when starting environment. Please contact an administrator.',
overlaySecondaryMessage: 'Startup should be fast, but if no machines are available it can take several minutes.'
overlayErrorMessage: 'An error occurred when starting environment. Please contact an administrator.',
overlaySecondaryMessage: 'Startup should be fast, but if no machines are available it can take several minutes.',
overlayLoadingIcon: '🌍',
overlayErrorSecondaryMessage: 'If the error persists, please contact an administrator.'
});

// Start polling
environmentStarter.startPolling();
environmentStarterRef.current = instance;

// Initialiser les effets de bord
instance.initialize();
instance.startPolling();

// Cleanup
return () => environmentStarter.cleanup();
}, [baseUrl, isFirstStart]);
return () => {
instance.cleanup();
environmentStarterRef.current = null;
};
}, [baseUrl]);

// During the first start, display a loading message
if (isFirstStart) {
return null;
}

// Once the environment is started, display the children
return <>{children}</>;

};

export default PlanetSaver;
Loading

0 comments on commit 649d7f9

Please sign in to comment.