Skip to content

Commit

Permalink
Implement autofocus when opening a dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed Oct 6, 2022
1 parent caaa0d4 commit 687ae58
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
21 changes: 20 additions & 1 deletion superset-frontend/src/dashboard/actions/hydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const HYDRATE_DASHBOARD = 'HYDRATE_DASHBOARD';

export const hydrateDashboard =
({
history,
dashboard,
charts,
filterboxMigrationState = FILTER_BOX_MIGRATION_STATES.NOOP,
Expand Down Expand Up @@ -291,8 +292,26 @@ export const hydrateDashboard =
future: [],
};

// Searches for a focused_chart parameter in the URL to automatically focus a chart
const { focused_chart: focusedChartId } = regularUrlParams;
let focusedChartLayoutId;
if (focusedChartId) {
// Converts focused_chart to dashboard layout id
const found = Object.values(dashboardLayout.present).find(
// eslint-disable-next-line eqeqeq
element => element.meta?.chartId == focusedChartId,
);
focusedChartLayoutId = found?.id;
// Removes the focused_chart parameter from the URL
const params = new URLSearchParams(window.location.search);
params.delete('focused_chart');
history.replace({
search: params.toString(),
});
}

// find direct link component and path from root
const directLinkComponentId = getLocationHash();
const directLinkComponentId = focusedChartLayoutId || getLocationHash();
let directPathToChild = dashboardState.directPathToChild || [];
if (layout[directLinkComponentId]) {
directPathToChild = (layout[directLinkComponentId].parents || []).slice();
Expand Down
3 changes: 3 additions & 0 deletions superset-frontend/src/dashboard/containers/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/
import React, { FC, useEffect, useMemo, useRef, useState } from 'react';
import { useHistory } from 'react-router-dom';
import {
CategoricalColorNamespace,
FeatureFlag,
Expand Down Expand Up @@ -155,6 +156,7 @@ const useSyncDashboardStateWithLocalStorage = () => {
export const DashboardPage: FC<PageProps> = ({ idOrSlug }: PageProps) => {
const dispatch = useDispatch();
const theme = useTheme();
const history = useHistory();
const user = useSelector<any, UserWithPermissionsAndRoles>(
state => state.user,
);
Expand Down Expand Up @@ -301,6 +303,7 @@ export const DashboardPage: FC<PageProps> = ({ idOrSlug }: PageProps) => {
}
dispatch(
hydrateDashboard({
history,
dashboard,
charts,
activeTabs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Menu } from 'src/components/Menu';
import { Link } from 'react-router-dom';

export interface DashboardsSubMenuProps {
chartId?: number;
dashboards?: { id: number; dashboard_title: string }[];
}

Expand All @@ -32,6 +33,7 @@ const HEIGHT = 300;
const SEARCH_THRESHOLD = 10;

const DashboardsSubMenu = ({
chartId,
dashboards = [],
...menuProps
}: DashboardsSubMenuProps) => {
Expand All @@ -48,6 +50,7 @@ const DashboardsSubMenu = ({
);
const noResults = dashboards.length === 0;
const noResultsFound = dashboardSearch && filteredDashboards.length === 0;
const urlQueryString = chartId ? `?focused_chart=${chartId}` : '';
return (
<>
{showSearch && (
Expand Down Expand Up @@ -80,7 +83,10 @@ const DashboardsSubMenu = ({
}}
{...menuProps}
>
<Link target="_blank" to={`/superset/dashboard/${dashboard.id}`}>
<Link
target="_blank"
to={`/superset/dashboard/${dashboard.id}${urlQueryString}`}
>
<div
css={css`
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ export const useExploreAdditionalActionsMenu = (
title={t('Dashboards added to')}
key={MENU_KEYS.DASHBOARDS_ADDED_TO}
>
<DashboardsSubMenu dashboards={dashboards} />
<DashboardsSubMenu
chartId={slice?.slice_id}
dashboards={dashboards}
/>
</Menu.SubMenu>
<Menu.Divider />
</>
Expand Down

0 comments on commit 687ae58

Please sign in to comment.