refs #master Merge branch 'origin/master' into 14007-ts-paths
[arvados-workbench2.git] / src / store / side-panel / side-panel-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as _ from "lodash";
6 import { sidePanelActions, SidePanelAction } from './side-panel-action';
7 import { SidePanelItem } from '~/components/side-panel/side-panel';
8 import { ProjectsIcon, ShareMeIcon, WorkflowIcon, RecentIcon, FavoriteIcon, TrashIcon } from "~/components/icon/icon";
9 import { Dispatch } from "redux";
10 import { push } from "react-router-redux";
11 import { favoritePanelActions } from "../favorite-panel/favorite-panel-action";
12 import { projectPanelActions } from "../project-panel/project-panel-action";
13 import { projectActions } from "../project/project-action";
14 import { getProjectUrl } from "../../models/project";
15
16 export type SidePanelState = SidePanelItem[];
17
18 export const sidePanelReducer = (state: SidePanelState = sidePanelData, action: SidePanelAction) => {
19     if (state.length === 0) {
20         return sidePanelData;
21     } else {
22         return sidePanelActions.match(action, {
23             TOGGLE_SIDE_PANEL_ITEM_OPEN: itemId =>
24                 state.map(it => ({...it, open: itemId === it.id && it.open === false})),
25             TOGGLE_SIDE_PANEL_ITEM_ACTIVE: itemId => {
26                 const sidePanel = _.cloneDeep(state);
27                 resetSidePanelActivity(sidePanel);
28                 sidePanel.forEach(it => {
29                     if (it.id === itemId) {
30                         it.active = true;
31                     }
32                 });
33                 return sidePanel;
34             },
35             RESET_SIDE_PANEL_ACTIVITY: () => {
36                 const sidePanel = _.cloneDeep(state);
37                 resetSidePanelActivity(sidePanel);
38                 return sidePanel;
39             },
40             default: () => state
41         });
42     }
43 };
44
45 export enum SidePanelIdentifiers {
46     PROJECTS = "Projects",
47     SHARED_WITH_ME = "SharedWithMe",
48     WORKFLOWS = "Workflows",
49     RECENT_OPEN = "RecentOpen",
50     FAVORITES = "Favourites",
51     TRASH = "Trash"
52 }
53
54 export const sidePanelData = [
55     {
56         id: SidePanelIdentifiers.PROJECTS,
57         name: "Projects",
58         icon: ProjectsIcon,
59         open: false,
60         active: false,
61         margin: true,
62         openAble: true,
63         activeAction: (dispatch: Dispatch, uuid: string) => {
64             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
65             dispatch(push(getProjectUrl(uuid)));
66             dispatch(projectPanelActions.RESET_PAGINATION());
67             dispatch(projectPanelActions.REQUEST_ITEMS()); 
68         }
69     },
70     {
71         id: SidePanelIdentifiers.SHARED_WITH_ME,
72         name: "Shared with me",
73         icon: ShareMeIcon,
74         active: false,
75     },
76     {
77         id: SidePanelIdentifiers.WORKFLOWS,
78         name: "Workflows",
79         icon: WorkflowIcon,
80         active: false,
81     },
82     {
83         id: SidePanelIdentifiers.RECENT_OPEN,
84         name: "Recent open",
85         icon: RecentIcon,
86         active: false,
87     },
88     {
89         id: SidePanelIdentifiers.FAVORITES,
90         name: "Favorites",
91         icon: FavoriteIcon,
92         active: false,
93         activeAction: (dispatch: Dispatch) => {
94             dispatch(push("/favorites"));
95             dispatch(favoritePanelActions.RESET_PAGINATION());
96             dispatch(favoritePanelActions.REQUEST_ITEMS());
97         }
98     },
99     {
100         id: SidePanelIdentifiers.TRASH,
101         name: "Trash",
102         icon: TrashIcon,
103         active: false,
104     }
105 ];
106
107 function resetSidePanelActivity(sidePanel: SidePanelItem[]) {
108     for (const t of sidePanel) {
109         t.active = false;
110     }
111 }