Add wrapper support for getActiveNotifications

Summary: Add support for getActive notifications in Sandy wrapped legacy plugins

Reviewed By: passy

Differential Revision: D29234071

fbshipit-source-id: e05861e27426a592756645441c934b2c76154d44
This commit is contained in:
Michel Weststrate
2021-06-21 08:35:52 -07:00
committed by Facebook GitHub Bot
parent 6fb28df855
commit 00e2c803ef

View File

@@ -24,16 +24,15 @@ import {
FlipperPlugin, FlipperPlugin,
Props as PluginProps, Props as PluginProps,
} from '../plugin'; } from '../plugin';
import {useDispatch, useStore} from './useStore'; import {useStore} from './useStore';
import {setStaticView, StaticView} from '../reducers/connections'; import {setStaticView, StaticView} from '../reducers/connections';
import {getStore} from '../store';
import {setActiveNotifications} from '../reducers/notifications';
export type SandyPluginModule = ConstructorParameters< export type SandyPluginModule = ConstructorParameters<
typeof _SandyPluginDefinition typeof _SandyPluginDefinition
>[1]; >[1];
// Wrapped features
// getActiveNotifications
export function createSandyPluginWrapper<S, A extends BaseAction, P>( export function createSandyPluginWrapper<S, A extends BaseAction, P>(
Plugin: typeof FlipperPlugin | typeof FlipperDevicePlugin, Plugin: typeof FlipperPlugin | typeof FlipperDevicePlugin,
): SandyPluginModule { ): SandyPluginModule {
@@ -44,12 +43,13 @@ export function createSandyPluginWrapper<S, A extends BaseAction, P>(
} in legacy mode. Please visit https://fbflipper.com/docs/extending/sandy-migration to learn how to migrate this plugin to the new Sandy architecture`, } in legacy mode. Please visit https://fbflipper.com/docs/extending/sandy-migration to learn how to migrate this plugin to the new Sandy architecture`,
); );
function plugin(client: PluginClient | DevicePluginClient) { function legacyPluginWrapper(client: PluginClient | DevicePluginClient) {
const store = getStore();
const appClient = isDevicePlugin const appClient = isDevicePlugin
? undefined ? undefined
: (client as PluginClient<any, any>); : (client as PluginClient<any, any>);
const instanceRef = React.createRef<FlipperPlugin<S, A, P> | null>();
const instanceRef = React.createRef<FlipperPlugin<S, A, P> | null>();
const persistedState = createState<P>(Plugin.defaultPersistedState); const persistedState = createState<P>(Plugin.defaultPersistedState);
const deeplink = createState<unknown>(); const deeplink = createState<unknown>();
@@ -135,6 +135,27 @@ export function createSandyPluginWrapper<S, A extends BaseAction, P>(
); );
} }
if (Plugin.getActiveNotifications && !isDevicePlugin) {
const unsub = persistedState.subscribe((state) => {
try {
const notifications = Plugin.getActiveNotifications!(state);
store.dispatch(
setActiveNotifications({
notifications,
client: appClient!.appId,
pluginId: Plugin.id,
}),
);
} catch (e) {
console.error(
'Failed to compute notifications for plugin ' + Plugin.id,
e,
);
}
});
client.onDestroy(unsub);
}
return { return {
instanceRef, instanceRef,
device: client.device.realDevice, device: client.device.realDevice,
@@ -153,15 +174,18 @@ export function createSandyPluginWrapper<S, A extends BaseAction, P>(
get isArchived() { get isArchived() {
return client.device.isArchived; return client.device.isArchived;
}, },
setStaticView(payload: StaticView) {
store.dispatch(setStaticView(payload));
},
}; };
} }
function Component() { function Component() {
const instance = usePlugin(plugin); const instance = usePlugin(legacyPluginWrapper);
const logger = useLogger(); const logger = useLogger();
const persistedState = useValue(instance.persistedState); const persistedState = useValue(instance.persistedState);
const deepLinkPayload = useValue(instance.deeplink); const deepLinkPayload = useValue(instance.deeplink);
const dispatch = useDispatch(); const settingsState = useStore((state) => state.settingsState);
const target = isDevicePlugin const target = isDevicePlugin
? instance.device ? instance.device
@@ -173,29 +197,28 @@ export function createSandyPluginWrapper<S, A extends BaseAction, P>(
throw new Error('Illegal state: missing target'); throw new Error('Illegal state: missing target');
} }
const settingsState = useStore((state) => state.settingsState); useEffect(
function triggerInitAndTeardown() {
useEffect(function triggerInitAndTeardown() {
const ref = instance.instanceRef.current!; const ref = instance.instanceRef.current!;
ref._init(); ref._init();
return () => { return () => {
ref._teardown(); ref._teardown();
}; };
}, []); },
[instance.instanceRef],
);
const props: PluginProps<P> = { const props: PluginProps<P> = {
logger, logger,
persistedState, persistedState,
target,
deepLinkPayload, deepLinkPayload,
settingsState, settingsState,
target,
setPersistedState: instance.setPersistedState, setPersistedState: instance.setPersistedState,
selectPlugin: instance.selectPlugin, selectPlugin: instance.selectPlugin,
isArchivedDevice: instance.isArchived, isArchivedDevice: instance.isArchived,
selectedApp: instance.appName, selectedApp: instance.appName,
setStaticView(payload: StaticView) { setStaticView: instance.setStaticView,
dispatch(setStaticView(payload));
},
// @ts-ignore ref is not on Props // @ts-ignore ref is not on Props
ref: instance.instanceRef, ref: instance.instanceRef,
}; };
@@ -204,9 +227,9 @@ export function createSandyPluginWrapper<S, A extends BaseAction, P>(
} }
return isDevicePlugin return isDevicePlugin
? {devicePlugin: plugin, Component} ? {devicePlugin: legacyPluginWrapper, Component}
: { : {
plugin, plugin: legacyPluginWrapper,
Component, Component,
}; };
} }