codeant-ai-for-open-source[bot] commented on code in PR #39434: URL: https://github.com/apache/superset/pull/39434#discussion_r3712538082
########## superset-frontend/src/dashboard/components/PropertiesModal/sections/LabelColorMapping.tsx: ########## @@ -0,0 +1,288 @@ +/** + * 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 } 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 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]); Review Comment: **Suggestion:** `label_colors` values are cast to strings without runtime validation, but they are passed directly to an HTML `input type="color"`, which only accepts valid color values such as hexadecimal colors. Existing metadata containing a named color, null, or another invalid value will be rendered as an invalid color input value and can display a different color or fail to edit the mapping correctly. Validate or normalize each value before constructing the rows. [type error] <details> <summary><b>Severity Level:</b> Minor ๐งน</summary> ```mdx - โ ๏ธ GUI swatches can misrepresent stored label colors. - โ ๏ธ Manual metadata edits can become hard to correct. - โ ๏ธ Invalid values can be resubmitted during label edits. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=30a2e83ee7454b28a8bf7215c790cdb1&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=30a2e83ee7454b28a8bf7215c790cdb1&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent ๐ค </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/dashboard/components/PropertiesModal/sections/LabelColorMapping.tsx **Line:** 136:140 **Comment:** *Type Error: `label_colors` values are cast to strings without runtime validation, but they are passed directly to an HTML `input type="color"`, which only accepts valid color values such as hexadecimal colors. Existing metadata containing a named color, null, or another invalid value will be rendered as an invalid color input value and can display a different color or fail to edit the mapping correctly. Validate or normalize each value before constructing the rows. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39434&comment_hash=714e24c4e5e59c2666c6e080e7c9887e80d8816cd5afc66bfbc81b56bbfcb262&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39434&comment_hash=714e24c4e5e59c2666c6e080e7c9887e80d8816cd5afc66bfbc81b56bbfcb262&reaction=dislike'>๐</a> ########## superset-frontend/src/dashboard/components/PropertiesModal/sections/LabelColorMapping.tsx: ########## @@ -0,0 +1,288 @@ +/** + * 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 } 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 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[]>([]); + + useEffect(() => { + const initialRows = Object.entries(labelColors).map(([label, color]) => ({ + id: generateId(), + label, + color, + })); + setRows(initialRows); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); Review Comment: **Suggestion:** The mappings are initialized only on the first mount, so the component remains empty or displays mappings from a previous dashboard when `jsonMetadata` is populated or changed later. This also breaks the claimed two-way binding when a user edits `label_colors` in the JSON editor. Reinitialize the rows when the parsed metadata changes, while preserving in-progress GUI edits appropriately. [stale reference] <details> <summary><b>Severity Level:</b> Major โ ๏ธ</summary> ```mdx - โ Existing dashboard mappings can disappear from the GUI. - โ Manual JSON edits are not reflected in mapping rows. - โ ๏ธ Users may overwrite unseen label color mappings. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=5f7ff27306e540e4a65a9a728f83551c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=5f7ff27306e540e4a65a9a728f83551c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent ๐ค </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/dashboard/components/PropertiesModal/sections/LabelColorMapping.tsx **Line:** 144:152 **Comment:** *Stale Reference: The mappings are initialized only on the first mount, so the component remains empty or displays mappings from a previous dashboard when `jsonMetadata` is populated or changed later. This also breaks the claimed two-way binding when a user edits `label_colors` in the JSON editor. Reinitialize the rows when the parsed metadata changes, while preserving in-progress GUI edits appropriately. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39434&comment_hash=0ea99f6fab64b9aad6a654a34777e3a53d9bc726339f6c3eedca16c2881a9e0f&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39434&comment_hash=0ea99f6fab64b9aad6a654a34777e3a53d9bc726339f6c3eedca16c2881a9e0f&reaction=dislike'>๐</a> -- 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]
