bito-code-review[bot] commented on code in PR #39434:
URL: https://github.com/apache/superset/pull/39434#discussion_r3714062397


##########
superset-frontend/src/dashboard/components/PropertiesModal/sections/LabelColorMapping.tsx:
##########
@@ -0,0 +1,295 @@
+/**
+ * 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, { useMemo, useState, useEffect, useRef } from 'react';
+import { t } from '@apache-superset/core/translation';
+import { styled } from '@apache-superset/core/theme';
+import Select from 'src/components/Select/Select';
+
+const Container = styled.div`
+  margin-bottom: ${({ theme }) => (theme?.gridUnit || 4) * 4}px;
+  padding: ${({ theme }) => (theme?.gridUnit || 4) * 4}px;
+  background-color: ${({ theme }) =>
+    theme?.colors?.grayscale?.light4 || '#f6f6f6'};
+  border-radius: ${({ theme }) => theme?.borderRadius || 4}px;
+`;
+
+const HeaderRow = styled.div`
+  display: flex;
+  justify-content: space-between;
+  align-items: center;
+  margin-bottom: ${({ theme }) => (theme?.gridUnit || 4) * 4}px;
+`;
+
+const Row = styled.div`
+  display: flex;
+  align-items: center;
+  margin-bottom: ${({ theme }) => (theme?.gridUnit || 4) * 2}px;
+  gap: ${({ theme }) => (theme?.gridUnit || 4) * 4}px;
+`;
+
+const SelectContainer = styled.div`
+  flex: 1;
+`;
+
+const ColorContainer = styled.div`
+  display: flex;
+  align-items: center;
+  gap: ${({ theme }) => theme?.gridUnit || 4}px;
+`;
+
+const StyledColorInput = styled.input`
+  cursor: pointer;
+  height: 34px;
+  width: 40px;
+  padding: 0;
+  border: 1px solid
+    ${({ theme }) => theme?.colors?.grayscale?.light2 || '#e0e0e0'};
+  border-radius: ${({ theme }) => theme?.borderRadius || 4}px;
+  outline: none;
+  background: none;
+
+  &::-webkit-color-swatch-wrapper {
+    padding: 2px;
+  }
+  &::-webkit-color-swatch {
+    border: none;
+    border-radius: 2px;
+  }
+`;
+
+const ActionButton = styled.button`
+  background: transparent;
+  border: none;
+  color: ${({ theme }) => theme?.colors?.grayscale?.base || '#666666'};
+  cursor: pointer;
+  padding: 0;
+  font-size: 16px;
+  transition: color 0.2s;
+
+  &:hover {
+    color: ${({ theme }) => theme?.colors?.error?.base || '#e04355'};
+  }
+`;
+
+const AddMoreLink = styled.div`
+  color: ${({ theme }) => theme?.colors?.primary?.dark1 || '#1a85a0'};
+  font-size: 14px;
+  font-weight: bold;
+  cursor: pointer;
+  margin-top: ${({ theme }) => (theme?.gridUnit || 4) * 2}px;
+  display: inline-block;
+
+  &:hover {
+    text-decoration: underline;
+  }
+`;
+
+interface LabelColorMappingProps {
+  jsonMetadata: string;
+  onJsonMetadataChange: (value: string) => void;
+}
+
+interface ColorMapping {
+  id: string;
+  label: string;
+  color: string;
+}
+
+const DEFAULT_NEW_COLOR = '#000000';
+
+const generateId = () => Math.random().toString(36).substring(2, 9);
+const isValidHex = (color: string) => /^#[0-9A-Fa-f]{6}$/i.test(color);
+
+const LabelColorMapping: React.FC<LabelColorMappingProps> = ({
+  jsonMetadata,
+  onJsonMetadataChange,
+}) => {
+  const metadataObj = useMemo<Record<string, unknown>>(() => {
+    try {
+      const parsed = jsonMetadata ? JSON.parse(jsonMetadata) : {};
+      return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
+        ? (parsed as Record<string, unknown>)
+        : {};
+    } catch (error: unknown) {
+      if (error instanceof SyntaxError) {
+        return {};
+      }
+      throw error;
+    }
+  }, [jsonMetadata]);
+
+  const labelColors = useMemo(() => metadataObj.label_colors &&
+      typeof metadataObj.label_colors === 'object' &&
+      !Array.isArray(metadataObj.label_colors)
+      ? (metadataObj.label_colors as Record<string, string>)
+      : {}, [metadataObj]);
+
+  const [rows, setRows] = useState<ColorMapping[]>([]);
+  const lastSyncMetadata = useRef<string>(jsonMetadata);
+
+  useEffect(() => {
+    if (lastSyncMetadata.current !== jsonMetadata) {
+      const initialRows = Object.entries(labelColors).map(([label, color]) => 
({
+        id: generateId(),
+        label,
+        color: isValidHex(color) ? color : DEFAULT_NEW_COLOR,
+      }));

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Stale closure from useEffect empty deps</b></div>
   <div id="fix">
   
   The useEffect with empty dependency array captures the initial `labelColors` 
via closure. When `jsonMetadata` changes externally (dashboard refresh, 
undo/redo, concurrent edits), the effect doesn't re-run, leaving `rows` state 
stale and causing the UI to desync from the actual metadata.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
       setRows(initialRows);
       // eslint-disable-next-line react-hooks/exhaustive-deps
     }, [jsonMetadata]);
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #3c69f1</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to