justinpark commented on code in PR #45495: URL: https://github.com/apache/superset/pull/45495#discussion_r4404541472
########## 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: Addressed in 13c9e1a: the guard now compares against the decoded address-bar path (`decodeURI`, falling back to the raw path on malformed escapes), so non-ASCII slugs like `/dashboard/café/` still sync. Added a hook test with a non-ASCII slug. ########## 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: Addressed in 13c9e1a: added a test with two deferred permalink lookups and a `rerender` that flips `readyToRender` off and on. Resolving the superseded lookup first keeps the builder withheld, and resolving the current one mounts it, so removing the `isCurrentLoad` guard fails it. -- 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]
