diff options
| author | Maximilian Hils <git@maximilianhils.com> | 2016-08-15 23:13:57 -0700 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-15 23:13:57 -0700 | 
| commit | f9f0ac848e8a691a78d4458f3d25f21b3e943658 (patch) | |
| tree | f7b81554206afbc221b9eaaf1b6dead713f26a74 | |
| parent | 2419ab153d3ce215e719b434f5f69405be9a6586 (diff) | |
| parent | 81b726bab1cc403977ef4aea060f88ab8a0a66fd (diff) | |
| download | mitmproxy-f9f0ac848e8a691a78d4458f3d25f21b3e943658.tar.gz mitmproxy-f9f0ac848e8a691a78d4458f3d25f21b3e943658.tar.bz2 mitmproxy-f9f0ac848e8a691a78d4458f3d25f21b3e943658.zip  | |
Merge pull request #1483 from gzzhanghao/history
[web] Use history object instead of react-router
| -rw-r--r-- | web/package.json | 8 | ||||
| -rw-r--r-- | web/src/js/app.jsx | 10 | ||||
| -rw-r--r-- | web/src/js/components/MainView.jsx | 6 | ||||
| -rw-r--r-- | web/src/js/components/ProxyApp.jsx | 84 | ||||
| -rw-r--r-- | web/src/js/ducks/ui/flow.js | 2 | 
5 files changed, 63 insertions, 47 deletions
diff --git a/web/package.json b/web/package.json index fb2c8c30..59b031b9 100644 --- a/web/package.json +++ b/web/package.json @@ -7,7 +7,7 @@      "start": "gulp"    },    "jest": { -    "testRegex": "__tests__/.*\\Spec.js$", +    "testRegex": "__tests__/.*Spec.js$",      "testPathDirs": [        "<rootDir>/src/js"      ], @@ -19,16 +19,16 @@      "bootstrap": "^3.3.6",      "classnames": "^2.2.5",      "flux": "^2.1.1", +    "history": "^3.0.0",      "lodash": "^4.11.2",      "react": "^15.1.0", +    "react-codemirror": "^0.2.6",      "react-dom": "^15.1.0",      "react-redux": "^4.4.5", -    "react-router": "^2.4.0",      "redux": "^3.5.2",      "redux-logger": "^2.6.1",      "redux-thunk": "^2.1.0", -    "shallowequal": "^0.2.2", -    "react-codemirror": "^0.2.6" +    "shallowequal": "^0.2.2"    },    "devDependencies": {      "babel-core": "^6.7.7", diff --git a/web/src/js/app.jsx b/web/src/js/app.jsx index 726b2ae1..f04baea0 100644 --- a/web/src/js/app.jsx +++ b/web/src/js/app.jsx @@ -3,10 +3,8 @@ import { render } from 'react-dom'  import { applyMiddleware, createStore } from 'redux'  import { Provider } from 'react-redux'  import thunk from 'redux-thunk' -import { Route, Router as ReactRouter, hashHistory, Redirect } from 'react-router'  import ProxyApp from './components/ProxyApp' -import MainView from './components/MainView'  import rootReducer from './ducks/index'  import { add as addLog } from './ducks/eventLog' @@ -32,13 +30,7 @@ window.addEventListener('error', msg => {  document.addEventListener('DOMContentLoaded', () => {      render(          <Provider store={store}> -            <ReactRouter history={hashHistory}> -                <Redirect from="/" to="/flows" /> -                <Route path="/" component={ProxyApp}> -                    <Route path="flows" component={MainView}/> -                    <Route path="flows/:flowId/:detailTab" component={MainView}/> -                </Route> -            </ReactRouter> +            <ProxyApp />          </Provider>,          document.getElementById("mitmproxy")      ) diff --git a/web/src/js/components/MainView.jsx b/web/src/js/components/MainView.jsx index f45f9eef..8be6f21c 100644 --- a/web/src/js/components/MainView.jsx +++ b/web/src/js/components/MainView.jsx @@ -29,8 +29,7 @@ class MainView extends Component {                      <FlowView                          key="flowDetails"                          ref="flowDetails" -                        tab={this.props.routeParams.detailTab} -                        query={this.props.query} +                        tab={this.props.tab}                          updateFlow={data => this.props.updateFlow(selectedFlow, data)}                          flow={selectedFlow}                      /> @@ -45,7 +44,8 @@ export default connect(          flows: state.flowView.data,          filter: state.flowView.filter,          highlight: state.flowView.highlight, -        selectedFlow: state.flows.byId[state.flows.selected[0]] +        selectedFlow: state.flows.byId[state.flows.selected[0]], +        tab: state.ui.flow.tab,      }),      {          selectFlow: flowsActions.select, diff --git a/web/src/js/components/ProxyApp.jsx b/web/src/js/components/ProxyApp.jsx index f8a6e262..d76816e5 100644 --- a/web/src/js/components/ProxyApp.jsx +++ b/web/src/js/components/ProxyApp.jsx @@ -1,58 +1,77 @@  import React, { Component, PropTypes } from 'react'  import { connect } from 'react-redux' +import { createHashHistory, useQueries } from 'history'  import { init as appInit, destruct as appDestruct } from '../ducks/app'  import { onKeyDown } from '../ducks/ui/keyboard' +import { updateFilter, updateHighlight } from '../ducks/flowView' +import { selectTab } from '../ducks/ui/flow' +import { select as selectFlow } from '../ducks/flows' +import { Query } from '../actions' +import MainView from './MainView'  import Header from './Header'  import EventLog from './EventLog'  import Footer from './Footer'  class ProxyAppMain extends Component { -    static contextTypes = { -        router: PropTypes.object.isRequired, +    flushToStore(location) { +        const components = location.pathname.split('/').filter(v => v) +        const query = location.query || {} + +        if (components.length > 2) { +            this.props.selectFlow(components[1]) +            this.props.selectTab(components[2]) +        } else { +            this.props.selectFlow(null) +            this.props.selectTab(null) +        } + +        this.props.updateFilter(query[Query.SEARCH]) +        this.props.updateHighlight(query[Query.HIGHLIGHT]) +    } + +    flushToHistory(props) { +        const query = { ...query } + +        if (props.filter) { +            query[Query.SEARCH] = props.filter +        } + +        if (props.highlight) { +            query[Query.HIGHLIGHT] = props.highlight +        } + +        if (props.selectedFlowId) { +            this.history.push({ pathname: `/flows/${props.selectedFlowId}/${props.tab}`, query }) +        } else { +            this.history.push({ pathname: '/flows', query }) +        }      }      componentWillMount() { -        this.props.appInit(this.context.router) +        this.props.appInit() +        this.history = useQueries(createHashHistory)() +        this.unlisten = this.history.listen(location => this.flushToStore(location))          window.addEventListener('keydown', this.props.onKeyDown);      }      componentWillUnmount() { -        this.props.appDestruct(this.context.router) +        this.props.appDestruct() +        this.unlisten()          window.removeEventListener('keydown', this.props.onKeyDown);      }      componentWillReceiveProps(nextProps) { -        /* -        FIXME: improve react-router -> redux integration. -        if (nextProps.location.query[Query.SEARCH] !== nextProps.filter) { -            this.props.updateFilter(nextProps.location.query[Query.SEARCH], false) -        } -        if (nextProps.location.query[Query.HIGHLIGHT] !== nextProps.highlight) { -            this.props.updateHighlight(nextProps.location.query[Query.HIGHLIGHT], false) -        } -        */ -        if (nextProps.query === this.props.query && nextProps.selectedFlowId === this.props.selectedFlowId && nextProps.panel === this.props.panel) { -            return -        } -        if (nextProps.selectedFlowId) { -            this.context.router.replace({ pathname: `/flows/${nextProps.selectedFlowId}/${nextProps.panel}`, query: nextProps.query }) -        } else { -            this.context.router.replace({ pathname: '/flows', query: nextProps.query }) -        } - +        this.flushToHistory(nextProps)      }      render() { -        const { showEventLog, location, children, query } = this.props +        const { showEventLog, location, filter, highlight } = this.props          return (              <div id="container" tabIndex="0">                  <Header/> -                {React.cloneElement( -                    children, -                    { ref: 'view', location, query } -                )} +                <MainView />                  {showEventLog && (                      <EventLog key="eventlog"/>                  )} @@ -65,13 +84,18 @@ class ProxyAppMain extends Component {  export default connect(      state => ({          showEventLog: state.eventLog.visible, -        query: state.flowView.filter, -        panel: state.ui.flow.tab, +        filter: state.flowView.filter, +        highlight: state.flowView.highlight, +        tab: state.ui.flow.tab,          selectedFlowId: state.flows.selected[0]      }),      {          appInit,          appDestruct, -        onKeyDown +        onKeyDown, +        updateFilter, +        updateHighlight, +        selectTab, +        selectFlow      }  )(ProxyAppMain) diff --git a/web/src/js/ducks/ui/flow.js b/web/src/js/ducks/ui/flow.js index fb2a846d..22a8c22d 100644 --- a/web/src/js/ducks/ui/flow.js +++ b/web/src/js/ducks/ui/flow.js @@ -85,7 +85,7 @@ export default function reducer(state = defaultState, action) {          case SET_TAB:              return {                  ...state, -                tab: action.tab, +                tab: action.tab ? action.tab : 'request',                  displayLarge: false,                  showFullContent: false              }  | 
