Skip to content

Commit 1722d34

Browse files
Removing Routes.pluginRoute in favor of constant routes. (#24323)
* Replacing usages of `pluginRoute` with actual routes. * Removing usages of `pluginRoute`. * Removing `Routes.pluginRoute` altogether. * Cleaning up. * Cleaning up tests. * Cleaning up route constants/functions. * Fixing dashboard route helper. * Adding key to link. * Removing mocks for `routing/Routes`.
1 parent a87f7b2 commit 1722d34

File tree

18 files changed

+37
-114
lines changed

18 files changed

+37
-114
lines changed

graylog2-web-interface/src/components/entityCreatorBindings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const entityCreatorBindings: PluginExports = {
4141
{
4242
id: 'Dashboard',
4343
title: 'Create dashboard',
44-
path: Routes.DASHBOARD_NEW,
44+
path: Routes.DASHBOARD.NEW,
4545
telemetryEvent: {
4646
type: TELEMETRY_EVENT_TYPE.DASHBOARD_ACTION.DASHBOARD_CREATE_CLICKED,
4747
section: 'dashboard',

graylog2-web-interface/src/components/permissions/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type TableElement<T extends EntityBase> = {
5757
entityId: string;
5858
entityType: string;
5959
}>;
60-
useCondition: () => true;
60+
useCondition: () => boolean;
6161
};
6262

6363
declare module 'graylog-web-plugin/plugin' {

graylog2-web-interface/src/components/welcome/EntityListItem.test.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import { render, screen } from 'wrappedTestingLibrary';
2121
import EntityListItem from 'components/welcome/EntityListItem';
2222
import { createGRN } from 'logic/permissions/GRN';
2323

24-
jest.mock('routing/Routes', () => ({ pluginRoute: (x) => x, dashboard_show: (x) => `/route/DASHBOARDS_VIEWID/${x}` }));
25-
2624
const grn = createGRN('dashboard', '1');
2725

2826
describe('EntityListItem', () => {
@@ -35,9 +33,8 @@ describe('EntityListItem', () => {
3533
it('Show correct link', async () => {
3634
render(<EntityListItem grn={grn} title="Title 1" />);
3735

38-
const title = await screen.findByText('Title 1');
39-
const link = title.closest('a');
36+
const link = await screen.findByText('Title 1');
4037

41-
expect(link).toHaveAttribute('href', '/route/DASHBOARDS_VIEWID/1');
38+
expect(link).toHaveAttribute('href', '/dashboards/1');
4239
});
4340
});

graylog2-web-interface/src/components/welcome/FavoriteItemsList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const FavoriteItemsList = () => {
4545
<NoSearchResult>
4646
You do not have any favorite items yet.
4747
<br />
48-
Make any <Link to={Routes.SEARCH}>Search</Link> or{' '}
49-
<Link to={Routes.pluginRoute('DASHBOARDS_NEW')}>Dashboard</Link> favorite to show up here.
48+
Make any <Link to={Routes.SEARCH}>Search</Link> or <Link to={Routes.DASHBOARD.NEW}>Dashboard</Link> favorite to
49+
show up here.
5050
</NoSearchResult>
5151
);
5252
}

graylog2-web-interface/src/components/welcome/LastOpenList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const LastOpenList = () => {
3939
You have not opened any searches/dashboards yet.
4040
<br />
4141
From now on, whenever you open a saved search/dashboard, it will show up here. In the meantime, you can start a
42-
new <Link to={Routes.SEARCH}>Search</Link> or <Link to={Routes.pluginRoute('DASHBOARDS_NEW')}>Dashboard</Link>.
42+
new <Link to={Routes.SEARCH}>Search</Link> or <Link to={Routes.DASHBOARD.NEW}>Dashboard</Link>.
4343
</NoSearchResult>
4444
);
4545
}

graylog2-web-interface/src/components/welcome/Welcome.test.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,6 @@ jest.mock('components/welcome/hooks/useRecentActivity', () =>
9595
})),
9696
);
9797

98-
jest.mock('routing/Routes', () => ({
99-
pluginRoute: (x: string) => x,
100-
dashboard_show: (x: string) => `/route/DASHBOARDS_VIEWID/${x}`,
101-
getPluginRoute: (x: string) => () => x,
102-
SEARCH: '/search',
103-
SYSTEM: {
104-
USERS: {
105-
edit: () => '/edit-profile-page',
106-
},
107-
},
108-
show_saved_search: (id: string) => `/search/${id}`,
109-
}));
110-
11198
jest.mock('hooks/useCurrentUser');
11299

113100
describe('Welcome', () => {
@@ -124,7 +111,7 @@ describe('Welcome', () => {
124111
);
125112
const linkToEditProfile = await screen.findByText('edit profile');
126113

127-
expect(linkToEditProfile).toHaveAttribute('href', '/edit-profile-page');
114+
expect(linkToEditProfile).toHaveAttribute('href', '/system/users/edit/admin-id');
128115
});
129116

130117
it('Shows appropriate message without link for readOnly users', async () => {

graylog2-web-interface/src/pages/InputsPage.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { DocumentTitle, PageHeader } from 'components/common';
2121
import { InputsList } from 'components/inputs';
2222
import AppConfig from 'util/AppConfig';
2323
import { Link } from 'components/common/router';
24-
import Routes from 'routing/Routes';
2524
import useProductName from 'brand-customization/useProductName';
2625

2726
const isCloud = AppConfig.isCloud();
@@ -50,7 +49,7 @@ const InputsPage = () => {
5049
</p>
5150
<p>
5251
If you are missing an input type on this page&apos;s list of available inputs, you can start the input
53-
on a <Link to={Routes.pluginRoute('SYSTEM_FORWARDERS')}>Forwarder</Link>.
52+
on a <Link to="/system/forwarders">Forwarder</Link>.
5453
</p>
5554
</>
5655
) : (

graylog2-web-interface/src/routing/Routes.ts

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
* along with this program. If not, see
1515
* <http://www.mongodb.com/licensing/server-side-public-license>.
1616
*/
17-
import { PluginStore } from 'graylog-web-plugin/plugin';
1817
import URI from 'urijs';
1918

2019
import AppConfig from 'util/AppConfig';
21-
import { extendedSearchPath, viewsPath } from 'views/Constants';
20+
import {
21+
extendedSearchPath,
22+
viewsPath,
23+
dashboardsTvPath,
24+
showDashboardsPath,
25+
newDashboardsPath,
26+
dashboardsPath,
27+
} from 'views/Constants';
2228
import type { TimeRangeTypes } from 'views/logic/queries/Query';
2329

2430
export const SECURITY_PATH = '/security';
@@ -49,6 +55,7 @@ const Routes = {
4955
STARTPAGE: '/',
5056
NOTFOUND: '/notfound',
5157
SEARCH: '/search',
58+
SEARCH_SHOW: (id: string) => `/search/${id}`,
5259
STREAMS: '/streams',
5360
STREAM_NEW: '/streams/new',
5461
ALERTS: {
@@ -78,8 +85,12 @@ const Routes = {
7885
ACTIVITY: `${SECURITY_PATH}/activity`,
7986
},
8087
SOURCES: '/sources',
81-
DASHBOARDS: '/dashboards',
82-
DASHBOARD_NEW: '/dashboards/new',
88+
DASHBOARDS: dashboardsPath,
89+
DASHBOARD: {
90+
NEW: newDashboardsPath,
91+
SHOW: showDashboardsPath,
92+
FULL_SCREEN: dashboardsTvPath,
93+
},
8394
WELCOME: '/welcome',
8495
GLOBAL_API_BROWSER_URL: '/api/api-browser/global/index.html',
8596
SYSTEM: {
@@ -299,8 +310,6 @@ const Routes = {
299310
Routes._common_search_url(`${Routes.STREAMS}/${streamId}/search`, query, timeRange, resolution),
300311
stream_alerts: (streamId: string) => `/alerts/?stream_id=${streamId}`,
301312

302-
legacy_stream_search: (streamId: string) => `/streams/${streamId}/messages`,
303-
304313
dashboard_show: (dashboardId: string) => `/dashboards/${dashboardId}`,
305314

306315
show_saved_search: (searchId: string) => `/search/${searchId}`,
@@ -411,62 +420,9 @@ const qualifiedRoutes = qualifyUrls(Routes);
411420

412421
const unqualified = Routes;
413422

414-
/*
415-
* Global registry of plugin routes. Route names are generated automatically from the route path, by removing
416-
* any colons, replacing slashes with underscores, and making the string uppercase. Below there is an example of how
417-
* to access the routes.
418-
*
419-
* Plugin register example:
420-
* routes: [
421-
* { path: '/system/pipelines', component: Foo },
422-
* { path: '/system/pipelines/:pipelineId', component: Bar },
423-
* ]
424-
*
425-
* Using routes on plugin components:
426-
* <LinkContainer to={Routes.pluginRoutes('SYSTEM_PIPELINES')}>...</LinkContainer>
427-
* <LinkContainer to={Routes.pluginRoutes('SYSTEM_PIPELINES_PIPELINEID')(123)}>...</LinkContainer>
428-
*
429-
*/
430-
const pluginRoute = (routeKey: string, throwError: boolean = true) => {
431-
const pluginRoutes = {};
432-
433-
PluginStore.exports('routes').forEach((route) => {
434-
const uri = new URI(route.path);
435-
const segments = uri.segment();
436-
const key = segments
437-
.map((segment) => segment.replace(':', ''))
438-
.join('_')
439-
.toUpperCase();
440-
const paramNames = segments.filter((segment) => segment.startsWith(':'));
441-
442-
if (paramNames.length > 0) {
443-
pluginRoutes[key] = (...paramValues) => {
444-
paramNames.forEach((param, idx) => {
445-
const value = String(paramValues[idx]);
446-
447-
uri.segment(segments.indexOf(param), value);
448-
});
449-
450-
return uri.pathname();
451-
};
452-
453-
return;
454-
}
455-
456-
pluginRoutes[key] = route.path;
457-
});
458-
459-
const route = qualifyUrls(pluginRoutes)[routeKey];
460-
461-
if (!route && throwError) {
462-
throw new Error(`Could not find plugin route '${routeKey}'.`);
463-
}
464-
465-
return route;
423+
const defaultExport = {
424+
...qualifiedRoutes,
425+
unqualified,
466426
};
467427

468-
const getPluginRoute = (routeKey: string) => pluginRoute(routeKey, false);
469-
470-
const defaultExport = Object.assign(qualifiedRoutes, { pluginRoute, getPluginRoute, unqualified });
471-
472428
export default defaultExport;

graylog2-web-interface/src/views/Constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ export const showSearchPath = `${searchPath}/:viewId`;
152152

153153
export const dashboardsPath = '/dashboards';
154154
export const newDashboardsPath = `${dashboardsPath}/new`;
155-
export const dashboardsTvPath = `${dashboardsPath}/tv/:viewId`;
156-
export const showDashboardsPath = `${dashboardsPath}/:viewId`;
155+
export const dashboardsTvPath = (viewId: string = ':viewId') => `${dashboardsPath}/tv/${viewId}`;
156+
export const showDashboardsPath = (viewId: string = ':viewId') => `${dashboardsPath}/${viewId}`;
157157

158158
export const extendedSearchPath = '/extendedsearch';
159159

graylog2-web-interface/src/views/bindings.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ const exports: PluginExports = {
161161
},
162162
routes: [
163163
{ path: newDashboardsPath, component: NewDashboardPage, parentComponent: App },
164-
{ path: dashboardsTvPath, component: ShowDashboardInBigDisplayMode, parentComponent: null },
164+
{ path: dashboardsTvPath(), component: ShowDashboardInBigDisplayMode, parentComponent: null },
165165
{ path: dashboardsPath, component: DashboardsPage },
166-
{ path: showDashboardsPath, component: ShowViewPage, parentComponent: App },
166+
{ path: showDashboardsPath(), component: ShowViewPage, parentComponent: App },
167167

168168
{ path: newSearchPath, component: NewSearchRedirectPage, parentComponent: null },
169169
{ path: showSearchPath, component: ShowViewPage, parentComponent: App },

0 commit comments

Comments
 (0)