aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/ducks/ui.js
blob: f745f0af2da49c76c23152613df58582fe9dc017 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { selectRelative as selectFlowRelative } from './flowView'
import { Key } from '../utils.js'
import * as flowsActions from './flows'

export const SET_ACTIVE_MENU = 'UI_SET_ACTIVE_MENU'
export const SET_CONTENT_VIEW = 'UI_SET_CONTENT_VIEW'
export const SET_SELECTED_INPUT = 'UI_SET_SELECTED_INPUT'
export const UPDATE_QUERY = 'UI_UPDATE_QUERY'
export const SELECT_TAB = 'UI_SELECT_TAB'
export const SET_PROMPT = 'UI_SET_PROMPT'
export const SET_DISPLAY_LARGE = 'UI_SET_DISPLAY_LARGE'

const defaultState = {
    activeMenu: 'Start',
    isFlowSelected: false,
    selectedInput: null,
    displayLarge: false,
    promptOpen: false,
    contentView: 'ViewAuto',
    query: {},
    panel: 'request'
}

export default function reducer(state = defaultState, action) {
    switch (action.type) {

        case SET_ACTIVE_MENU:
            return {
                ...state,
                activeMenu: action.activeMenu,
            }

        case flowsActions.SELECT:
            if (action.flowIds.length && !state.isFlowSelected) {
                return {
                    ...state,
                    displayLarge: false,
                    activeMenu: 'Flow',
                    isFlowSelected: true,
                }
            }

            if (!action.flowIds.length && state.isFlowSelected) {
                let activeMenu = state.activeMenu
                if (activeMenu == 'Flow') {
                    activeMenu = 'Start'
                }
                return {
                    ...state,
                    activeMenu,
                    isFlowSelected: false,
                }
            }

            return {
                ...state,
                displayLarge: false,
            }

        case SET_CONTENT_VIEW:
            return {
                ...state,
                contentView: action.contentView,
            }

        case SET_SELECTED_INPUT:
            return {
                ...state,
                selectedInput: action.input
            }

        case UPDATE_QUERY:
            return {
                ...state,
                query: { ...state.query, ...action.query }
            }

        case SELECT_TAB:
            return {
                ...state,
                panel: action.panel
            }

        case SET_PROMPT:
            return {
                ...state,
                promptOpen: action.open,
            }

        case SET_DISPLAY_LARGE:
            return {
                ...state,
                displayLarge: action.displayLarge,
            }

        default:
            return state
    }
}

export function setActiveMenu(activeMenu) {
    return { type: SET_ACTIVE_MENU, activeMenu }
}

export function setContentView(contentView) {
    return { type: SET_CONTENT_VIEW, contentView }
}

export function setSelectedInput(input) {
    return { type: SET_SELECTED_INPUT, input }
}

export function updateQuery(query) {
    return { type: UPDATE_QUERY, query }
}

export function selectTab(panel) {
    return { type: SELECT_TAB, panel }
}

export function setPrompt(open) {
    return { type: SET_PROMPT, open }
}

export function setDisplayLarge(displayLarge) {
    return { type: SET_DISPLAY_LARGE, displayLarge }
}

export function onKeyDown(e) {
    if (e.ctrlKey) {
        return () => {
        }
    }
    var key = e.keyCode
    var shiftKey = e.shiftKey
    e.preventDefault()
    return (dispatch, getState) => {

        const flow = getState().flows.byId[getState().flows.selected[0]]

        switch (key) {

            case Key.I:
                dispatch(setSelectedInput('intercept'))
                break

            case Key.L:
                dispatch(setSelectedInput('search'))
                break

            case Key.H:
                dispatch(setSelectedInput('highlight'))
                break

            case Key.K:
            case Key.UP:
                dispatch(selectFlowRelative(-1))
                break

            case Key.J:
            case Key.DOWN:
                dispatch(selectFlowRelative(+1))
                break

            case Key.SPACE:
            case Key.PAGE_DOWN:
                dispatch(selectFlowRelative(+10))
                break

            case Key.PAGE_UP:
                dispatch(selectFlowRelative(-10))
                break

            case Key.END:
                dispatch(selectFlowRelative(+1e10))
                break

            case Key.HOME:
                dispatch(selectFlowRelative(-1e10))
                break

            case Key.ESC:
                dispatch(flowsActions.select(null))
                break

            case Key.LEFT:
            {
                let tabs = ['request', 'response', 'error'].filter(k => flow[k]).concat(['details']),
                    currentTab = getState().ui.panel,
                    nextTab = tabs[(tabs.indexOf(currentTab) - 1 + tabs.length) % tabs.length]
                dispatch(selectTab(nextTab))
                break
            }

            case Key.TAB:
            case Key.RIGHT:
            {
                let tabs = ['request', 'response', 'error'].filter(k => flow[k]).concat(['details']),
                    currentTab = getState().ui.panel,
                    nextTab = tabs[(tabs.indexOf(currentTab) + 1) % tabs.length]
                dispatch(selectTab(nextTab))
                break
            }

            case Key.C:
                if (shiftKey) {
                    dispatch(flowsActions.clear())
                }
                break

            case Key.D:
            {
                if (!flow) {
                    return
                }
                if (shiftKey) {
                    dispatch(flowsActions.duplicate(flow))
                } else {
                    dispatch(flowsActions.remove(flow))
                }
                break
            }

            case Key.A:
            {
                if (shiftKey) {
                    dispatch(flowsActions.acceptAll())
                } else if (flow && flow.intercepted) {
                    dispatch(flowsActions.accept(flow))
                }
                break
            }

            case Key.R:
            {
                if (!shiftKey && flow) {
                    dispatch(flowsActions.replay(flow))
                }
                break
            }

            case Key.V:
            {
                if (!shiftKey && flow && flow.modified) {
                    dispatch(flowsActions.revert(flow))
                }
                break
            }

            case Key.E:
                dispatch(setPrompt(true))
                break

            default:
                return () => {
                }
        }
    }
}