Repository navigation
Expand file tree
/
Copy pathinput.tsx
More file actions
462 lines (428 loc) · 13.3 KB
/
Copy pathinput.tsx
File metadata and controls
462 lines (428 loc) · 13.3 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import * as C from '@/constants'
import * as React from 'react'
import * as Constants from '@/constants/crypto'
import * as FsConstants from '@/constants/fs'
import type * as T from '@/constants/types'
import * as Kb from '@/common-adapters'
import type {IconType} from '@/common-adapters/icon.constants-gen'
import capitalize from 'lodash/capitalize'
import {pickFiles} from '@/util/pick-files'
type CommonProps = {
operation: T.Crypto.Operations
}
type TextProps = CommonProps & {
onChangeText: (text: string) => void
onSetFile: (path: string) => void
value: string
}
type FileProps = CommonProps & {
path: string
size?: number
onClearFiles: () => void
}
type DragAndDropProps = CommonProps & {
prompt: string
children: React.ReactNode
}
type RunOperationProps = CommonProps & {
children?: React.ReactNode
}
// Tese magic numbers set the width of the single line `textarea` such that the
// placeholder text is visible and pushes the "browse" button far enough to the
// right to be exactly one empty character with from the end of the placeholder text
const operationToEmptyInputWidth = {
[Constants.Operations.Encrypt]: 207,
[Constants.Operations.Decrypt]: 320,
[Constants.Operations.Sign]: 207,
[Constants.Operations.Verify]: 342,
}
const inputTextType = new Map([
['decrypt', 'cipher'],
['encrypt', 'plain'],
['sign', 'plain'],
['verify', 'cipher'],
] as const)
const inputPlaceholder = new Map([
[
'decrypt',
C.isMobile ? 'Enter text to decrypt' : 'Enter ciphertext, drop an encrypted file or folder, or',
],
['encrypt', C.isMobile ? 'Enter text to encrypt' : 'Enter text, drop a file or folder, or'],
['sign', C.isMobile ? 'Enter text to sign' : 'Enter text, drop a file or folder, or'],
[
'verify',
C.isMobile ? 'Enter text to verify' : 'Enter a signed message, drop a signed file or folder, or',
],
] as const)
/*
* Before user enters text:
* - Single line input
* - Browse file button
*
* Afte user enters text:
* - Multiline input
* - Clear button
*/
const TextInput = (props: TextProps) => {
const {value, operation, onChangeText, onSetFile} = props
const textType = inputTextType.get(operation)
const placeholder = inputPlaceholder.get(operation)
const emptyWidth = operationToEmptyInputWidth[operation]
// When 'browse file' is show, focus input by clicking anywhere in the input box
// (despite the input being one line tall)
const inputRef = React.useRef<Kb.PlainInput>(null)
const onFocusInput = () => {
inputRef.current?.focus()
}
const onOpenFile = () => {
const f = async () => {
// On Windows and Linux only files will be able to be selected. Their native pickers don't allow for selecting both directories and files at once.
// To set a directory as input, a user will need to drag the directory into Keybase.
const filePaths = await pickFiles({
allowDirectories: C.isDarwin,
buttonLabel: 'Select',
})
if (!filePaths.length) return
const path = filePaths[0]!
onSetFile(path)
}
C.ignorePromise(f())
}
// Styling
const rowsMax = Kb.Styles.isMobile ? undefined : value ? undefined : 1
const growAndScroll = !Kb.Styles.isMobile
const inputStyle = Kb.Styles.collapseStyles([
styles.input,
value ? styles.inputFull : styles.inputEmpty,
!value && !Kb.Styles.isMobile && {width: emptyWidth},
])
const inputContainerStyle = value ? styles.inputContainer : styles.inputContainerEmpty
const browseButton = value ? null : (
<Kb.Text type="BodyPrimaryLink" style={styles.browseFile} onClick={onOpenFile}>
browse
</Kb.Text>
)
const clearButton = value ? (
<Kb.Box2 direction="vertical" style={styles.clearButtonInput}>
<Kb.Text type="BodySmallPrimaryLink" onClick={() => onChangeText('')}>
Clear
</Kb.Text>
</Kb.Box2>
) : null
return (
<Kb.Box onClick={onFocusInput} style={styles.containerInputFocus}>
<Kb.Box2 direction="vertical" fullWidth={true} fullHeight={true} style={styles.commonContainer}>
<Kb.Box2
direction={Kb.Styles.isMobile ? 'vertical' : 'horizontal'}
alignItems="flex-start"
alignSelf="flex-start"
fullWidth={Kb.Styles.isMobile || !!value}
fullHeight={Kb.Styles.isMobile || !!value}
style={styles.inputAndFilePickerContainer}
>
<Kb.NewInput
value={value}
placeholder={placeholder}
multiline={true}
autoFocus={true}
allowKeyboardEvents={true}
hideBorder={true}
rowsMax={rowsMax}
growAndScroll={growAndScroll}
padding="tiny"
containerStyle={inputContainerStyle}
style={inputStyle}
textType={textType === 'cipher' ? 'Terminal' : 'Body'}
autoCorrect={textType !== 'cipher'}
spellCheck={textType !== 'cipher'}
onChangeText={onChangeText}
ref={inputRef}
/>
{!Kb.Styles.isMobile && browseButton}
</Kb.Box2>
</Kb.Box2>
{!Kb.Styles.isMobile && clearButton}
</Kb.Box>
)
}
const inputFileIcon = new Map([
['decrypt', 'icon-file-saltpack-64'],
['encrypt', 'icon-file-64'],
['sign', 'icon-file-64'],
['verify', 'icon-file-saltpack-64'],
] as const)
const FileInput = (props: FileProps) => {
const {path, size, operation} = props
const fileIcon = inputFileIcon.get(operation) as IconType
const waiting = C.Waiting.useAnyWaiting(Constants.waitingKey)
return (
<Kb.Box2
direction="vertical"
fullWidth={true}
fullHeight={true}
alignItems="stretch"
style={styles.commonContainer}
>
<Kb.Box2 direction="horizontal" fullHeight={true} fullWidth={true}>
<Kb.Box2 direction="horizontal" fullWidth={true} alignItems="center" style={styles.fileContainer}>
<Kb.Icon type={fileIcon} sizeType="Huge" />
<Kb.Box2 direction="vertical">
<Kb.Text type="BodySemibold">{path}</Kb.Text>
{size ? (
<Kb.Text type="BodySmallSemibold">{FsConstants.humanReadableFileSize(size)}</Kb.Text>
) : null}
</Kb.Box2>
</Kb.Box2>
{path && !waiting && (
<Kb.Box2 direction="vertical" style={styles.clearButtonInput}>
<Kb.Text
type="BodySmallPrimaryLink"
onClick={() => props.onClearFiles()}
style={styles.clearButtonInput}
>
Clear
</Kb.Text>
</Kb.Box2>
)}
</Kb.Box2>
</Kb.Box2>
)
}
export const Input = (props: CommonProps) => {
const {operation} = props
const {input: _input, inputType} = C.useCryptoState(
C.useShallow(s => {
const o = s[operation]
const {input, inputType} = o
return {input, inputType}
})
)
const input = _input.stringValue()
const [inputValue, setInputValue] = React.useState(input)
const setInput = C.useCryptoState(s => s.dispatch.setInput)
const clearInput = C.useCryptoState(s => s.dispatch.clearInput)
const onSetInput = (type: T.Crypto.InputTypes, newValue: string) => {
setInput(operation, type, newValue)
}
const onClearInput = () => {
clearInput(operation)
}
return inputType === 'file' ? (
<FileInput
operation={operation}
path={input}
onClearFiles={() => {
setInputValue('')
onClearInput()
}}
/>
) : (
<TextInput
operation={operation}
value={inputValue}
onSetFile={path => {
onSetInput('file', path)
}}
onChangeText={text => {
setInputValue(text)
onSetInput('text', text)
}}
/>
)
}
const allowInputFolders = new Map([
['decrypt', false],
['encrypt', true],
['sign', true],
['verify', false],
] as const)
export const DragAndDrop = (props: DragAndDropProps) => {
const {prompt, children, operation} = props
const inProgress = C.useCryptoState(s => s[operation].inProgress)
const setInput = C.useCryptoState(s => s.dispatch.setInput)
const onAttach = (localPaths: Array<string>) => {
const path = localPaths[0]
setInput(operation, 'file', path ?? '')
}
const allowFolders = allowInputFolders.get(operation) as boolean
return (
<Kb.Box2 direction="vertical" fullWidth={true} fullHeight={true}>
<Kb.DragAndDrop
disabled={inProgress}
allowFolders={allowFolders}
fullHeight={true}
fullWidth={true}
onAttach={onAttach}
prompt={prompt}
>
{children}
</Kb.DragAndDrop>
</Kb.Box2>
)
}
export const OperationBanner = (props: CommonProps) => {
const {operation} = props
const infoMessage = Constants.infoMessage[operation]
const {errorMessage: _errorMessage, warningMessage: _warningMessage} = C.useCryptoState(
C.useShallow(s => {
const {errorMessage, warningMessage} = s[operation]
return {errorMessage, warningMessage}
})
)
const errorMessage = _errorMessage.stringValue()
const warningMessage = _warningMessage.stringValue()
if (!errorMessage && !warningMessage) {
return (
<Kb.Banner color="grey">
<Kb.BannerParagraph bannerColor="grey" content={infoMessage} />
</Kb.Banner>
)
}
return (
<>
{errorMessage ? (
<Kb.Banner color="red">
<Kb.BannerParagraph bannerColor="red" content={errorMessage} />
</Kb.Banner>
) : null}
{warningMessage ? (
<Kb.Banner color="yellow">
<Kb.BannerParagraph bannerColor="yellow" content={warningMessage} />
</Kb.Banner>
) : null}
</>
)
}
// Mobile only
export const InputActionsBar = (props: RunOperationProps) => {
const {operation, children} = props
const waitingKey = Constants.waitingKey
const operationTitle = capitalize(operation)
const runTextOperation = C.useCryptoState(s => s.dispatch.runTextOperation)
const onRunOperation = () => {
runTextOperation(operation)
}
return Kb.Styles.isMobile ? (
<Kb.Box2
direction="vertical"
fullWidth={true}
gap={Kb.Styles.isTablet ? 'small' : 'tiny'}
style={styles.inputActionsBarContainer}
>
{children}
<Kb.WaitingButton
mode="Primary"
waitingKey={waitingKey}
label={operationTitle}
fullWidth={true}
onClick={onRunOperation}
/>
</Kb.Box2>
) : null
}
const styles = Kb.Styles.styleSheetCreate(
() =>
({
browseFile: {
flexShrink: 0,
},
clearButtonInput: {
alignSelf: 'flex-start',
flexShrink: 1,
padding: Kb.Styles.globalMargins.tiny,
},
commonContainer: {
...Kb.Styles.globalStyles.flexGrow,
...Kb.Styles.globalStyles.positionRelative,
},
containerInputFocus: Kb.Styles.platformStyles({
common: {
...Kb.Styles.globalStyles.flexGrow,
...Kb.Styles.globalStyles.fullHeight,
display: 'flex',
},
isMobile: {
flexShrink: 1,
// Give space on mobile for Recipients divider
marginTop: 1,
},
}),
fileContainer: {
alignSelf: 'flex-start',
...Kb.Styles.padding(Kb.Styles.globalMargins.small),
},
hidden: {
display: 'none',
},
input: Kb.Styles.platformStyles({
common: {
color: Kb.Styles.globalColors.black,
},
isMobile: {
...Kb.Styles.globalStyles.fullHeight,
},
}),
inputActionsBarContainer: Kb.Styles.platformStyles({
isMobile: {
...Kb.Styles.padding(Kb.Styles.globalMargins.small),
alignItems: 'flex-start',
backgroundColor: Kb.Styles.globalColors.blueGrey,
},
}),
inputAndFilePickerContainer: Kb.Styles.platformStyles({
isElectron: {
paddingBottom: 0,
paddingLeft: Kb.Styles.globalMargins.tiny,
paddingRight: 0,
paddingTop: Kb.Styles.globalMargins.tiny,
},
}),
inputContainer: Kb.Styles.platformStyles({
isElectron: {
// We want the immediate container not to overflow, so we tell it be height: 100% to match the parent
...Kb.Styles.globalStyles.fullHeight,
alignItems: 'stretch',
padding: 0,
},
isMobile: {
...Kb.Styles.globalStyles.fullHeight,
...Kb.Styles.padding(0),
},
}),
inputContainerEmpty: Kb.Styles.platformStyles({
isElectron: {
...Kb.Styles.padding(0),
},
isMobile: {
...Kb.Styles.globalStyles.fullHeight,
...Kb.Styles.padding(0),
},
}),
inputEmpty: Kb.Styles.platformStyles({
isElectron: {
...Kb.Styles.padding(0),
minHeight: 'initial',
overflowY: 'hidden',
},
isMobile: {
paddingLeft: Kb.Styles.globalMargins.xsmall,
paddingRight: Kb.Styles.globalMargins.xsmall,
paddingTop: Kb.Styles.globalMargins.xsmall,
},
}),
inputFull: Kb.Styles.platformStyles({
common: {
...Kb.Styles.padding(0),
},
isElectron: {
paddingRight: 46,
},
isMobile: {
paddingBottom: Kb.Styles.globalMargins.xsmall,
paddingLeft: Kb.Styles.globalMargins.xsmall,
paddingRight: Kb.Styles.globalMargins.xsmall,
paddingTop: Kb.Styles.globalMargins.xsmall,
},
}),
}) as const
)