|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import styles from "./Module.module.scss"; |
| 3 | +import { useMeasurementsStore } from "common"; |
| 4 | + |
| 5 | +interface CellProps { |
| 6 | + value: number; |
| 7 | +} |
| 8 | + |
| 9 | +const Module: React.FC<{ id: string | number }> = ({ id }) => { |
| 10 | + const numericInfo = useMeasurementsStore( |
| 11 | + (state) => state.getNumericMeasurementInfo(`module/${id}`) |
| 12 | + ); |
| 13 | + |
| 14 | + const [cellValues, setCellValues] = useState(Array(48).fill(0)); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const intervalId = setInterval(() => { |
| 18 | + const newValue = numericInfo.getUpdate(); |
| 19 | + setCellValues((prev) => prev.map(() => newValue)); |
| 20 | + }, 1); |
| 21 | + |
| 22 | + return () => clearInterval(intervalId); |
| 23 | + }, [numericInfo]); |
| 24 | + |
| 25 | + const getColorFromValue = (value: number, min: number | null, max: number | null) => { |
| 26 | + if (min !== null && max !== null) { |
| 27 | + if (value < min) return styles.red; |
| 28 | + if (value > max) return styles.red; |
| 29 | + if (value >= min && value <= max) return styles.green; |
| 30 | + } |
| 31 | + return styles.yellow; |
| 32 | + }; |
| 33 | + |
| 34 | + const Cell: React.FC<CellProps> = ({ value }) => { |
| 35 | + const colorClass = getColorFromValue(value, numericInfo.range[0], numericInfo.range[1]); |
| 36 | + return <div className={`${styles.cell} ${colorClass}`}></div>; |
| 37 | + }; |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className={styles.boxContainer1}> |
| 41 | + <div className={styles.boxContainer2}> |
| 42 | + <article className={styles.titleDecorationModule}> |
| 43 | + <h2 className={styles.h2Module}>Module {id}</h2> |
| 44 | + </article> |
| 45 | + <div className={styles.boxContainer3}> |
| 46 | + <div className={styles.voltajeContainer}> |
| 47 | + <h3 className={styles.h3}>Voltage</h3> |
| 48 | + <p className={styles.p}>max: {numericInfo.range[1]} V</p> |
| 49 | + <p className={styles.p}>min: {numericInfo.range[0]} V</p> |
| 50 | + <p className={styles.p}>mean: {cellValues.reduce((a, b) => a + b, 0) / cellValues.length}</p> |
| 51 | + </div> |
| 52 | + <div className={styles.intensityContainer}> |
| 53 | + <h3 className={styles.h3}>Intensity</h3> |
| 54 | + <p className={styles.p}>max: {numericInfo.range[1]} A</p> |
| 55 | + <p className={styles.p}>min: {numericInfo.range[0]} A</p> |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + <div className={styles.flexCells}> |
| 60 | + {cellValues.map((value, index) => ( |
| 61 | + <Cell key={index} value={value} /> |
| 62 | + ))} |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +export default Module; |
0 commit comments