-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseInstances.js
More file actions
43 lines (39 loc) · 1.04 KB
/
useInstances.js
File metadata and controls
43 lines (39 loc) · 1.04 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
import { useState, useEffect } from 'react';
import instanceService from '../services/instanceService';
const useInstances = () => {
const [state, setState] = useState({
instances: [],
allInstances: [],
loading: true,
error: null,
lastUpdate: null
});
useEffect(() => {
const handleUpdate = (newState) => {
setState(newState);
};
instanceService.addListener(handleUpdate);
const initialState = instanceService.getHealthyInstances();
const allInstancesState = instanceService.getAllInstancesWithStatus();
setState({
...initialState,
allInstances: allInstancesState.instances
});
return () => {
instanceService.removeListener(handleUpdate);
};
}, []);
const refresh = () => {
instanceService.refresh();
};
return {
instances: state.instances,
allInstances: state.allInstances,
loading: state.loading,
error: state.error,
lastUpdate: state.lastUpdate,
refresh,
hasInstances: state.instances.length > 0
};
};
export default useInstances;