@@ -3,15 +3,18 @@ import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
3
3
import { Stack } from "components/Stack/Stack"
4
4
import { ChangeEvent , FC , useState } from "react"
5
5
import Typography from "@material-ui/core/Typography"
6
+ import { allowedExtensions , isAllowedFile } from "util/templateVersion"
7
+ import { FileTree , validatePath } from "util/filetree"
6
8
7
9
export const CreateFileDialog : FC < {
8
10
onClose : ( ) => void
9
11
checkExists : ( path : string ) => boolean
10
12
onConfirm : ( path : string ) => void
11
13
open : boolean
12
- } > = ( { checkExists, onClose, onConfirm, open } ) => {
14
+ fileTree : FileTree
15
+ } > = ( { checkExists, onClose, onConfirm, open, fileTree } ) => {
13
16
const [ pathValue , setPathValue ] = useState ( "" )
14
- const [ error , setError ] = useState ( "" )
17
+ const [ error , setError ] = useState < string > ( )
15
18
const handleChange = ( event : ChangeEvent < HTMLInputElement > ) => {
16
19
setPathValue ( event . target . value )
17
20
}
@@ -24,7 +27,20 @@ export const CreateFileDialog: FC<{
24
27
setError ( "File already exists" )
25
28
return
26
29
}
30
+ if ( ! isAllowedFile ( pathValue ) ) {
31
+ const extensions = allowedExtensions . join ( ", " )
32
+ setError (
33
+ `This extension is not allowed. You only can create files with the following extensions: ${ extensions } .` ,
34
+ )
35
+ return
36
+ }
37
+ const pathError = validatePath ( pathValue , fileTree )
38
+ if ( pathError ) {
39
+ setError ( pathError )
40
+ return
41
+ }
27
42
onConfirm ( pathValue )
43
+ setError ( undefined )
28
44
setPathValue ( "" )
29
45
}
30
46
@@ -33,6 +49,7 @@ export const CreateFileDialog: FC<{
33
49
open = { open }
34
50
onClose = { ( ) => {
35
51
onClose ( )
52
+ setError ( undefined )
36
53
setPathValue ( "" )
37
54
} }
38
55
onConfirm = { handleConfirm }
@@ -42,10 +59,10 @@ export const CreateFileDialog: FC<{
42
59
confirmText = "Create"
43
60
title = "Create File"
44
61
description = {
45
- < Stack spacing = { 1 } >
62
+ < Stack >
46
63
< Typography >
47
64
Specify the path to a file to be created. This path can contain
48
- slashes too!
65
+ slashes too.
49
66
</ Typography >
50
67
< TextField
51
68
autoFocus
@@ -54,14 +71,18 @@ export const CreateFileDialog: FC<{
54
71
handleConfirm ( )
55
72
}
56
73
} }
74
+ error = { Boolean ( error ) }
57
75
helperText = { error }
58
76
name = "file-path"
59
77
autoComplete = "off"
60
78
id = "file-path"
61
- placeholder = "main .tf"
79
+ placeholder = "example .tf"
62
80
value = { pathValue }
63
81
onChange = { handleChange }
64
82
label = "File Path"
83
+ InputLabelProps = { {
84
+ shrink : true ,
85
+ } }
65
86
/>
66
87
</ Stack >
67
88
}
@@ -82,7 +103,12 @@ export const DeleteFileDialog: FC<{
82
103
open = { open }
83
104
onConfirm = { onConfirm }
84
105
title = "Delete File"
85
- description = { `Are you sure you want to delete "${ filename } "?` }
106
+ description = {
107
+ < >
108
+ Are you sure you want to delete < strong > { filename } </ strong > ? It will
109
+ be deleted permanently.
110
+ </ >
111
+ }
86
112
/>
87
113
)
88
114
}
@@ -93,9 +119,10 @@ export const RenameFileDialog: FC<{
93
119
checkExists : ( path : string ) => boolean
94
120
open : boolean
95
121
filename : string
96
- } > = ( { checkExists, onClose, onConfirm, open, filename } ) => {
122
+ fileTree : FileTree
123
+ } > = ( { checkExists, onClose, onConfirm, open, filename, fileTree } ) => {
97
124
const [ pathValue , setPathValue ] = useState ( filename )
98
- const [ error , setError ] = useState ( "" )
125
+ const [ error , setError ] = useState < string > ( )
99
126
const handleChange = ( event : ChangeEvent < HTMLInputElement > ) => {
100
127
setPathValue ( event . target . value )
101
128
}
@@ -108,7 +135,20 @@ export const RenameFileDialog: FC<{
108
135
setError ( "File already exists" )
109
136
return
110
137
}
138
+ if ( ! isAllowedFile ( pathValue ) ) {
139
+ const extensions = allowedExtensions . join ( ", " )
140
+ setError (
141
+ `This extension is not allowed. You only can rename files with the following extensions: ${ extensions } .` ,
142
+ )
143
+ return
144
+ }
145
+ const pathError = validatePath ( pathValue , fileTree )
146
+ if ( pathError ) {
147
+ setError ( pathError )
148
+ return
149
+ }
111
150
onConfirm ( pathValue )
151
+ setError ( undefined )
112
152
setPathValue ( "" )
113
153
}
114
154
@@ -117,33 +157,34 @@ export const RenameFileDialog: FC<{
117
157
open = { open }
118
158
onClose = { ( ) => {
119
159
onClose ( )
160
+ setError ( undefined )
120
161
setPathValue ( "" )
121
162
} }
122
163
onConfirm = { handleConfirm }
123
164
hideCancel = { false }
124
165
type = "success"
125
166
cancelText = "Cancel"
126
- confirmText = "Create "
167
+ confirmText = "Rename "
127
168
title = "Rename File"
128
169
description = {
129
- < Stack spacing = { 1 } >
130
- < Typography >
131
- Rename { `" ${ filename } "` } to something else. This path can contain
132
- slashes too!
133
- </ Typography >
170
+ < Stack >
171
+ < p >
172
+ Rename < strong > { filename } </ strong > to something else. This path can
173
+ contain slashes too!
174
+ </ p >
134
175
< TextField
135
176
autoFocus
136
177
onKeyDown = { ( event ) => {
137
178
if ( event . key === "Enter" ) {
138
179
handleConfirm ( )
139
180
}
140
181
} }
182
+ error = { Boolean ( error ) }
141
183
helperText = { error }
142
184
name = "file-path"
143
185
autoComplete = "off"
144
186
id = "file-path"
145
- placeholder = "main.tf"
146
- defaultValue = { filename }
187
+ placeholder = { filename }
147
188
value = { pathValue }
148
189
onChange = { handleChange }
149
190
label = "File Path"
0 commit comments