-
Notifications
You must be signed in to change notification settings - Fork 14.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Implement drag and drop for metrics #13575
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
301 changes: 301 additions & 0 deletions
301
superset-frontend/src/explore/components/controls/DndColumnSelectControl/DndMetricSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,301 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with work for additional information | ||
* regarding copyright ownership. The ASF licenses file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use 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, { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { ensureIsArray, Metric, t } from '@superset-ui/core'; | ||
import { ColumnMeta } from '@superset-ui/chart-controls'; | ||
import { isEqual } from 'lodash'; | ||
import { usePrevious } from 'src/common/hooks/usePrevious'; | ||
import AdhocMetric from '../MetricControl/AdhocMetric'; | ||
import AdhocMetricPopoverTrigger from '../MetricControl/AdhocMetricPopoverTrigger'; | ||
import MetricDefinitionValue from '../MetricControl/MetricDefinitionValue'; | ||
import { OptionValueType } from './types'; | ||
import { DatasourcePanelDndItem } from '../../DatasourcePanel/types'; | ||
import { DndItemType } from '../../DndItemType'; | ||
import DndSelectLabel from './DndSelectLabel'; | ||
import { savedMetricType } from '../MetricControl/types'; | ||
|
||
const isDictionaryForAdhocMetric = (value: any) => | ||
value && !(value instanceof AdhocMetric) && value.expressionType; | ||
|
||
const coerceAdhocMetrics = (value: any) => { | ||
if (!value) { | ||
return []; | ||
} | ||
if (!Array.isArray(value)) { | ||
if (isDictionaryForAdhocMetric(value)) { | ||
return [new AdhocMetric(value)]; | ||
} | ||
return [value]; | ||
} | ||
return value.map(val => { | ||
if (isDictionaryForAdhocMetric(val)) { | ||
return new AdhocMetric(val); | ||
} | ||
return val; | ||
}); | ||
}; | ||
|
||
const getOptionsForSavedMetrics = ( | ||
savedMetrics: savedMetricType[], | ||
currentMetricValues: (string | AdhocMetric)[], | ||
currentMetric?: string, | ||
) => | ||
savedMetrics?.filter(savedMetric => | ||
Array.isArray(currentMetricValues) | ||
? !currentMetricValues.includes(savedMetric.metric_name ?? '') || | ||
savedMetric.metric_name === currentMetric | ||
: savedMetric, | ||
) ?? []; | ||
|
||
const columnsContainAllMetrics = ( | ||
value: (string | AdhocMetric | ColumnMeta)[], | ||
columns: ColumnMeta[], | ||
savedMetrics: savedMetricType[], | ||
) => { | ||
const columnNames = new Set( | ||
[...(columns || []), ...(savedMetrics || [])] | ||
// eslint-disable-next-line camelcase | ||
.map( | ||
item => | ||
(item as ColumnMeta).column_name || | ||
(item as savedMetricType).metric_name, | ||
), | ||
); | ||
|
||
return ( | ||
ensureIsArray(value) | ||
.filter(metric => metric) | ||
// find column names | ||
.map(metric => | ||
(metric as AdhocMetric).column | ||
? (metric as AdhocMetric).column.column_name | ||
: (metric as ColumnMeta).column_name || metric, | ||
) | ||
.filter(name => name && typeof name === 'string') | ||
.every(name => columnNames.has(name)) | ||
Comment on lines
+86
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add type guards for the types in |
||
); | ||
}; | ||
|
||
export const DndMetricSelect = (props: any) => { | ||
const { onChange, multi, columns, savedMetrics } = props; | ||
|
||
const handleChange = useCallback( | ||
opts => { | ||
// if clear out options | ||
if (opts === null) { | ||
onChange(null); | ||
return; | ||
} | ||
|
||
const transformedOpts = ensureIsArray(opts); | ||
const optionValues = transformedOpts | ||
.map(option => { | ||
// pre-defined metric | ||
if (option.metric_name) { | ||
return option.metric_name; | ||
} | ||
return option; | ||
}) | ||
.filter(option => option); | ||
onChange(multi ? optionValues : optionValues[0]); | ||
}, | ||
[multi, onChange], | ||
); | ||
|
||
const [value, setValue] = useState<(AdhocMetric | Metric | string)[]>( | ||
coerceAdhocMetrics(props.value), | ||
); | ||
const [droppedItem, setDroppedItem] = useState<DatasourcePanelDndItem | null>( | ||
null, | ||
); | ||
const [newMetricPopoverVisible, setNewMetricPopoverVisible] = useState(false); | ||
const prevColumns = usePrevious(columns); | ||
const prevSavedMetrics = usePrevious(savedMetrics); | ||
|
||
useEffect(() => { | ||
setValue(coerceAdhocMetrics(props.value)); | ||
}, [JSON.stringify(props.value)]); | ||
|
||
useEffect(() => { | ||
if ( | ||
!isEqual(prevColumns, columns) || | ||
!isEqual(prevSavedMetrics, savedMetrics) | ||
) { | ||
// Remove all metrics if selected value no longer a valid column | ||
// in the dataset. Must use `nextProps` here because Redux reducers may | ||
// have already updated the value for this control. | ||
if (!columnsContainAllMetrics(props.value, columns, savedMetrics)) { | ||
onChange([]); | ||
} | ||
} | ||
}, [ | ||
prevColumns, | ||
columns, | ||
prevSavedMetrics, | ||
savedMetrics, | ||
props.value, | ||
onChange, | ||
]); | ||
|
||
const canDrop = (item: DatasourcePanelDndItem) => { | ||
const isMetricAlreadyInValues = | ||
item.type === 'metric' ? value.includes(item.value.metric_name) : false; | ||
return (props.multi || value.length === 0) && !isMetricAlreadyInValues; | ||
}; | ||
|
||
const onNewMetric = (newMetric: Metric) => { | ||
const newValue = [...value, newMetric]; | ||
setValue(newValue); | ||
handleChange(newValue); | ||
}; | ||
|
||
const onMetricEdit = ( | ||
changedMetric: Metric | AdhocMetric, | ||
oldMetric: Metric | AdhocMetric, | ||
) => { | ||
const newValue = value.map(value => { | ||
if ( | ||
// compare saved metrics | ||
value === (oldMetric as Metric).metric_name || | ||
// compare adhoc metrics | ||
typeof (value as AdhocMetric).optionName !== 'undefined' | ||
? (value as AdhocMetric).optionName === | ||
(oldMetric as AdhocMetric).optionName | ||
: false | ||
) { | ||
return changedMetric; | ||
} | ||
return value; | ||
}); | ||
setValue(newValue); | ||
handleChange(newValue); | ||
}; | ||
|
||
const onRemoveMetric = (index: number) => { | ||
if (!Array.isArray(value)) { | ||
return; | ||
} | ||
const valuesCopy = [...value]; | ||
valuesCopy.splice(index, 1); | ||
setValue(valuesCopy); | ||
onChange(valuesCopy); | ||
}; | ||
|
||
const moveLabel = (dragIndex: number, hoverIndex: number) => { | ||
const newValues = [...value]; | ||
[newValues[hoverIndex], newValues[dragIndex]] = [ | ||
newValues[dragIndex], | ||
newValues[hoverIndex], | ||
]; | ||
setValue(newValues); | ||
}; | ||
|
||
const valueRenderer = ( | ||
option: Metric | AdhocMetric | string, | ||
index: number, | ||
) => ( | ||
<MetricDefinitionValue | ||
key={index} | ||
index={index} | ||
option={option} | ||
onMetricEdit={onMetricEdit} | ||
onRemoveMetric={() => onRemoveMetric(index)} | ||
columns={props.columns} | ||
savedMetrics={props.savedMetrics} | ||
savedMetricsOptions={getOptionsForSavedMetrics( | ||
props.savedMetrics, | ||
props.value, | ||
props.value?.[index], | ||
)} | ||
datasourceType={props.datasourceType} | ||
onMoveLabel={moveLabel} | ||
onDropLabel={() => onChange(value)} | ||
/> | ||
); | ||
|
||
const valuesRenderer = () => | ||
value.map((value, index) => valueRenderer(value, index)); | ||
|
||
const togglePopover = (visible: boolean) => { | ||
setNewMetricPopoverVisible(visible); | ||
}; | ||
|
||
const closePopover = () => { | ||
togglePopover(false); | ||
}; | ||
|
||
const { savedMetric, adhocMetric } = useMemo(() => { | ||
if (droppedItem?.type === 'column') { | ||
const itemValue = droppedItem?.value as ColumnMeta; | ||
return { | ||
savedMetric: {} as savedMetricType, | ||
adhocMetric: new AdhocMetric({ | ||
column: { column_name: itemValue?.column_name }, | ||
}), | ||
}; | ||
} | ||
if (droppedItem?.type === 'metric') { | ||
const itemValue = droppedItem?.value as savedMetricType; | ||
return { | ||
savedMetric: itemValue, | ||
adhocMetric: new AdhocMetric({ isNew: true }), | ||
}; | ||
} | ||
return { | ||
savedMetric: {} as savedMetricType, | ||
adhocMetric: new AdhocMetric({ isNew: true }), | ||
}; | ||
}, [droppedItem?.type, droppedItem?.value]); | ||
|
||
return ( | ||
<div className="metrics-select"> | ||
<DndSelectLabel<OptionValueType, OptionValueType[]> | ||
values={value} | ||
onDrop={(item: DatasourcePanelDndItem) => { | ||
setDroppedItem(item); | ||
togglePopover(true); | ||
}} | ||
canDrop={canDrop} | ||
valuesRenderer={valuesRenderer} | ||
accept={[DndItemType.Column, DndItemType.Metric]} | ||
placeholderText={t('Drop columns or metrics')} | ||
{...props} | ||
/> | ||
<AdhocMetricPopoverTrigger | ||
adhocMetric={adhocMetric} | ||
onMetricEdit={onNewMetric} | ||
columns={props.columns} | ||
savedMetricsOptions={getOptionsForSavedMetrics( | ||
props.savedMetrics, | ||
props.value, | ||
)} | ||
savedMetric={savedMetric} | ||
datasourceType={props.datasourceType} | ||
isControlledComponent | ||
visible={newMetricPopoverVisible} | ||
togglePopover={togglePopover} | ||
closePopover={closePopover} | ||
createNew | ||
> | ||
<div /> | ||
</AdhocMetricPopoverTrigger> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bycatch: I believe
Metric
is originally defined in@superset-ui/core
(probably better to import from there, as I'm unsure why it's exported by@superset-ui/chart-controls
)