rusackas commented on a change in pull request #11814:
URL: 
https://github.com/apache/incubator-superset/pull/11814#discussion_r544857588



##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterConfigModal.tsx
##########
@@ -0,0 +1,283 @@
+/**
+ * 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 React, { useCallback, useEffect, useMemo, useState } from 'react';
+import { findLastIndex, uniq } from 'lodash';
+import shortid from 'shortid';
+import { DeleteFilled } from '@ant-design/icons';
+import { styled, t } from '@superset-ui/core';
+import { Form } from 'src/common/components';
+import { StyledModal } from 'src/common/components/Modal';
+import { LineEditableTabs } from 'src/common/components/Tabs';
+import { DASHBOARD_ROOT_ID } from 'src/dashboard/util/constants';
+import { usePrevious } from 'src/common/hooks/usePrevious';
+import ErrorBoundary from 'src/components/ErrorBoundary';
+import { useFilterConfigMap, useFilterConfiguration } from './state';
+import FilterConfigForm from './FilterConfigForm';
+import { FilterConfiguration, NativeFiltersForm } from './types';
+
+const StyledModalBody = styled.div`
+  display: flex;
+  flex-direction: row;
+  .filters-list {
+    width: 200px;
+    overflow: auto;
+  }
+`;
+
+const RemovedStatus = styled.span`
+  &.removed {
+    text-decoration: line-through;
+  }
+`;
+
+function generateFilterId() {
+  return `FILTER_V2-${shortid.generate()}`;
+}
+
+export interface FilterConfigModalProps {
+  isOpen: boolean;
+  initialFilterId?: string;
+  createNewOnOpen?: boolean;
+  save: (filterConfig: FilterConfiguration) => Promise<void>;
+  onCancel: () => void;
+}
+
+const getFilterIds = (config: FilterConfiguration) =>
+  config.map(filter => filter.id);
+
+/**
+ * This is the modal to configure all the dashboard-native filters.
+ * Manages modal-level state, such as what filters are in the list,
+ * and which filter is currently being edited.
+ *
+ * Calls the `save` callback with the new FilterConfiguration object
+ * when the user saves the filters.
+ */
+export function FilterConfigModal({
+  isOpen,
+  initialFilterId,
+  createNewOnOpen,
+  save,
+  onCancel,
+}: FilterConfigModalProps) {
+  const [form] = Form.useForm<NativeFiltersForm>();
+
+  const filterConfig = useFilterConfiguration();
+  const filterConfigMap = useFilterConfigMap();
+  // new filter ids may belong to filters that do not exist yet
+  const [newFilterIds, setNewFilterIds] = useState<string[]>([]);
+  // store ids of filters that have been removed but keep them around in the 
state
+  const [removedFilters, setRemovedFilters] = useState<Record<string, 
boolean>>(
+    {},
+  );
+  const filterIds = useMemo(
+    () => uniq([...getFilterIds(filterConfig), ...newFilterIds]),
+    [filterConfig, newFilterIds],
+  );
+  const getInitialCurrentFilterId = useCallback(
+    () => initialFilterId ?? filterIds[0],
+    [initialFilterId, filterIds],
+  );
+  const [currentFilterId, setCurrentFilterId] = useState(
+    getInitialCurrentFilterId,
+  );
+  // the form values are managed by the antd form, but we copy them to here
+  const [formValues, setFormValues] = useState<NativeFiltersForm>({
+    filters: {},
+  });
+  const wasOpen = usePrevious(isOpen);
+
+  const addFilter = useCallback(() => {
+    const newFilterId = generateFilterId();
+    setNewFilterIds([...newFilterIds, newFilterId]);
+    setCurrentFilterId(newFilterId);
+  }, [newFilterIds, setCurrentFilterId]);
+
+  useEffect(() => {
+    if (createNewOnOpen && isOpen && !wasOpen) {
+      addFilter();
+    }
+  }, [createNewOnOpen, isOpen, wasOpen, addFilter]);
+
+  const resetForm = useCallback(() => {
+    form.resetFields();
+    setNewFilterIds([]);
+    setCurrentFilterId(getInitialCurrentFilterId());
+    setRemovedFilters({});
+  }, [form, getInitialCurrentFilterId]);
+
+  function onTabEdit(filterId: string, action: 'add' | 'remove') {
+    if (action === 'remove') {
+      setRemovedFilters({
+        ...removedFilters,
+        // trash can button is actually a toggle
+        [filterId]: !removedFilters[filterId],
+      });
+      if (filterId === currentFilterId && !removedFilters[filterId]) {
+        // when a filter is removed, switch the view to a non-removed one
+        const lastNotRemoved = findLastIndex(
+          filterIds,
+          id => !removedFilters[id] && id !== filterId,
+        );
+        if (lastNotRemoved !== -1)
+          setCurrentFilterId(filterIds[lastNotRemoved]);
+      }
+    } else if (action === 'add') {
+      addFilter();
+    }
+  }
+
+  function getFilterTitle(id: string) {
+    return (
+      formValues.filters[id]?.name ?? filterConfigMap[id]?.name ?? 'New Filter'

Review comment:
       The `?` is strong with this line.
   
![riddler](https://user-images.githubusercontent.com/812905/102454743-57f1cf80-3ff3-11eb-8b6d-a6f2924b92e7.gif)
   




----------------------------------------------------------------
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]

Reply via email to