Skip to content

Commit

Permalink
Refactor MetricMonitorStepForm to eliminate unnecessary state managem…
Browse files Browse the repository at this point in the history
…ent and streamline onChange handling
  • Loading branch information
simlarsen committed Dec 12, 2024
1 parent ceead6e commit cd58b72
Showing 1 changed file with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import MonitorStepMetricMonitor, {
MonitorStepMetricMonitorUtil,
} from "Common/Types/Monitor/MonitorStepMetricMonitor";
import React, { FunctionComponent, ReactElement, useEffect } from "react";
import React, { FunctionComponent, ReactElement } from "react";
import MetricView from "../../../Metrics/MetricView";
import RollingTime from "Common/Types/RollingTime/RollingTime";
import InBetween from "Common/Types/BaseDatabase/InBetween";
Expand All @@ -18,15 +18,8 @@ export interface ComponentProps {
const MetricMonitorStepForm: FunctionComponent<ComponentProps> = (
props: ComponentProps,
): ReactElement => {
const [monitorStepMetricMonitor, setMonitorStepMetricMonitor] =
React.useState<MonitorStepMetricMonitor>(
props.monitorStepMetricMonitor ||
MonitorStepMetricMonitorUtil.getDefault(),
);

useEffect(() => {
props.onChange(monitorStepMetricMonitor);
}, [monitorStepMetricMonitor]);
const monitorStepMetricMonitor: MonitorStepMetricMonitor =
props.monitorStepMetricMonitor || MonitorStepMetricMonitorUtil.getDefault();

const startAndEndDate: InBetween<Date> =
RollingTimeUtil.convertToStartAndEndDate(
Expand All @@ -43,7 +36,7 @@ const MetricMonitorStepForm: FunctionComponent<ComponentProps> = (
<RollingTimePicker
value={monitorStepMetricMonitor.rollingTime}
onChange={(value: RollingTime) => {
setMonitorStepMetricMonitor({
props.onChange({
...monitorStepMetricMonitor,
rollingTime: value,
});
Expand Down Expand Up @@ -73,7 +66,7 @@ const MetricMonitorStepForm: FunctionComponent<ComponentProps> = (
chartCssClass="rounded-md border border-gray-200 mt-2"
onChange={(data: MetricViewData) => {
// we dont care about start and end time here because it is not editable in metric view but editable in rolling time picker.
setMonitorStepMetricMonitor({
props.onChange({
...monitorStepMetricMonitor,
metricViewConfig: {
queryConfigs: data.queryConfigs,
Expand Down

0 comments on commit cd58b72

Please sign in to comment.