From adb1d6e9768b1a0af249dfb6f027a89c90c9c2aa Mon Sep 17 00:00:00 2001 From: Chaiwat Ekkaewnumchai Date: Tue, 17 Mar 2020 10:05:27 -0700 Subject: [PATCH] (Server) Add ManageMockResponsePanel Summary: - Add ManageMockResponsePanel to add, modify and remove mocked route Note: - This is a part of this PR: https://github.com/facebook/flipper/pull/488 Reviewed By: mweststrate Differential Revision: D20440147 fbshipit-source-id: 3af127c4b091f288c13b41b74d78c07b4eb0e52f --- .../network/ManageMockResponsePanel.tsx | 200 ++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 desktop/plugins/network/ManageMockResponsePanel.tsx diff --git a/desktop/plugins/network/ManageMockResponsePanel.tsx b/desktop/plugins/network/ManageMockResponsePanel.tsx new file mode 100644 index 000000000..771e91e55 --- /dev/null +++ b/desktop/plugins/network/ManageMockResponsePanel.tsx @@ -0,0 +1,200 @@ +/** + * 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 { + ManagedTable, + Text, + FlexBox, + FlexRow, + FlexColumn, + Glyph, + styled, + colors, + Panel, +} from 'flipper'; +import React, {useContext, useState, useMemo, useEffect} from 'react'; + +import {Route} from './types'; + +import {MockResponseDetails} from './MockResponseDetails'; +import {NetworkRouteContext} from './index'; +import {RequestId} from './types'; + +type Props = {routes: {[id: string]: Route}}; + +const ColumnSizes = {route: 'flex'}; + +const Columns = {route: {value: 'Route', resizable: false}}; + +const AddRouteButton = styled(FlexBox)({ + color: colors.blackAlpha50, + alignItems: 'center', + padding: 10, + flexShrink: 0, + whiteSpace: 'nowrap', + overflow: 'hidden', + textOverflow: 'ellipsis', +}); + +const Container = styled(FlexRow)({ + flex: 1, + justifyContent: 'space-around', + alignItems: 'stretch', +}); + +const LeftPanel = styled(FlexColumn)({ + flex: 1, +}); + +const RightPanel = styled(FlexColumn)({ + flex: 3, + height: '100%', +}); + +const TextEllipsis = styled(Text)({ + overflowX: 'hidden', + textOverflow: 'ellipsis', + maxWidth: '100%', + lineHeight: '18px', + paddingTop: 4, + display: 'block', + whiteSpace: 'nowrap', +}); + +const Icon = styled(Glyph)({ + marginTop: 5, + marginRight: 8, +}); + +// return ids that have the same pair of requestUrl and method; this will return only the duplicate +function _duplicateIds(routes: {[id: string]: Route}): Array { + const idSet: {[id: string]: {[method: string]: boolean}} = {}; + return Object.entries(routes).reduce((acc: Array, [id, route]) => { + if (idSet.hasOwnProperty(route.requestUrl)) { + if (idSet[route.requestUrl].hasOwnProperty(route.requestMethod)) { + return acc.concat(id); + } + idSet[route.requestUrl] = { + ...idSet[route.requestUrl], + [route.requestMethod]: true, + }; + return acc; + } else { + idSet[route.requestUrl] = {[route.requestMethod]: true}; + return acc; + } + }, []); +} + +function _buildRows( + routes: {[id: string]: Route}, + duplicatedIds: Array, +) { + return Object.entries(routes).map(([id, route]) => ({ + columns: { + route: { + value: duplicatedIds.includes(id) ? ( + + + {route.requestUrl} + + ) : ( + {route.requestUrl} + ), + }, + }, + key: id, + })); +} + +function ManagedMockResponseRightPanel(props: { + id: string; + route: Route; + isDuplicated: boolean; +}) { + const {id, route, isDuplicated} = props; + return ( + + + + ); +} + +export function ManageMockResponsePanel(props: Props) { + const networkRouteManager = useContext(NetworkRouteContext); + const [selectedId, setSelectedId] = useState(null); + const [currentRouteSize, setCurrentRouteSize] = useState(0); + + const {routes} = props; + useEffect(() => { + const keys = Object.keys(routes); + const routeSize = keys.length; + if (currentRouteSize === routeSize) { + return; + } + if (routeSize > 0 && routeSize > currentRouteSize) { + setSelectedId(keys[routeSize - 1]); + } + setCurrentRouteSize(routeSize); + }, [routes]); + const duplicatedIds = useMemo(() => _duplicateIds(routes), [routes]); + return ( + + + { + networkRouteManager.addRoute(); + }}> + +  Add Route + + { + const newSelectedId = + selectedIds.length === 1 ? selectedIds[0] : null; + setSelectedId(newSelectedId); + }} + highlightedRows={new Set(selectedId)} + /> + + + {selectedId && routes.hasOwnProperty(selectedId) && ( + + )} + + + ); +}