From fd0862a696318118dc75eaa3cbb92569b2f1e010 Mon Sep 17 00:00:00 2001 From: Ville Brofeldt <33317356+villebro@users.noreply.github.com> Date: Wed, 8 Sep 2021 13:21:21 +0300 Subject: [PATCH] fix(dnd): make clicked dnd metrics unique (#16632) --- .../components/DatasourcePanel/types.ts | 6 ++++++ .../DndMetricSelect.tsx | 20 ++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/superset-frontend/src/explore/components/DatasourcePanel/types.ts b/superset-frontend/src/explore/components/DatasourcePanel/types.ts index 2dcc51732cd1f..22904776fa6f3 100644 --- a/superset-frontend/src/explore/components/DatasourcePanel/types.ts +++ b/superset-frontend/src/explore/components/DatasourcePanel/types.ts @@ -25,3 +25,9 @@ export interface DatasourcePanelDndItem { value: DndItemValue; type: DndItemType; } + +export function isDatasourcePanelDndItem( + item: any, +): item is DatasourcePanelDndItem { + return item?.value && item?.type; +} diff --git a/superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndMetricSelect.tsx b/superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndMetricSelect.tsx index 2fcf53f8a87bf..a200768d82da7 100644 --- a/superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndMetricSelect.tsx +++ b/superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndMetricSelect.tsx @@ -34,7 +34,10 @@ import { usePrevious } from 'src/common/hooks/usePrevious'; import AdhocMetric from 'src/explore/components/controls/MetricControl/AdhocMetric'; import AdhocMetricPopoverTrigger from 'src/explore/components/controls/MetricControl/AdhocMetricPopoverTrigger'; import MetricDefinitionValue from 'src/explore/components/controls/MetricControl/MetricDefinitionValue'; -import { DatasourcePanelDndItem } from 'src/explore/components/DatasourcePanel/types'; +import { + DatasourcePanelDndItem, + isDatasourcePanelDndItem, +} from 'src/explore/components/DatasourcePanel/types'; import { DndItemType } from 'src/explore/components/DndItemType'; import DndSelectLabel from 'src/explore/components/controls/DndColumnSelectControl/DndSelectLabel'; import { savedMetricType } from 'src/explore/components/controls/MetricControl/types'; @@ -143,9 +146,9 @@ export const DndMetricSelect = (props: any) => { const [value, setValue] = useState( coerceAdhocMetrics(props.value), ); - const [droppedItem, setDroppedItem] = useState( - null, - ); + const [droppedItem, setDroppedItem] = useState< + DatasourcePanelDndItem | typeof EMPTY_OBJECT + >({}); const [newMetricPopoverVisible, setNewMetricPopoverVisible] = useState(false); const prevColumns = usePrevious(columns); const prevSavedMetrics = usePrevious(savedMetrics); @@ -323,13 +326,16 @@ export const DndMetricSelect = (props: any) => { ); const handleClickGhostButton = useCallback(() => { - setDroppedItem(null); + setDroppedItem({}); togglePopover(true); }, [togglePopover]); const adhocMetric = useMemo(() => { - if (droppedItem?.type === DndItemType.Column) { - const itemValue = droppedItem?.value as ColumnMeta; + if ( + isDatasourcePanelDndItem(droppedItem) && + droppedItem.type === DndItemType.Column + ) { + const itemValue = droppedItem.value as ColumnMeta; const config: Partial = { column: { column_name: itemValue?.column_name }, };