kgabryje commented on a change in pull request #13484:
URL: https://github.com/apache/superset/pull/13484#discussion_r590323728
##########
File path:
superset-frontend/src/filters/components/Select/SelectFilterPlugin.tsx
##########
@@ -89,18 +89,16 @@ export default function PluginFilterSelect(props:
PluginFilterSelectProps) {
useEffect(() => {
handleChange(currentValue ?? []);
- }, [JSON.stringify(currentValue)]);
+ }, [JSON.stringify(currentValue), multiSelect, enableEmptyFilter,
inverseSelection]);
useEffect(() => {
handleChange(defaultValue ?? []);
- // I think after Config Modal update some filter it re-creates default
value for all other filters
- // so we can process it like this `JSON.stringify` or start to use `Immer`
- }, [JSON.stringify(defaultValue)]);
+ }, [JSON.stringify(defaultValue), multiSelect, enableEmptyFilter,
inverseSelection]);
Review comment:
What do you think about replacing `JSON.stringify` with a hook with
built-in deep comparison? https://github.com/kentcdodds/use-deep-compare-effect
That applies to this file and other use cases of `JSON.stringify` in
`useEffect`'s dependency array
##########
File path:
superset-frontend/src/filters/components/TimeColumn/TimeColumnFilterPlugin.tsx
##########
@@ -0,0 +1,112 @@
+/**
+ * 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 { Behavior, DataMask, styled, t, tn } from '@superset-ui/core';
+import React, { useEffect, useState } from 'react';
+import { Select } from 'src/common/components';
+import { PluginFilterTimeColumnProps } from './types';
+import { PluginFilterStylesProps } from '../types';
+
+const Styles = styled.div<PluginFilterStylesProps>`
+ height: ${({ height }) => height};
+ width: ${({ width }) => width};
+`;
+
+const { Option } = Select;
+
+export default function PluginFilterTimeColumn(
+ props: PluginFilterTimeColumnProps,
+) {
+ const { behaviors, data, formData, height, width, setDataMask } = props;
+ const { defaultValue, currentValue, inputRef } = formData;
+
+ const [value, setValue] = useState<string[]>(defaultValue ?? []);
+
+ const handleChange = (value?: string[] | string | null) => {
+ let resultValue: string[];
+ if (Array.isArray(value)) {
+ resultValue = value;
+ } else {
+ resultValue = value ? [value] : [];
+ }
+ setValue(resultValue);
+
+ const dataMask = {
+ extraFormData: {
+ override_form_data: {
+ granularity_sqla: resultValue.length ? resultValue[0] : null,
+ },
+ },
+ currentState: {
+ value: resultValue.length ? resultValue : null,
+ },
+ };
+
+ const dataMaskObject: DataMask = {};
+ if (behaviors.includes(Behavior.NATIVE_FILTER)) {
+ dataMaskObject.nativeFilters = dataMask;
+ }
+
+ if (behaviors.includes(Behavior.CROSS_FILTER)) {
+ dataMaskObject.crossFilters = dataMask;
+ }
+
+ setDataMask(dataMaskObject);
+ };
+
+ useEffect(() => {
+ handleChange(currentValue ?? null);
+ }, [JSON.stringify(currentValue)]);
+
+ useEffect(() => {
+ handleChange(defaultValue ?? null);
+ // I think after Config Modal update some filter it re-creates default
value for all other filters
+ // so we can process it like this `JSON.stringify` or start to use `Immer`
+ }, [JSON.stringify(defaultValue)]);
+
+ const timeColumns = (data || []).filter(row => row.dtype === 2);
Review comment:
Can we replace `2` with a constant?
##########
File path:
superset-frontend/src/filters/components/TimeColumn/TimeColumnFilterPlugin.tsx
##########
@@ -0,0 +1,112 @@
+/**
+ * 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 { Behavior, DataMask, styled, t, tn } from '@superset-ui/core';
+import React, { useEffect, useState } from 'react';
+import { Select } from 'src/common/components';
+import { PluginFilterTimeColumnProps } from './types';
+import { PluginFilterStylesProps } from '../types';
+
+const Styles = styled.div<PluginFilterStylesProps>`
+ height: ${({ height }) => height};
+ width: ${({ width }) => width};
+`;
+
+const { Option } = Select;
+
+export default function PluginFilterTimeColumn(
+ props: PluginFilterTimeColumnProps,
+) {
+ const { behaviors, data, formData, height, width, setDataMask } = props;
+ const { defaultValue, currentValue, inputRef } = formData;
+
+ const [value, setValue] = useState<string[]>(defaultValue ?? []);
+
+ const handleChange = (value?: string[] | string | null) => {
+ let resultValue: string[];
+ if (Array.isArray(value)) {
+ resultValue = value;
+ } else {
+ resultValue = value ? [value] : [];
+ }
+ setValue(resultValue);
+
+ const dataMask = {
+ extraFormData: {
+ override_form_data: {
+ granularity_sqla: resultValue.length ? resultValue[0] : null,
+ },
+ },
+ currentState: {
+ value: resultValue.length ? resultValue : null,
+ },
+ };
+
+ const dataMaskObject: DataMask = {};
+ if (behaviors.includes(Behavior.NATIVE_FILTER)) {
+ dataMaskObject.nativeFilters = dataMask;
+ }
+
+ if (behaviors.includes(Behavior.CROSS_FILTER)) {
+ dataMaskObject.crossFilters = dataMask;
+ }
+
+ setDataMask(dataMaskObject);
+ };
+
+ useEffect(() => {
+ handleChange(currentValue ?? null);
+ }, [JSON.stringify(currentValue)]);
+
+ useEffect(() => {
+ handleChange(defaultValue ?? null);
+ // I think after Config Modal update some filter it re-creates default
value for all other filters
+ // so we can process it like this `JSON.stringify` or start to use `Immer`
+ }, [JSON.stringify(defaultValue)]);
+
+ const timeColumns = (data || []).filter(row => row.dtype === 2);
+
+ const placeholderText =
+ timeColumns.length === 0
+ ? t('No time columns')
+ : tn('%s option', '%s options', timeColumns.length, timeColumns.length);
+ return (
+ <Styles height={height} width={width}>
+ <Select
+ allowClear
+ value={value}
+ style={{ width: '100%' }}
Review comment:
Maybe replace inline style with styled component?
##########
File path: superset-frontend/src/filters/components/TimeGrain/index.ts
##########
@@ -0,0 +1,42 @@
+/**
+ * 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 { Behavior, ChartMetadata, ChartPlugin, t } from '@superset-ui/core';
+import buildQuery from './buildQuery';
+import controlPanel from './controlPanel';
+import transformProps from './transformProps';
+import thumbnail from './images/thumbnail.png';
+
+export default class FilterTimeGrainPlugin extends ChartPlugin {
+ constructor() {
+ const metadata = new ChartMetadata({
+ name: t('Time grain'),
+ description: 'Time grain filter plugin',
Review comment:
```suggestion
description: t('Time grain filter plugin'),
```
##########
File path:
superset-frontend/src/filters/components/TimeGrain/TimeGrainFilterPlugin.tsx
##########
@@ -0,0 +1,111 @@
+/**
+ * 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 { QueryObjectExtras, styled, t, tn } from '@superset-ui/core';
+import React, { useEffect, useState } from 'react';
+import { Select } from 'src/common/components';
+import { PluginFilterTimeGrainProps } from './types';
+import { PluginFilterStylesProps } from '../types';
+
+const Styles = styled.div<PluginFilterStylesProps>`
+ height: ${({ height }) => height};
+ width: ${({ width }) => width};
+`;
+
+const { Option } = Select;
+
+export default function PluginFilterTimegrain(
+ props: PluginFilterTimeGrainProps,
+) {
+ const { data, formData, height, width, setDataMask } = props;
+ const { defaultValue, currentValue, inputRef } = formData;
+
+ const [value, setValue] = useState<string[]>(defaultValue ?? []);
+
+ const handleChange = (values: string[] | string | undefined | null) => {
+ let selectedValues: string[];
+ let timeGrain: string | null;
+ if (values === null || values === undefined) {
+ selectedValues = [];
+ timeGrain = null;
+ } else if (!Array.isArray(values)) {
+ selectedValues = [values];
+ timeGrain = values;
+ } else if (Array.isArray(values) && values.length > 0) {
+ selectedValues = values;
+ timeGrain = values[0];
+ } else {
+ selectedValues = [];
+ timeGrain = null;
+ }
+ const extras: QueryObjectExtras = {};
+ if (timeGrain !== null) {
+ extras.time_grain_sqla = timeGrain;
+ }
+ setValue(selectedValues);
+ setDataMask({
+ nativeFilters: {
+ extraFormData: {
+ override_form_data: {
+ extras,
+ },
+ },
+ currentState: {
+ value: selectedValues.length ? selectedValues : null,
+ },
+ },
+ });
+ };
+
+ useEffect(() => {
+ handleChange(currentValue ?? []);
+ }, [JSON.stringify(currentValue)]);
+
+ useEffect(() => {
+ handleChange(defaultValue ?? []);
+ // I think after Config Modal update some filter it re-creates default
value for all other filters
+ // so we can process it like this `JSON.stringify` or start to use `Immer`
+ }, [JSON.stringify(defaultValue)]);
+
+ const placeholderText =
+ (data || []).length === 0
+ ? t('No data')
+ : tn('%s option', '%s options', data.length, data.length);
+ return (
+ <Styles height={height} width={width}>
+ <Select
+ allowClear
+ value={value}
+ style={{ width: '100%' }}
Review comment:
Same as above - inline style to styled component
##########
File path:
superset-frontend/src/filters/components/TimeColumn/TimeColumnFilterPlugin.tsx
##########
@@ -0,0 +1,112 @@
+/**
+ * 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 { Behavior, DataMask, styled, t, tn } from '@superset-ui/core';
+import React, { useEffect, useState } from 'react';
+import { Select } from 'src/common/components';
+import { PluginFilterTimeColumnProps } from './types';
+import { PluginFilterStylesProps } from '../types';
+
+const Styles = styled.div<PluginFilterStylesProps>`
+ height: ${({ height }) => height};
+ width: ${({ width }) => width};
+`;
+
+const { Option } = Select;
+
+export default function PluginFilterTimeColumn(
+ props: PluginFilterTimeColumnProps,
+) {
+ const { behaviors, data, formData, height, width, setDataMask } = props;
+ const { defaultValue, currentValue, inputRef } = formData;
+
+ const [value, setValue] = useState<string[]>(defaultValue ?? []);
+
+ const handleChange = (value?: string[] | string | null) => {
+ let resultValue: string[];
+ if (Array.isArray(value)) {
Review comment:
Replace with `ensureIsArray` from `@superset-ui/core`?
##########
File path: superset-frontend/src/filters/components/TimeColumn/index.ts
##########
@@ -0,0 +1,42 @@
+/**
+ * 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 { Behavior, ChartMetadata, ChartPlugin, t } from '@superset-ui/core';
+import buildQuery from './buildQuery';
+import controlPanel from './controlPanel';
+import transformProps from './transformProps';
+import thumbnail from './images/thumbnail.png';
+
+export default class FilterTimeColumnPlugin extends ChartPlugin {
+ constructor() {
+ const metadata = new ChartMetadata({
+ name: t('Time column'),
+ description: 'Time column filter plugin',
Review comment:
```suggestion
description: t('Time column filter plugin'),
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]