Fix showing project explorer after going back from favourites
[arvados.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 import { columns as projectPanelColumns } from "../../views/project-panel/project-panel";
16 import { columns as favoritePanelColumns } from "../../views/favorite-panel/favorite-panel";
17
18 export type SidePanelState = SidePanelItem[];
19
20 export const sidePanelReducer = (state: SidePanelState = sidePanelData, action: SidePanelAction) => {
21     if (state.length === 0) {
22         return sidePanelData;
23     } else {
24         return sidePanelActions.match(action, {
25             TOGGLE_SIDE_PANEL_ITEM_OPEN: itemId =>
26                 state.map(it => ({...it, open: itemId === it.id && it.open === false})),
27             TOGGLE_SIDE_PANEL_ITEM_ACTIVE: itemId => {
28                 const sidePanel = _.cloneDeep(state);
29                 resetSidePanelActivity(sidePanel);
30                 sidePanel.forEach(it => {
31                     if (it.id === itemId) {
32                         it.active = true;
33                     }
34                 });
35                 return sidePanel;
36             },
37             RESET_SIDE_PANEL_ACTIVITY: () => {
38                 const sidePanel = _.cloneDeep(state);
39                 resetSidePanelActivity(sidePanel);
40                 return sidePanel;
41             },
42             default: () => state
43         });
44     }
45 };
46
47 export enum SidePanelIdentifiers {
48     PROJECTS = "Projects",
49     SHARED_WITH_ME = "SharedWithMe",
50     WORKFLOWS = "Workflows",
51     RECENT_OPEN = "RecentOpen",
52     FAVORITES = "Favourites",
53     TRASH = "Trash"
54 }
55
56 export const sidePanelData = [
57     {
58         id: SidePanelIdentifiers.PROJECTS,
59         name: "Projects",
60         icon: ProjectsIcon,
61         open: false,
62         active: false,
63         margin: true,
64         openAble: true,
65         activeAction: (dispatch: Dispatch, uuid: string) => {
66             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
67             dispatch(push(getProjectUrl(uuid)));
68             dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
69             dispatch(projectPanelActions.RESET_PAGINATION());
70             dispatch(projectPanelActions.REQUEST_ITEMS());
71         }
72     },
73     {
74         id: SidePanelIdentifiers.SHARED_WITH_ME,
75         name: "Shared with me",
76         icon: ShareMeIcon,
77         active: false,
78     },
79     {
80         id: SidePanelIdentifiers.WORKFLOWS,
81         name: "Workflows",
82         icon: WorkflowIcon,
83         active: false,
84     },
85     {
86         id: SidePanelIdentifiers.RECENT_OPEN,
87         name: "Recent open",
88         icon: RecentIcon,
89         active: false,
90     },
91     {
92         id: SidePanelIdentifiers.FAVORITES,
93         name: "Favorites",
94         icon: FavoriteIcon,
95         active: false,
96         activeAction: (dispatch: Dispatch) => {
97             dispatch(push("/favorites"));
98             dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }))
99             dispatch(favoritePanelActions.RESET_PAGINATION());
100             dispatch(favoritePanelActions.REQUEST_ITEMS());
101         }
102     },
103     {
104         id: SidePanelIdentifiers.TRASH,
105         name: "Trash",
106         icon: TrashIcon,
107         active: false,
108     }
109 ];
110
111 function resetSidePanelActivity(sidePanel: SidePanelItem[]) {
112     for (const t of sidePanel) {
113         t.active = false;
114     }
115 }