Rewrite instances page

Render the table in the instances page using an antd Table component
directly and feed it the data via an automatically updating list of
instances.
This commit is contained in:
Hornwitser committed 2021-06-10 10:26:40 +02:00
1 parent 0e9893fcb6
commit 82c3d27bff
4 files changed
+136 -36

No files matched your search

+41
View File
@@ -39,3 +39,44 @@ export function useInstance(id) {
return [instance, updateInstance];
}
export function useInstanceList() {
let control = useContext(ControlContext);
let [instanceList, setInstanceList] = useState([]);
function updateInstanceList() {
libLink.messages.listInstances.send(control).then(result => {
setInstanceList(result.list);
}).catch(err => {
logger.error(`Failed to list instances:\n${err}`);
});
}
useEffect(() => {
updateInstanceList();
function updateHandler(newInstance) {
setInstanceList(oldList => {
let newList = oldList.concat();
let index = newList.findIndex(s => s.id === newInstance.id);
if (newInstance.status !== "deleted") {
if (index !== -1) {
newList[index] = newInstance;
} else {
newList.push(newInstance);
}
} else if (index !== -1) {
newList.splice(index, 1);
}
return newList;
});
}
control.onInstanceUpdate(null, updateHandler);
return () => {
control.offInstanceUpdate(null, updateHandler);
};
}, []);
return [instanceList];
}