Merge branch 'master' into 14452-my-account
[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     REPOSITORIES: '/repositories',
20     SHARED_WITH_ME: '/shared-with-me',
21     RUN_PROCESS: '/run-process',
22     VIRTUAL_MACHINES: '/virtual-machines',
23     WORKFLOWS: '/workflows',
24     SEARCH_RESULTS: '/search-results',
25     SSH_KEYS: `/ssh-keys`,
26     MY_ACCOUNT: '/my-account'
27 };
28
29 export const getResourceUrl = (uuid: string) => {
30     const kind = extractUuidKind(uuid);
31     switch (kind) {
32         case ResourceKind.PROJECT:
33             return getProjectUrl(uuid);
34         case ResourceKind.COLLECTION:
35             return getCollectionUrl(uuid);
36         case ResourceKind.PROCESS:
37             return getProcessUrl(uuid);
38         default:
39             return undefined;
40     }
41 };
42
43 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
44
45 export const getProcessLogUrl = (uuid: string) => `/process-logs/${uuid}`;
46
47 export interface ResourceRouteParams {
48     id: string;
49 }
50
51 export const matchRootRoute = (route: string) =>
52     matchPath(route, { path: Routes.ROOT, exact: true });
53
54 export const matchFavoritesRoute = (route: string) =>
55     matchPath(route, { path: Routes.FAVORITES });
56
57 export const matchTrashRoute = (route: string) =>
58     matchPath(route, { path: Routes.TRASH });
59
60 export const matchProjectRoute = (route: string) =>
61     matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
62
63 export const matchCollectionRoute = (route: string) =>
64     matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
65
66 export const matchProcessRoute = (route: string) =>
67     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
68
69 export const matchProcessLogRoute = (route: string) =>
70     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESS_LOGS });
71
72 export const matchSharedWithMeRoute = (route: string) =>
73     matchPath(route, { path: Routes.SHARED_WITH_ME });
74
75 export const matchRunProcessRoute = (route: string) =>
76     matchPath(route, { path: Routes.RUN_PROCESS });
77
78 export const matchWorkflowRoute = (route: string) =>
79     matchPath<ResourceRouteParams>(route, { path: Routes.WORKFLOWS });
80
81 export const matchSearchResultsRoute = (route: string) =>
82     matchPath<ResourceRouteParams>(route, { path: Routes.SEARCH_RESULTS });
83
84 export const matchVirtualMachineRoute = (route: string) =>
85     matchPath<ResourceRouteParams>(route, { path: Routes.VIRTUAL_MACHINES });
86     
87 export const matchRepositoriesRoute = (route: string) =>
88     matchPath<ResourceRouteParams>(route, { path: Routes.REPOSITORIES });
89     
90 export const matchSshKeysRoute = (route: string) =>
91     matchPath(route, { path: Routes.SSH_KEYS });
92
93 export const matchMyAccountRoute = (route: string) =>
94     matchPath(route, { path: Routes.MY_ACCOUNT });