generated from graasp/graasp-app-starter-ts-vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: solve the accordion issue on mobile * feat: add first heat loss graphic
- Loading branch information
Showing
13 changed files
with
603 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { | ||
Stack, | ||
ToggleButton, | ||
ToggleButtonGroup, | ||
Typography, | ||
} from '@mui/material'; | ||
|
||
import { | ||
CartesianGrid, | ||
Legend, | ||
Line, | ||
LineChart, | ||
Tooltip, | ||
XAxis, | ||
YAxis, | ||
} from 'recharts'; | ||
|
||
import { useSimulation } from '@/context/SimulationContext'; | ||
|
||
type Period = { | ||
numberOfDays: number; | ||
labelKey: | ||
| 'ONE_MONTH' | ||
| 'THREE_MONTHS' | ||
| 'SIX_MONTHS' | ||
| 'ONE_YEAR' | ||
| 'THREE_YEARS'; | ||
}; | ||
|
||
const PERIODS: Period[] = [ | ||
{ numberOfDays: 30, labelKey: 'ONE_MONTH' }, | ||
{ numberOfDays: 90, labelKey: 'THREE_MONTHS' }, | ||
{ numberOfDays: 180, labelKey: 'SIX_MONTHS' }, | ||
{ numberOfDays: 365, labelKey: 'ONE_YEAR' }, | ||
{ numberOfDays: 1_095, labelKey: 'THREE_YEARS' }, | ||
] as const; | ||
|
||
type Props = { width: number }; | ||
export const HeatLossCharts = ({ width }: Props): JSX.Element => { | ||
const { t } = useTranslation('SIMULATION_GRAPHICS', { | ||
keyPrefix: 'HEAT_LOSS', | ||
}); | ||
const { | ||
days: { simulationDays, currentIdx }, | ||
} = useSimulation('simulation'); | ||
|
||
const [period, setPeriod] = useState(PERIODS[0]); | ||
|
||
const data = simulationDays | ||
.slice(Math.max(currentIdx - period.numberOfDays, 0), currentIdx + 1) | ||
.map((d) => ({ | ||
name: d.date.toLocaleDateString(), | ||
hl: Number.parseFloat((d.heatLoss.global / 1000).toFixed(1)), | ||
outdoor: Math.round(d.weatherTemperature), | ||
})); | ||
|
||
const handlePeriodChange = (value: number): void => { | ||
setPeriod(PERIODS.find((p) => value === p.numberOfDays) ?? PERIODS[0]); | ||
}; | ||
|
||
return ( | ||
<Stack spacing={2} alignItems="center"> | ||
<ToggleButtonGroup | ||
value={period.numberOfDays} | ||
exclusive | ||
onChange={(_, v) => handlePeriodChange(v)} | ||
aria-label="graphic period" | ||
> | ||
{PERIODS.map((p) => ( | ||
<ToggleButton | ||
key={p.labelKey} | ||
value={p.numberOfDays} | ||
aria-label={t(p.labelKey)} | ||
> | ||
<Typography>{t(p.labelKey)}</Typography> | ||
</ToggleButton> | ||
))} | ||
</ToggleButtonGroup> | ||
<LineChart | ||
width={width} | ||
height={300} | ||
data={data} | ||
margin={{ | ||
top: 10, | ||
right: 5, | ||
left: 10, | ||
bottom: 10, | ||
}} | ||
> | ||
<CartesianGrid strokeDasharray="3 3" /> | ||
<XAxis dataKey="name" /> | ||
<YAxis domain={[0.5, 30]} /> | ||
<Tooltip /> | ||
<Legend /> | ||
<Line | ||
type="monotone" | ||
dataKey="hl" | ||
stroke="#8884d8" | ||
activeDot={{ r: 8 }} | ||
name="Heat Loss" | ||
unit=" kilowatt" | ||
dot={false} | ||
animationDuration={0} | ||
/> | ||
|
||
<Line | ||
dataKey="outdoor" | ||
unit=" °C" | ||
name="Outdoor" | ||
dot={false} | ||
animationDuration={0} | ||
/> | ||
</LineChart> | ||
</Stack> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { useTabContext } from '@mui/lab'; | ||
|
||
type Props = { | ||
children: JSX.Element; | ||
value: string; | ||
unmountOnExit?: boolean; | ||
}; | ||
|
||
/** | ||
* This component allow to sue the MUI Tabs without the conditional rendering. | ||
* | ||
* Original code source: https://github.com/mui/material-ui/issues/21250. | ||
* @param unmountOnExit indicates if the children must be unmount on tab changed. | ||
*/ | ||
export const TabPanel = ({ | ||
children, | ||
value: id, | ||
unmountOnExit = false, | ||
}: Props): JSX.Element | null => { | ||
const context = useTabContext(); | ||
|
||
if (context === null) { | ||
throw new TypeError('No TabContext provided'); | ||
} | ||
const tabId = context.value; | ||
const isCurrent = id === tabId; | ||
|
||
const [visited, setVisited] = useState(false); | ||
useEffect(() => { | ||
if (isCurrent) { | ||
setVisited(true); | ||
} | ||
}, [isCurrent]); | ||
|
||
return (unmountOnExit && isCurrent) || !unmountOnExit ? ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'center', | ||
position: 'absolute', | ||
left: 0, | ||
right: 0, | ||
top: 0, | ||
bottom: 0, | ||
margin: 'auto', | ||
visibility: isCurrent ? 'visible' : 'hidden', | ||
}} | ||
> | ||
{visited && children} | ||
</div> | ||
) : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useState } from 'react'; | ||
|
||
import { TabContext, TabList } from '@mui/lab'; | ||
import { Stack, Tab } from '@mui/material'; | ||
|
||
import { TabPanel } from './TabPanel'; | ||
|
||
export type DataTab = { | ||
label: string; | ||
icon?: JSX.Element; | ||
unmountOnExit?: boolean; | ||
element: JSX.Element; | ||
}; | ||
|
||
type Props = { | ||
height: string | number; | ||
width: string | number; | ||
tabs: DataTab[]; | ||
}; | ||
|
||
export const Tabs = ({ height, width, tabs }: Props): JSX.Element => { | ||
const [tab, setTab] = useState('0'); | ||
|
||
return ( | ||
<Stack> | ||
<TabContext value={tab}> | ||
<Stack sx={{ borderBottom: 1, borderColor: 'divider' }}> | ||
<TabList onChange={(_, v) => setTab(v)}> | ||
{tabs.map(({ icon, label }, idx) => ( | ||
<Tab | ||
key={label} | ||
icon={icon} | ||
iconPosition="start" | ||
label={label} | ||
value={idx.toString()} | ||
/> | ||
))} | ||
</TabList> | ||
</Stack> | ||
<Stack position="relative" height={height} width={width}> | ||
{tabs.map(({ element, label, unmountOnExit }, idx) => ( | ||
<TabPanel | ||
key={label} | ||
value={idx.toString()} | ||
unmountOnExit={unmountOnExit} | ||
> | ||
{element} | ||
</TabPanel> | ||
))} | ||
</Stack> | ||
</TabContext> | ||
</Stack> | ||
); | ||
}; |
Oops, something went wrong.