LevisNgigi commented on code in PR #33831: URL: https://github.com/apache/superset/pull/33831#discussion_r2305008520
########## superset-frontend/src/dashboard/components/nativeFilters/ChartCustomization/ChartCustomizationForm.tsx: ########## @@ -0,0 +1,1457 @@ +/** + * 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 { + FC, + useEffect, + useMemo, + useState, + useRef, + useCallback, + ReactNode, +} from 'react'; +import { useSelector } from 'react-redux'; +import { t, styled, css, useTheme } from '@superset-ui/core'; +import { debounce } from 'lodash'; +import { DatasourcesState, ChartsState, RootState } from 'src/dashboard/types'; +import { + Constants, + FormItem, + Input, + Select, + Collapse, + InfoTooltip, + Loading, + Radio, + type SelectValue, + FormInstance, + Checkbox, + CheckboxChangeEvent, +} from '@superset-ui/core/components'; +import { DatasetSelectLabel } from 'src/features/datasets/DatasetSelectLabel'; +import { CollapsibleControl } from '../FiltersConfigModal/FiltersConfigForm/CollapsibleControl'; +import DatasetSelect from '../FiltersConfigModal/FiltersConfigForm/DatasetSelect'; +import { mostUsedDataset } from '../FiltersConfigModal/FiltersConfigForm/utils'; +import { ChartCustomizationItem } from './types'; +import { selectChartCustomizationItems } from './selectors'; + +const { TextArea } = Input; + +interface Metric { + metric_name: string; + verbose_name?: string; +} + +interface DatasetDetails { + id: number; + table_name: string; + schema?: string; + database?: { database_name: string }; +} + +interface ApiError { + message?: string; + error?: string; +} + +interface DatasetColumn { + column_name?: string; + name?: string; + verbose_name?: string; + filterable?: boolean; +} + +interface DatasetData { + id: number; + table_name: string; + schema?: string; + database?: { database_name: string }; + metrics?: Metric[]; + columns?: DatasetColumn[]; +} + +interface CachedDataset { + data: DatasetData; + timestamp: number; +} + +interface ColumnOption { + label: string; + value: string; +} + +interface Props { + form: FormInstance<Record<string, unknown>>; + item: ChartCustomizationItem; + onUpdate: (updatedItem: ChartCustomizationItem) => void; + removedItems: Record<string, { isPending: boolean; timerId?: number } | null>; + allItems?: ChartCustomizationItem[]; +} + +const datasetCache = new Map<number, CachedDataset>(); + +const CACHE_TTL = 5 * 60 * 1000; + +function getCachedDataset(datasetId: number): DatasetData | null { + const cached = datasetCache.get(datasetId); + if (!cached) return null; + + if (Date.now() - cached.timestamp > CACHE_TTL) { + datasetCache.delete(datasetId); + return null; + } + + return cached.data; +} + +function setCachedDataset(datasetId: number, data: DatasetData): void { + datasetCache.set(datasetId, { + data, + timestamp: Date.now(), + }); +} + +const StyledContainer = styled.div` + ${({ theme }) => ` + display: flex; + flex-direction: row; + gap: ${theme.sizeUnit * 4}px; + padding: ${theme.sizeUnit * 2}px; + `} +`; + +const FORM_ITEM_WIDTH = 300; + +const StyledFormItem = styled(FormItem)` + ${({ theme }) => ` + width: ${FORM_ITEM_WIDTH}px; + margin-bottom: ${theme.sizeUnit * 4}px; + + .ant-form-item-label > label { + font-size: ${theme.fontSizeSM}px; + font-weight: ${theme.fontWeightNormal}; + color: ${theme.colorText}; + } + `} +`; + +const CheckboxLabel = styled.span` + ${({ theme }) => ` + font-size: ${theme.fontSizeSM}px; + color: ${theme.colorTextSecondary}; + `} +`; + +const StyledTextArea = styled(TextArea)` + min-height: ${({ theme }) => theme.sizeUnit * 24}px; + resize: vertical; +`; + +const StyledRadioGroup = styled(Radio.Group)` + .ant-radio-wrapper { + font-size: ${({ theme }) => theme.fontSizeSM}px; + } +`; + +const StyledMarginTop = styled.div` + margin-top: ${({ theme }) => theme.sizeUnit * 2}px; +`; + +const ChartCustomizationForm: FC<Props> = ({ + form, + item, + onUpdate, + removedItems, + allItems, +}) => { + const theme = useTheme(); + const customization = useMemo( + () => item.customization || {}, + [item.customization], + ); + + const isRemoved = !!removedItems[item.id]; + + const loadedDatasets = useSelector<RootState, DatasourcesState>( + ({ datasources }) => datasources, + ); + const charts = useSelector<RootState, ChartsState>(({ charts }) => charts); + const globalChartCustomizationItems = useSelector( + selectChartCustomizationItems, + ); + + const chartCustomizationItems = allItems || globalChartCustomizationItems; + + const [metrics, setMetrics] = useState<Metric[]>([]); + const [isDefaultValueLoading, setIsDefaultValueLoading] = useState(false); + const [error, setError] = useState<ApiError | null>(null); + const [datasetDetails, setDatasetDetails] = useState<DatasetDetails | null>( + null, + ); + const [hasDefaultValue, setHasDefaultValue] = useState( + customization.hasDefaultValue ?? false, + ); + const [isRequired, setIsRequired] = useState( + customization.isRequired ?? false, + ); + const [selectFirst, setSelectFirst] = useState( + customization.selectFirst ?? false, + ); + + const [canSelectMultiple, setCanSelectMultiple] = useState( + customization.canSelectMultiple ?? true, + ); + + const fetchedRef = useRef({ + dataset: null, + column: null, + hasDefaultValue: false, + defaultValueDataFetched: false, + }); + + const getDatasetId = useCallback( + ( + dataset: + | string + | number + | { value: string | number } + | { id: string | number } + | null, + ): number | null => { + if (!dataset) return null; + + if (typeof dataset === 'number') return dataset; + if (typeof dataset === 'string') { + const id = Number(dataset); + return Number.isNaN(id) ? null : id; + } + if ( + typeof dataset === 'object' && + dataset !== null && + 'value' in dataset + ) { + const id = Number(dataset.value); + return Number.isNaN(id) ? null : id; + } + if (typeof dataset === 'object' && dataset !== null && 'id' in dataset) { + const id = Number(dataset.id); + return Number.isNaN(id) ? null : id; + } + + return null; + }, + [], + ); + + const excludeDatasetIds = useMemo(() => { + const usedIds: number[] = []; + + chartCustomizationItems.forEach(customItem => { + if (customItem.id === item.id || customItem.removed) { + return; + } + + const { dataset } = customItem.customization; + const datasetId = getDatasetId(dataset); + if (datasetId !== null) { + usedIds.push(datasetId); + } + }); + + return usedIds; + }, [chartCustomizationItems, item.id, getDatasetId]); + + const datasetValue = useMemo(() => { + const datasetId = getDatasetId(customization.dataset); + + if (!datasetId) { + return null; + } + + const loadedDataset = Object.values(loadedDatasets).find( + dataset => dataset.id === Number(datasetId), + ); + + if (loadedDataset) { + return { + value: datasetId, + label: DatasetSelectLabel({ + id: Number(datasetId), + table_name: loadedDataset.table_name || '', + schema: loadedDataset.schema || '', + database: { + database_name: + (loadedDataset.database?.database_name as string) || + (loadedDataset.database?.name as string) || + '', + }, + }), + table_name: loadedDataset.table_name, + schema: loadedDataset.schema, + }; + } + + if (datasetDetails && datasetDetails.id === datasetId) { + return { + value: datasetId, + label: DatasetSelectLabel({ + id: Number(datasetId), + table_name: datasetDetails.table_name || '', + schema: datasetDetails.schema || '', + database: { + database_name: + (datasetDetails.database?.database_name as string) || '', + }, + }), + table_name: datasetDetails.table_name, + schema: datasetDetails.schema, + }; + } + + if (customization.datasetInfo) { + const datasetInfo = customization.datasetInfo as { + value: number; + label: string; + table_name: string; + schema?: string; + }; + return { + value: datasetId, + label: datasetInfo.label, + table_name: datasetInfo.table_name, + schema: datasetInfo.schema, + }; + } + + return { + value: datasetId, + label: `Dataset ${datasetId}`, + }; + }, [ + customization.dataset, + customization.datasetInfo, + datasetDetails, + loadedDatasets, + charts, + getDatasetId, + ]); + + const formChanged = useCallback(() => { + form.setFields([{ name: 'changed', value: true }]); + + const formValues = form.getFieldValue('filters')?.[item.id] || {}; Review Comment: solved -- 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: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org