/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ import React, {ChangeEvent} from 'react'; import {Button, Input, Typography, Spin, message} from 'antd'; import { PluginClient, createState, usePlugin, useValue, Layout, } from 'flipper-plugin'; export type Sandbox = { name: string; value: string; }; type ClientMethods = { getSandbox(): Promise; setSandbox(sandbox: {sandbox: string}): Promise; }; type SetSandboxResult = {result: boolean}; export function plugin(client: PluginClient<{}, ClientMethods>) { const sandboxes = createState>([]); const customSandbox = createState(''); const isLoadingSandboxes = createState(false); client.onConnect(() => { isLoadingSandboxes.set(true); client .send('getSandbox', undefined) .then((results: Array) => { sandboxes.set(results); isLoadingSandboxes.set(false); }) .catch((e) => { console.error('[sandbox] getSandbox call failed:', e); isLoadingSandboxes.set(false); displayError(e); }); }); const onSendSandboxEnvironment = (sandbox: string) => { client .send('setSandbox', { sandbox, }) .then((result: SetSandboxResult) => { if (result.result) displaySuccess('Update to ' + sandbox + ' successful'); else displayError('Update to ' + sandbox + ' failed'); }) .catch((e) => { console.error('[sandbox] setSandbox call failed:', e); displayError(e); }); }; const onChangeSandbox = (e: ChangeEvent) => { customSandbox.set(e.target.value); }; const displaySuccess = (title: string) => { message.success(title); }; const displayError = (title: string) => { message.error(title); }; return { client, onChangeSandbox, onSendSandboxEnvironment, customSandbox, sandboxes, isLoadingSandboxes, }; } export function Component() { const instance = usePlugin(plugin); const customSandbox = useValue(instance.customSandbox); const sandboxes = useValue(instance.sandboxes); const isLoadingSandboxes = useValue(instance.isLoadingSandboxes); return ( Select the environment: {sandboxes.map((sandbox) => ( ))} Provide custom Sandbox URL { if (event.key === 'Enter') { instance.onSendSandboxEnvironment(customSandbox); } }} /> ); }