Merge branch '14098-log-view'
[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 { History, Location } from 'history';
6 import { RootStore } from '~/store/store';
7 import { matchPath } from 'react-router';
8 import { ResourceKind, RESOURCE_UUID_PATTERN, extractUuidKind } from '~/models/resource';
9 import { getProjectUrl } from '../models/project';
10 import { getCollectionUrl } from '~/models/collection';
11 import { loadProject, loadFavorites, loadCollection, loadProcessLog } from '~/store/workbench/workbench-actions';
12 import { loadProcess } from '~/store/processes/processes-actions';
13
14 export const Routes = {
15     ROOT: '/',
16     TOKEN: '/token',
17     PROJECTS: `/projects/:id(${RESOURCE_UUID_PATTERN})`,
18     COLLECTIONS: `/collections/:id(${RESOURCE_UUID_PATTERN})`,
19     PROCESSES: `/processes/:id(${RESOURCE_UUID_PATTERN})`,
20     FAVORITES: '/favorites',
21     PROCESS_LOGS: `/process-logs/:id(${RESOURCE_UUID_PATTERN})`
22 };
23
24 export const getResourceUrl = (uuid: string) => {
25     const kind = extractUuidKind(uuid);
26     switch (kind) {
27         case ResourceKind.PROJECT:
28             return getProjectUrl(uuid);
29         case ResourceKind.COLLECTION:
30             return getCollectionUrl(uuid);
31         default:
32             return undefined;
33     }
34 };
35
36 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
37
38 export const getProcessLogUrl = (uuid: string) => `/process-logs/${uuid}`;
39
40 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
41     const handler = handleLocationChange(store);
42     handler(history.location);
43     history.listen(handler);
44 };
45
46 export const matchRootRoute = (route: string) =>
47     matchPath(route, { path: Routes.ROOT, exact: true });
48
49 export const matchFavoritesRoute = (route: string) =>
50     matchPath(route, { path: Routes.FAVORITES });
51
52 export interface ResourceRouteParams {
53     id: string;
54 }
55
56 export const matchProjectRoute = (route: string) =>
57     matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
58
59 export const matchCollectionRoute = (route: string) =>
60     matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
61
62 export const matchProcessRoute = (route: string) =>
63     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
64
65 export const matchProcessLogRoute = (route: string) =>
66     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESS_LOGS });
67
68 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
69     const projectMatch = matchProjectRoute(pathname);
70     const collectionMatch = matchCollectionRoute(pathname);
71     const favoriteMatch = matchFavoritesRoute(pathname);
72     const processMatch = matchProcessRoute(pathname);
73     const processLogMatch = matchProcessLogRoute(pathname);
74     
75     if (projectMatch) {
76         store.dispatch(loadProject(projectMatch.params.id));
77     } else if (collectionMatch) {
78         store.dispatch(loadCollection(collectionMatch.params.id));
79     } else if (favoriteMatch) {
80         store.dispatch(loadFavorites());
81     } else if (processMatch) {
82         store.dispatch(loadProcess(processMatch.params.id));
83     } else if (processLogMatch) {
84         store.dispatch(loadProcessLog(processLogMatch.params.id));
85     }
86 };