-
Notifications
You must be signed in to change notification settings - Fork 375
/
PublicKeyWithActions.js
228 lines (214 loc) · 6.36 KB
/
PublicKeyWithActions.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
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
import { ScrollView, TextInput, Platform, Clipboard, TouchableNativeFeedback, TouchableOpacity } from 'react-native'
import { btoa } from 'b64-lite'
import React, { PureComponent } from 'react'
import { Button, Flex, TextInputMultilineFix, Text } from './index'
import { RelayContext } from '../../relay'
import { colors } from '../../constants'
import {
extractPublicKeyFromId, makeShareableUrl,
shareLinkOther,
shareLinkSelf,
} from '../../helpers/contacts'
import { marginTop, padding, rounded, textTiny } from '../../styles'
import QRGenerator from './QRGenerator'
import { ThemeProvider, ButtonGroup } from 'react-native-elements'
const CopyKeyButton = ({ id }) => (
<ActionButton
icon='copy'
onPress={() => Clipboard.setString(id)}
label={'Copy the key'}
/>
)
const ShareKeyButton = ({ id, displayName, self }) => (
<ActionButton
icon='share'
onPress={() =>
self
? shareLinkSelf({ id, displayName })
: shareLinkOther({ id, displayName })
}
label={'Share the key'}
/>
)
const ActionButton = props => (
<Button
icon={props.icon}
background={colors.blue}
margin
padding
rounded={23}
height={24}
medium
middle
center
self='stretch'
onPress={props.onPress}
>
{props.label}
</Button>
)
const AddButton = ({ onPress }) => (
<ActionButton icon='plus' onPress={onPress} label={'Add this key'} />
)
export default class PublicKeyWithActions extends PureComponent {
static contextType = RelayContext
constructor (props) {
super(props)
const [initialKey, initialName] = props.navigation
? [
props.navigation.getParam('initialKey'),
props.navigation.getParam('initialName'),
]
: [props.initialKey, props.initialName]
const missingInitialData = props.initialKey === undefined
this.state = {
err: null,
contact: {
id: initialKey || '',
displayName: initialName || '',
displayStatus: '',
overrideDisplayName: '',
overrideDisplayStatus: '',
},
mode: props.mode || 'key',
}
if (missingInitialData && props.data !== undefined) {
try {
this.state.contact = {
...props.data,
id: extractPublicKeyFromId(props.data.id),
}
} catch (e) {
console.error(e)
}
}
}
onSubmit = async () => {
const input = {
contact: {
...this.state.contact,
id: btoa(`contact:${this.state.contact.id}`),
displayName: this.state.contact.displayName,
},
introText: '',
}
try {
await this.props.screenProps.context.mutations.contactRequest(input)
this.props.navigation.goBack(null)
} catch (err) {
this.setState({ err })
}
}
render () {
const {
navigation,
shareButton,
addButton,
copyButton,
readOnly,
self,
} = this.props
const {
contact: { id, displayName },
mode,
} = this.state
let errors = []
try {
errors = this.state.err.res.errors
} catch (e) {
// noop
}
const modeButtons = [
{ element: () => <Text icon={'edit'} big color={mode === 'key' ? colors.white : colors.grey1} /> },
{
element: () => <Text icon={'material-qrcode-scan'} big
color={mode === 'qrcode' ? colors.white : colors.grey1} />,
},
]
return (
<ScrollView>
<Flex.Rows style={[padding]} align='center'>
<Flex.Cols style={{ width: 330 }}>
<TextInput
placeholder={'Contact name (optional)'}
onChangeText={displayName =>
this.setState({ contact: { ...this.state.contact, displayName } })
}
value={displayName}
style={[
{
backgroundColor: colors.grey7,
color: colors.black,
textAlign: 'left',
flex: 1,
...(Platform.OS === 'web' ? { outline: 'none' } : {}),
},
padding,
rounded,
]}
/>
{/* TODO: Use a lighter button group impl? */}
{readOnly
? <ThemeProvider theme={{ colors: { primary: colors.blue } }}>
<ButtonGroup
onPress={() => this.setState({ mode: mode === 'key' ? 'qrcode' : 'key' })}
selectedIndex={mode === 'key' ? 0 : 1}
buttons={modeButtons}
containerStyle={{ height: 32, flex: 1 }}
selectedBackgroundColor={colors.green}
component={Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity}
/>
</ThemeProvider>
: null}
</Flex.Cols>
{(!readOnly || mode === 'key')
? <TextInputMultilineFix
style={[
{
width: 330,
height: 248,
backgroundColor: colors.grey7,
color: colors.black,
flexWrap: 'wrap',
fontFamily: Platform.OS === 'ios' ? 'Courier' : 'monospace',
},
textTiny,
padding,
marginTop,
rounded,
]}
multiline
placeholder='Type or copy/paste a berty user public key here'
value={id}
onChangeText={id =>
this.setState({ contact: { ...this.state.contact, id } })
}
editable={!readOnly}
selectTextOnFocus
/>
: null}
{readOnly && mode === 'qrcode'
? <QRGenerator
value={makeShareableUrl({ id, displayName })}
size={248}
style={[marginTop]} />
: null // TODO: implement camera
}
{shareButton ? (
<ShareKeyButton id={id} displayName={displayName} self={self} />
) : null}
{copyButton ? <CopyKeyButton id={id} /> : null}
{addButton ? (
<AddButton
id={id}
displayName={displayName}
navigation={navigation}
onPress={this.onSubmit}
/>
) : null}
{errors.map((err, i) => <Text multiline key={i}>{err.message}</Text>)}
</Flex.Rows>
</ScrollView>
)
}
}