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, loadProcessLog } 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',
21 PROCESS_LOGS: `/process-logs/:id(${RESOURCE_UUID_PATTERN})`
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 getProcessLogUrl = (uuid: string) => `/process-logs/${uuid}`;
40 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
41 const handler = handleLocationChange(store);
42 handler(history.location);
43 history.listen(handler);
46 export const matchRootRoute = (route: string) =>
47 matchPath(route, { path: Routes.ROOT, exact: true });
49 export const matchFavoritesRoute = (route: string) =>
50 matchPath(route, { path: Routes.FAVORITES });
52 export interface ResourceRouteParams {
56 export const matchProjectRoute = (route: string) =>
57 matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
59 export const matchCollectionRoute = (route: string) =>
60 matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
62 export const matchProcessRoute = (route: string) =>
63 matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
65 export const matchProcessLogRoute = (route: string) =>
66 matchPath<ResourceRouteParams>(route, { path: Routes.PROCESS_LOGS });
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);
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));