From 9d485b1d5dad165c0b254d0672bad24a30767a62 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Tue, 8 Jun 2021 09:51:54 -0700 Subject: [PATCH] Convert plugin to Sandy Reviewed By: passy Differential Revision: D28964502 fbshipit-source-id: 76322dc7aec09497f6ba7e4dfb1af8f0318837ca --- desktop/plugins/public/example/index.tsx | 169 ++++++++++---------- desktop/plugins/public/example/package.json | 3 +- 2 files changed, 88 insertions(+), 84 deletions(-) diff --git a/desktop/plugins/public/example/index.tsx b/desktop/plugins/public/example/index.tsx index 96e8481bc..7349a5e1a 100644 --- a/desktop/plugins/public/example/index.tsx +++ b/desktop/plugins/public/example/index.tsx @@ -7,112 +7,115 @@ * @format */ -import {Button, Input, FlipperPlugin, FlexColumn, styled, Text} from 'flipper'; +import { + createState, + Layout, + PluginClient, + usePlugin, + useValue, +} from 'flipper-plugin'; +import {Button, Input, message, Typography} from 'antd'; import React from 'react'; +const {Text} = Typography; + type DisplayMessageResponse = { greeting: string; }; -type Message = { - id: number; - msg: string | null | undefined; -}; - -type State = { - prompt: string; - message: string; -}; - -type PersistedState = { - currentNotificationIds: Array; - receivedMessage: string | null; -}; - -const Container = styled(FlexColumn)({ - alignItems: 'center', - justifyContent: 'space-around', - padding: 20, -}); - -export default class Example extends FlipperPlugin { - static defaultPersistedState = { - currentNotificationIds: [], - receivedMessage: null, +/** + * Events that can be received FROM the client application + */ +type Events = { + triggerNotification: { + id: number; }; - - state = { - prompt: 'Type a message below to see it displayed on the mobile app', - message: '', + displayMessage: { + msg: string; }; +}; + +/** + * Methods that can be invoked ON the client application + */ +type Methods = { + displayMessage(payload: {message: string}): Promise; +}; + +export function plugin(client: PluginClient) { + const receivedMessage = createState(''); + const prompt = createState( + 'Type a message below to see it displayed on the mobile app', + ); + const nextMessage = createState(''); /* - * Reducer to process incoming "send" messages from the mobile counterpart. + * Process incoming messages */ - static persistedStateReducer( - persistedState: PersistedState, - method: string, - payload: Message, - ) { - if (method === 'triggerNotification') { - return Object.assign({}, persistedState, { - currentNotificationIds: persistedState.currentNotificationIds.concat([ - payload.id, - ]), - }); - } - if (method === 'displayMessage') { - return Object.assign({}, persistedState, { - receivedMessage: payload.msg, - }); - } - return persistedState; - } - - /* - * Callback to provide the currently active notifications. - */ - static getActiveNotifications(persistedState: PersistedState) { - return persistedState.currentNotificationIds.map((x: number) => { - return { - id: 'test-notification:' + x, - message: 'Example Notification', - severity: 'warning' as 'warning', - title: 'Notification: ' + x, - }; + client.onMessage('triggerNotification', ({id}) => { + client.showNotification({ + id: 'test-notification:' + id, + message: 'Example Notification', + severity: 'warning' as 'warning', + title: 'Notification: ' + id, }); - } + }); + + client.onMessage('displayMessage', ({msg}) => { + receivedMessage.set(msg); + }); /* * Call a method of the mobile counterpart, to display a message. */ - sendMessage() { - if (this.client.isConnected) { - this.client - .call('displayMessage', {message: this.state.message || 'Weeeee!'}) - .then((_params: DisplayMessageResponse) => { - this.setState({ - prompt: 'Nice', - }); + async function sendMessage() { + if (client.isConnected) { + try { + const response = await client.send('displayMessage', { + message: nextMessage.get() || 'Weeeee!', }); + prompt.set(response.greeting || 'Nice'); + nextMessage.set(''); + } catch (e) { + console.warn('Error returned from client', e); + message.error('Failed to get response from client ' + e); + } } } - render() { - return ( - - {this.state.prompt} + return { + sendMessage, + prompt, + nextMessage, + receivedMessage, + }; +} + +export function Component() { + const instance = usePlugin(plugin); + const prompt = useValue(instance.prompt); + const nextMessage = useValue(instance.nextMessage); + const receivedMessage = useValue(instance.receivedMessage); + + return ( + + + {prompt} { - this.setState({message: event.target.value}); + instance.nextMessage.set(event.target.value); }} /> - - {this.props.persistedState.receivedMessage && ( - {this.props.persistedState.receivedMessage} - )} - - ); - } + + {receivedMessage && {receivedMessage} } + + + ); } diff --git a/desktop/plugins/public/example/package.json b/desktop/plugins/public/example/package.json index 10d5853bf..7da69ca76 100644 --- a/desktop/plugins/public/example/package.json +++ b/desktop/plugins/public/example/package.json @@ -22,7 +22,8 @@ "prepack": "flipper-pkg lint && flipper-pkg bundle --production" }, "peerDependencies": { - "flipper": "*", + "antd": "*", + "flipper-plugin": "*", "flipper-pkg": "*" } }