aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/components/mainview.js
blob: 99cdeb3cac24c81b02640904116f565957328854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React from "react";

import {FlowActions} from "../actions.js";
import {Query} from "../actions.js";
import {Key} from "../utils.js";
import {Splitter} from "./common.js"
import FlowTable from "./flowtable.js";
import FlowView from "./flowview/index.js";
import {connect} from 'react-redux'
import {selectFlow, setFilter, setHighlight, setSort} from "../ducks/flows";


var MainView = React.createClass({
    componentWillReceiveProps: function (nextProps) {
        // Update redux store with route changes
        if(nextProps.routeParams.flowId !== (nextProps.selectedFlow || {}).id) {
            this.props.selectFlow(nextProps.routeParams.flowId)
        }
        if(nextProps.location.query[Query.SEARCH] !== nextProps.filter) {
            this.props.setFilter(nextProps.location.query[Query.SEARCH], false)
        }
        if (nextProps.location.query[Query.HIGHLIGHT] !== nextProps.highlight) {
            this.props.setHighlight(nextProps.location.query[Query.HIGHLIGHT], false)
        }
    },
    setSortKeyFun: function (sortKeyFun) {
        // FIXME: Move to redux. This requires that sortKeyFun is not a function anymore.
    },
    selectFlow: function (flow) {
        // TODO: This belongs into redux
        if (flow) {
            let tab = this.props.routeParams.detailTab || "request";
            this.props.updateLocation(`/flows/${flow.id}/${tab}`);
        } else {
            this.props.updateLocation("/flows");
        }
    },
    selectFlowRelative: function (shift) {
        // TODO: This belongs into redux
        let flows = this.props.flows,
            index
        if (!this.props.routeParams.flowId) {
            if (shift < 0) {
                index = flows.length - 1
            } else {
                index = 0
            }
        } else {
            index = flows.indexOf(this.props.selectedFlow)
            index = Math.min(
                Math.max(0, index + shift),
                flows.length - 1
            )
        }
        this.selectFlow(flows[index])
    },
    onMainKeyDown: function (e) {
        var flow = this.props.selectedFlow;
        if (e.ctrlKey) {
            return;
        }
        switch (e.keyCode) {
            case Key.K:
            case Key.UP:
                this.selectFlowRelative(-1);
                break;
            case Key.J:
            case Key.DOWN:
                this.selectFlowRelative(+1);
                break;
            case Key.SPACE:
            case Key.PAGE_DOWN:
                this.selectFlowRelative(+10);
                break;
            case Key.PAGE_UP:
                this.selectFlowRelative(-10);
                break;
            case Key.END:
                this.selectFlowRelative(+1e10);
                break;
            case Key.HOME:
                this.selectFlowRelative(-1e10);
                break;
            case Key.ESC:
                this.selectFlow(null);
                break;
            case Key.H:
            case Key.LEFT:
                if (this.refs.flowDetails) {
                    this.refs.flowDetails.nextTab(-1);
                }
                break;
            case Key.L:
            case Key.TAB:
            case Key.RIGHT:
                if (this.refs.flowDetails) {
                    this.refs.flowDetails.nextTab(+1);
                }
                break;
            case Key.C:
                if (e.shiftKey) {
                    FlowActions.clear();
                }
                break;
            case Key.D:
                if (flow) {
                    if (e.shiftKey) {
                        FlowActions.duplicate(flow);
                    } else {
                        FlowActions.delete(flow);
                    }
                }
                break;
            case Key.A:
                if (e.shiftKey) {
                    FlowActions.accept_all();
                } else if (flow && flow.intercepted) {
                    FlowActions.accept(flow);
                }
                break;
            case Key.R:
                if (!e.shiftKey && flow) {
                    FlowActions.replay(flow);
                }
                break;
            case Key.V:
                if (e.shiftKey && flow && flow.modified) {
                    FlowActions.revert(flow);
                }
                break;
            case Key.E:
                if (this.refs.flowDetails) {
                    this.refs.flowDetails.promptEdit();
                }
                break;
            case Key.SHIFT:
                break;
            default:
                console.debug("keydown", e.keyCode);
                return;
        }
        e.preventDefault();
    },
    render: function () {

        var details = null;
        if (this.props.selectedFlow) {
            details = [
                <Splitter key="splitter"/>,
                <FlowView
                    key="flowDetails"
                    ref="flowDetails"
                    tab={this.props.routeParams.detailTab}
                    query={this.props.query}
                    updateLocation={this.props.updateLocation}
                    flow={this.props.selectedFlow}/>
            ]
        }

        return (
            <div className="main-view">
                <FlowTable ref="flowTable"
                    selectFlow={this.selectFlow}
                    setSortKeyFun={(f) => console.log("asdf")}
                    setSort={this.props.setSort}
                    selected={this.props.selectedFlow} />
                {details}
            </div>
        );
    }
});

const MainViewContainer = connect(
    state => ({
        flows: state.flows.view,
        filter: state.flows.filter,
        sort: state.flows.sort,
        highlight: state.flows.highlight,
        selectedFlow: state.flows.all.byId[state.flows.selected[0]]
    }),
    dispatch => ({
        selectFlow: flowId => dispatch(selectFlow(flowId)),
        setFilter: filter => dispatch(setFilter(filter)),
        setSort: (sort) => dispatch(setSort(sort)),
        setHighlight: highlight => dispatch(setHighlight(highlight))
    }),
    undefined,
    {withRef: true}
)(MainView);

export default MainViewContainer;