15230: Link to other workbenches when double-clicking search results.
[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, COLLECTION_PDH_REGEX } from '~/models/resource';
7 import { getProjectUrl } from '~/models/project';
8 import { getCollectionUrl } from '~/models/collection';
9 import { Config } from '~/common/config';
10 import { Session } from "~/models/session";
11
12 export interface FederationConfig {
13     localCluster: string;
14     remoteHostsConfig: { [key: string]: Config };
15     sessions: Session[];
16 }
17
18 export const Routes = {
19     ROOT: '/',
20     TOKEN: '/token',
21     FED_LOGIN: '/fedtoken',
22     PROJECTS: `/projects/:id(${RESOURCE_UUID_PATTERN})`,
23     COLLECTIONS: `/collections/:id(${RESOURCE_UUID_PATTERN})`,
24     PROCESSES: `/processes/:id(${RESOURCE_UUID_PATTERN})`,
25     FAVORITES: '/favorites',
26     TRASH: '/trash',
27     PROCESS_LOGS: `/process-logs/:id(${RESOURCE_UUID_PATTERN})`,
28     REPOSITORIES: '/repositories',
29     SHARED_WITH_ME: '/shared-with-me',
30     RUN_PROCESS: '/run-process',
31     VIRTUAL_MACHINES_ADMIN: '/virtual-machines-admin',
32     VIRTUAL_MACHINES_USER: '/virtual-machines-user',
33     WORKFLOWS: '/workflows',
34     SEARCH_RESULTS: '/search-results',
35     SSH_KEYS_ADMIN: `/ssh-keys-admin`,
36     SSH_KEYS_USER: `/ssh-keys-user`,
37     SITE_MANAGER: `/site-manager`,
38     MY_ACCOUNT: '/my-account',
39     KEEP_SERVICES: `/keep-services`,
40     COMPUTE_NODES: `/nodes`,
41     USERS: '/users',
42     API_CLIENT_AUTHORIZATIONS: `/api_client_authorizations`,
43     GROUPS: '/groups',
44     GROUP_DETAILS: `/group/:id(${RESOURCE_UUID_PATTERN})`,
45     LINKS: '/links',
46     PUBLIC_FAVORITES: '/public-favorites',
47     COLLECTIONS_CONTENT_ADDRESS: '/collections/:id',
48 };
49
50 export const getResourceUrl = (uuid: string) => {
51     const kind = extractUuidKind(uuid);
52     console.log(`for ${uuid} the kind is ${kind}`);
53     switch (kind) {
54         case ResourceKind.PROJECT:
55             return getProjectUrl(uuid);
56         case ResourceKind.USER:
57             return getProjectUrl(uuid);
58         case ResourceKind.COLLECTION:
59             return getCollectionUrl(uuid);
60         case ResourceKind.PROCESS:
61             return getProcessUrl(uuid);
62         default:
63             return undefined;
64     }
65 };
66
67 export const getNavUrl = (uuid: string, config: FederationConfig) => {
68     const path = getResourceUrl(uuid) || "";
69     const cls = uuid.substr(0, 5);
70     if (cls === config.localCluster || extractUuidKind(uuid) === ResourceKind.USER || COLLECTION_PDH_REGEX.exec(uuid)) {
71         return path;
72     } else if (config.remoteHostsConfig[cls]) {
73         let u: URL;
74         if (config.remoteHostsConfig[cls].workbench2Url) {
75             u = new URL(config.remoteHostsConfig[cls].workbench2Url || "");
76         } else {
77             u = new URL(config.remoteHostsConfig[cls].workbenchUrl);
78             u.search = "api_token=" + config.sessions.filter((s) => s.clusterId === cls)[0].token;
79         }
80         u.pathname = path;
81         return u.toString();
82     } else {
83         return "";
84     }
85 };
86
87
88 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
89
90 export const getProcessLogUrl = (uuid: string) => `/process-logs/${uuid}`;
91
92 export const getGroupUrl = (uuid: string) => `/group/${uuid}`;
93
94 export interface ResourceRouteParams {
95     id: string;
96 }
97
98 export const matchRootRoute = (route: string) =>
99     matchPath(route, { path: Routes.ROOT, exact: true });
100
101 export const matchFavoritesRoute = (route: string) =>
102     matchPath(route, { path: Routes.FAVORITES });
103
104 export const matchTrashRoute = (route: string) =>
105     matchPath(route, { path: Routes.TRASH });
106
107 export const matchProjectRoute = (route: string) =>
108     matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
109
110 export const matchCollectionRoute = (route: string) =>
111     matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
112
113 export const matchProcessRoute = (route: string) =>
114     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
115
116 export const matchProcessLogRoute = (route: string) =>
117     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESS_LOGS });
118
119 export const matchSharedWithMeRoute = (route: string) =>
120     matchPath(route, { path: Routes.SHARED_WITH_ME });
121
122 export const matchRunProcessRoute = (route: string) =>
123     matchPath(route, { path: Routes.RUN_PROCESS });
124
125 export const matchWorkflowRoute = (route: string) =>
126     matchPath<ResourceRouteParams>(route, { path: Routes.WORKFLOWS });
127
128 export const matchSearchResultsRoute = (route: string) =>
129     matchPath<ResourceRouteParams>(route, { path: Routes.SEARCH_RESULTS });
130
131 export const matchUserVirtualMachineRoute = (route: string) =>
132     matchPath<ResourceRouteParams>(route, { path: Routes.VIRTUAL_MACHINES_USER });
133
134 export const matchAdminVirtualMachineRoute = (route: string) =>
135     matchPath<ResourceRouteParams>(route, { path: Routes.VIRTUAL_MACHINES_ADMIN });
136
137 export const matchRepositoriesRoute = (route: string) =>
138     matchPath<ResourceRouteParams>(route, { path: Routes.REPOSITORIES });
139
140 export const matchSshKeysUserRoute = (route: string) =>
141     matchPath(route, { path: Routes.SSH_KEYS_USER });
142
143 export const matchSshKeysAdminRoute = (route: string) =>
144     matchPath(route, { path: Routes.SSH_KEYS_ADMIN });
145
146 export const matchSiteManagerRoute = (route: string) =>
147     matchPath(route, { path: Routes.SITE_MANAGER });
148
149 export const matchMyAccountRoute = (route: string) =>
150     matchPath(route, { path: Routes.MY_ACCOUNT });
151
152 export const matchKeepServicesRoute = (route: string) =>
153     matchPath(route, { path: Routes.KEEP_SERVICES });
154
155 export const matchUsersRoute = (route: string) =>
156     matchPath(route, { path: Routes.USERS });
157
158 export const matchComputeNodesRoute = (route: string) =>
159     matchPath(route, { path: Routes.COMPUTE_NODES });
160
161 export const matchApiClientAuthorizationsRoute = (route: string) =>
162     matchPath(route, { path: Routes.API_CLIENT_AUTHORIZATIONS });
163
164 export const matchGroupsRoute = (route: string) =>
165     matchPath(route, { path: Routes.GROUPS });
166
167 export const matchGroupDetailsRoute = (route: string) =>
168     matchPath<ResourceRouteParams>(route, { path: Routes.GROUP_DETAILS });
169
170 export const matchLinksRoute = (route: string) =>
171     matchPath(route, { path: Routes.LINKS });
172
173 export const matchPublicFavoritesRoute = (route: string) =>
174     matchPath(route, { path: Routes.PUBLIC_FAVORITES });
175
176 export const matchCollectionsContentAddressRoute = (route: string) =>
177     matchPath(route, { path: Routes.COLLECTIONS_CONTENT_ADDRESS });