import React from 'react' import JSONPretty from 'react-json-pretty' import { Flow, IResponse } from '../message' import { isTextBody } from '../utils' import EditFlow from './EditFlow' interface Iprops { flow: Flow | null onClose: () => void onReRenderFlows: () => void onMessage: (msg: ArrayBufferLike) => void } interface IState { flowTab: 'Headers' | 'Preview' | 'Response' } class ViewFlow extends React.Component { constructor(props: Iprops) { super(props) this.state = { flowTab: 'Headers', } } preview() { const { flow } = this.props if (!flow) return null const response = flow.response if(!response) return null if (!(response.body && response.body.byteLength)) { return
No response
} const pv = flow.previewResponseBody() if (!pv) return
Not support preview
if (pv.type === 'image') { return } else if (pv.type === 'json') { return
} return
Not support preview
} render() { if (!this.props.flow) return null const flow = this.props.flow const flowTab = this.state.flowTab const request = flow.request const response: IResponse = (flow.response || {}) as any return (
{ this.props.onClose() }}>x { this.setState({ flowTab: 'Headers' }) }}>Headers { this.setState({ flowTab: 'Preview' }) }}>Preview { this.setState({ flowTab: 'Response' }) }}>Response { flow.request.method = request.method flow.request.url = request.url flow.request.header = request.header if (isTextBody(flow.request)) flow.request.body = request.body this.props.onReRenderFlows() }} onChangeResponse={response => { if (!flow.response) flow.response = {} as IResponse flow.response.statusCode = response.statusCode flow.response.header = response.header if (isTextBody(flow.response)) flow.response.body = response.body this.props.onReRenderFlows() }} onMessage={msg => { this.props.onMessage(msg) flow.waitIntercept = false this.props.onReRenderFlows() }} />
{ !(flowTab === 'Headers') ? null :

General

Request URL: {request.url}

Request Method: {request.method}

Status Code: {`${response.statusCode || '(pending)'}`}

{ !(response.header) ? null :

Response Headers

{ Object.keys(response.header).map(key => { return (

{key}: {response.header[key].join(' ')}

) }) }
}

Request Headers

{ !(request.header) ? null : Object.keys(request.header).map(key => { return (

{key}: {request.header[key].join(' ')}

) }) }
{ !(request.body && request.body.byteLength) ? null :

Request Body

{ !(flow.isTextRequest()) ? Not text : flow.requestBody() }

}
} { !(flowTab === 'Response') ? null : !(response.body && response.body.byteLength) ?
No response
: !(flow.isTextResponse()) ?
Not text response
:
{flow.responseBody()}
} { !(flowTab === 'Preview') ? null :
{this.preview()}
}
) } } export default ViewFlow