merge master
[arvados-workbench2.git] / src / routes / routes.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { matchPath } from 'react-router';
6 import { ResourceKind, RESOURCE_UUID_PATTERN, extractUuidKind } from '~/models/resource';
7 import { getProjectUrl } from '~/models/project';
8 import { getCollectionUrl } from '~/models/collection';
9
10 export const Routes = {
11     ROOT: '/',
12     TOKEN: '/token',
13     PROJECTS: `/projects/:id(${RESOURCE_UUID_PATTERN})`,
14     COLLECTIONS: `/collections/:id(${RESOURCE_UUID_PATTERN})`,
15     PROCESSES: `/processes/:id(${RESOURCE_UUID_PATTERN})`,
16     FAVORITES: '/favorites',
17     TRASH: '/trash',
18     PROCESS_LOGS: `/process-logs/:id(${RESOURCE_UUID_PATTERN})`
19 };
20
21 export const getResourceUrl = (uuid: string) => {
22     const kind = extractUuidKind(uuid);
23     switch (kind) {
24         case ResourceKind.PROJECT:
25             return getProjectUrl(uuid);
26         case ResourceKind.COLLECTION:
27             return getCollectionUrl(uuid);
28         default:
29             return undefined;
30     }
31 };
32
33 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
34
35 export const getProcessLogUrl = (uuid: string) => `/process-logs/${uuid}`;
36
37 export interface ResourceRouteParams {
38     id: string;
39 }
40
41 export const matchRootRoute = (route: string) =>
42     matchPath(route, { path: Routes.ROOT, exact: true });
43
44 export const matchFavoritesRoute = (route: string) =>
45     matchPath(route, { path: Routes.FAVORITES });
46
47 export const matchTrashRoute = (route: string) =>
48     matchPath(route, { path: Routes.TRASH });
49
50 export const matchProjectRoute = (route: string) =>
51     matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
52
53 export const matchCollectionRoute = (route: string) =>
54     matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
55
56 export const matchProcessRoute = (route: string) =>
57     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
58
59 export const matchProcessLogRoute = (route: string) =>
60     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESS_LOGS });