Setup routing for process panel
[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 } 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 };
22
23 export const getResourceUrl = (uuid: string) => {
24     const kind = extractUuidKind(uuid);
25     switch (kind) {
26         case ResourceKind.PROJECT:
27             return getProjectUrl(uuid);
28         case ResourceKind.COLLECTION:
29             return getCollectionUrl(uuid);
30         default:
31             return undefined;
32     }
33 };
34
35 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
36
37 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
38     const handler = handleLocationChange(store);
39     handler(history.location);
40     history.listen(handler);
41 };
42
43 export const matchRootRoute = (route: string) =>
44     matchPath(route, { path: Routes.ROOT, exact: true });
45
46 export const matchFavoritesRoute = (route: string) =>
47     matchPath(route, { path: Routes.FAVORITES });
48
49 export interface ResourceRouteParams {
50     id: string;
51 }
52
53 export const matchProjectRoute = (route: string) =>
54     matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
55
56 export const matchCollectionRoute = (route: string) =>
57     matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
58
59 export const matchProcessRoute = (route: string) =>
60     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
61
62
63 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
64     const projectMatch = matchProjectRoute(pathname);
65     const collectionMatch = matchCollectionRoute(pathname);
66     const favoriteMatch = matchFavoritesRoute(pathname);
67     const processMatch = matchProcessRoute(pathname);
68     if (projectMatch) {
69         store.dispatch(loadProject(projectMatch.params.id));
70     } else if (collectionMatch) {
71         store.dispatch(loadCollection(collectionMatch.params.id));
72     } else if (favoriteMatch) {
73         store.dispatch(loadFavorites());
74     } else if (processMatch) {
75         store.dispatch(loadProcess(processMatch.params.id));
76     }
77 };