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 } 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',
23 export const getResourceUrl = (uuid: string) => {
24 const kind = extractUuidKind(uuid);
26 case ResourceKind.PROJECT:
27 return getProjectUrl(uuid);
28 case ResourceKind.COLLECTION:
29 return getCollectionUrl(uuid);
35 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
37 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
38 const handler = handleLocationChange(store);
39 handler(history.location);
40 history.listen(handler);
43 export const matchRootRoute = (route: string) =>
44 matchPath(route, { path: Routes.ROOT, exact: true });
46 export const matchFavoritesRoute = (route: string) =>
47 matchPath(route, { path: Routes.FAVORITES });
49 export interface ResourceRouteParams {
53 export const matchProjectRoute = (route: string) =>
54 matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
56 export const matchCollectionRoute = (route: string) =>
57 matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
59 export const matchProcessRoute = (route: string) =>
60 matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
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);
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));