// eslint-disable-file no-unused-vars import marker from '../../../../assets/marker.svg'; import map from '../../../../assets/map.svg'; import React, { useState, useEffect } from 'react'; import { useSelector } from 'react-redux'; import { makeStyles, createStyles, useTheme } from '@material-ui/core'; import { Skeleton } from '@material-ui/lab'; import { IExternalDeviceId } from '@energyweb/origin-backend-core'; import { useTranslation } from 'react-i18next'; import { formatDate, EnergyFormatter, PowerFormatter, LightenColor, downloadFileHandler, // SmartMeterReadingsTable, // SmartMeterReadingsChart, // SHOULD BE REMOVED getConfiguration, getCertificatesClient, fromGeneralSelectors } from '@energyweb/origin-ui-core'; import { ComposedPublicDevice } from '../../../types'; import { useOriginConfiguration } from '../../../utils/configuration'; import { getAllDevices } from '../../../features/devices'; import { DeviceMap } from '../map'; import { selectIconOnDeviceType } from '../../../utils/deviceIcons'; import moment from 'moment'; // import { RegisterDeviceGroup } from '../../pages/RegisterDeviceGroup'; interface IProps { id: string; showSmartMeterReadings: boolean; showCertificates: boolean; externalId?: IExternalDeviceId; } export function DeviceDetailView(props: IProps) { // should be removed const configuration = useSelector(getConfiguration); const allDevices = useSelector(getAllDevices); const backendClient = useSelector(fromGeneralSelectors.getBackendClient); const certificatesClient = useSelector(getCertificatesClient); const organizationClient = backendClient?.organizationClient; const originContext = useOriginConfiguration(); const originBgColor = originContext?.styleConfig?.MAIN_BACKGROUND_COLOR; const originTextColor = originContext?.styleConfig?.TEXT_COLOR_DEFAULT; const originSimpleTextColor = originContext?.styleConfig?.SIMPLE_TEXT_COLOR; const { t } = useTranslation(); const bgColorDarken = LightenColor(originBgColor, -2); const attributionTextColor = LightenColor(originBgColor, 20); const textColorDarken = LightenColor(originTextColor, -4); const textColorLighten = LightenColor(originTextColor, 52); const bgColorLighten = LightenColor(originBgColor, 5); const useStyles = makeStyles(() => createStyles({ attributionText: { fontSize: '10px', color: attributionTextColor } }) ); const classes = useStyles(useTheme()); const [selectedDevice, setSelectedDevice] = useState(null); useEffect(() => { if (props.id !== null && props.id !== undefined) { const device = allDevices.find((d) => d.id === props.id); setSelectedDevice(device); } else if (props.externalId) { const device = allDevices.find((p) => p.externalDeviceIds?.find( (deviceExternalId) => deviceExternalId.id === props.externalId.id && deviceExternalId.type === props.externalId.type ) ); setSelectedDevice(device); } }, [props.id, props.externalId]); const [tooltip, setTooltip] = useState(''); const [deviceTypeImage, setDeviceTypeImage] = useState(); useEffect(() => { if (selectedDevice) { const [newTooltip, newImage] = selectIconOnDeviceType(selectedDevice, t); setTooltip(newTooltip); setDeviceTypeImage(newImage); } }, [selectedDevice]); const [meterReadCertified, setMeterReadCertified] = useState(null); useEffect(() => { const getMeterRead = async (id: string): Promise => { if (id) { const start = new Date(new Date().getFullYear(), 0, 1).toISOString(); const end = new Date(new Date().getFullYear(), 11, 31).toISOString(); const { data: meterReads } = await certificatesClient?.getAggregateCertifiedEnergyByDeviceId(id, start, end); setMeterReadCertified(meterReads); } }; getMeterRead(selectedDevice?.externalRegistryId); }, [selectedDevice]); const data = [ [ { label: t('device.properties.facilityName'), data: selectedDevice?.name }, { label: t('device.properties.deviceOwner'), data: selectedDevice?.registrantOrganization }, { label: t('device.properties.complianceRegistry'), data: 'I-REC' } ], [ { label: t('device.properties.deviceType'), data: selectedDevice?.deviceType, deviceTypeImage, rowspan: 2 }, { label: t('device.properties.meterReadCertified'), data: EnergyFormatter.format(meterReadCertified ?? 0), tip: EnergyFormatter.displayUnit }, { label: t('device.properties.meterReadToBeCertified'), data: 'Read to be certified', // EnergyFormatter.format(selectedDevice.meterStats?.uncertified ?? 0), tip: EnergyFormatter.displayUnit }, // { // label: t('device.properties.publicSupport'), // data: 'what is instead?', // selectedDevice.typeOfPublicSupport, // description: '' // }, { label: t('device.properties.vintageCod'), data: formatDate(moment(selectedDevice?.registrationDate).unix() * 1000) } ], [ { label: t('device.properties.nameplateCapacity'), data: PowerFormatter.format(selectedDevice?.capacity), tip: PowerFormatter.displayUnit }, { label: t('device.properties.geoLocation'), data: selectedDevice?.latitude + ', ' + selectedDevice?.longitude, image: map, type: 'map', rowspan: 3, colspan: 2 } ], [ { label: t('device.properties.filesUpload'), rowspan: 3, colspan: 2, ul: true, li: selectedDevice?.imageIds.map((fileId) => (
  • downloadFileHandler(backendClient?.fileClient, fileId, fileId) } > {fileId}
  • )) } ] ]; if (!configuration || !organizationClient || !selectedDevice) { return ; } const pageBody = (
    {data.map((row: any, rowIndex) => ( {row.map((col, colIndex) => { return ( ); })} ))}
    {col.label}
    {col.data}{' '} {col.tip && ( {col.tip} )}
    {col.ul && (
      {col.li}
    )} {col.image && (col.type !== 'map' ? (
    {tooltip && (
    {tooltip}
    )} {col.type === 'map' && ( )}
    ) : (
    ))} {col.description && (
    {col.description}
    )}
    ); return (
    {pageBody}
    {props.showSmartMeterReadings && (
    {t('meterReads.properties.smartMeterReadings')}
    {/* Types issue of prop producingDevice */} {/*
    */}
    )} {/* {selectedDevice?.deviceGroup && ( )} */}
    ); }