From 00e2c803ef40b3b51f25ad45108579e55fe291c8 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Mon, 21 Jun 2021 08:35:52 -0700 Subject: [PATCH] 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 --- .../src/utils/createSandyPluginWrapper.tsx | 69 ++++++++++++------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/desktop/app/src/utils/createSandyPluginWrapper.tsx b/desktop/app/src/utils/createSandyPluginWrapper.tsx index e9e4d16b0..4f2737d1b 100644 --- a/desktop/app/src/utils/createSandyPluginWrapper.tsx +++ b/desktop/app/src/utils/createSandyPluginWrapper.tsx @@ -24,16 +24,15 @@ import { FlipperPlugin, Props as PluginProps, } from '../plugin'; -import {useDispatch, useStore} from './useStore'; +import {useStore} from './useStore'; import {setStaticView, StaticView} from '../reducers/connections'; +import {getStore} from '../store'; +import {setActiveNotifications} from '../reducers/notifications'; export type SandyPluginModule = ConstructorParameters< typeof _SandyPluginDefinition >[1]; -// Wrapped features -// getActiveNotifications - export function createSandyPluginWrapper( Plugin: typeof FlipperPlugin | typeof FlipperDevicePlugin, ): SandyPluginModule { @@ -44,12 +43,13 @@ export function createSandyPluginWrapper( } 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 ? undefined : (client as PluginClient); - const instanceRef = React.createRef | null>(); + const instanceRef = React.createRef | null>(); const persistedState = createState

(Plugin.defaultPersistedState); const deeplink = createState(); @@ -135,6 +135,27 @@ export function createSandyPluginWrapper( ); } + 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 { instanceRef, device: client.device.realDevice, @@ -153,15 +174,18 @@ export function createSandyPluginWrapper( get isArchived() { return client.device.isArchived; }, + setStaticView(payload: StaticView) { + store.dispatch(setStaticView(payload)); + }, }; } function Component() { - const instance = usePlugin(plugin); + const instance = usePlugin(legacyPluginWrapper); const logger = useLogger(); const persistedState = useValue(instance.persistedState); const deepLinkPayload = useValue(instance.deeplink); - const dispatch = useDispatch(); + const settingsState = useStore((state) => state.settingsState); const target = isDevicePlugin ? instance.device @@ -173,29 +197,28 @@ export function createSandyPluginWrapper( throw new Error('Illegal state: missing target'); } - const settingsState = useStore((state) => state.settingsState); - - useEffect(function triggerInitAndTeardown() { - const ref = instance.instanceRef.current!; - ref._init(); - return () => { - ref._teardown(); - }; - }, []); + useEffect( + function triggerInitAndTeardown() { + const ref = instance.instanceRef.current!; + ref._init(); + return () => { + ref._teardown(); + }; + }, + [instance.instanceRef], + ); const props: PluginProps

= { logger, persistedState, - target, deepLinkPayload, settingsState, + target, setPersistedState: instance.setPersistedState, selectPlugin: instance.selectPlugin, isArchivedDevice: instance.isArchived, selectedApp: instance.appName, - setStaticView(payload: StaticView) { - dispatch(setStaticView(payload)); - }, + setStaticView: instance.setStaticView, // @ts-ignore ref is not on Props ref: instance.instanceRef, }; @@ -204,9 +227,9 @@ export function createSandyPluginWrapper( } return isDevicePlugin - ? {devicePlugin: plugin, Component} + ? {devicePlugin: legacyPluginWrapper, Component} : { - plugin, + plugin: legacyPluginWrapper, Component, }; }