diff --git a/desktop/plugins/public/sandbox/index.tsx b/desktop/plugins/public/sandbox/index.tsx index 54a22b5c3..9bdb1ac4e 100644 --- a/desktop/plugins/public/sandbox/index.tsx +++ b/desktop/plugins/public/sandbox/index.tsx @@ -7,27 +7,22 @@ * @format */ -import { - FlipperPlugin, - FlexColumn, - ButtonGroup, - Button, - styled, - colors, -} from 'flipper'; +import {FlexColumn, ButtonGroup, Button, styled, colors} from 'flipper'; import React, {ChangeEvent} from 'react'; +import {PluginClient, createState, usePlugin, useValue} from 'flipper-plugin'; export type Sandbox = { name: string; value: string; }; -type SandboxState = { - sandboxes: Array; - customSandbox: string; - showFeedback: boolean; +type ClientMethods = { + getSandbox(): Promise; + setSandbox(sandbox: {sandbox: string}): Promise; }; +type SetSandboxResult = {result: boolean}; + const BigButton = styled(Button)({ flexGrow: 1, fontSize: 24, @@ -39,112 +34,114 @@ const ButtonContainer = styled(FlexColumn)({ padding: 20, }); -export default class SandboxView extends FlipperPlugin< - SandboxState, - any, - unknown -> { - state: SandboxState = { - sandboxes: [], - customSandbox: '', - showFeedback: false, - }; +const TextInput = styled.input({ + border: `1px solid ${colors.light10}`, + fontSize: '1em', + padding: '0 5px', + borderRight: 0, + borderTopLeftRadius: 4, + borderBottomLeftRadius: 4, + flexGrow: 1, +}); - static TextInput = styled.input({ - border: `1px solid ${colors.light10}`, - fontSize: '1em', - padding: '0 5px', - borderRight: 0, - borderTopLeftRadius: 4, - borderBottomLeftRadius: 4, - flexGrow: 1, - }); +const FeedbackMessage = styled.span({ + fontSize: '1.2em', + paddingTop: '10px', + color: 'green', +}); - static FeedbackMessage = styled.span({ - fontSize: '1.2em', - paddingTop: '10px', - color: 'green', - }); +const TextInputLayout = styled(FlexColumn)({ + float: 'left', + justifyContent: 'center', + flexGrow: 1, + borderRadius: 4, + marginRight: 15, + marginTop: 15, + marginLeft: 15, +}); - static TextInputLayout = styled(FlexColumn)({ - float: 'left', - justifyContent: 'center', - flexGrow: 1, - borderRadius: 4, - marginRight: 15, - marginTop: 15, - marginLeft: 15, - }); +export function plugin(client: PluginClient<{}, ClientMethods>) { + const sandboxes = createState>([]); + const customSandbox = createState(''); + const showFeedback = createState(false); - init() { - if (!this.client.isConnected) { - return; - } - this.client - .call('getSandbox', {}) + client.onConnect(() => { + client + .send('getSandbox', undefined) .then((results: Array) => { - this.setState({sandboxes: results}); + sandboxes.set(results); }) .catch((e) => console.error('[sandbox] getSandbox call failed:', e)); - } + }); - onSendSandboxEnvironment = (sandbox: string) => { - this.client - .call('setSandbox', { - sandbox: sandbox, + const onSendSandboxEnvironment = (sandbox: string) => { + client + .send('setSandbox', { + sandbox, }) - .then((result: {result: boolean}) => { + .then((result: SetSandboxResult) => { setTimeout(() => { - this.setState({showFeedback: false}); + showFeedback.set(false); }, 3000); - this.setState({showFeedback: result.result}); + showFeedback.set(result.result); }) .catch((e) => console.error('[sandbox] setSandbox call failed:', e)); }; - onChangeSandbox = (e: ChangeEvent) => { - this.setState({customSandbox: e.target.value}); + const onChangeSandbox = (e: ChangeEvent) => { + customSandbox.set(e.target.value); }; - render() { - return ( - - - - { - if (event.key === 'Enter') { - this.onSendSandboxEnvironment(this.state.customSandbox); - } - }} - /> -