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 React from 'react';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {setPluginState} from './reducers/pluginStates.js'; import {setPluginState} from './reducers/pluginStates.js';
import {selectPlugin} from './reducers/connections';
import {devicePlugins, clientPlugins} from './plugins/index.js'; import {devicePlugins, clientPlugins} from './plugins/index.js';
import NotificationsHub from './NotificationsHub'; import NotificationsHub from './NotificationsHub';
import {activateMenuItems} from './MenuBar.js'; import {activateMenuItems} from './MenuBar.js';
@@ -53,6 +54,11 @@ type Props = {
state: Object, state: Object,
}) => void, }) => void,
deepLinkPayload: ?string, deepLinkPayload: ?string,
selectPlugin: (payload: {|
selectedPlugin: ?string,
selectedApp?: ?string,
deepLinkPayload: ?string,
|}) => mixed,
}; };
type State = { type State = {
@@ -144,6 +150,19 @@ class PluginContainer extends Component<Props, State> {
setPersistedState: state => setPluginState({pluginKey, state}), setPersistedState: state => setPluginState({pluginKey, state}),
target, target,
deepLinkPayload: this.props.deepLinkPayload, 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, ref: this.refChanged,
}; };
@@ -185,5 +204,6 @@ export default connect(
}), }),
{ {
setPluginState, setPluginState,
selectPlugin,
}, },
)(PluginContainer); )(PluginContainer);

View File

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

View File

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