This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
generated_setting.tsx
105 lines (94 loc) · 3.15 KB
/
generated_setting.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import crypto from 'crypto';
import React from 'react';
import {FormattedMessage} from 'react-intl';
import SetByEnv from './set_by_env';
type Props = {
id: string;
label: React.ReactNode;
placeholder?: string;
value: string;
onChange: (id: string, s: string) => void;
disabled: boolean;
setByEnv: boolean;
disabledText?: React.ReactNode;
helpText: React.ReactNode;
regenerateText: React.ReactNode;
regenerateHelpText?: React.ReactNode;
}
export default class GeneratedSetting extends React.PureComponent<Props> {
public static get defaultProps() {
return {
disabled: false,
regenerateText: (
<FormattedMessage
id='admin.regenerate'
defaultMessage='Regenerate'
/>
),
};
}
private regenerate = (e: React.MouseEvent) => {
e.preventDefault();
this.props.onChange(this.props.id, crypto.randomBytes(256).toString('base64').substring(0, 32));
}
public render() {
let disabledText = null;
if (this.props.disabled && this.props.disabledText) {
disabledText = (
<div className='admin-console__disabled-text'>
{this.props.disabledText}
</div>
);
}
let regenerateHelpText = null;
if (this.props.regenerateHelpText) {
regenerateHelpText = (
<div className='help-text'>
{this.props.regenerateHelpText}
</div>
);
}
let text: React.ReactNode = this.props.value;
if (!text) {
text = (
<span className='placeholder-text'>{this.props.placeholder}</span>
);
}
return (
<div className='form-group'>
<label
className='control-label col-sm-4'
htmlFor={this.props.id}
>
{this.props.label}
</label>
<div className='col-sm-8'>
<div
className='form-control disabled'
id={this.props.id}
>
{text}
</div>
{disabledText}
<div className='help-text'>
{this.props.helpText}
</div>
<div className='help-text'>
<button
type='button'
className='btn btn-default'
onClick={this.regenerate}
disabled={this.props.disabled || this.props.setByEnv}
>
{this.props.regenerateText}
</button>
</div>
{regenerateHelpText}
{this.props.setByEnv ? <SetByEnv/> : null}
</div>
</div>
);
}
}