devabhishekpal commented on code in PR #7042:
URL: https://github.com/apache/ozone/pull/7042#discussion_r1707120553


##########
hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/components/eChart/eChart.tsx:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import React, { useRef, useEffect } from "react";
+import { init, getInstanceByDom } from 'echarts';
+import type { CSSProperties } from "react";
+import type { EChartsOption, ECharts, SetOptionOpts } from 'echarts';
+
+export interface EChartProps {
+  option: EChartsOption;
+  style?: CSSProperties;
+  settings?: SetOptionOpts;
+  loading?: boolean;
+  theme?: 'light';
+  onClick?: () => any | void;
+}
+
+const EChart = ({
+  option,
+  style,
+  settings,
+  loading,
+  theme,
+  onClick
+}: EChartProps): JSX.Element => {
+  const chartRef = useRef<HTMLDivElement>(null);
+  useEffect(() => {
+    // Initialize chart
+    let chart: ECharts | undefined;
+    if (chartRef.current !== null) {
+      chart = init(chartRef.current, theme);
+      if (onClick) {
+        chart.on('click', onClick);
+      }
+    }
+
+    // Add chart resize listener
+    // ResizeObserver is leading to a bit janky UX
+    function resizeChart() {
+      chart?.resize();
+    }
+    window.addEventListener("resize", resizeChart);
+
+    // Return cleanup function
+    return () => {
+      chart?.dispose();
+      window.removeEventListener("resize", resizeChart);
+    };
+  }, [theme]);
+
+  useEffect(() => {
+    // Update chart
+    if (chartRef.current !== null) {
+      const chart = getInstanceByDom(chartRef.current);
+      chart!.setOption(option, settings);
+      if (onClick) {
+        chart!.on('click', onClick);

Review Comment:
   So in this scenario actually the non-null assert is being done to avoid 
TypeScript giving errors.
   We are already sure that the element won't be undefined since the first 
useEffect hook will be running on the first render allocating the chart.
   But TypeScript doesn't check if it has already been allocated and gives an 
error before-hand as the type is possibly undefined for it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org
For additional commands, e-mail: issues-h...@ozone.apache.org

Reply via email to