codeant-ai-for-open-source[bot] commented on code in PR #45599: URL: https://github.com/apache/superset/pull/45599#discussion_r4343993744
########## superset-frontend/src/filters/components/Condition/ConditionFilterPlugin.tsx: ########## @@ -0,0 +1,347 @@ +/** + * 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 { t } from '@apache-superset/core/translation'; +import { + AppSection, + ensureIsArray, + finestTemporalGrainFormatter, + getColumnLabel, +} from '@superset-ui/core'; +import { GenericDataType } from '@apache-superset/core/common'; +import { styled } from '@apache-superset/core/theme'; +import { useCallback, useEffect, useMemo, useState } from 'react'; +import { FormItem, Input, Select } from '@superset-ui/core/components'; +import { FilterBarOrientation } from 'src/dashboard/types'; +import { getDataRecordFormatter } from '../../utils'; +import { FilterPluginStyle, StatusMessage } from '../common'; +import { + ConditionOperator, + ConditionValue, + PluginFilterConditionProps, +} from './types'; +import { + getAllowedOperators, + getConditionError, + getConditionExtraFormData, + getConditionLabel, + isConditionComplete, + isSetConditionOperator, + isUnaryConditionOperator, +} from './utils'; + +const OPERATOR_LABELS: Record<ConditionOperator, string> = { + '==': '=', + '!=': '!=', + '>': '>', + '<': '<', + '>=': '>=', + '<=': '<=', + LIKE: 'LIKE', + IN: 'IN', + 'NOT IN': 'NOT IN', + 'IS NULL': 'IS NULL', + 'IS NOT NULL': 'IS NOT NULL', +}; + +// Operators that pick from the column's existing values. The rest are +// free text (comparisons and LIKE patterns rarely match an existing value). +const PICKER_OPERATORS: ConditionOperator[] = ['==', '!=', 'IN', 'NOT IN']; + +// The Select only takes string or number values. Booleans are shown as +// strings and cast back by coerceConditionValue. +const toSelectValue = (value: unknown): string | number => + typeof value === 'number' ? value : String(value); + +const Wrapper = styled.div<{ horizontal: boolean }>` + display: flex; + flex-direction: ${({ horizontal }) => (horizontal ? 'row' : 'column')}; + gap: ${({ theme }) => theme.sizeUnit * 2}px; + width: 100%; + + .condition-operator { + min-width: ${({ theme }) => theme.sizeUnit * 30}px; + } + + .condition-value { + flex: 1; + min-width: ${({ theme }) => theme.sizeUnit * 40}px; + } +`; + +export default function ConditionFilterPlugin( + props: PluginFilterConditionProps, +) { + const { + data, + coltype, + formData, + height, + width, + setDataMask, + setFocusedFilter, + unsetFocusedFilter, + setHoveredFilter, + unsetHoveredFilter, + setFilterActive, + filterState, + inputRef, + appSection, + filterBarOrientation = FilterBarOrientation.Vertical, + isOverflowingFilterBar, + clearAllTrigger, + onClearAllComplete, + } = props; + const { groupby, enableEmptyFilter, defaultValue } = formData; + const [col = ''] = ensureIsArray(groupby).map(getColumnLabel); + + const [condition, setCondition] = useState<ConditionValue>( + filterState.value ?? + defaultValue ?? { op: getAllowedOperators(coltype)[0] }, + ); + + // The filter bar overrides validateStatus with its own required-value + // check, so errors about the condition itself are computed and shown here. + const conditionError = getConditionError(condition, coltype); + + const updateDataMask = useCallback( + (next: ConditionValue, { restored = false } = {}) => { + if (getConditionError(next, coltype)) { + // While the user is typing, keep the last valid condition so Apply + // can't silently drop the filter. A restored condition that no longer + // fits the column type is cleared so the charts don't receive it. + if (restored) { + setDataMask({ + extraFormData: {}, + filterState: { value: null, label: '', validateStatus: undefined }, + }); + } + return; + } + if (isConditionComplete(next)) { + setDataMask({ + extraFormData: getConditionExtraFormData(col, next, coltype), + filterState: { + value: next, + label: getConditionLabel(col, next), + validateStatus: undefined, + }, + }); + } else if (enableEmptyFilter) { + setDataMask({ + extraFormData: {}, + filterState: { + value: null, + label: '', + validateStatus: 'error', + validateMessage: t('Please enter a condition'), + }, + }); + } else { + setDataMask({ + extraFormData: {}, + filterState: { value: null, label: '', validateStatus: undefined }, + }); + } + }, + [col, coltype, enableEmptyFilter, setDataMask], + ); + + const handleChange = useCallback( + (next: ConditionValue) => { + setCondition(next); + updateDataMask(next); + }, + [updateDataMask], + ); + + // Keep local state in sync with external changes (defaults, "Clear all", + // permalinks) and make sure a restored value is applied to the charts. + useEffect(() => { + const value = filterState.value as ConditionValue | null | undefined; + if (isConditionComplete(value)) { + setCondition(value); + updateDataMask(value, { restored: true }); + } else if (value === null || value === undefined) { + // Cleared from outside: keep the chosen operator, drop the value. + setCondition(prev => ({ op: prev.op })); Review Comment: **Suggestion:** When `filterState.value` is undefined on mount, this clears the configured `defaultValue` from local state, so the default condition is neither shown nor applied. **Assessment:** ๐ `Major` ยท ๐ `Occurrence: Rarely` ยท ๐ท๏ธ `Logic error` [](https://docs.codeant.ai/cli/resolve-pr-comments-skill) [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=57ec89925691433bb40134619f2b40da&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=57ec89925691433bb40134619f2b40da&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) <details> <summary><b>Prompt for AI Agent ๐ค </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-frontend/src/filters/components/Condition/ConditionFilterPlugin.tsx **Line:** 182:182 **Comment:** *Logic Error: When `filterState.value` is undefined on mount, this clears the configured `defaultValue` from local state, so the default condition is neither shown nor applied. 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%2F45599&comment_hash=2f49e5f818414bfcf824e13cfe4d9219f7c40d034222a13b424f30e83fe65827&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F45599&comment_hash=2f49e5f818414bfcf824e13cfe4d9219f7c40d034222a13b424f30e83fe65827&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]
