You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/01/06 16:36:34 UTC

[GitHub] [superset] etr2460 commented on a change in pull request #17911: feat(alerts): Select Tabs to send - UI

etr2460 commented on a change in pull request #17911:
URL: https://github.com/apache/superset/pull/17911#discussion_r779677184



##########
File path: superset-frontend/src/views/CRUD/utils.tsx
##########
@@ -380,3 +393,57 @@ export const hasTerminalValidation = (errors: Record<string, any>[]) =>
             isNeedsPassword(payload) || isAlreadyExists(payload),
         ),
   );
+
+function buildDashboardTabsTree(
+  nodeId: string,
+  data: { [key: string]: any },
+  tree: any,
+  traversedIds: any,
+) {
+  if (nodeId in traversedIds) {
+    return traversedIds[nodeId];
+  }
+  const element = data[nodeId];
+
+  const parents: string[] = element.parents.filter(
+    (parent: string) => data[parent] && data[parent].type === 'TAB',
+  );
+  const newNode = {
+    key: nodeId,
+    title: element.meta.text,
+    children: [],
+  };
+
+  // eslint-disable-next-line no-param-reassign
+  traversedIds[nodeId] = newNode;
+  if (parents.length === 0) {
+    tree.push(newNode);
+
+    return newNode;
+  }
+  // Only take the last parent.
+  const parent = parents[parents.length - 1];
+  const parentNode = buildDashboardTabsTree(parent, data, tree, traversedIds);
+  parentNode.children.push(newNode);
+  return newNode;
+}
+
+export function useDashboardTabTree(id: string | number) {
+  const tree: DataNode[] = [];
+  const traversed = {};
+  const dashboardInfo = useDashboard(id);
+  const positionData: PositionData | null = dashboardInfo.result?.position_data;

Review comment:
       couldn't this possibly be undefined too, since you're using the `?.` operator?

##########
File path: superset-frontend/src/views/CRUD/alert/AlertTabsSelection.tsx
##########
@@ -0,0 +1,53 @@
+import Tree from 'antd/lib/tree';
+import React from 'react';
+import { styled, t } from '@superset-ui/core';
+import { useDashboardTabTree } from '../utils';
+
+interface Props {
+  id: string | number;
+  onSelect: (selectedTabIds: string[]) => void;
+  selectedKeys: string[];
+}
+
+const StyledSectionTitle = styled.div`
+  display: flex;
+  align-items: center;
+  margin: ${({ theme }) => theme.gridUnit * 2}px auto
+    ${({ theme }) => theme.gridUnit * 4}px auto;
+
+  h4 {
+    margin: 0;
+  }
+
+  .required {
+    margin-left: ${({ theme }) => theme.gridUnit}px;
+    color: ${({ theme }) => theme.colors.error.base};
+  }
+`;
+
+const AlertsTabsSelection: React.FC<Props> = ({
+  id,
+  onSelect,
+  selectedKeys,
+}: Props) => {
+  const tree = useDashboardTabTree(id);
+  // onTabsLoaded(tree);

Review comment:
       should this be here?

##########
File path: superset-frontend/src/views/CRUD/alert/AlertReportModal.tsx
##########
@@ -432,6 +433,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
 
   // Chart metadata
   const [chartVizType, setChartVizType] = useState<string>('');
+  const [selectedTabs, setSelectedTabs] = useState<string[] | null>(null);

Review comment:
       would it be easier for selectedTabs to be empty array when nothing is selected instead of null? If we need to pass `null` to the backend, we could always put that logic in the api call instead of needing to worry about it in our components

##########
File path: superset-frontend/src/views/CRUD/utils.tsx
##########
@@ -380,3 +393,57 @@ export const hasTerminalValidation = (errors: Record<string, any>[]) =>
             isNeedsPassword(payload) || isAlreadyExists(payload),
         ),
   );
+
+function buildDashboardTabsTree(
+  nodeId: string,
+  data: { [key: string]: any },
+  tree: any,
+  traversedIds: any,
+) {
+  if (nodeId in traversedIds) {
+    return traversedIds[nodeId];
+  }
+  const element = data[nodeId];
+
+  const parents: string[] = element.parents.filter(
+    (parent: string) => data[parent] && data[parent].type === 'TAB',
+  );
+  const newNode = {
+    key: nodeId,
+    title: element.meta.text,
+    children: [],
+  };
+
+  // eslint-disable-next-line no-param-reassign
+  traversedIds[nodeId] = newNode;
+  if (parents.length === 0) {
+    tree.push(newNode);
+
+    return newNode;
+  }
+  // Only take the last parent.
+  const parent = parents[parents.length - 1];
+  const parentNode = buildDashboardTabsTree(parent, data, tree, traversedIds);
+  parentNode.children.push(newNode);
+  return newNode;
+}
+
+export function useDashboardTabTree(id: string | number) {
+  const tree: DataNode[] = [];
+  const traversed = {};
+  const dashboardInfo = useDashboard(id);
+  const positionData: PositionData | null = dashboardInfo.result?.position_data;
+
+  if (!positionData) {
+    return null;
+  }
+  Object.entries(positionData).forEach(([_, component]) => {
+    if (['TABS'].includes(component.type)) {
+      // @ts-ignore

Review comment:
       what are we ts-ignoring? can we avoid it, or instead use ts-expect-error with a description of what the error is?

##########
File path: superset-frontend/src/views/CRUD/utils.tsx
##########
@@ -380,3 +393,57 @@ export const hasTerminalValidation = (errors: Record<string, any>[]) =>
             isNeedsPassword(payload) || isAlreadyExists(payload),
         ),
   );
+
+function buildDashboardTabsTree(
+  nodeId: string,
+  data: { [key: string]: any },
+  tree: any,
+  traversedIds: any,

Review comment:
       can we type these better? It looks like `tree` is an array of some sort, and traversedIds is a `Record<string, any>`, but maybe we can do even better than my cursory glance at typing
   
   

##########
File path: superset-frontend/src/views/CRUD/utils.tsx
##########
@@ -380,3 +393,57 @@ export const hasTerminalValidation = (errors: Record<string, any>[]) =>
             isNeedsPassword(payload) || isAlreadyExists(payload),
         ),
   );
+
+function buildDashboardTabsTree(
+  nodeId: string,
+  data: { [key: string]: any },
+  tree: any,
+  traversedIds: any,
+) {
+  if (nodeId in traversedIds) {
+    return traversedIds[nodeId];
+  }
+  const element = data[nodeId];
+
+  const parents: string[] = element.parents.filter(
+    (parent: string) => data[parent] && data[parent].type === 'TAB',
+  );
+  const newNode = {
+    key: nodeId,
+    title: element.meta.text,
+    children: [],
+  };
+
+  // eslint-disable-next-line no-param-reassign
+  traversedIds[nodeId] = newNode;
+  if (parents.length === 0) {
+    tree.push(newNode);
+
+    return newNode;
+  }
+  // Only take the last parent.
+  const parent = parents[parents.length - 1];
+  const parentNode = buildDashboardTabsTree(parent, data, tree, traversedIds);
+  parentNode.children.push(newNode);
+  return newNode;
+}
+
+export function useDashboardTabTree(id: string | number) {
+  const tree: DataNode[] = [];
+  const traversed = {};
+  const dashboardInfo = useDashboard(id);

Review comment:
       i assume this means that the `useDashboardTabTree` always comes with an API call to get the dashboard data. Do you think we should then move this hook into the api hooks file, or at least add a comment for folks who might use this again and not realize it makes an api call?




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

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org