-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLatestRequestNotifications.js
158 lines (124 loc) · 4.44 KB
/
LatestRequestNotifications.js
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
import React from 'react';
import styled from 'styled-components';
import URLStamp from 'ui/components/common/URLStamp';
import Icon from 'ui/components/common/Icon';
import throttle from 'lodash/throttle';
import EVENTS from 'api/constants/events';
import get from 'lodash/get';
import API from 'api';
import UIState from 'ui/states/UIState';
import QuickEditState from 'ui/states/QuickEditState';
import SettingsState from 'ui/states/SettingsState';
import { connectToState } from 'ui/states/connector';
import { Div } from 'ui/components/common/base';
const Container = styled(Div)`
left: ${() => SettingsState.alwaysShowMimicButtons ? '102px' : '0'};
bottom: 0;
position: fixed;
opacity: ${(props) => Number(!props.fadingOut)};
transition: opacity ${(props) => props.fadingOut ? 2 : 0.1}s ease-in-out;
max-width: 500px;
`;
const Control = styled(Div)`
display: flex;
align-items: center;
background-color: #f7f7f7;
padding: 0 8px;
height: 25px;
cursor: pointer;
${(props) => props.rounded ? 'border-radius: 0 0 0 11px' : ''}
&:hover {
background-color: #c8dcf4;
}
`;
const Status = styled(Div)`
margin-left: 20px;
color: ${(props) => props.children > 400 ? props.theme.red : '' };
`;
class LatestRequestNotifications extends React.Component {
lastX = 0;
lastY = 0;
canFadeOut = true;
state = {
latestRequest: null,
fadingOut: true
};
componentDidMount() {
API.on(EVENTS.REQUEST_CAPTURED, this.setLatestRequest);
API.on(EVENTS.RESPONSE_RECEIVED, this.setLatestRequest);
document.addEventListener('mousemove', this.resetFadeoutTimerByMouse);
}
componentWillReceiveProps() {
this.resetFadeoutTimer();
}
componentWillUnmount() {
API.off(EVENTS.REQUEST_CAPTURED, this.setLatestRequest);
API.off(EVENTS.RESPONSE_RECEIVED, this.setLatestRequest);
document.removeEventListener('mousemove', this.resetFadeoutTimerByMouse);
this.stopFadeoutTimer(true);
}
resetFadeoutTimer = () => {
clearTimeout(this.willFadeOut);
clearTimeout(this.willDisappear);
this.canFadeOut = true;
this.setState({ fadingOut: false }, () => {
this.willFadeOut = setTimeout(() => this.setState({ fadingOut: true }, () => {
this.willDisappear = setTimeout(() => this.setState({ latestRequest: null }), 2000);
}), 3000);
});
};
stopFadeoutTimer = (unmounting) => {
clearTimeout(this.willFadeOut);
clearTimeout(this.willDisappear);
this.canFadeOut = false;
if (!unmounting) {
this.setState({ fadingOut: false });
}
};
resetFadeoutTimerByMouse = throttle((event) => {
if (!this.canFadeOut || !this.state.latestRequest) {
return;
}
const halfHeight = Math.round(window.innerHeight / 2);
const halfWidth = Math.round(window.innerWidth - window.innerWidth / 2);
const deltaX = event.clientX - this.lastX;
const deltaY = event.clientY - this.lastY;
const inBottomLeftCorner = event.clientY > halfHeight && event.clientX < halfWidth;
const movingToCorner = deltaX <= 0 && deltaY >= 0;
if (inBottomLeftCorner && movingToCorner) {
this.resetFadeoutTimer();
}
this.lastX = event.clientX;
this.lastY = event.clientY;
}, 30);
setLatestRequest = ({ requestId }) => {
this.setState({ latestRequest: API.getCapturedRequest(requestId) });
this.resetFadeoutTimer();
};
onEdit = () => {
UIState.setViewMode('quickEdit');
QuickEditState.selectRequest(this.state.latestRequest.id);
};
render() {
if (!this.state.latestRequest) {
return null
}
const status = get(this.state.latestRequest, 'response.status');
const mock = get(this.state.latestRequest, 'mock');
return (
<Container fadingOut={ this.state.fadingOut }
onMouseEnter={ this.stopFadeoutTimer }
onMouseLeave={ this.resetFadeoutTimer }>
<Control onClick={ this.onEdit }>
{ mock && mock.isActive && <Icon style={{ marginRight: 6, fill: '#4b82d5' }} src="mocked"/> }
{ mock && !mock.isActive && <Icon style={{ marginRight: 6, fill: '#b0b0b0' }} src="unmocked"/> }
<URLStamp request={ this.state.latestRequest}/>
{ status && <Status>{ status }</Status> }
{ !this.state.latestRequest.response.status && <Icon style={{ marginLeft: 20 }} src="spin"/> }
<Icon src="edit" style={{ marginLeft: 4 }}/>
</Control>
</Container>
);
}
}
export default connectToState(UIState, LatestRequestNotifications);