Convert Flipper plugin "Example" to TypeScript
Summary: _typescript_ Reviewed By: passy Differential Revision: D17047622 fbshipit-source-id: ff25bf4355bb42a0754b2b3304a6b5bdc3806909
This commit is contained in:
committed by
Facebook Github Bot
parent
922019a8d0
commit
e3285c4f15
@@ -3,23 +3,28 @@
|
|||||||
* This source code is licensed under the MIT license found in the
|
* This source code is licensed under the MIT license found in the
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
* @format
|
* @format
|
||||||
* @flow
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Button, Input, FlipperPlugin, FlexColumn, styled, Text} from 'flipper';
|
import {Button, Input, FlipperPlugin, FlexColumn, styled, Text} from 'flipper';
|
||||||
import type {Notification} from '../../plugin.tsx';
|
import React from 'react';
|
||||||
|
|
||||||
type DisplayMessageResponse = {
|
type DisplayMessageResponse = {
|
||||||
greeting: string,
|
greeting: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Message = {
|
||||||
|
id: number;
|
||||||
|
msg: string | null | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
prompt: string,
|
prompt: string;
|
||||||
message: string,
|
message: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type PersistedState = {
|
type PersistedState = {
|
||||||
currentNotificationIds: Array<number>,
|
currentNotificationIds: Array<number>;
|
||||||
receivedMessage: ?string,
|
receivedMessage: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Container = styled(FlexColumn)({
|
const Container = styled(FlexColumn)({
|
||||||
@@ -28,7 +33,7 @@ const Container = styled(FlexColumn)({
|
|||||||
padding: 20,
|
padding: 20,
|
||||||
});
|
});
|
||||||
|
|
||||||
export default class extends FlipperPlugin<*, State, PersistedState> {
|
export default class Example extends FlipperPlugin<State, any, PersistedState> {
|
||||||
static defaultPersistedState = {
|
static defaultPersistedState = {
|
||||||
currentNotificationIds: [],
|
currentNotificationIds: [],
|
||||||
receivedMessage: null,
|
receivedMessage: null,
|
||||||
@@ -42,43 +47,39 @@ export default class extends FlipperPlugin<*, State, PersistedState> {
|
|||||||
/*
|
/*
|
||||||
* Reducer to process incoming "send" messages from the mobile counterpart.
|
* Reducer to process incoming "send" messages from the mobile counterpart.
|
||||||
*/
|
*/
|
||||||
static persistedStateReducer = (
|
static persistedStateReducer(
|
||||||
persistedState: PersistedState,
|
persistedState: PersistedState,
|
||||||
method: string,
|
method: string,
|
||||||
payload: Object,
|
payload: Message,
|
||||||
): PersistedState => {
|
) {
|
||||||
if (method === 'triggerNotification') {
|
if (method === 'triggerNotification') {
|
||||||
return {
|
return Object.assign({}, persistedState, {
|
||||||
...persistedState,
|
|
||||||
currentNotificationIds: persistedState.currentNotificationIds.concat([
|
currentNotificationIds: persistedState.currentNotificationIds.concat([
|
||||||
payload.id,
|
payload.id,
|
||||||
]),
|
]),
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
if (method === 'displayMessage') {
|
if (method === 'displayMessage') {
|
||||||
return {
|
return Object.assign({}, persistedState, {
|
||||||
...persistedState,
|
|
||||||
receivedMessage: payload.msg,
|
receivedMessage: payload.msg,
|
||||||
};
|
});
|
||||||
}
|
}
|
||||||
return persistedState || {};
|
return persistedState;
|
||||||
};
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Callback to provide the currently active notifications.
|
* Callback to provide the currently active notifications.
|
||||||
*/
|
*/
|
||||||
static getActiveNotifications = (
|
static getActiveNotifications(persistedState: PersistedState) {
|
||||||
persistedState: PersistedState,
|
|
||||||
): Array<Notification> => {
|
|
||||||
return persistedState.currentNotificationIds.map((x: number) => {
|
return persistedState.currentNotificationIds.map((x: number) => {
|
||||||
return {
|
return {
|
||||||
id: 'test-notification:' + x,
|
id: 'test-notification:' + x,
|
||||||
message: 'Example Notification',
|
message: 'Example Notification',
|
||||||
severity: 'warning',
|
severity: 'warning' as 'warning',
|
||||||
title: 'Notification: ' + x,
|
title: 'Notification: ' + x,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Call a method of the mobile counterpart, to display a message.
|
* Call a method of the mobile counterpart, to display a message.
|
||||||
@@ -86,7 +87,7 @@ export default class extends FlipperPlugin<*, State, PersistedState> {
|
|||||||
sendMessage() {
|
sendMessage() {
|
||||||
this.client
|
this.client
|
||||||
.call('displayMessage', {message: this.state.message || 'Weeeee!'})
|
.call('displayMessage', {message: this.state.message || 'Weeeee!'})
|
||||||
.then((params: DisplayMessageResponse) => {
|
.then((_params: DisplayMessageResponse) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
prompt: 'Nice',
|
prompt: 'Nice',
|
||||||
});
|
});
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "Example",
|
"name": "Example",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "index.js",
|
"main": "index.tsx",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"title": "Example Plugin",
|
"title": "Example Plugin",
|
||||||
"icon": "apps",
|
"icon": "apps",
|
||||||
|
|||||||
Reference in New Issue
Block a user