diff --git a/sge-frontend/src/views/DataSet/MainDataTables.js b/sge-frontend/src/views/DataSet/MainDataTables.js index 88ce4fa..b8cb114 100644 --- a/sge-frontend/src/views/DataSet/MainDataTables.js +++ b/sge-frontend/src/views/DataSet/MainDataTables.js @@ -1,8 +1,16 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useEffect, useMemo } from "react"; import { MaterialReactTable } from "material-react-table"; import { useDispatch, useSelector } from "react-redux"; import { useTranslation } from "react-i18next"; -import { Card, CardHeader, CardTitle, Alert, Row, Col, Label } from "reactstrap"; +import { + Card, + CardHeader, + CardTitle, + Alert, + Row, + Col, + Label, +} from "reactstrap"; import { getVMEmissionSummary } from "../../redux/actions/mainDataTables/index"; import { getDataCenters } from "../../redux/actions/dataCenter"; import { editNumbers } from "../../components/edit-numbers"; @@ -44,7 +52,7 @@ function MainDataTables() { setError(null); await dispatch(getVMEmissionSummary(selectedDataCenter.value)); } catch (err) { - console.error('Error in MainDataTables:', err); + console.error("Error in MainDataTables:", err); setError(err.message); } finally { setLoading(false); @@ -54,30 +62,90 @@ function MainDataTables() { } }, [selectedDataCenter, dispatch]); - // Debug log for store data - useEffect(() => { - console.log('Current store data:', mainDataTablesStore); - }, [mainDataTablesStore]); + // Memoize columns to prevent re-renders + const columns = useMemo( + () => [ + { + header: t("VM ID"), + accessorKey: "vmId", + size: 150, + Cell: ({ cell }) => {cell.getValue() || "-"}, + }, + { + header: t("VM Name"), + accessorKey: "vmName", + size: 200, + Cell: ({ cell }) => {cell.getValue() || "-"}, + }, + { + header: t("VM Power"), + accessorKey: "vmPower", + size: 120, + Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, + }, + { + header: t("VM Status"), + accessorKey: "vmStatus", + size: 100, + Cell: ({ cell }) => {cell.getValue() || "-"}, + }, + { + header: t("Total Emission"), + accessorKey: "totalEmission", + size: 150, + Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, + }, + { + header: t("Created Date"), + accessorKey: "createdDate", + size: 180, + Cell: ({ cell }) => ( + + {cell.getValue() ? new Date(cell.getValue()).toLocaleString() : "-"} + + ), + sortable: true, + }, + { + header: t("Physical Machine"), + accessorKey: "physicalMachine", + size: 150, + Cell: ({ cell }) => {cell.getValue() || "-"}, + }, + { + header: t("Data Center"), + accessorKey: "dataCenter", + size: 150, + Cell: ({ cell }) => {cell.getValue() || "-"}, + }, + { + header: "CO2", + accessorKey: "co2", + size: 100, + Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, + }, + { + header: "CH4", + accessorKey: "ch4", + size: 100, + Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, + }, + { + header: "N2O", + accessorKey: "n2o", + size: 100, + Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"}, + }, + ], + [t] + ); - const columns = [ - { header: t("VM ID"), accessorKey: "vmId", Cell: ({ cell }) => {cell.getValue() || "-"} }, - { header: t("VM Name"), accessorKey: "vmName", Cell: ({ cell }) => {cell.getValue() || "-"} }, - { header: t("VM Power"), accessorKey: "vmPower", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"} }, - { header: t("VM Status"), accessorKey: "vmStatus", Cell: ({ cell }) => {cell.getValue() || "-"} }, - { header: t("Total Emission"), accessorKey: "totalEmission", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"} }, - { header: t("Created Date"), accessorKey: "createdDate", Cell: ({ cell }) => ({cell.getValue() ? new Date(cell.getValue()).toLocaleString() : "-"}), sortable: true }, - { header: t("Physical Machine"), accessorKey: "physicalMachine", Cell: ({ cell }) => {cell.getValue() || "-"} }, - { header: t("Data Center"), accessorKey: "dataCenter", Cell: ({ cell }) => {cell.getValue() || "-"} }, - { header: "CO2", accessorKey: "co2", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"} }, - { header: "CH4", accessorKey: "ch4", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"} }, - { header: "N2O", accessorKey: "n2o", Cell: ({ cell }) => {editNumbers(cell.getValue()) || "-"} }, - ]; - - - - - const tableData = mainDataTablesStore?.vmEmissionSummary || []; - console.log('VM Emission data:', tableData); + // Memoize table data to prevent unnecessary re-renders + const tableData = useMemo(() => { + const data = mainDataTablesStore?.vmEmissionSummary || []; + console.log("VM Emission data count:", data.length); + return data; + }, [mainDataTablesStore?.vmEmissionSummary]); if (error) { return ( @@ -92,8 +160,13 @@ function MainDataTables() { {t("Raw Data")} + {tableData.length > 0 && ( + + {tableData.length} records loaded + + )} - + {/* Datacenter Selection */}
@@ -109,41 +182,84 @@ function MainDataTables() { isSearchable isLoading={dataCenterStore?.loading} noOptionsMessage={() => t("No data centers available")} + styles={{ + menu: (provided) => ({ + ...provided, + zIndex: 9999, // Ensure dropdown appears above other elements + }), + menuPortal: (provided) => ({ + ...provided, + zIndex: 9999, + }), + }} + menuPortalTarget={document.body} // Render dropdown in body to avoid container overflow />
+ {selectedDataCenter ? ( ( +
+ )} /> ) : (