/** * Copyright (c) Facebook, Inc. and its 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 { FlipperPlugin, FlipperDevicePlugin, Props as PluginProps, } from './plugin'; import {Logger} from './fb-interfaces/Logger'; import BaseDevice from './devices/BaseDevice'; import {pluginKey as getPluginKey} from './reducers/pluginStates'; import Client from './Client'; import { ErrorBoundary, FlexColumn, FlexRow, colors, styled, ArchivedDevice, Glyph, Label, VBox, View, } from 'flipper'; import {StaticView, setStaticView} from './reducers/connections'; import React, {PureComponent} from 'react'; import {connect, ReactReduxContext} from 'react-redux'; import {setPluginState} from './reducers/pluginStates'; import {selectPlugin} from './reducers/connections'; import {State as Store, MiddlewareAPI} from './reducers/index'; import {activateMenuItems} from './MenuBar'; import {Message} from './reducers/pluginMessageQueue'; import {Idler} from './utils/Idler'; import {processMessageQueue} from './utils/messageQueue'; const Container = styled(FlexColumn)({ width: 0, flexGrow: 1, flexShrink: 1, backgroundColor: colors.white, }); export const SidebarContainer = styled(FlexRow)({ backgroundColor: colors.light02, height: '100%', overflow: 'scroll', }); const Waiting = styled(FlexColumn)({ width: '100%', height: '100%', flexGrow: 1, background: colors.light02, alignItems: 'center', justifyContent: 'center', textAlign: 'center', }); function ProgressBar({progress}: {progress: number}) { return ( ); } const ProgressBarContainer = styled.div({ border: `1px solid ${colors.cyan}`, borderRadius: 4, width: 300, }); const ProgressBarBar = styled.div<{progress: number}>(({progress}) => ({ background: colors.cyan, width: `${Math.min(100, Math.round(progress * 100))}%`, height: 8, })); type OwnProps = { logger: Logger; }; type StateFromProps = { pluginState: Object; activePlugin: typeof FlipperPlugin | typeof FlipperDevicePlugin | null; target: Client | BaseDevice | null; pluginKey: string | null; deepLinkPayload: string | null; selectedApp: string | null; isArchivedDevice: boolean; pendingMessages: Message[] | undefined; }; type DispatchFromProps = { selectPlugin: (payload: { selectedPlugin: string | null; selectedApp?: string | null; deepLinkPayload: string | null; }) => any; setPluginState: (payload: {pluginKey: string; state: any}) => void; setStaticView: (payload: StaticView) => void; }; type Props = StateFromProps & DispatchFromProps & OwnProps; type State = { progress: {current: number; total: number}; }; class PluginContainer extends PureComponent { static contextType = ReactReduxContext; plugin: | FlipperPlugin | FlipperDevicePlugin | null | undefined; refChanged = ( ref: | FlipperPlugin | FlipperDevicePlugin | null | undefined, ) => { if (this.plugin) { this.plugin._teardown(); this.plugin = null; } if (ref && this.props.target) { activateMenuItems(ref); ref._init(); this.props.logger.trackTimeSince(`activePlugin-${ref.constructor.id}`); this.plugin = ref; } }; idler?: Idler; pluginBeingProcessed: string | null = null; state = {progress: {current: 0, total: 0}}; get store(): MiddlewareAPI { return this.context.store; } componentWillUnmount() { if (this.plugin) { this.plugin._teardown(); this.plugin = null; } this.cancelCurrentQueue(); } componentDidMount() { this.processMessageQueue(); } componentDidUpdate() { this.processMessageQueue(); } processMessageQueue() { const {pluginKey, pendingMessages, activePlugin} = this.props; if (pluginKey !== this.pluginBeingProcessed) { this.pluginBeingProcessed = pluginKey; this.cancelCurrentQueue(); this.setState({progress: {current: 0, total: 0}}); if ( activePlugin && activePlugin.persistedStateReducer && pluginKey && pendingMessages?.length ) { // this.setState({progress: {current: 0, total: 0}}); this.idler = new Idler(); processMessageQueue( activePlugin, pluginKey, this.store, progress => { this.setState({progress}); }, this.idler, ); } } } cancelCurrentQueue() { if (this.idler && !this.idler.isCancelled()) { this.idler.cancel(); } } render() { const {activePlugin, pluginKey, target, pendingMessages} = this.props; if (!activePlugin || !target || !pluginKey) { console.warn(`No selected plugin. Rendering empty!`); return null; } if (!pendingMessages || pendingMessages.length === 0) { return this.renderPlugin(); } else { return this.renderPluginLoader(); } } renderPluginLoader() { return ( ); } renderPlugin() { const { pluginState, setPluginState, activePlugin, pluginKey, target, isArchivedDevice, selectedApp, } = this.props; if (!activePlugin || !target || !pluginKey) { console.warn(`No selected plugin. Rendering empty!`); return null; } const props: PluginProps & { key: string; ref: ( ref: | FlipperPlugin | FlipperDevicePlugin | null | undefined, ) => void; } = { key: pluginKey, logger: this.props.logger, selectedApp, persistedState: activePlugin.defaultPersistedState ? { ...activePlugin.defaultPersistedState, ...pluginState, } : pluginState, setStaticView: (payload: StaticView) => this.props.setStaticView(payload), setPersistedState: state => setPluginState({pluginKey, state}), target, deepLinkPayload: this.props.deepLinkPayload, selectPlugin: (pluginID: string, deepLinkPayload: string | null) => { const {target} = this.props; // check if plugin will be available if ( target instanceof Client && target.plugins.some(p => p === pluginID) ) { this.props.selectPlugin({selectedPlugin: pluginID, deepLinkPayload}); return true; } else if (target instanceof BaseDevice) { this.props.selectPlugin({selectedPlugin: pluginID, deepLinkPayload}); return true; } else { return false; } }, ref: this.refChanged, isArchivedDevice, }; return ( {React.createElement(activePlugin, props)} ); } } export default connect( ({ connections: { selectedPlugin, selectedDevice, selectedApp, clients, deepLinkPayload, }, pluginStates, plugins: {devicePlugins, clientPlugins}, pluginMessageQueue, }) => { let pluginKey = null; let target = null; let activePlugin: | typeof FlipperDevicePlugin | typeof FlipperPlugin | null = null; if (selectedPlugin) { activePlugin = devicePlugins.get(selectedPlugin) || null; target = selectedDevice; if (selectedDevice && activePlugin) { pluginKey = getPluginKey(selectedDevice.serial, activePlugin.id); } else { target = clients.find((client: Client) => client.id === selectedApp) || null; activePlugin = clientPlugins.get(selectedPlugin) || null; if (activePlugin && target) { pluginKey = getPluginKey(target.id, activePlugin.id); } } } const isArchivedDevice = !selectedDevice ? false : selectedDevice instanceof ArchivedDevice; const pendingMessages = pluginKey ? pluginMessageQueue[pluginKey] : undefined; const s: StateFromProps = { pluginState: pluginStates[pluginKey as string], activePlugin: activePlugin, target, deepLinkPayload, pluginKey, isArchivedDevice, selectedApp: selectedApp || null, pendingMessages, }; return s; }, { setPluginState, selectPlugin, setStaticView, }, )(PluginContainer);