-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
210 lines (181 loc) · 6.42 KB
/
index.jsx
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
const classNames = require('classnames');
const PropTypes = require('prop-types');
const React = require('react');
const OauthContext = require('./contexts/Oauth');
const SelectedAppContext = require('./contexts/SelectedApp');
const VariablesContext = require('./contexts/Variables');
class Variable extends React.Component {
constructor(props) {
super(props);
this.state = { showDropdown: false };
this.getName = this.getName.bind(this);
this.toggleVarDropdown = this.toggleVarDropdown.bind(this);
this.toggleAuthDropdown = this.toggleAuthDropdown.bind(this);
this.renderVarDropdown = this.renderVarDropdown.bind(this);
this.onChange = this.onChange.bind(this);
}
onChange(event) {
this.toggleVarDropdown();
this.props.changeSelected(event.target.value);
}
getName() {
const name = this.props.name || this.props.variable;
if (!name) throw new TypeError("Missing prop 'name' or 'variable' for Variable component!");
return name;
}
getDefault() {
const def = this.props.defaults.filter(Boolean).find(d => d.name === this.getName()) || {};
if (def.default) return def.default;
return this.getName().toUpperCase();
}
getSelectedValue(selected) {
const { user } = this.props;
let selectedValue = {};
if (Array.isArray(user.keys) && user.keys.length) {
selectedValue = selected ? user.keys.find(key => key.name === selected) : user.keys[0];
}
return selectedValue;
}
// Determining whether to show a dropdown or not onClick
// based upon whether the currently displayed value has come
// from one of the nested user.keys[] or not.
// Additionally do not show the dropdown if there is only a
// single key, since there's nothing to change it to.
shouldShowVarDropdown(selected) {
const { user } = this.props;
return !!this.getSelectedValue(selected)[this.getName()] && Array.isArray(user.keys) && user.keys.length > 1;
}
// Return value in this order
// - selected user keys value (or first user keys)
// - top level user value
// - default value
// - uppercase key
getValue(selected) {
const selectedValue = this.getSelectedValue(selected);
const value = selectedValue[this.getName()] || this.props.user[this.getName()] || this.getDefault();
return typeof value === 'object' ? JSON.stringify(value) : value;
}
toggleVarDropdown() {
if (!this.shouldShowVarDropdown()) return;
this.setState(prevState => ({ showDropdown: !prevState.showDropdown }));
}
toggleAuthDropdown() {
this.setState(prevState => ({ showAuthDropdown: !prevState.showAuthDropdown }));
}
static renderAuthDropdown() {
return (
<div
className={classNames('ns-popover-dropdown-theme', 'ns-popover-bottom-placement', 'ns-popover-right-align')}
style={{ position: 'absolute' }}
>
<div className="ns-popover-tooltip" id="loginDropdown">
<div className="ns-triangle" />
<div className="triangle" />
<div className="pad">
<div className="text-center">
Authenticate to personalize this page
<a className={classNames('btn', 'btn-primary')} href="/oauth" target="_self">
Authenticate
</a>
</div>
</div>
</div>
</div>
);
}
renderVarDropdown() {
return (
<select onChange={this.onChange} value={this.props.selected}>
{this.props.user.keys.map(key => (
<option key={key.name} value={key.name}>
{key.name}
</option>
))}
</select>
);
}
render() {
const { user, selected } = this.props;
if (Array.isArray(user.keys) && user.keys.length) {
return (
<span>
{!this.state.showDropdown && (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<span className="variable-underline" onClick={this.toggleVarDropdown}>
{this.getValue(selected)}
</span>
)}
{this.state.showDropdown && this.renderVarDropdown()}
</span>
);
}
// If default is shown and project has oauth, display login dropdown
if (this.getValue() === this.getDefault() && this.props.oauth) {
return (
<span>
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<span className="variable-underline" onClick={this.toggleAuthDropdown}>
{this.getValue()}
</span>
{this.state.showAuthDropdown && Variable.renderAuthDropdown()}
</span>
);
}
return <span>{this.getValue()}</span>;
}
}
Variable.propTypes = {
changeSelected: PropTypes.func.isRequired,
defaults: PropTypes.arrayOf(PropTypes.shape({ default: PropTypes.string, name: PropTypes.string })).isRequired,
name: PropTypes.string,
oauth: PropTypes.bool,
selected: PropTypes.string.isRequired,
user: PropTypes.shape({
keys: PropTypes.array,
}).isRequired,
variable: PropTypes.string,
};
Variable.defaultProps = {
oauth: false,
};
/* istanbul ignore next */
// eslint-disable-next-line react/display-name
module.exports = props => (
<VariablesContext.Consumer>
{({ user, defaults }) => (
<OauthContext.Consumer>
{oauth => (
<SelectedAppContext.Consumer>
{({ selected, changeSelected }) => (
<Variable
{...props}
changeSelected={changeSelected}
defaults={defaults}
oauth={oauth}
selected={selected}
user={user}
/>
)}
</SelectedAppContext.Consumer>
)}
</OauthContext.Consumer>
)}
</VariablesContext.Consumer>
);
module.exports.Variable = Variable;
// Regex to match the following:
// - \<<apiKey\>> - escaped variables
// - <<apiKey>> - regular variables
// - <<glossary:glossary items>> - glossary
module.exports.VARIABLE_REGEXP = /(?:\\)?<<((?:(?![\r\n])[-_\p{L}:.\s\d])+)(?:\\)?>>/iu.source;
/**
* @example `{user.api_key}`
* @example `{user.apiKeY}`
* @example `{user.片仮名}`
* @example `{user.P2P}`
* @see {@link https://stackoverflow.com/a/6926184}
*/
module.exports.MDX_VARIABLE_REGEXP =
/(\\)?\{user.([$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\u200C\u200D]*)(\\)?\}/iu.source;
module.exports.VariablesContext = VariablesContext;
module.exports.SelectedAppContext = SelectedAppContext;