From 97665f2d384822f520b68d80111cfdbfdc1c9042 Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Thu, 13 May 2021 03:55:48 +0800 Subject: [PATCH] addon web FlowPreview shouldComponentUpdate --- addon/web/client/src/App.tsx | 21 +++------ .../web/client/src/components/FlowPreview.tsx | 43 +++++++++++++++++++ addon/web/client/src/message.ts | 15 ++++++- addon/web/client/src/utils.ts | 14 ++++++ 4 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 addon/web/client/src/components/FlowPreview.tsx diff --git a/addon/web/client/src/App.tsx b/addon/web/client/src/App.tsx index da84ff0..5d8f84a 100644 --- a/addon/web/client/src/App.tsx +++ b/addon/web/client/src/App.tsx @@ -6,6 +6,7 @@ import './App.css' import BreakPoint from './components/BreakPoint' import EditFlow from './components/EditFlow' +import FlowPreview from './components/FlowPreview' import { FlowManager } from './flow' import { isTextBody } from './utils' @@ -256,23 +257,15 @@ class App extends React.Component { flows.map(f => { const fp = f.preview() - const classNames = [] - if (this.state.flow && this.state.flow.id === fp.id) classNames.push('tr-selected') - if (fp.waitIntercept) classNames.push('tr-wait-intercept') - return ( - { + { this.setState({ flow: f }) }} - > - {fp.no} - {fp.host} - {fp.path} - {fp.method} - {fp.statusCode} - {fp.size} - + /> ) }) } diff --git a/addon/web/client/src/components/FlowPreview.tsx b/addon/web/client/src/components/FlowPreview.tsx new file mode 100644 index 0000000..0a03ea3 --- /dev/null +++ b/addon/web/client/src/components/FlowPreview.tsx @@ -0,0 +1,43 @@ +import React from 'react' +import { IFlowPreview } from '../message' +import { shallowEqual } from '../utils' + +interface IProps { + flow: IFlowPreview + isSelected: boolean + onShowDetail: () => void +} + +class FlowPreview extends React.Component { + shouldComponentUpdate(nextProps: IProps) { + if (nextProps.isSelected === this.props.isSelected && shallowEqual(nextProps.flow, this.props.flow)) { + return false + } + return true + } + + render() { + const fp = this.props.flow + + const classNames = [] + if (this.props.isSelected) classNames.push('tr-selected') + if (fp.waitIntercept) classNames.push('tr-wait-intercept') + + return ( + { + this.props.onShowDetail() + }} + > + {fp.no} + {fp.host} + {fp.path} + {fp.method} + {fp.statusCode} + {fp.size} + + ) + } +} + +export default FlowPreview diff --git a/addon/web/client/src/message.ts b/addon/web/client/src/message.ts index bb175ea..7d2e9b2 100644 --- a/addon/web/client/src/message.ts +++ b/addon/web/client/src/message.ts @@ -30,6 +30,17 @@ export interface IMessage { content?: ArrayBuffer | IRequest | IResponse } +export interface IFlowPreview { + no: number + id: string + waitIntercept: boolean + host: string + path: string + method: string + statusCode: string + size: string +} + export class Flow { public no: number public id: string @@ -71,7 +82,7 @@ export class Flow { return this } - public preview() { + public preview(): IFlowPreview { return { no: this.no, id: this.id, @@ -79,7 +90,7 @@ export class Flow { host: this.host, path: this.path, method: this.request.method, - statusCode: this.response ? this.response.statusCode : '(pending)', + statusCode: this.response ? String(this.response.statusCode) : '(pending)', size: this.size, } } diff --git a/addon/web/client/src/utils.ts b/addon/web/client/src/utils.ts index ca9e7eb..ae8e59f 100644 --- a/addon/web/client/src/utils.ts +++ b/addon/web/client/src/utils.ts @@ -26,3 +26,17 @@ export const getSize = (response: IResponse) => { if (len < 1024*1024) return `${(len/1024).toFixed(2)} KB` return `${(len/(1024*1024)).toFixed(2)} MB` } + +export const shallowEqual = (objA: any, objB: any) => { + if (objA === objB) return true + + const keysA = Object.keys(objA) + const keysB = Object.keys(objB) + if (keysA.length !== keysB.length) return false + + for (let i = 0; i < keysA.length; i++) { + const key = keysA[i] + if (objB[key] === undefined || objA[key] !== objB[key]) return false + } + return true +}