sadpandajoe commented on code in PR #45495:
URL: https://github.com/apache/superset/pull/45495#discussion_r4332986075
##########
superset-frontend/src/dashboard/actions/hydrate.ts:
##########
@@ -351,7 +351,7 @@ export const hydrateDashboard =
// activeTabs, no stored activeTabs, and no deep-link (directPathToChild),
// which the live Tabs component resolves on its own.
const seededActiveTabs =
- activeTabs ||
+ activeTabs?.filter(tabId => layout[tabId]) ||
Review Comment:
The new `.filter(tabId => layout[tabId])` has no test through
`hydrateDashboard`: `hydrate.test.ts` only passes valid tab IDs, and
`DashboardContainer.test.tsx` puts `TAB-deleted` straight into Redux, bypassing
this line. Removing the filter would leave all tests green while a permalink or
`active_tabs` naming a deleted tab seeds a nonexistent ID into `activeTabs`.
Could a hydrate test pass `activeTabs: ['TAB-2', 'TAB-deleted']` and assert the
dispatched `dashboardState.activeTabs` is `['TAB-2']`?
##########
superset-frontend/src/dashboard/hooks/useSyncActiveTabsToUrl.ts:
##########
@@ -0,0 +1,103 @@
+/**
+ * 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 directPathToChild = useSelector(
+ (state: RootState) => state.dashboardState.directPathToChild,
+ );
+ 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;
+ }
+
+ const params = new URLSearchParams(history.location.search);
+ const current = params.get(URL_PARAMS.activeTabs.name);
+ const next = activeTabs?.length ? [...activeTabs].sort().join(',') : null;
+ const currentNormalized = current
+ ? parseActiveTabsParam(current).sort().join(',')
+ : null;
+ if (currentNormalized === next) {
+ return;
+ }
+
+ if (next) {
+ params.set(URL_PARAMS.activeTabs.name, next);
+ } else {
+ params.delete(URL_PARAMS.activeTabs.name);
+ }
+ // A deep link hash outranks `active_tabs` on load, so it is dropped once
+ // the selected tabs no longer include the tabs on its path.
+ const hashIsStale =
Review Comment:
The stale-hash check compares the current `directPathToChild` against
`activeTabs`, but a tab click replaces `directPathToChild` with the clicked
tab's own path (`DashboardBuilder.handleChangeTab`), so that path is always
inside `activeTabs` and `hashIsStale` stays false. Open
`/dashboard/7/#TAB-first`, click `TAB-second`: the URL becomes
`?active_tabs=TAB-second#TAB-first`, and reloading or sharing it reopens the
first tab because the resolved hash outranks `active_tabs`. Should the hash be
dropped whenever a tab change moves the selection away from the tab the hash
targets, rather than judged from the current click path?
##########
superset-frontend/src/dashboard/containers/DashboardPage.tsx:
##########
@@ -247,6 +266,23 @@ export const DashboardPage: FC<PageProps> = ({ idOrSlug }:
PageProps) => {
} else if (nativeFilterKeyValue) {
dataMask = await getFilterValue(id, nativeFilterKeyValue);
}
+ // An explicit active_tabs param holds the latest tab state, so it takes
+ // precedence over the one stored in a permalink
+ const activeTabsFromUrl = parseActiveTabsParam(
+ getUrlParam(URL_PARAMS.activeTabs),
+ );
+ if (activeTabsFromUrl.length > 0) {
+ activeTabs = activeTabsFromUrl;
+ // A click path left over from a previous visit would otherwise outrank
+ // the tabs requested by the URL. Deep links (hash, focused chart)
still
+ // win, as hydration derives their path itself.
+ if (
+ !getLocationHash() &&
+ !getUrlParam(URL_PARAMS.dashboardFocusedChart)
+ ) {
+ dispatch(setDirectPathToChild([]));
Review Comment:
Nothing in `DashboardPage.test.tsx` seeds a stale `directPathToChild` or
asserts it is cleared here, so deleting this dispatch leaves every added test
green while a retained click path from an earlier visit again outranks
`?active_tabs=TAB-B` in both tab consumers. Could the explicit-`active_tabs`
case start with `directPathToChild: ['ROOT_ID', 'TABS-root', 'TAB-A']` in the
store and assert it is `[]` after hydration (and unchanged when a hash or
focused chart is present)?
--
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]