Skip to content

Commit

Permalink
Add temperature change card in site dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Markopoulos committed Oct 25, 2024
1 parent a7dc5b4 commit 8743014
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/website/src/assets/caret.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
152 changes: 152 additions & 0 deletions packages/website/src/common/SiteDetails/TemperatureChange/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import React from 'react';
import {
Box,
Card,
CardContent,
CardHeader,
createStyles,
Grid,
Theme,
Typography,
WithStyles,
withStyles,
} from '@material-ui/core';
import cls from 'classnames';
import red from '@material-ui/core/colors/red';
import UpdateInfo from 'common/UpdateInfo';
import { colors } from 'layout/App/theme';
import { DailyData, LatestDataASSofarValue } from 'store/Sites/types';
import { toRelativeTime } from 'helpers/dates';
import { ReactComponent as Caret } from 'assets/caret.svg';
import { styles as incomingStyles } from '../styles';

const TemperatureChangeComponent = ({
data,
dailyData,
classes,
}: TemperatureChangeProps) => {
const { satelliteTemperature } = data;
const lastWeekData = dailyData.slice(0, 7);
const temperatureChange =
lastWeekData[0].satelliteTemperature - lastWeekData[6].satelliteTemperature;
const avgTemp =
lastWeekData.reduce((acc, curr) => acc + curr.satelliteTemperature, 0) / 7;

const increased = temperatureChange >= 0;
const relativeTime =
satelliteTemperature?.timestamp &&
toRelativeTime(satelliteTemperature.timestamp);

return (
<Card className={classes.root}>
<CardHeader
className={classes.header}
title={
<Grid container>
<Grid item>
<Typography
className={classes.cardTitle}
color="textSecondary"
variant="h6"
>
7-DAYS CHANGE
</Typography>
</Grid>
</Grid>
}
/>
<CardContent className={classes.content}>
<Box className={classes.cardContent}>
<Box
className={cls(
classes.tempContainer,
increased ? classes.tempIncreased : classes.tempDecreased,
)}
>
<Caret
className={cls(classes.icon, { [classes.rotate]: !increased })}
/>
<Typography variant="h1" className={classes.temperatureChange}>
{increased ? '+' : ''}
{temperatureChange.toFixed(1)}°C
</Typography>
</Box>
<Box>
<Typography
className={classes.temperature}
color="textSecondary"
variant="h4"
>
{avgTemp.toFixed(1)}°C
</Typography>
<Typography color="textSecondary" variant="h6">
AVERAGE 7-DAYS TEMP
</Typography>
</Box>
</Box>
<UpdateInfo
relativeTime={relativeTime}
timeText="Last data received"
live
frequency="hourly"
/>
</CardContent>
</Card>
);
};

const styles = (theme: Theme) =>
createStyles({
...incomingStyles,
root: {
height: '100%',
display: 'flex',
flexDirection: 'column',
backgroundColor: colors.backgroundGray,
},
content: {
display: 'flex',
flexDirection: 'column',
flexGrow: 1,
padding: 0,
},
cardContent: {
margin: 'auto',
},
temperatureChange: {
fontWeight: 500,
},
tempContainer: {
display: 'flex',
alignItems: 'center',
gap: theme.spacing(2),
marginBottom: theme.spacing(2),
},
tempIncreased: {
color: red[500],
},
tempDecreased: {
color: theme.palette.primary.main,
},
icon: {
fill: 'currentColor',
fontSize: '52px',
},
rotate: {
transform: 'rotate(180deg)',
},
temperature: {
fontSize: 24,
fontWeight: 500,
},
});

interface IncomingTemperatureChangeProps {
data: LatestDataASSofarValue;
dailyData: DailyData[];
}

type TemperatureChangeProps = WithStyles<typeof styles> &
IncomingTemperatureChangeProps;

export const TemperatureChange = withStyles(styles)(TemperatureChangeComponent);
26 changes: 18 additions & 8 deletions packages/website/src/common/SiteDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
forecastDataSelector,
latestDataRequest,
latestDataSelector,
siteDetailsSelector,
siteTimeSeriesDataRangeSelector,
spotterPositionRequest,
spotterPositionSelector,
Expand All @@ -42,7 +43,7 @@ import Map from './Map';
import SketchFab from './SketchFab';
import FeaturedMedia from './FeaturedMedia';
import Satellite from './Satellite';
import Sensor from './Sensor';
// import Sensor from './Sensor';
import CoralBleaching from './CoralBleaching';
import Waves from './Waves';
import OceanSenseMetrics from './OceanSenseMetrics';
Expand All @@ -54,6 +55,7 @@ import WaterSamplingCard from './WaterSampling';
import { styles as incomingStyles } from './styles';
import LoadingSkeleton from '../LoadingSkeleton';
import playIcon from '../../assets/play-icon.svg';
import { TemperatureChange } from './TemperatureChange';

const acceptHUIInterval = Interval.fromDateTimes(
DateTime.now().minus({ years: 2 }),
Expand Down Expand Up @@ -125,6 +127,7 @@ const SiteDetails = ({
const latestData = useSelector(latestDataSelector);
const forecastData = useSelector(forecastDataSelector);
const timeSeriesRange = useSelector(siteTimeSeriesDataRangeSelector);
const siteDetails = useSelector(siteDetailsSelector);
const isMobile = useMediaQuery(theme.breakpoints.down('xs'));
const [lng, lat] = site?.polygon ? getMiddlePoint(site.polygon) : [];
const isLoading = !site;
Expand Down Expand Up @@ -183,17 +186,24 @@ const SiteDetails = ({
maxMonthlyMean={site.maxMonthlyMean}
/>,
(() => {
if ((hasHUIData || hasSondeData) && !hasSpotterData) {
return <CoralBleaching data={latestDataAsSofarValues} />;
}

// TODO: Conditionally render temperature change card
return (
<Sensor
depth={site.depth}
id={site.id}
<TemperatureChange
data={latestDataAsSofarValues}
dailyData={siteDetails?.dailyData ?? []}
/>
);
// if ((hasHUIData || hasSondeData) && !hasSpotterData) {
// return <CoralBleaching data={latestDataAsSofarValues} />;
// }

// return (
// <Sensor
// depth={site.depth}
// id={site.id}
// data={latestDataAsSofarValues}
// />
// );
})(),
(() => {
if (hasHUIData) {
Expand Down

0 comments on commit 8743014

Please sign in to comment.