import { Button, Card, Grid2 as Grid, Slider } from "@mui/material"; import { useMobile } from "hooks"; import { GrainCable } from "models/GrainCable"; import { pond } from "protobuf-ts/pond"; import { useBinAPI, useGlobalState } from "providers"; import React, { useEffect, useState } from "react"; interface Props { binKey: string; componentDevMap: Map; cables: GrainCable[]; } interface CableNodeInfo { numNodes: number; cableKey: string; cableName: string; device: number; currentTopNode: number; } export default function CableTopNodeSummary(props: Props) { const { binKey, cables, componentDevMap } = props; const [{as}] = useGlobalState() const [cableNodes, setCableNodes] = useState([]); const isMobile = useMobile(); const binAPI = useBinAPI(); useEffect(() => { //go through the cables setting the node list and the name map let nodeList: CableNodeInfo[] = []; cables.forEach(cable => { nodeList.push({ device: componentDevMap.get(cable.key()) ?? 0, cableKey: cable.key(), currentTopNode: cable.settings.grainFilledTo, cableName: cable.name(), numNodes: cable.temperatures.length }); }); setCableNodes(nodeList); }, [cables, componentDevMap]); //function to build the new topnode array from the cableNodeInfo and send it to the backend to update the cables and their interactions const submit = () => { let newTopNodes: pond.NewTopNode[] = []; cableNodes.forEach(cable => { newTopNodes.push( pond.NewTopNode.create({ cableKey: cable.cableKey, device: cable.device, nodeNumber: cable.currentTopNode }) ); }); binAPI .updateTopNodes(binKey, newTopNodes, as) .then(resp => { console.log("Set Top Nodes for cables, components and interactions have been updated"); }) .catch(err => { console.log("One or more cables or interactions failed to update to the new top nodes"); }); }; const updateCableNode = (index: number, newNode: number) => { let nodeList = cableNodes; if (nodeList[index]) { nodeList[index].currentTopNode = newNode; } setCableNodes([...nodeList]); }; const verticalSliders = () => { return ( {cableNodes.map((cable, index) => ( {cable.cableName} { updateCableNode(index, val as number); }} /> {cable.currentTopNode} ))} ); }; const horizontalSliders = () => { return ( {cableNodes.map((cable, index) => ( {cable.cableName} { updateCableNode(index, val as number); }} /> {cable.currentTopNode} ))} ); }; return ( {isMobile ? horizontalSliders() : verticalSliders()} ); }