diff --git a/packages/grafana-runtime/src/utils/returnToPrevious.ts b/packages/grafana-runtime/src/utils/returnToPrevious.ts index f4c1654593a..7b699cad7b4 100644 --- a/packages/grafana-runtime/src/utils/returnToPrevious.ts +++ b/packages/grafana-runtime/src/utils/returnToPrevious.ts @@ -1,9 +1,4 @@ -interface ReturnToPreviousData { - title: string; - href: string; -} - -type ReturnToPreviousHook = () => (rtp: ReturnToPreviousData) => void; +type ReturnToPreviousHook = () => (title: string, href?: string) => void; let rtpHook: ReturnToPreviousHook | undefined = undefined; diff --git a/public/app/core/components/AppChrome/AppChrome.tsx b/public/app/core/components/AppChrome/AppChrome.tsx index 0bec848ad60..2846bda2b93 100644 --- a/public/app/core/components/AppChrome/AppChrome.tsx +++ b/public/app/core/components/AppChrome/AppChrome.tsx @@ -55,17 +55,18 @@ export function AppChrome({ children }: Props) { chrome.setMegaMenuOpen(!state.megaMenuOpen); }; - const path = locationService.getLocation().pathname; + const { pathname, search } = locationService.getLocation(); + const url = pathname + search; const shouldShowReturnToPrevious = - config.featureToggles.returnToPrevious && state.returnToPrevious && path !== state.returnToPrevious.href; + config.featureToggles.returnToPrevious && state.returnToPrevious && url !== state.returnToPrevious.href; useEffect(() => { - if (state.returnToPrevious && path === state.returnToPrevious.href) { + if (state.returnToPrevious && url === state.returnToPrevious.href) { chrome.clearReturnToPrevious(); } // We only want to pay attention when the location changes // eslint-disable-next-line react-hooks/exhaustive-deps - }, [chrome, path]); + }, [chrome, url]); // Chromeless routes are without topNav, mega menu, search & command palette // We check chromeless twice here instead of having a separate path so {children} diff --git a/public/app/core/components/AppChrome/AppChromeService.tsx b/public/app/core/components/AppChrome/AppChromeService.tsx index 3ce5628ab95..3567e3efa8a 100644 --- a/public/app/core/components/AppChrome/AppChromeService.tsx +++ b/public/app/core/components/AppChrome/AppChromeService.tsx @@ -24,8 +24,8 @@ export interface AppChromeState { kioskMode: KioskMode | null; layout: PageLayoutType; returnToPrevious?: { - href: ReturnToPreviousProps['href']; title: ReturnToPreviousProps['title']; + href: ReturnToPreviousProps['href']; }; } diff --git a/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx b/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx index 3b25134f5ff..196709b5901 100644 --- a/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx +++ b/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx @@ -10,8 +10,8 @@ import { t } from 'app/core/internationalization'; import { DismissableButton } from './DismissableButton'; export interface ReturnToPreviousProps { - href: string; title: string; + href: string; } export const ReturnToPrevious = ({ href, title }: ReturnToPreviousProps) => { diff --git a/public/app/core/context/GrafanaContext.ts b/public/app/core/context/GrafanaContext.ts index a7eea0d2308..b53788c23cc 100644 --- a/public/app/core/context/GrafanaContext.ts +++ b/public/app/core/context/GrafanaContext.ts @@ -1,8 +1,7 @@ -import React, { useContext } from 'react'; +import React, { useCallback, useContext } from 'react'; import { GrafanaConfig } from '@grafana/data'; -import { LocationService } from '@grafana/runtime/src/services/LocationService'; -import { BackendSrv } from '@grafana/runtime/src/services/backendSrv'; +import { LocationService, locationService, BackendSrv } from '@grafana/runtime'; import { AppChromeService } from '../components/AppChrome/AppChromeService'; import { NewFrontendAssetsChecker } from '../services/NewFrontendAssetsChecker'; @@ -31,5 +30,14 @@ export function useGrafana(): GrafanaContextType { // @grafana/runtime export function useReturnToPreviousInternal() { const { chrome } = useGrafana(); - return chrome.setReturnToPrevious; + return useCallback( + (title: string, href?: string) => { + const { pathname, search } = locationService.getLocation(); + chrome.setReturnToPrevious({ + title: title, + href: href ?? pathname + search, + }); + }, + [chrome] + ); } diff --git a/public/app/features/alerting/unified/components/rules/RuleDetailsActionButtons.tsx b/public/app/features/alerting/unified/components/rules/RuleDetailsActionButtons.tsx index bf6bd89917e..48bbf056030 100644 --- a/public/app/features/alerting/unified/components/rules/RuleDetailsActionButtons.tsx +++ b/public/app/features/alerting/unified/components/rules/RuleDetailsActionButtons.tsx @@ -4,7 +4,7 @@ import React, { Fragment, useState } from 'react'; import { useLocation } from 'react-router-dom'; import { GrafanaTheme2, textUtil, urlUtil } from '@grafana/data'; -import { config, locationService, useReturnToPrevious } from '@grafana/runtime'; +import { config, useReturnToPrevious } from '@grafana/runtime'; import { Button, ClipboardButton, @@ -145,7 +145,7 @@ export const RuleDetailsActionButtons = ({ rule, rulesSource, isViewMode }: Prop icon="apps" href={`d/${encodeURIComponent(dashboardUID)}`} onClick={() => { - setReturnToPrevious({ title: rule.name, href: locationService.getLocation().pathname }); + setReturnToPrevious(rule.name); }} > Go to dashboard