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/navigation/navigation-action';
13 export const Routes = {
15 PROJECTS: `/projects/:id(${RESOURCE_UUID_PATTERN})`,
16 COLLECTIONS: `/collections/:id(${RESOURCE_UUID_PATTERN})`,
17 FAVORITES: '/favorites',
20 export const getResourceUrl = (uuid: string) => {
21 const kind = extractUuidKind(uuid);
23 case ResourceKind.PROJECT:
24 return getProjectUrl(uuid);
25 case ResourceKind.COLLECTION:
26 return getCollectionUrl(uuid);
32 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
33 const handler = handleLocationChange(store);
34 handler(history.location);
35 history.listen(handler);
38 export const matchRootRoute = (route: string) =>
39 matchPath(route, { path: Routes.ROOT, exact: true });
41 export const matchFavoritesRoute = (route: string) =>
42 matchPath(route, { path: Routes.FAVORITES });
44 export interface ProjectRouteParams {
48 export const matchProjectRoute = (route: string) =>
49 matchPath<ProjectRouteParams>(route, { path: Routes.PROJECTS });
51 export interface CollectionRouteParams {
55 export const matchCollectionRoute = (route: string) =>
56 matchPath<ProjectRouteParams>(route, { path: Routes.COLLECTIONS });
59 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
60 const projectMatch = matchProjectRoute(pathname);
61 const collectionMatch = matchCollectionRoute(pathname);
62 const favoriteMatch = matchFavoritesRoute(pathname);
64 store.dispatch(loadProject(projectMatch.params.id));
65 } else if (collectionMatch) {
66 store.dispatch(loadCollection(collectionMatch.params.id));
67 } else if (favoriteMatch) {
68 store.dispatch(loadFavorites());