sadpandajoe commented on code in PR #45495:
URL: https://github.com/apache/superset/pull/45495#discussion_r4396181736


##########
superset-frontend/src/dashboard/hooks/useSyncActiveTabsToUrl.ts:
##########
@@ -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 { useEffect } from 'react';
+import { matchPath, useHistory } from 'react-router-dom';
+import { useSelector } from 'react-redux';
+import { URL_PARAMS } from 'src/constants';
+import { RoutePaths } from 'src/views/routePaths';
+import { TAB_TYPE } from 'src/dashboard/util/componentTypes';
+import type { RootState } from 'src/dashboard/types';
+
+const TAB_ID_SEPARATOR = ',';
+
+/**
+ * Parses the `active_tabs` URL param into a list of tab ids.
+ */
+export function parseActiveTabsParam(value: string | null): string[] {
+  return (value ?? '')
+    .split(TAB_ID_SEPARATOR)
+    .map(tabId => tabId.trim())
+    .filter(Boolean);
+}
+
+/**
+ * Keeps the `active_tabs` URL param in line with the dashboard's active tabs,
+ * so that a link to the dashboard opens on the same tabs. Follows the redux
+ * state rather than tab clicks so that tabs restored from a permalink or from
+ * the URL itself are reflected as well.
+ *
+ * @param enabled Whether the dashboard has been hydrated
+ */
+export default function useSyncActiveTabsToUrl(enabled: boolean): void {
+  const history = useHistory();
+  const activeTabs = useSelector(
+    (state: RootState) => state.dashboardState.activeTabs,
+  );
+  const layout = useSelector(
+    (state: RootState) => state.dashboardLayout?.present ?? {},
+  );
+  const editMode = useSelector(
+    (state: RootState) => state.dashboardState.editMode,
+  );
+
+  useEffect(() => {
+    // The pathname check avoids racing with navigation to Explore
+    if (
+      !enabled ||
+      editMode ||
+      matchPath(history.location.pathname, {
+        path: [RoutePaths.DASHBOARD_LIST, RoutePaths.DASHBOARD],
+        exact: true,
+      })?.path !== RoutePaths.DASHBOARD
+    ) {
+      return;
+    }
+
+    // `navigateWithState` updates the address bar without the router, so after
+    // an in-session save the router can hold a stale pathname (e.g. an old
+    // slug). Writing it back would undo that navigation.
+    if (
+      window.location.pathname !==

Review Comment:
   This guard compares `window.location.pathname` (still percent-encoded) with 
`history.createHref({ pathname: history.location.pathname })`, but React 
Router's history decodes the pathname and `createHref` doesn't re-encode it. 
Superset allows Unicode slugs (the slug cleanup keeps `\w` characters), so at 
`/dashboard/caf%C3%A9/` the two never match, this returns early on every run, 
and switching tabs never writes `active_tabs`, so a copied or refreshed URL 
loses the selection with no error.
   
   Could the comparison normalize both sides (for example `decodeURI` on 
`window.location.pathname`), or detect the `navigateWithState` divergence 
another way? A hook test with a non-ASCII slug would also pin it, since the 
current tests only use ASCII paths.



##########
superset-frontend/src/dashboard/containers/DashboardPage.tsx:
##########
@@ -339,6 +374,9 @@ export const DashboardPage: FC<PageProps> = ({ idOrSlug }: 
PageProps) => {
           } as unknown as Parameters<typeof hydrateDashboard>[0]),
         );
         dispatch(clearDashboardHistory());
+        if (isCurrentLoad) {

Review Comment:
   The `isCurrentLoad` check is the only thing that keeps a superseded 
permalink/filter-key lookup from calling `setHydratedLoadId`, but no test in 
`DashboardPage.test.tsx` exercises it: none holds a lookup pending while the 
effect re-runs. Take lookup A pending, flip `readyToRender` false then true to 
start lookup B for the same dashboard id, then resolve A while B is still 
pending. With the guard deleted A sets the marker, the builder mounts and tab 
sync is enabled before B hydrates, and every added test stays green.
   
   Could a case with deferred permalink promises and a `rerender` assert that 
the builder stays withheld until the current lookup resolves?



-- 
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: [email protected]

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