Skip to content

Commit 1700851

Browse files
committed
v6.6.3
1 parent 1370cab commit 1700851

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netdata/charts",
3-
"version": "6.6.2",
3+
"version": "6.6.3",
44
"description": "Netdata frontend SDK and chart utilities",
55
"main": "dist/index.js",
66
"module": "dist/es6/index.js",

src/components/drawer/compare/customPeriodForm.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import React, { useState } from "react"
22
import { Flex, TextSmall, TextMicro, Button } from "@netdata/netdata-ui"
33

4-
const CustomPeriodForm = ({ onAdd, onCancel }) => {
5-
const [label, setLabel] = useState("")
6-
const [offsetDays, setOffsetDays] = useState("")
7-
const [offsetHours, setOffsetHours] = useState("")
4+
const normalizeInitialValues = initialValues => {
5+
const { label = "", offsetSeconds = 0 } = initialValues || {}
86

9-
const generateLabel = (days, hours) => {
10-
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "always" })
7+
const days = Math.floor(offsetSeconds / (24 * 60 * 60))
8+
const hours = Math.floor((offsetSeconds % (24 * 60 * 60)) / (60 * 60))
9+
return { label, days: days ? days.toString() : "", hours: hours ? hours.toString() : "" }
10+
}
1111

12-
if (days > 0 && hours === 0) {
13-
return rtf.format(-days, "day")
14-
}
15-
if (hours > 0 && days === 0) {
16-
return rtf.format(-hours, "hour")
17-
}
18-
if (days > 0 && hours > 0) {
19-
return rtf.format(-days, "day")
20-
}
21-
return ""
12+
const generateLabel = (days, hours) => {
13+
const rtf = new Intl.RelativeTimeFormat("en", { numeric: "always" })
14+
15+
if (days > 0 && hours === 0) {
16+
return rtf.format(-days, "day")
2217
}
18+
if (hours > 0 && days === 0) {
19+
return rtf.format(-hours, "hour")
20+
}
21+
if (days > 0 && hours > 0) {
22+
return `${rtf.format(-days, "day")} and ${rtf.format(-hours, "hour")}`
23+
}
24+
return ""
25+
}
26+
27+
const CustomPeriodForm = ({ onSubmit, onCancel, initialValues }) => {
28+
const normalizedValues = normalizeInitialValues(initialValues)
29+
30+
const [label, setLabel] = useState(normalizedValues.label)
31+
const [offsetDays, setOffsetDays] = useState(normalizedValues.days)
32+
const [offsetHours, setOffsetHours] = useState(normalizedValues.hours)
2333

2434
const handleAdd = () => {
2535
const days = parseInt(offsetDays) || 0
@@ -32,12 +42,12 @@ const CustomPeriodForm = ({ onAdd, onCancel }) => {
3242
if (!finalLabel) return
3343

3444
const customPeriod = {
35-
id: `custom_${Date.now()}`,
45+
id: initialValues.id || `custom_${Date.now()}`,
3646
label: finalLabel,
3747
offsetSeconds,
3848
}
3949

40-
onAdd(customPeriod)
50+
onSubmit(customPeriod)
4151
}
4252

4353
return (
@@ -103,7 +113,7 @@ const CustomPeriodForm = ({ onAdd, onCancel }) => {
103113
</Flex>
104114

105115
<Flex gap={2}>
106-
<Button tiny label="Add" onClick={handleAdd} />
116+
<Button tiny label={initialValues ? "Update" : "Add"} onClick={handleAdd} />
107117
<Button tiny label="Cancel" onClick={onCancel} />
108118
</Flex>
109119
</Flex>

src/components/drawer/compare/index.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import React, { useState } from "react"
22
import { Flex, TextSmall, TextMicro, Button } from "@netdata/netdata-ui"
33
import styled from "styled-components"
44
import Tooltip from "@/components/tooltip"
5+
import Icon, { Button as IconButton } from "@/components/icon"
56
import useData from "./useData"
67
import { useChart, convert, useAttributeValue } from "@/components/provider"
8+
import pencilIcon from "@netdata/netdata-ui/dist/components/icon/assets/pencil_outline.svg"
79
import ChangeIndicator from "./changeIndicator"
810
import CustomPeriodForm from "./customPeriodForm"
911

@@ -60,19 +62,44 @@ const ComparisonCard = ({ period, showAdvanced, tab }) => {
6062
const dateRange = formatDateRange(chart, period.after, period.before)
6163
const hasData = period.payload && period.stats && !period.error
6264

65+
const [showEditForm, setShowEditForm] = useState(false)
66+
67+
const updatePeriod = updated => {
68+
const currentCustomPeriods = chart.getAttribute("customPeriods", [])
69+
70+
const updatedPeriods = currentCustomPeriods.map(p => (p.id === updated.id ? updated : p))
71+
chart.updateAttribute("customPeriods", updatedPeriods)
72+
setShowEditForm(false)
73+
}
74+
6375
return (
6476
<Flex column gap={2} padding={[3]} border="all" round>
65-
<Flex column gap={1}>
66-
<TextSmall strong>{period.label}</TextSmall>
67-
<TextMicro color="textDescription">{dateRange}</TextMicro>
68-
</Flex>
77+
<Tooltip content={dateRange}>
78+
<Flex alignItems="center" gap={1} justifyContent="between">
79+
<TextSmall strong>{period.label}</TextSmall>
80+
{!period.isBase && (
81+
<IconButton
82+
icon={<Icon svg={pencilIcon} size="10px" />}
83+
onClick={() => setShowEditForm(true)}
84+
data-testid="period-edit"
85+
data-track={chart.track("period-edit")}
86+
/>
87+
)}
88+
</Flex>
89+
</Tooltip>
6990

7091
{!hasData ? (
7192
<Flex column gap={1}>
7293
<TextMicro color="textDescription">
7394
{period.error ? "Error loading data" : "No data available for the selected time range"}
7495
</TextMicro>
7596
</Flex>
97+
) : showEditForm ? (
98+
<CustomPeriodForm
99+
initialValues={period}
100+
onSubmit={updatePeriod}
101+
onCancel={() => setShowEditForm(false)}
102+
/>
76103
) : (
77104
<Flex column gap={1}>
78105
{basicStats.map(stat => (
@@ -133,7 +160,7 @@ const Compare = () => {
133160
<ComparisonCard key={period.id} period={period} showAdvanced={showAllStats} tab={tab} />
134161
))}
135162

136-
{periods.length > 0 && !showCustomForm && (
163+
{!showCustomForm ? (
137164
<Flex
138165
column
139166
gap={2}
@@ -149,10 +176,8 @@ const Compare = () => {
149176
<Button tiny label="Select a timeframe" onClick={() => setShowCustomForm(true)} />
150177
</Tooltip>
151178
</Flex>
152-
)}
153-
154-
{showCustomForm && (
155-
<CustomPeriodForm onAdd={addCustomPeriod} onCancel={() => setShowCustomForm(false)} />
179+
) : (
180+
<CustomPeriodForm onSubmit={addCustomPeriod} onCancel={() => setShowCustomForm(false)} />
156181
)}
157182
</GridContainer>
158183
</Flex>

0 commit comments

Comments
 (0)