/
request.tsx
152 lines (133 loc) · 3.85 KB
/
request.tsx
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
import * as React from 'react';
import styled from '@emotion/styled';
import Button from 'sentry/components/button';
import ButtonBar from 'sentry/components/buttonBar';
import EventDataSection from 'sentry/components/events/eventDataSection';
import RichHttpContent from 'sentry/components/events/interfaces/richHttpContent/richHttpContent';
import {getCurlCommand, getFullUrl} from 'sentry/components/events/interfaces/utils';
import ExternalLink from 'sentry/components/links/externalLink';
import Truncate from 'sentry/components/truncate';
import {IconOpen} from 'sentry/icons';
import {t} from 'sentry/locale';
import space from 'sentry/styles/space';
import {EntryRequest, Event} from 'sentry/types/event';
import {isUrl} from 'sentry/utils';
type Props = {
data: EntryRequest['data'];
event: Event;
type: string;
};
type State = {
view: string;
};
class RequestInterface extends React.Component<Props, State> {
state: State = {
view: 'formatted',
};
isPartial = () =>
// We assume we only have a partial interface is we're missing
// an HTTP method. This means we don't have enough information
// to reliably construct a full HTTP request.
!this.props.data.method || !this.props.data.url;
toggleView = (value: string) => {
this.setState({
view: value,
});
};
render() {
const {data, type} = this.props;
const view = this.state.view;
let fullUrl = getFullUrl(data);
if (!isUrl(fullUrl)) {
// Check if the url passed in is a safe url to avoid XSS
fullUrl = undefined;
}
let parsedUrl: HTMLAnchorElement | null = null;
if (fullUrl) {
// use html tag to parse url, lol
parsedUrl = document.createElement('a');
parsedUrl.href = fullUrl;
}
let actions: React.ReactNode = null;
if (!this.isPartial() && fullUrl) {
actions = (
<ButtonBar merged active={view}>
<Button
barId="formatted"
size="xsmall"
onClick={this.toggleView.bind(this, 'formatted')}
>
{/* Translators: this means "formatted" rendering (fancy tables) */}
{t('Formatted')}
</Button>
<MonoButton
barId="curl"
size="xsmall"
onClick={this.toggleView.bind(this, 'curl')}
>
curl
</MonoButton>
</ButtonBar>
);
}
const title = (
<Header key="title">
<ExternalLink href={fullUrl} title={fullUrl}>
<Path>
<strong>{data.method || 'GET'}</strong>
<Truncate
value={parsedUrl ? parsedUrl.pathname : ''}
maxLength={36}
leftTrim
/>
</Path>
{fullUrl && <StyledIconOpen size="xs" />}
</ExternalLink>
<small>{parsedUrl ? parsedUrl.hostname : ''}</small>
</Header>
);
return (
<EventDataSection
type={type}
title={title}
actions={actions}
wrapTitle={false}
className="request"
>
{view === 'curl' ? (
<pre>{getCurlCommand(data)}</pre>
) : (
<RichHttpContent data={data} />
)}
</EventDataSection>
);
}
}
const MonoButton = styled(Button)`
font-family: ${p => p.theme.text.familyMono};
`;
const Path = styled('span')`
color: ${p => p.theme.textColor};
text-transform: none;
font-weight: normal;
& strong {
margin-right: ${space(0.5)};
}
`;
const Header = styled('h3')`
display: flex;
align-items: center;
`;
// Nudge the icon down so it is centered. the `external-icon` class
// doesn't quite get it in place.
const StyledIconOpen = styled(IconOpen)`
transition: 0.1s linear color;
margin: 0 ${space(0.5)};
color: ${p => p.theme.subText};
position: relative;
top: 1px;
&:hover {
color: ${p => p.theme.textColor};
}
`;
export default RequestInterface;