mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Routing: Remove useRouteMatch hook (#94152)
* Update useSilenceNavData.ts * Update MuteTimings * Update AppRootPage.tsx * Cleanup props * Remove empty file
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
import { render } from '@testing-library/react';
|
import { render } from '@testing-library/react';
|
||||||
import { useRouteMatch } from 'react-router-dom';
|
import { useMatch } from 'react-router-dom-v5-compat';
|
||||||
|
|
||||||
import { useSilenceNavData } from './useSilenceNavData';
|
import { useSilenceNavData } from './useSilenceNavData';
|
||||||
|
|
||||||
jest.mock('react-router-dom', () => ({
|
jest.mock('react-router-dom-v5-compat', () => ({
|
||||||
...jest.requireActual('react-router-dom'),
|
...jest.requireActual('react-router-dom-v5-compat'),
|
||||||
useRouteMatch: jest.fn(),
|
useMatch: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const setup = () => {
|
const setup = () => {
|
||||||
@@ -21,7 +21,7 @@ const setup = () => {
|
|||||||
};
|
};
|
||||||
describe('useSilenceNavData', () => {
|
describe('useSilenceNavData', () => {
|
||||||
it('should return correct nav data when route is "/alerting/silence/new"', () => {
|
it('should return correct nav data when route is "/alerting/silence/new"', () => {
|
||||||
(useRouteMatch as jest.Mock).mockReturnValue({ isExact: true, path: '/alerting/silence/new' });
|
(useMatch as jest.Mock).mockImplementation((param) => param === '/alerting/silence/new');
|
||||||
const { result } = setup();
|
const { result } = setup();
|
||||||
|
|
||||||
expect(result).toMatchObject({
|
expect(result).toMatchObject({
|
||||||
@@ -30,7 +30,7 @@ describe('useSilenceNavData', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return correct nav data when route is "/alerting/silence/:id/edit"', () => {
|
it('should return correct nav data when route is "/alerting/silence/:id/edit"', () => {
|
||||||
(useRouteMatch as jest.Mock).mockReturnValue({ isExact: true, path: '/alerting/silence/:id/edit' });
|
(useMatch as jest.Mock).mockImplementation((param) => param === '/alerting/silence/:id/edit');
|
||||||
const { result } = setup();
|
const { result } = setup();
|
||||||
|
|
||||||
expect(result).toMatchObject({
|
expect(result).toMatchObject({
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useRouteMatch } from 'react-router-dom';
|
import { useMatch } from 'react-router-dom-v5-compat';
|
||||||
|
|
||||||
import { NavModelItem } from '@grafana/data';
|
import { NavModelItem } from '@grafana/data';
|
||||||
|
|
||||||
@@ -8,18 +8,19 @@ const defaultPageNav: Partial<NavModelItem> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function useSilenceNavData() {
|
export function useSilenceNavData() {
|
||||||
const { isExact, path } = useRouteMatch();
|
|
||||||
const [pageNav, setPageNav] = useState<NavModelItem | undefined>();
|
const [pageNav, setPageNav] = useState<NavModelItem | undefined>();
|
||||||
|
const isNewPath = useMatch('/alerting/silence/new');
|
||||||
|
const isEditPath = useMatch('/alerting/silence/:id/edit');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (path === '/alerting/silence/new') {
|
if (isNewPath) {
|
||||||
setPageNav({
|
setPageNav({
|
||||||
...defaultPageNav,
|
...defaultPageNav,
|
||||||
id: 'silence-new',
|
id: 'silence-new',
|
||||||
text: 'Silence alert rule',
|
text: 'Silence alert rule',
|
||||||
subTitle: 'Configure silences to stop notifications from a particular alert rule',
|
subTitle: 'Configure silences to stop notifications from a particular alert rule',
|
||||||
});
|
});
|
||||||
} else if (path === '/alerting/silence/:id/edit') {
|
} else if (isEditPath) {
|
||||||
setPageNav({
|
setPageNav({
|
||||||
...defaultPageNav,
|
...defaultPageNav,
|
||||||
id: 'silence-edit',
|
id: 'silence-edit',
|
||||||
@@ -27,7 +28,7 @@ export function useSilenceNavData() {
|
|||||||
subTitle: 'Recreate existing silence to stop notifications from a particular alert rule',
|
subTitle: 'Recreate existing silence to stop notifications from a particular alert rule',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [path, isExact]);
|
}, [isEditPath, isNewPath]);
|
||||||
|
|
||||||
return pageNav;
|
return pageNav;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import { useParams } from 'react-router-dom-v5-compat';
|
import { useParams } from 'react-router-dom-v5-compat';
|
||||||
|
|
||||||
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
|
||||||
|
|
||||||
import { playlistSrv } from './PlaylistSrv';
|
import { playlistSrv } from './PlaylistSrv';
|
||||||
|
|
||||||
interface Props extends GrafanaRouteComponentProps<{ uid: string }> {}
|
|
||||||
|
|
||||||
// This is a react page that just redirects to new URLs
|
// This is a react page that just redirects to new URLs
|
||||||
export default function PlaylistStartPage({ match }: Props) {
|
export default function PlaylistStartPage() {
|
||||||
const { uid = '' } = useParams();
|
const { uid = '' } = useParams();
|
||||||
playlistSrv.start(uid);
|
playlistSrv.start(uid);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { AnyAction, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
import { AnyAction, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
||||||
import { useCallback, useEffect, useMemo, useReducer } from 'react';
|
import { useCallback, useEffect, useMemo, useReducer } from 'react';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useLocation, useRouteMatch } from 'react-router-dom';
|
import { useLocation } from 'react-router-dom';
|
||||||
import { useParams } from 'react-router-dom-v5-compat';
|
import { useParams } from 'react-router-dom-v5-compat';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -61,7 +61,6 @@ export function AppRootPage({ pluginId, pluginNavSection }: Props) {
|
|||||||
const addedLinksRegistry = useAddedLinksRegistry();
|
const addedLinksRegistry = useAddedLinksRegistry();
|
||||||
const addedComponentsRegistry = useAddedComponentsRegistry();
|
const addedComponentsRegistry = useAddedComponentsRegistry();
|
||||||
const exposedComponentsRegistry = useExposedComponentsRegistry();
|
const exposedComponentsRegistry = useExposedComponentsRegistry();
|
||||||
const match = useRouteMatch();
|
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [state, dispatch] = useReducer(stateSlice.reducer, initialState);
|
const [state, dispatch] = useReducer(stateSlice.reducer, initialState);
|
||||||
const currentUrl = config.appSubUrl + location.pathname + location.search;
|
const currentUrl = config.appSubUrl + location.pathname + location.search;
|
||||||
@@ -110,7 +109,7 @@ export function AppRootPage({ pluginId, pluginNavSection }: Props) {
|
|||||||
>
|
>
|
||||||
<plugin.root
|
<plugin.root
|
||||||
meta={plugin.meta}
|
meta={plugin.meta}
|
||||||
basename={match.url}
|
basename={location.pathname}
|
||||||
onNavChanged={onNavChanged}
|
onNavChanged={onNavChanged}
|
||||||
query={queryParams}
|
query={queryParams}
|
||||||
path={location.pathname}
|
path={location.pathname}
|
||||||
|
|||||||
Reference in New Issue
Block a user