PluginContainer.tsx

Reviewed By: passy

Differential Revision: D17258340

fbshipit-source-id: 26ce421345f6152efe7da07daf649395e299af14
This commit is contained in:
John Knox
2019-09-09 06:49:30 -07:00
committed by Facebook Github Bot
parent 96d9c9638c
commit 6e8b31e2ed
2 changed files with 21 additions and 25 deletions

View File

@@ -48,24 +48,21 @@ type OwnProps = {
type StateFromProps = { type StateFromProps = {
pluginState: Object; pluginState: Object;
activePlugin: typeof FlipperPlugin | typeof FlipperDevicePlugin; activePlugin: typeof FlipperPlugin | typeof FlipperDevicePlugin | null;
target: Client | BaseDevice | null; target: Client | BaseDevice | null;
pluginKey: string | null | undefined; pluginKey: string | null;
deepLinkPayload: string | null | undefined; deepLinkPayload: string | null;
selectedApp: string | null | undefined; selectedApp: string | null;
isArchivedDevice: boolean; isArchivedDevice: boolean;
}; };
type DispatchFromProps = { type DispatchFromProps = {
selectPlugin: (payload: { selectPlugin: (payload: {
selectedPlugin: string | null | undefined; selectedPlugin: string | null;
selectedApp?: string | null | undefined; selectedApp?: string | null;
deepLinkPayload: string | null | undefined; deepLinkPayload: string | null;
}) => any; }) => any;
setPluginState: (payload: { setPluginState: (payload: {pluginKey: string; state: Object}) => void;
pluginKey: string;
state: Partial<Object>;
}) => void;
}; };
type Props = StateFromProps & DispatchFromProps & OwnProps; type Props = StateFromProps & DispatchFromProps & OwnProps;
@@ -136,13 +133,10 @@ class PluginContainer extends PureComponent<Props> {
...pluginState, ...pluginState,
} }
: pluginState, : pluginState,
setPersistedState: state => setPluginState({pluginKey, state}), setPersistedState: _state => setPluginState({pluginKey, state: Object}),
target, target,
deepLinkPayload: this.props.deepLinkPayload, deepLinkPayload: this.props.deepLinkPayload,
selectPlugin: ( selectPlugin: (pluginID: string, deepLinkPayload: string | null) => {
pluginID: string,
deepLinkPayload: string | null | undefined,
) => {
const {target} = this.props; const {target} = this.props;
// check if plugin will be available // check if plugin will be available
if ( if (
@@ -199,14 +193,15 @@ export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>(
if (selectedPlugin === NotificationsHub.id) { if (selectedPlugin === NotificationsHub.id) {
activePlugin = NotificationsHub; activePlugin = NotificationsHub;
} else if (selectedPlugin) { } else if (selectedPlugin) {
activePlugin = devicePlugins.get(selectedPlugin); activePlugin = devicePlugins.get(selectedPlugin) || null;
} }
target = selectedDevice; target = selectedDevice;
if (activePlugin) { if (selectedDevice && activePlugin) {
pluginKey = getPluginKey(selectedDevice.serial, activePlugin.id); pluginKey = getPluginKey(selectedDevice.serial, activePlugin.id);
} else { } else {
target = clients.find((client: Client) => client.id === selectedApp); target =
activePlugin = clientPlugins.get(selectedPlugin); clients.find((client: Client) => client.id === selectedApp) || null;
activePlugin = clientPlugins.get(selectedPlugin) || null;
if (activePlugin && target) { if (activePlugin && target) {
pluginKey = getPluginKey(target.id, activePlugin.id); pluginKey = getPluginKey(target.id, activePlugin.id);
} }
@@ -216,15 +211,16 @@ export default connect<StateFromProps, DispatchFromProps, OwnProps, Store>(
? false ? false
: selectedDevice instanceof ArchivedDevice; : selectedDevice instanceof ArchivedDevice;
return { const s: StateFromProps = {
pluginState: pluginStates[pluginKey], pluginState: pluginStates[pluginKey as string],
activePlugin, activePlugin: activePlugin,
target, target,
deepLinkPayload, deepLinkPayload,
pluginKey, pluginKey,
isArchivedDevice, isArchivedDevice,
selectedApp, selectedApp,
}; };
return s;
}, },
{ {
setPluginState, setPluginState,

View File

@@ -46,14 +46,14 @@ export default function reducer(
return {...state}; return {...state};
} else if (action.type === 'CLEAR_PLUGIN_STATE') { } else if (action.type === 'CLEAR_PLUGIN_STATE') {
const {payload} = action; const {payload} = action;
return Object.keys(state).reduce((newState, pluginKey) => { return Object.keys(state).reduce((newState: State, pluginKey) => {
// Only add the pluginState, if its from a plugin other than the one that // Only add the pluginState, if its from a plugin other than the one that
// was removed. pluginKeys are in the form of ${clientID}#${pluginID}. // was removed. pluginKeys are in the form of ${clientID}#${pluginID}.
const clientId = pluginKey.slice(0, pluginKey.lastIndexOf('#')); const clientId = pluginKey.slice(0, pluginKey.lastIndexOf('#'));
const pluginId = pluginKey.split('#').pop(); const pluginId = pluginKey.split('#').pop();
if ( if (
clientId !== payload.clientId || clientId !== payload.clientId ||
payload.devicePlugins.has(pluginId) (pluginId && payload.devicePlugins.has(pluginId))
) { ) {
newState[pluginKey] = state[pluginKey]; newState[pluginKey] = state[pluginKey];
} }