17426: Add plugin ability to modify +New and account menu
[arvados-workbench2.git] / src / plugins / example / index.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 // Example plugin.
6
7 import { PluginConfig } from '~/common/plugintypes';
8 import * as React from 'react';
9 import { Dispatch } from 'redux';
10 import { RootState } from '~/store/store';
11 import { push } from "react-router-redux";
12 import { Typography } from "@material-ui/core";
13 import { Route, matchPath } from "react-router";
14 import { RootStore } from '~/store/store';
15 import { activateSidePanelTreeItem } from '~/store/side-panel-tree/side-panel-tree-actions';
16 import { setSidePanelBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
17 import { DispatchProp, connect } from 'react-redux';
18 import { MenuItem } from "@material-ui/core";
19 import { propertiesActions } from '~/store/properties/properties-actions';
20 import { Location } from 'history';
21
22 const categoryName = "Plugin Example";
23 export const routePath = "/examplePlugin";
24 const propertyKey = "Example_menu_item_pressed_count";
25
26 interface ExampleProps {
27     pressedCount: number;
28 }
29
30 const exampleMapStateToProps = (state: RootState) => ({ pressedCount: state.properties[propertyKey] || 0 });
31
32 const incrementPressedCount = (dispatch: Dispatch, pressedCount: number) => {
33     dispatch(propertiesActions.SET_PROPERTY({ key: propertyKey, value: pressedCount + 1 }));
34 };
35
36 const ExampleMenuComponent = connect(exampleMapStateToProps)(
37     ({ pressedCount, dispatch }: ExampleProps & DispatchProp<any>) =>
38         <MenuItem onClick={() => incrementPressedCount(dispatch, pressedCount)}>Example menu item</MenuItem >
39 );
40
41 const ExamplePluginMainPanel = connect(exampleMapStateToProps)(
42     ({ pressedCount }: ExampleProps) =>
43         <Typography>
44             This is a example main panel plugin.  The example menu item has been pressed {pressedCount} times.
45         </Typography>);
46
47 export const register = (pluginConfig: PluginConfig) => {
48
49     pluginConfig.centerPanelList.push((elms) => {
50         elms.push(<Route path={routePath} component={ExamplePluginMainPanel} />);
51         return elms;
52     });
53
54     pluginConfig.accountMenuList.push((elms) => {
55         elms.push(<ExampleMenuComponent />);
56         return elms;
57     });
58
59     pluginConfig.newButtonMenuList.push((elms) => {
60         elms.push(<ExampleMenuComponent />);
61         return elms;
62     });
63
64     pluginConfig.navigateToHandlers.push((dispatch: Dispatch, getState: () => RootState, uuid: string) => {
65         if (uuid === categoryName) {
66             dispatch(push(routePath));
67             return true;
68         }
69         return false;
70     });
71
72     pluginConfig.sidePanelCategories.push((cats: string[]): string[] => { cats.push(categoryName); return cats; });
73
74     pluginConfig.locationChangeHandlers.push((store: RootStore, pathname: string): boolean => {
75         if (matchPath(pathname, { path: routePath, exact: true })) {
76             store.dispatch(activateSidePanelTreeItem(categoryName));
77             store.dispatch<any>(setSidePanelBreadcrumbs(categoryName));
78             return true;
79         }
80         return false;
81     });
82
83     pluginConfig.enableNewButtonMatchers.push((location: Location) => (!!matchPath(location.pathname, { path: routePath, exact: true })));
84 };