Merge branch '17426-plug-ins' refs #17426
[arvados-workbench2.git] / src / routes / route-change-handlers.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 * as Routes from '~/routes/routes';
8 import * as WorkbenchActions from '~/store/workbench/workbench-actions';
9 import { navigateToRootProject } from '~/store/navigation/navigation-action';
10 import { dialogActions } from '~/store/dialog/dialog-actions';
11 import { contextMenuActions } from '~/store/context-menu/context-menu-actions';
12 import { searchBarActions } from '~/store/search-bar/search-bar-actions';
13 import { pluginConfig } from '~/plugins';
14
15 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
16     const handler = handleLocationChange(store);
17     handler(history.location);
18     history.listen(handler);
19 };
20
21 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
22
23     const rootMatch = Routes.matchRootRoute(pathname);
24     const projectMatch = Routes.matchProjectRoute(pathname);
25     const collectionMatch = Routes.matchCollectionRoute(pathname);
26     const favoriteMatch = Routes.matchFavoritesRoute(pathname);
27     const publicFavoritesMatch = Routes.matchPublicFavoritesRoute(pathname);
28     const trashMatch = Routes.matchTrashRoute(pathname);
29     const processMatch = Routes.matchProcessRoute(pathname);
30     const processLogMatch = Routes.matchProcessLogRoute(pathname);
31     const repositoryMatch = Routes.matchRepositoriesRoute(pathname);
32     const searchResultsMatch = Routes.matchSearchResultsRoute(pathname);
33     const sharedWithMeMatch = Routes.matchSharedWithMeRoute(pathname);
34     const runProcessMatch = Routes.matchRunProcessRoute(pathname);
35     const virtualMachineUserMatch = Routes.matchUserVirtualMachineRoute(pathname);
36     const virtualMachineAdminMatch = Routes.matchAdminVirtualMachineRoute(pathname);
37     const workflowMatch = Routes.matchWorkflowRoute(pathname);
38     const sshKeysUserMatch = Routes.matchSshKeysUserRoute(pathname);
39     const sshKeysAdminMatch = Routes.matchSshKeysAdminRoute(pathname);
40     const siteManagerMatch = Routes.matchSiteManagerRoute(pathname);
41     const keepServicesMatch = Routes.matchKeepServicesRoute(pathname);
42     const computeNodesMatch = Routes.matchComputeNodesRoute(pathname);
43     const apiClientAuthorizationsMatch = Routes.matchApiClientAuthorizationsRoute(pathname);
44     const myAccountMatch = Routes.matchMyAccountRoute(pathname);
45     const linkAccountMatch = Routes.matchLinkAccountRoute(pathname);
46     const userMatch = Routes.matchUsersRoute(pathname);
47     const groupsMatch = Routes.matchGroupsRoute(pathname);
48     const groupDetailsMatch = Routes.matchGroupDetailsRoute(pathname);
49     const linksMatch = Routes.matchLinksRoute(pathname);
50     const collectionsContentAddressMatch = Routes.matchCollectionsContentAddressRoute(pathname);
51     const allProcessesMatch = Routes.matchAllProcessesRoute(pathname);
52
53     store.dispatch(dialogActions.CLOSE_ALL_DIALOGS());
54     store.dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
55     store.dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
56
57     for (const locChangeFn of pluginConfig.locationChangeHandlers) {
58         if (locChangeFn(store, pathname)) {
59             return;
60         }
61     }
62
63     if (projectMatch) {
64         store.dispatch(WorkbenchActions.loadProject(projectMatch.params.id));
65     } else if (collectionMatch) {
66         store.dispatch(WorkbenchActions.loadCollection(collectionMatch.params.id));
67     } else if (favoriteMatch) {
68         store.dispatch(WorkbenchActions.loadFavorites());
69     } else if (publicFavoritesMatch) {
70         store.dispatch(WorkbenchActions.loadPublicFavorites());
71     } else if (trashMatch) {
72         store.dispatch(WorkbenchActions.loadTrash());
73     } else if (processMatch) {
74         store.dispatch(WorkbenchActions.loadProcess(processMatch.params.id));
75     } else if (processLogMatch) {
76         store.dispatch(WorkbenchActions.loadProcessLog(processLogMatch.params.id));
77     } else if (rootMatch) {
78         store.dispatch(navigateToRootProject);
79     } else if (sharedWithMeMatch) {
80         store.dispatch(WorkbenchActions.loadSharedWithMe);
81     } else if (runProcessMatch) {
82         store.dispatch(WorkbenchActions.loadRunProcess);
83     } else if (workflowMatch) {
84         store.dispatch(WorkbenchActions.loadWorkflow);
85     } else if (searchResultsMatch) {
86         store.dispatch(WorkbenchActions.loadSearchResults);
87     } else if (virtualMachineUserMatch) {
88         store.dispatch(WorkbenchActions.loadVirtualMachines);
89     } else if (virtualMachineAdminMatch) {
90         store.dispatch(WorkbenchActions.loadVirtualMachines);
91     } else if (repositoryMatch) {
92         store.dispatch(WorkbenchActions.loadRepositories);
93     } else if (sshKeysUserMatch) {
94         store.dispatch(WorkbenchActions.loadSshKeys);
95     } else if (sshKeysAdminMatch) {
96         store.dispatch(WorkbenchActions.loadSshKeys);
97     } else if (siteManagerMatch) {
98         store.dispatch(WorkbenchActions.loadSiteManager);
99     } else if (keepServicesMatch) {
100         store.dispatch(WorkbenchActions.loadKeepServices);
101     } else if (computeNodesMatch) {
102         store.dispatch(WorkbenchActions.loadComputeNodes);
103     } else if (apiClientAuthorizationsMatch) {
104         store.dispatch(WorkbenchActions.loadApiClientAuthorizations);
105     } else if (myAccountMatch) {
106         store.dispatch(WorkbenchActions.loadMyAccount);
107     } else if (linkAccountMatch) {
108         store.dispatch(WorkbenchActions.loadLinkAccount);
109     } else if (userMatch) {
110         store.dispatch(WorkbenchActions.loadUsers);
111     } else if (groupsMatch) {
112         store.dispatch(WorkbenchActions.loadGroupsPanel);
113     } else if (groupDetailsMatch) {
114         store.dispatch(WorkbenchActions.loadGroupDetailsPanel(groupDetailsMatch.params.id));
115     } else if (linksMatch) {
116         store.dispatch(WorkbenchActions.loadLinks);
117     } else if (collectionsContentAddressMatch) {
118         store.dispatch(WorkbenchActions.loadCollectionContentAddress);
119     } else if (allProcessesMatch) {
120         store.dispatch(WorkbenchActions.loadAllProcesses());
121     }
122 };