[web] remove react-router

This commit is contained in:
Jason 2016-08-15 23:09:45 +08:00
parent a0391db1ae
commit 1d27334ff7
5 changed files with 61 additions and 46 deletions

View File

@ -19,16 +19,16 @@
"bootstrap": "^3.3.6", "bootstrap": "^3.3.6",
"classnames": "^2.2.5", "classnames": "^2.2.5",
"flux": "^2.1.1", "flux": "^2.1.1",
"history": "^3.0.0",
"lodash": "^4.11.2", "lodash": "^4.11.2",
"react": "^15.1.0", "react": "^15.1.0",
"react-codemirror": "^0.2.6",
"react-dom": "^15.1.0", "react-dom": "^15.1.0",
"react-redux": "^4.4.5", "react-redux": "^4.4.5",
"react-router": "^2.4.0",
"redux": "^3.5.2", "redux": "^3.5.2",
"redux-logger": "^2.6.1", "redux-logger": "^2.6.1",
"redux-thunk": "^2.1.0", "redux-thunk": "^2.1.0",
"shallowequal": "^0.2.2", "shallowequal": "^0.2.2"
"react-codemirror": "^0.2.6"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "^6.7.7", "babel-core": "^6.7.7",

View File

@ -3,10 +3,8 @@ import { render } from 'react-dom'
import { applyMiddleware, createStore } from 'redux' import { applyMiddleware, createStore } from 'redux'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import thunk from 'redux-thunk' import thunk from 'redux-thunk'
import { Route, Router as ReactRouter, hashHistory, Redirect } from 'react-router'
import ProxyApp from './components/ProxyApp' import ProxyApp from './components/ProxyApp'
import MainView from './components/MainView'
import rootReducer from './ducks/index' import rootReducer from './ducks/index'
import { add as addLog } from './ducks/eventLog' import { add as addLog } from './ducks/eventLog'
@ -32,13 +30,7 @@ window.addEventListener('error', msg => {
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
render( render(
<Provider store={store}> <Provider store={store}>
<ReactRouter history={hashHistory}> <ProxyApp />
<Redirect from="/" to="/flows" />
<Route path="/" component={ProxyApp}>
<Route path="flows" component={MainView}/>
<Route path="flows/:flowId/:detailTab" component={MainView}/>
</Route>
</ReactRouter>
</Provider>, </Provider>,
document.getElementById("mitmproxy") document.getElementById("mitmproxy")
) )

View File

@ -29,8 +29,7 @@ class MainView extends Component {
<FlowView <FlowView
key="flowDetails" key="flowDetails"
ref="flowDetails" ref="flowDetails"
tab={this.props.routeParams.detailTab} tab={this.props.tab}
query={this.props.query}
updateFlow={data => this.props.updateFlow(selectedFlow, data)} updateFlow={data => this.props.updateFlow(selectedFlow, data)}
flow={selectedFlow} flow={selectedFlow}
/> />
@ -45,7 +44,8 @@ export default connect(
flows: state.flowView.data, flows: state.flowView.data,
filter: state.flowView.filter, filter: state.flowView.filter,
highlight: state.flowView.highlight, 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, selectFlow: flowsActions.select,

View File

@ -1,58 +1,76 @@
import React, { Component, PropTypes } from 'react' import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { createHashHistory } from 'history'
import { init as appInit, destruct as appDestruct } from '../ducks/app' import { init as appInit, destruct as appDestruct } from '../ducks/app'
import { onKeyDown } from '../ducks/ui/keyboard' 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 Header from './Header'
import EventLog from './EventLog' import EventLog from './EventLog'
import Footer from './Footer' import Footer from './Footer'
class ProxyAppMain extends Component { class ProxyAppMain extends Component {
static contextTypes = { flushToStore(location) {
router: PropTypes.object.isRequired, const components = location.pathname.split('/')
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(location.query[Query.SEARCH])
this.props.updateHighlight(location.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() { componentWillMount() {
this.props.appInit(this.context.router) this.props.appInit()
this.history = createHashHistory()
this.unlisten = this.history.listen(location => this.flushToStore(location))
window.addEventListener('keydown', this.props.onKeyDown); window.addEventListener('keydown', this.props.onKeyDown);
} }
componentWillUnmount() { componentWillUnmount() {
this.props.appDestruct(this.context.router) this.props.appDestruct()
this.unlisten()
window.removeEventListener('keydown', this.props.onKeyDown); window.removeEventListener('keydown', this.props.onKeyDown);
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
/* this.flushToHistory(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 })
}
} }
render() { render() {
const { showEventLog, location, children, query } = this.props const { showEventLog, location, filter, highlight } = this.props
return ( return (
<div id="container" tabIndex="0"> <div id="container" tabIndex="0">
<Header/> <Header/>
{React.cloneElement( <MainView />
children,
{ ref: 'view', location, query }
)}
{showEventLog && ( {showEventLog && (
<EventLog key="eventlog"/> <EventLog key="eventlog"/>
)} )}
@ -65,13 +83,18 @@ class ProxyAppMain extends Component {
export default connect( export default connect(
state => ({ state => ({
showEventLog: state.eventLog.visible, showEventLog: state.eventLog.visible,
query: state.flowView.filter, filter: state.flowView.filter,
panel: state.ui.flow.tab, highlight: state.flowView.highlight,
tab: state.ui.flow.tab,
selectedFlowId: state.flows.selected[0] selectedFlowId: state.flows.selected[0]
}), }),
{ {
appInit, appInit,
appDestruct, appDestruct,
onKeyDown onKeyDown,
updateFilter,
updateHighlight,
selectTab,
selectFlow
} }
)(ProxyAppMain) )(ProxyAppMain)

View File

@ -58,7 +58,7 @@ export default function reducer(state = defaultState, action) {
case SET_TAB: case SET_TAB:
return { return {
...state, ...state,
tab: action.tab, tab: action.tab ? action.tab : 'request',
displayLarge: false, displayLarge: false,
} }