-
Notifications
You must be signed in to change notification settings - Fork 490
/
AddByPathModal.js
120 lines (100 loc) · 3.42 KB
/
AddByPathModal.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
import React from 'react'
import PropTypes from 'prop-types'
import Button from '../../../components/button/Button.js'
import { Modal, ModalActions, ModalBody } from '../../../components/modal/Modal.js'
import { withTranslation } from 'react-i18next'
import * as isIPFS from 'is-ipfs'
import Icon from '../../../icons/StrokeDecentralization.js'
class AddByPathModal extends React.Component {
static propTypes = {
onCancel: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
className: PropTypes.string
}
static defaultProps = {
className: ''
}
state = {
path: '',
name: ''
}
validatePath = (p) => {
if (!p.startsWith('/ipfs/')) {
p = `/ipfs/${p}`
}
return isIPFS.ipfsPath(p)
}
onChange = (event) => {
const target = event.target
const value = target.value
const name = target.name
this.setState({ [name]: value })
}
onSubmit = () => {
let { path, name } = this.state
if (this.validatePath(path)) {
// avoid issues with paths by forcing a flat filename without leading/trailing spaces
name = name.replaceAll('/', '_').trim()
this.props.onSubmit(path, name)
}
}
onKeyPress = (event) => {
if (event.key === 'Enter') {
this.onSubmit()
}
}
get inputClass () {
if (this.state.path === '') {
return ''
}
if (this.validatePath(this.state.path)) {
return 'b--green-muted focus-outline-green'
}
return 'b--red-muted focus-outline-red'
}
get isDisabled () {
if (this.state.path === '') {
return true
}
return !this.validatePath(this.state.path)
}
render () {
const {
t, tReady, onCancel, onSubmit, className, ...props
} = this.props
const codeClass = 'w-90 mb1 pa1 tl bg-snow f7 charcoal-muted truncate'
return (
<Modal {...props} className={className} onCancel={onCancel}>
<ModalBody title={t('addByPathModal.title')} Icon={Icon}>
<div className='mb3 flex flex-column items-center'>
<p className='mt0 charcoal tl w-90'>{t('addByPathModal.description') + ' ' + t('addByPathModal.examples')}</p>
<code className={codeClass}>/ipfs/QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V</code>
<code className={codeClass}>QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB</code>
</div>
<input
onChange={this.onChange}
onKeyPress={this.onKeyPress}
value={this.state.value}
name='path'
required
placeholder={t('addByPathModal.importPathPlaceholder')}
className={`input-reset charcoal ba b--black-20 br1 pa2 mb2 db w-90 center focus-outline ${this.inputClass}`}
type='text' />
<input
onChange={this.onChange}
onKeyPress={this.onKeyPress}
value={this.state.name}
placeholder={t('addByPathModal.namePlaceholder')}
name='name'
className='input-reset charcoal ba b--black-20 br1 pa2 mb2 db w-90 center focus-outline'
type='text' />
</ModalBody>
<ModalActions>
<Button className='ma2 tc' bg='bg-gray' onClick={onCancel}>{t('actions.cancel')}</Button>
<Button className='ma2 tc' bg='bg-teal' disabled={this.isDisabled} onClick={this.onSubmit}>{t('app:actions.import')}</Button>
</ModalActions>
</Modal>
)
}
}
export default withTranslation('files')(AddByPathModal)