17426: Fix stuck on loading screen.
[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 import { handleFirstTimeLoad } from '~/store/workbench/workbench-actions';
22
23 const categoryName = "Plugin Example";
24 export const routePath = "/examplePlugin";
25 const propertyKey = "Example_menu_item_pressed_count";
26
27 interface ExampleProps {
28     pressedCount: number;
29 }
30
31 const exampleMapStateToProps = (state: RootState) => ({ pressedCount: state.properties[propertyKey] || 0 });
32
33 const incrementPressedCount = (dispatch: Dispatch, pressedCount: number) => {
34     dispatch(propertiesActions.SET_PROPERTY({ key: propertyKey, value: pressedCount + 1 }));
35 };
36
37 const ExampleMenuComponent = connect(exampleMapStateToProps)(
38     ({ pressedCount, dispatch }: ExampleProps & DispatchProp<any>) =>
39         <MenuItem onClick={() => incrementPressedCount(dispatch, pressedCount)}>Example menu item</MenuItem >
40 );
41
42 const ExamplePluginMainPanel = connect(exampleMapStateToProps)(
43     ({ pressedCount }: ExampleProps) =>
44         <Typography>
45             This is a example main panel plugin.  The example menu item has been pressed {pressedCount} times.
46         </Typography>);
47
48 export const register = (pluginConfig: PluginConfig) => {
49
50     pluginConfig.centerPanelList.push((elms) => {
51         elms.push(<Route path={routePath} component={ExamplePluginMainPanel} />);
52         return elms;
53     });
54
55     pluginConfig.accountMenuList.push((elms) => {
56         elms.push(<ExampleMenuComponent />);
57         return elms;
58     });
59
60     pluginConfig.newButtonMenuList.push((elms) => {
61         elms.push(<ExampleMenuComponent />);
62         return elms;
63     });
64
65     pluginConfig.navigateToHandlers.push((dispatch: Dispatch, getState: () => RootState, uuid: string) => {
66         if (uuid === categoryName) {
67             dispatch(push(routePath));
68             return true;
69         }
70         return false;
71     });
72
73     pluginConfig.sidePanelCategories.push((cats: string[]): string[] => { cats.push(categoryName); return cats; });
74
75     pluginConfig.locationChangeHandlers.push((store: RootStore, pathname: string): boolean => {
76         if (matchPath(pathname, { path: routePath, exact: true })) {
77             store.dispatch(handleFirstTimeLoad(
78                 (dispatch: Dispatch) => {
79                     dispatch<any>(activateSidePanelTreeItem(categoryName));
80                     dispatch<any>(setSidePanelBreadcrumbs(categoryName));
81                 }));
82             return true;
83         }
84         return false;
85     });
86
87     pluginConfig.enableNewButtonMatchers.push((location: Location) => (!!matchPath(location.pathname, { path: routePath, exact: true })));
88 };