Skip to content

Commit

Permalink
fix(dnd): make clicked dnd metrics unique (apache#16632)
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored Sep 8, 2021
1 parent 5027d42 commit fd0862a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ export interface DatasourcePanelDndItem {
value: DndItemValue;
type: DndItemType;
}

export function isDatasourcePanelDndItem(
item: any,
): item is DatasourcePanelDndItem {
return item?.value && item?.type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -143,9 +146,9 @@ export const DndMetricSelect = (props: any) => {
const [value, setValue] = useState<ValueType[]>(
coerceAdhocMetrics(props.value),
);
const [droppedItem, setDroppedItem] = useState<DatasourcePanelDndItem | null>(
null,
);
const [droppedItem, setDroppedItem] = useState<
DatasourcePanelDndItem | typeof EMPTY_OBJECT
>({});
const [newMetricPopoverVisible, setNewMetricPopoverVisible] = useState(false);
const prevColumns = usePrevious(columns);
const prevSavedMetrics = usePrevious(savedMetrics);
Expand Down Expand Up @@ -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<AdhocMetric> = {
column: { column_name: itemValue?.column_name },
};
Expand Down

0 comments on commit fd0862a

Please sign in to comment.