-
Notifications
You must be signed in to change notification settings - Fork 16.6k
refactor: convert controlUtils to TypeScript (1 of 2) #13401
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * 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 memoizeOne from 'memoize-one'; | ||
| import { getChartControlPanelRegistry } from '@superset-ui/core'; | ||
| import { | ||
| ControlPanelSectionConfig, | ||
| expandControlConfig, | ||
| } from '@superset-ui/chart-controls'; | ||
|
|
||
| const getMemoizedControlConfig = memoizeOne( | ||
| (controlKey, controlPanelConfig) => { | ||
| const { | ||
| controlOverrides = {}, | ||
| controlPanelSections = [], | ||
| } = controlPanelConfig; | ||
| const control = expandControlConfig( | ||
| findControlItem(controlPanelSections, controlKey), | ||
| controlOverrides, | ||
| ); | ||
| return control && 'config' in control ? control.config : control; | ||
| }, | ||
| ); | ||
|
|
||
| /** | ||
| * Find control item from control panel config. | ||
| */ | ||
| export function findControlItem( | ||
| controlPanelSections: ControlPanelSectionConfig[], | ||
| controlKey: string, | ||
| ) { | ||
| return ( | ||
| controlPanelSections | ||
| .map(section => section.controlSetRows) | ||
| .flat(2) | ||
| .find( | ||
| control => | ||
| controlKey === control || | ||
| (control !== null && | ||
| typeof control === 'object' && | ||
| 'name' in control && | ||
| control.name === controlKey), | ||
| ) ?? null | ||
| ); | ||
| } | ||
|
|
||
| export const getControlConfig = function getControlConfig( | ||
| controlKey: string, | ||
| vizType: string, | ||
| ) { | ||
| const controlPanelConfig = getChartControlPanelRegistry().get(vizType) || {}; | ||
| return getMemoizedControlConfig(controlKey, controlPanelConfig); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /** | ||
| * 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 memoizeOne from 'memoize-one'; | ||
| import { | ||
| DatasourceType, | ||
| getChartControlPanelRegistry, | ||
| } from '@superset-ui/core'; | ||
| import { | ||
| ControlPanelConfig, | ||
| expandControlConfig, | ||
| } from '@superset-ui/chart-controls'; | ||
|
|
||
| import * as SECTIONS from 'src/explore/controlPanels/sections'; | ||
|
|
||
| const getMemoizedSectionsToRender = memoizeOne( | ||
| (datasourceType: DatasourceType, controlPanelConfig: ControlPanelConfig) => { | ||
| const { | ||
| sectionOverrides = {}, | ||
| controlOverrides, | ||
| controlPanelSections = [], | ||
| } = controlPanelConfig; | ||
|
|
||
| // default control panel sections | ||
| const sections = { ...SECTIONS }; | ||
|
|
||
| // apply section overrides | ||
| Object.entries(sectionOverrides).forEach(([section, overrides]) => { | ||
| if (typeof overrides === 'object' && overrides.constructor === Object) { | ||
|
||
| sections[section] = { | ||
| ...sections[section], | ||
| ...overrides, | ||
| }; | ||
| } else { | ||
| sections[section] = overrides; | ||
| } | ||
| }); | ||
|
|
||
| const { datasourceAndVizType } = sections; | ||
|
|
||
| // list of datasource-specific controls that should be removed | ||
| const invalidControls = | ||
| datasourceType === 'table' | ||
| ? ['granularity', 'druid_time_origin'] | ||
| : ['granularity_sqla', 'time_grain_sqla']; | ||
|
|
||
| return [datasourceAndVizType] | ||
| .concat(controlPanelSections) | ||
| .filter(section => !!section) | ||
| .map(section => { | ||
| const { controlSetRows } = section; | ||
| return { | ||
| ...section, | ||
| controlSetRows: | ||
| controlSetRows?.map(row => | ||
| row | ||
| .filter( | ||
| control => | ||
| typeof control !== 'string' || | ||
| !invalidControls.includes(control), | ||
| ) | ||
| .map(item => expandControlConfig(item, controlOverrides)), | ||
| ) || [], | ||
| }; | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| /** | ||
| * Get the clean and processed control panel sections | ||
| */ | ||
| export function getSectionsToRender( | ||
| vizType: string, | ||
| datasourceType: DatasourceType, | ||
| ) { | ||
| const controlPanelConfig = | ||
| // TODO: update `chartControlPanelRegistry` type to use ControlPanelConfig | ||
| (getChartControlPanelRegistry().get(vizType) as ControlPanelConfig) || {}; | ||
| return getMemoizedSectionsToRender(datasourceType, controlPanelConfig); | ||
| } | ||
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.
do we not have a type for
vizTypeyet? Seems like it should exist...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.
Unfortunately it doesn't. I've thought about creating a
VizTypeand actually did create a temporary type in an earlier iteration:But decided against it because it may not work well with dynamic plugins where viz types could be anything. If we do still find value in restricting viz types, we can do that while we convert
MainPreset.jsto TS.