adding selectPlugin API to plugins

Summary:
Allow linking from one plugin to another. Adds a prop `selectPlugin` to the plugin that can be passed a pluginID and an optional `deepLinkPayload`.

The return value tells you if switching the plugin was successful.

```
selectPlugin: (pluginID: string, deepLinkPayload: ?string) => boolean,
```

Reviewed By: passy

Differential Revision: D10483925

fbshipit-source-id: 6f821277150b2db185b7d545c310214a11432eac
This commit is contained in:
Daniel Büchele
2018-10-22 06:58:02 -07:00
committed by Facebook Github Bot
parent b1c735c40d
commit 86c796a706
3 changed files with 34 additions and 9 deletions

View File

@@ -22,6 +22,7 @@ import {
import React from 'react';
import {connect} from 'react-redux';
import {setPluginState} from './reducers/pluginStates.js';
import {selectPlugin} from './reducers/connections';
import {devicePlugins, clientPlugins} from './plugins/index.js';
import NotificationsHub from './NotificationsHub';
import {activateMenuItems} from './MenuBar.js';
@@ -53,6 +54,11 @@ type Props = {
state: Object,
}) => void,
deepLinkPayload: ?string,
selectPlugin: (payload: {|
selectedPlugin: ?string,
selectedApp?: ?string,
deepLinkPayload: ?string,
|}) => mixed,
};
type State = {
@@ -144,6 +150,19 @@ class PluginContainer extends Component<Props, State> {
setPersistedState: state => setPluginState({pluginKey, state}),
target,
deepLinkPayload: this.props.deepLinkPayload,
selectPlugin: (pluginID: string, deepLinkPayload: ?string) => {
const {target} = this.state;
// check if plugin will be available
if (
target instanceof Client &&
target.plugins.some(p => p === pluginID)
) {
this.props.selectPlugin({selectedPlugin: pluginID, deepLinkPayload});
return true;
} else {
return false;
}
},
ref: this.refChanged,
};
@@ -185,5 +204,6 @@ export default connect(
}),
{
setPluginState,
selectPlugin,
},
)(PluginContainer);

View File

@@ -40,6 +40,7 @@ export type Props<T> = {
setPersistedState: (state: $Shape<T>) => void,
target: PluginTarget,
deepLinkPayload: ?string,
selectPlugin: (pluginID: string, deepLinkPayload: ?string) => boolean,
};
export class FlipperBasePlugin<

View File

@@ -8,7 +8,7 @@
import type BaseDevice from '../devices/BaseDevice';
import type Client from '../Client';
export type State = {
export type State = {|
devices: Array<BaseDevice>,
androidEmulators: Array<string>,
selectedDevice: ?BaseDevice,
@@ -19,7 +19,8 @@ export type State = {
userPreferredApp: ?string,
error: ?string,
clients: Array<Client>,
};
deepLinkPayload: ?string,
|};
export type Action =
| {
@@ -40,10 +41,11 @@ export type Action =
}
| {
type: 'SELECT_PLUGIN',
payload: {
payload: {|
selectedPlugin: ?string,
selectedApp: ?string,
},
deepLinkPayload: ?string,
|},
}
| {
type: 'SELECT_USER_PREFERRED_PLUGIN',
@@ -79,6 +81,7 @@ const INITAL_STATE: State = {
userPreferredApp: null,
error: null,
clients: [],
deepLinkPayload: null,
};
export default function reducer(
@@ -170,7 +173,7 @@ export default function reducer(
}
case 'SELECT_PLUGIN': {
const {payload} = action;
const {selectedPlugin} = payload;
const {selectedPlugin, selectedApp} = payload;
if (selectedPlugin) {
performance.mark(`activePlugin-${selectedPlugin}`);
}
@@ -178,7 +181,7 @@ export default function reducer(
return {
...state,
...payload,
userPreferredApp: payload.selectedApp,
userPreferredApp: selectedApp || state.userPreferredApp,
userPreferredPlugin: selectedPlugin,
};
}
@@ -249,10 +252,11 @@ export const preferDevice = (payload: string): Action => ({
payload,
});
export const selectPlugin = (payload: {
export const selectPlugin = (payload: {|
selectedPlugin: ?string,
selectedApp: ?string,
}): Action => ({
selectedApp?: ?string,
deepLinkPayload: ?string,
|}): Action => ({
type: 'SELECT_PLUGIN',
payload,
});