-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/test rechart #723
base: development/1.0
Are you sure you want to change the base?
Feature/test rechart #723
Changes from all commits
999899c
d6e23a1
160f810
a468573
cc84bd9
38c55c0
6c6dfad
b684938
70018a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import styled, { css, useTheme } from 'styled-components'; | ||
import { Box } from '../../next'; | ||
import { Text, FormattedDateTime, Wrap, spacing } from '../../index'; | ||
|
||
const TootlipContainer = styled.div<{ tooltipInset }>` | ||
${(props) => { | ||
const theme = useTheme(); | ||
return css` | ||
border: 1px solid ${theme.border}; | ||
width: 24rem; | ||
color: ${theme.textSecondary}; | ||
background-color: ${theme.backgroundLevel1}; | ||
border-radius: 4px; | ||
position: absolute; | ||
inset: ${props.tooltipInset.top}px auto auto ${props.tooltipInset.left}px; | ||
padding: ${spacing.r8}; | ||
font-size: 1rem; | ||
`; | ||
}} | ||
`; | ||
|
||
export const CustomTooltip = (props) => { | ||
const { tooltipData, coordinate } = props; | ||
const tooltipRef = useRef<HTMLDivElement>(null); | ||
const [tooltipInset, setTooltipInset] = useState({ top: 0, left: 0 }); | ||
|
||
useEffect(() => { | ||
if (tooltipRef.current) { | ||
// console.log('tooltip', tooltipRef.current); | ||
// console.log('tooltipCoord', tooltipRef.current.getBoundingClientRect()); | ||
// left and top < 0 = tooltip is out of the screen | ||
// right or bottom > window.innerWidth or window.innerheight = tooltip is out of the screen | ||
|
||
setTooltipInset({ | ||
left: coordinate.x - tooltipRef.current.offsetWidth / 2, | ||
top: coordinate.y + 20, | ||
}); | ||
} | ||
}, [tooltipRef.current, coordinate]); | ||
if (tooltipData) { | ||
const { payload, name } = tooltipData[0]; | ||
const tooltipName = name.replace('range', ''); | ||
return ( | ||
<TootlipContainer ref={tooltipRef} tooltipInset={tooltipInset}> | ||
<Box | ||
style={{ | ||
display: 'flex', | ||
justifyContent: 'center', | ||
marginBottom: spacing.r8, | ||
}} | ||
> | ||
<Text isEmphazed>View details on Alert Page</Text> | ||
</Box> | ||
<Wrap style={{ flexDirection: 'column', gap: spacing.r8 }}> | ||
<Wrap> | ||
<span>Severity:</span> | ||
<Text>{payload[`${tooltipName}Severity`]}</Text> | ||
</Wrap> | ||
<Wrap> | ||
<span>Start:</span> | ||
<Text> | ||
<FormattedDateTime | ||
format="date-time-second" | ||
value={payload[`range${tooltipName}`][0]} | ||
/> | ||
</Text> | ||
</Wrap> | ||
<Wrap> | ||
<span>End:</span> | ||
<Text> | ||
<FormattedDateTime | ||
format="date-time-second" | ||
value={payload[`range${tooltipName}`][1]} | ||
/> | ||
</Text> | ||
</Wrap> | ||
<Wrap> | ||
<span>Description:</span> | ||
<Text>{payload[`${tooltipName}Description`]}</Text> | ||
</Wrap> | ||
</Wrap> | ||
</TootlipContainer> | ||
); | ||
} | ||
|
||
return null; | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,190 @@ | ||||||
import { Bar, BarChart, XAxis, YAxis, Tooltip, Rectangle } from 'recharts'; | ||||||
import { useTheme } from 'styled-components'; | ||||||
import { useEffect, useState } from 'react'; | ||||||
import { useHistoryAlert } from './HistoryProvider'; | ||||||
import { getDataListOptions, getRadius, getTickFormatter } from './utils'; | ||||||
import { HistoryAlertSlider } from './HistorySlider'; | ||||||
import { CustomTooltip } from './CustomTooltip'; | ||||||
|
||||||
export type GlobalHealthProps = { | ||||||
id: string; | ||||||
alerts: { | ||||||
description: string; | ||||||
startsAt: string; | ||||||
endsAt: string; | ||||||
severity: string; | ||||||
}[]; | ||||||
start: string; | ||||||
end: string; | ||||||
}; | ||||||
const barWidth = 600; // width of the bar chart | ||||||
|
||||||
export function GlobalHealthBar({ id, alerts, start, end }: GlobalHealthProps) { | ||||||
const history = useHistoryAlert(); | ||||||
const [tooltipData, setTooltipData] = useState(null); | ||||||
const theme = useTheme(); | ||||||
|
||||||
useEffect(() => { | ||||||
if (history.selectedDate === 0) { | ||||||
history.setSelectedDate(endDate); | ||||||
} | ||||||
}, []); | ||||||
|
||||||
const startDate = new Date(start).getTime(); | ||||||
const endDate = new Date(end).getTime(); | ||||||
|
||||||
const data = [ | ||||||
{ | ||||||
start: startDate, | ||||||
end: endDate, | ||||||
range: [startDate, endDate], | ||||||
...alerts.reduce((acc, alert, index) => { | ||||||
const key = `${alert.severity}${index}`; | ||||||
acc['range' + key] = [ | ||||||
new Date(alert.startsAt).getTime(), | ||||||
new Date(alert.endsAt).getTime(), | ||||||
]; | ||||||
acc[`${key}Severity`] = alert.severity; | ||||||
acc[`${key}Description`] = alert.description; | ||||||
return acc; | ||||||
}, {}), | ||||||
id, | ||||||
}, | ||||||
]; | ||||||
const warningKeys = Object.keys(data[0]).filter((key) => | ||||||
key.startsWith('rangewarning'), | ||||||
); | ||||||
const criticalKeys = Object.keys(data[0]).filter((key) => | ||||||
key.startsWith('rangecritical'), | ||||||
); | ||||||
|
||||||
const rectangleRenderer = (props, key) => { | ||||||
const { x, y, height, fill } = props; | ||||||
|
||||||
const start = props[key][0] < startDate ? startDate : props[key][0]; | ||||||
const end = props[key][1] > endDate ? endDate : props[key][1]; | ||||||
const relativeSize = (end - start) / (endDate - startDate); | ||||||
return ( | ||||||
<Rectangle | ||||||
x={x < 0 ? 0 : x} | ||||||
y={y} | ||||||
width={relativeSize * barWidth} | ||||||
height={height} | ||||||
fill={fill} | ||||||
radius={getRadius(start, end, startDate, endDate)} | ||||||
></Rectangle> | ||||||
); | ||||||
}; | ||||||
|
||||||
return ( | ||||||
<div | ||||||
style={{ | ||||||
padding: 60, | ||||||
position: 'relative', | ||||||
width: '100%', | ||||||
}} | ||||||
> | ||||||
<HistoryAlertSlider | ||||||
start={start} | ||||||
end={end} | ||||||
startDate={startDate} | ||||||
endDate={endDate} | ||||||
/> | ||||||
|
||||||
<BarChart | ||||||
width={barWidth} | ||||||
height={60} | ||||||
data={data} | ||||||
layout="vertical" | ||||||
margin={{ left: 0, right: 0, bottom: 10 }} | ||||||
maxBarSize={barWidth} | ||||||
> | ||||||
<XAxis | ||||||
allowDataOverflow={true} | ||||||
dataKey="start" | ||||||
type="number" | ||||||
domain={[new Date(start).getTime(), new Date(end).getTime()]} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
tickSize={8} | ||||||
minTickGap={0} | ||||||
interval={0} | ||||||
ticks={getDataListOptions(startDate, endDate)} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
tick={(props) => { | ||||||
const { x, y, payload } = props; | ||||||
return ( | ||||||
<g transform={`translate(${x},${y})`} overflow={'visible'}> | ||||||
<text | ||||||
x={0} | ||||||
y={0} | ||||||
dy={12} | ||||||
textAnchor={'middle'} | ||||||
fill={theme.textSecondary} | ||||||
fontSize={11} | ||||||
> | ||||||
{getTickFormatter( | ||||||
startDate, | ||||||
endDate, | ||||||
new Date(payload.value), | ||||||
)} | ||||||
</text> | ||||||
</g> | ||||||
); | ||||||
}} | ||||||
tickLine={{ stroke: theme.textSecondary }} | ||||||
axisLine={false} | ||||||
/> | ||||||
{!history.selectedDate && ( | ||||||
<Tooltip | ||||||
allowEscapeViewBox={{ x: true, y: true }} | ||||||
offset={20} | ||||||
isAnimationActive={false} | ||||||
cursor={false} | ||||||
content={<CustomTooltip tooltipData={tooltipData}></CustomTooltip>} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could be self-closed |
||||||
/> | ||||||
)} | ||||||
|
||||||
<YAxis yAxisId={'background'} type="category" hide /> | ||||||
<YAxis yAxisId="clip" type="category" hide></YAxis> | ||||||
{[...criticalKeys, ...warningKeys].map((key) => ( | ||||||
<YAxis key={`yAxis${key}`} yAxisId={key} type="category" hide /> | ||||||
))} | ||||||
|
||||||
<Bar | ||||||
dataKey="range" | ||||||
fill={theme.statusHealthy} | ||||||
radius={15} | ||||||
yAxisId="background" | ||||||
isAnimationActive={false} | ||||||
/> | ||||||
|
||||||
{warningKeys.map((key) => ( | ||||||
<Bar | ||||||
dataKey={key} | ||||||
yAxisId={key} | ||||||
key={key} | ||||||
onPointerEnter={(e) => { | ||||||
setTooltipData(e.tooltipPayload); | ||||||
}} | ||||||
onPointerLeave={() => setTooltipData(null)} | ||||||
fill={theme.statusWarning} | ||||||
shape={(props) => rectangleRenderer(props, key)} | ||||||
></Bar> | ||||||
))} | ||||||
|
||||||
{criticalKeys.map((key) => ( | ||||||
<Bar | ||||||
dataKey={key} | ||||||
yAxisId={key} | ||||||
key={key} | ||||||
fill={theme.statusCritical} | ||||||
radius={15} | ||||||
onPointerEnter={(e) => { | ||||||
setTooltipData(e.tooltipPayload); | ||||||
}} | ||||||
onPointerLeave={() => setTooltipData(null)} | ||||||
shape={(props) => rectangleRenderer(props, key)} | ||||||
/> | ||||||
))} | ||||||
</BarChart> | ||||||
</div> | ||||||
); | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createContext, useContext, useState } from 'react'; | ||
|
||
type HistoryAlertContextType = { | ||
selectedDate: number; | ||
setSelectedDate: (date: number) => void; | ||
}; | ||
export const HistoryAlertContext = createContext< | ||
HistoryAlertContextType | undefined | ||
>(undefined); | ||
|
||
export const HistoryAlertProvider = ({ children }) => { | ||
const [selectedDate, setSelectedDate] = useState<number>(Date.now()); | ||
return ( | ||
<HistoryAlertContext.Provider value={{ selectedDate, setSelectedDate }}> | ||
{children} | ||
</HistoryAlertContext.Provider> | ||
); | ||
}; | ||
|
||
export const useHistoryAlert = () => { | ||
const context = useContext(HistoryAlertContext); | ||
if (!context) { | ||
return { selectedDate: null }; | ||
} | ||
return context; | ||
}; | ||
Comment on lines
+3
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This component is not specific to alerts, it is more a "Point in time" selector/provider that can be used more widely. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.