ReturnToPrevious: make href optional (#81691)

This commit is contained in:
Laura Fernández 2024-02-02 13:14:58 +01:00 committed by GitHub
parent e1197641f3
commit 42cd4266b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 22 additions and 18 deletions

View File

@ -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;

View File

@ -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}

View File

@ -24,8 +24,8 @@ export interface AppChromeState {
kioskMode: KioskMode | null;
layout: PageLayoutType;
returnToPrevious?: {
href: ReturnToPreviousProps['href'];
title: ReturnToPreviousProps['title'];
href: ReturnToPreviousProps['href'];
};
}

View File

@ -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) => {

View File

@ -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]
);
}

View File

@ -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