1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
23 const categoryName = "Plugin Example";
24 export const routePath = "/examplePlugin";
25 const propertyKey = "Example_menu_item_pressed_count";
27 interface ExampleProps {
31 const exampleMapStateToProps = (state: RootState) => ({ pressedCount: state.properties[propertyKey] || 0 });
33 const incrementPressedCount = (dispatch: Dispatch, pressedCount: number) => {
34 dispatch(propertiesActions.SET_PROPERTY({ key: propertyKey, value: pressedCount + 1 }));
37 const ExampleMenuComponent = connect(exampleMapStateToProps)(
38 ({ pressedCount, dispatch }: ExampleProps & DispatchProp<any>) =>
39 <MenuItem onClick={() => incrementPressedCount(dispatch, pressedCount)}>Example menu item</MenuItem >
42 const ExamplePluginMainPanel = connect(exampleMapStateToProps)(
43 ({ pressedCount }: ExampleProps) =>
45 This is a example main panel plugin. The example menu item has been pressed {pressedCount} times.
48 export const register = (pluginConfig: PluginConfig) => {
50 pluginConfig.centerPanelList.push((elms) => {
51 elms.push(<Route path={routePath} component={ExamplePluginMainPanel} />);
55 pluginConfig.accountMenuList.push((elms) => {
56 elms.push(<ExampleMenuComponent />);
60 pluginConfig.newButtonMenuList.push((elms) => {
61 elms.push(<ExampleMenuComponent />);
65 pluginConfig.navigateToHandlers.push((dispatch: Dispatch, getState: () => RootState, uuid: string) => {
66 if (uuid === categoryName) {
67 dispatch(push(routePath));
73 pluginConfig.sidePanelCategories.push((cats: string[]): string[] => { cats.push(categoryName); return cats; });
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));
87 pluginConfig.enableNewButtonMatchers.push((location: Location) => (!!matchPath(location.pathname, { path: routePath, exact: true })));