1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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, loadTrash } from '~/store/workbench/workbench-actions';
12 import { loadProcess } from '~/store/processes/processes-actions';
14 export const Routes = {
17 PROJECTS: `/projects/:id(${RESOURCE_UUID_PATTERN})`,
18 COLLECTIONS: `/collections/:id(${RESOURCE_UUID_PATTERN})`,
19 PROCESSES: `/processes/:id(${RESOURCE_UUID_PATTERN})`,
20 FAVORITES: '/favorites',
24 export const getResourceUrl = (uuid: string) => {
25 const kind = extractUuidKind(uuid);
27 case ResourceKind.PROJECT:
28 return getProjectUrl(uuid);
29 case ResourceKind.COLLECTION:
30 return getCollectionUrl(uuid);
36 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
38 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
39 const handler = handleLocationChange(store);
40 handler(history.location);
41 history.listen(handler);
44 export interface ResourceRouteParams {
48 export const matchRootRoute = (route: string) =>
49 matchPath(route, { path: Routes.ROOT, exact: true });
51 export const matchFavoritesRoute = (route: string) =>
52 matchPath(route, { path: Routes.FAVORITES });
54 export const matchTrashRoute = (route: string) =>
55 matchPath(route, { path: Routes.TRASH });
57 export const matchProjectRoute = (route: string) =>
58 matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
60 export const matchCollectionRoute = (route: string) =>
61 matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
63 export const matchProcessRoute = (route: string) =>
64 matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
67 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
68 const projectMatch = matchProjectRoute(pathname);
69 const collectionMatch = matchCollectionRoute(pathname);
70 const favoriteMatch = matchFavoritesRoute(pathname);
71 const trashMatch = matchTrashRoute(pathname);
72 const processMatch = matchProcessRoute(pathname);
74 store.dispatch(loadProject(projectMatch.params.id));
75 } else if (collectionMatch) {
76 store.dispatch(loadCollection(collectionMatch.params.id));
77 } else if (favoriteMatch) {
78 store.dispatch(loadFavorites());
79 } else if (trashMatch) {
80 store.dispatch(loadTrash());
81 } else if (processMatch) {
82 store.dispatch(loadProcess(processMatch.params.id));