import { Box, Table, TableBody, TableCell, TableHead, TableRow, Theme, Typography } from "@mui/material"; import { pond } from "protobuf-ts/pond"; import { makeStyles } from "@mui/styles"; import { useEffect, useMemo, useState } from "react"; import { avg, celsiusToFahrenheit } from "utils"; import { useGlobalState } from "providers"; import { darken } from "@mui/material"; import React from "react"; const useStyles = makeStyles((theme: Theme) => ({ tableStyle: { borderCollapse: "collapse" }, headerCellStyle: { fontWeight: 650, backgroundColor: darken(theme.palette.background.paper, 0.2), fontSize: 15, textAlign: "center", border: "1px solid black" }, cellStyle: { padding: 2, fontWeight: 650, fontSize: 17, textAlign: "center", border: "1px solid black", }, })); // interface Column { // label: string // e.g. "Cable 1 Temp", "Cable 2 Moisture" // cableIndex: number // type: "temp" | "moisture" // } interface Props { cables: pond.GrainCable[] targetTemp: number // in Celsius, for colouring targetMoisture: number // for colouring inBounds?: string caution?: string warning?: string } export default function BinTableExpanded(props: Props) { const { cables, targetTemp, targetMoisture, inBounds = "#43a047", caution = "#f9a825", warning = "#c62828" } = props const classes = useStyles() const [{ user }] = useGlobalState() const useFahrenheit = user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT // Build column definitions by iterating cables once // const columns = useMemo(() => { // const cols: Column[] = [] // cables.forEach((cable, cableIndex) => { // cols.push({ label: `Cable ${cableIndex + 1} Temp`, cableIndex, type: "temp" }) // // Same moisture-validity check as BinTableView // if (cable.moisture.length > 0 && avg(cable.moisture) > 0) { // cols.push({ label: `Cable ${cableIndex + 1} Moisture`, cableIndex, type: "moisture" }) // } // }) // return cols // }, [cables]) // Row count driven by the longest cable const rowCount = useMemo( () => Math.max(0, ...cables.map(c => c.celcius.length)), [cables] ) // Render a single cell value + background colour const renderCell = (cable: pond.GrainCable, nodeIndex: number, type: "temp" | "moisture") => { if (type === "temp") { const raw = cable.celcius[nodeIndex] if (raw === undefined) return { display: "--", color: "" } const isExcluded = cable.excludedNodes.includes(nodeIndex) if (isExcluded) return { display: "--", color: "" } let display: string let color = "" const inGrain = nodeIndex < cable.topNode if (inGrain) { if (raw > targetTemp + 5) color = warning else if (raw > targetTemp) color = caution else color = inBounds } const converted = useFahrenheit ? celsiusToFahrenheit(raw) : raw display = converted.toFixed(1) + (useFahrenheit ? " °F" : " °C") return { display, color } } // moisture const raw = cable.moisture[nodeIndex] if (raw === undefined || raw === 0) return { display: "--", color: "" } const isExcluded = cable.excludedNodes.includes(nodeIndex) if (isExcluded) return { display: "--", color: "" } let color = "" const inGrain = nodeIndex < cable.topNode if (inGrain) { if (raw > targetMoisture + 1.5) color = warning else if (raw > targetMoisture) color = caution else color = inBounds } return { display: raw.toFixed(2) + "%", color } } const cableTable = (cable: pond.GrainCable) => { return ( Level Temperature Moisture {/* Rows are reversed so highest node is at top, matching BinTableView */} {Array.from({ length: rowCount }, (_, i) => rowCount - 1 - i).map(nodeIndex => { const tempData = renderCell(cable, nodeIndex, "temp") const moistureData = renderCell(cable, nodeIndex, "moisture") return ( {nodeIndex + 1} {tempData.display} {moistureData.display} )})}
) } return ( // // // // Level // {columns.map((col, i) => ( // // {col.label} // // ))} // // // // {/* Rows are reversed so highest node is at top, matching BinTableView */} // {Array.from({ length: rowCount }, (_, i) => rowCount - 1 - i).map(nodeIndex => ( // // // {nodeIndex + 1} // // {columns.map((col, colI) => { // const cable = cables[col.cableIndex] // const { display, color } = renderCell(cable, nodeIndex, col.type) // return ( // // {display} // // ) // })} // // ))} // //
{/* Table Legend */} On or below target {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "+10 °F" : "+5 °C"} above target {user.tempUnit() === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT ? "+20 °F" : "+10 °C"} above target {cables.map((cable, i) => ( {cable.name} {cableTable(cable)} ))} ) }