rusackas commented on code in PR #39434: URL: https://github.com/apache/superset/pull/39434#discussion_r3716905210
########## superset-frontend/src/dashboard/components/PropertiesModal/sections/LabelColorMapping.tsx: ########## @@ -0,0 +1,207 @@ +/** + * 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 } from 'react'; +import { t } from '@apache-superset/core/translation'; +import { styled } from '@apache-superset/core/theme'; + +const Container = styled.div` + margin-bottom: ${({ theme }: any) => (theme?.gridUnit || 4) * 4}px; + padding: ${({ theme }: any) => (theme?.gridUnit || 4) * 4}px; + background-color: ${({ theme }: any) => theme?.colors?.grayscale?.light4 || '#f6f6f6'}; + border-radius: ${({ theme }: any) => theme?.borderRadius || 4}px; +`; + +const Row = styled.div` + display: flex; + align-items: center; + margin-bottom: ${({ theme }: any) => (theme?.gridUnit || 4) * 2}px; + gap: ${({ theme }: any) => (theme?.gridUnit || 4) * 2}px; +`; + +const HeaderRow = styled.div` + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: ${({ theme }: any) => (theme?.gridUnit || 4) * 4}px; +`; + +// Bulletproof Styled Components to mimic Ant Design +const StyledInput = styled.input` + flex: 1; + width: 100%; + padding: ${({ theme }: any) => (theme?.gridUnit || 4) * 1.5}px ${({ theme }: any) => (theme?.gridUnit || 4) * 2}px; + font-size: 14px; + border-radius: ${({ theme }: any) => theme?.borderRadius || 4}px; + border: 1px solid ${({ theme }: any) => theme?.colors?.grayscale?.light2 || '#e0e0e0'}; + color: ${({ theme }: any) => theme?.colors?.grayscale?.dark1 || '#333333'}; + outline: none; + + &:focus { + border-color: ${({ theme }: any) => theme?.colors?.primary?.base || '#20a7c9'}; + } +`; + +const StyledColorInput = styled.input` + cursor: pointer; + height: 34px; + width: 40px; + padding: 0; + border: 1px solid ${({ theme }: any) => theme?.colors?.grayscale?.light2 || '#e0e0e0'}; + border-radius: ${({ theme }: any) => theme?.borderRadius || 4}px; + outline: none; + background: none; + + &::-webkit-color-swatch-wrapper { + padding: 2px; + } + &::-webkit-color-swatch { + border: none; + border-radius: 2px; + } +`; + +const StyledButton = styled.button<{ variant?: 'danger' | 'primary' }>` + display: flex; + align-items: center; + justify-content: center; + padding: ${({ theme }: any) => theme?.gridUnit || 4}px ${({ theme }: any) => (theme?.gridUnit || 4) * 3}px; + font-size: 12px; + font-weight: bold; + text-transform: uppercase; + cursor: pointer; + border: none; + border-radius: ${({ theme }: any) => theme?.borderRadius || 4}px; + background-color: ${({ theme, variant }: any) => + variant === 'danger' Review Comment: This one's stale, StyledButton with a variant prop doesn't exist anymore. It got refactored into ActionButton with no variant logic. The same any-typing pattern is still open on the sibling comments for this file though. ########## 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: Fixed, color values are validated against isValidHex with a theme fallback before they reach the color input now. ########## 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: Fixed, rows resync whenever jsonMetadata changes now via the lastSyncMetadata ref guard. ########## 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: Fixed, and taken further than the suggested diff. deps are [jsonMetadata, labelColors, theme.colors.grayscale.dark2] now, plus a ref guard against the effect's own writes. -- 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]
