Merge pull request #1194 from ethereum/reserved-names

Catch reserved keywords and display modal
pull/5370/head
David Disu 4 years ago committed by GitHub
commit dd6d025817
  1. 18
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

@ -97,7 +97,8 @@ export const FileExplorer = (props: FileExplorerProps) => {
modals: [],
toasterMsg: '',
mouseOverElement: null,
showContextMenu: false
showContextMenu: false,
reservedKeywords: [name, 'gist-']
})
const [fileSystem, dispatch] = useReducer(fileSystemReducer, fileSystemInitialState)
const editRef = useRef(null)
@ -222,6 +223,11 @@ export const FileExplorer = (props: FileExplorerProps) => {
return keyPath.join('/')
}
const hasReservedKeyword = (content: string): boolean => {
if (state.reservedKeywords.findIndex(value => content.startsWith(value)) !== -1) return true
else return false
}
const createNewFile = async (newFilePath: string) => {
const fileManager = state.fileManager
@ -584,8 +590,17 @@ export const FileExplorer = (props: FileExplorerProps) => {
modal('Validation Error', 'Special characters are not allowed', 'OK', () => {})
} else {
if (state.focusEdit.isNew) {
if (hasReservedKeyword(content)) {
removeInputField(parentFolder)(dispatch)
modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, 'Close', () => {})
} else {
state.focusEdit.type === 'file' ? createNewFile(joinPath(parentFolder, content)) : createNewFolder(joinPath(parentFolder, content))
removeInputField(parentFolder)(dispatch)
}
} else {
if (hasReservedKeyword(content)) {
editRef.current.textContent = state.focusEdit.lastEdit
modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, 'Close', () => {})
} else {
const oldPath: string = state.focusEdit.element
const oldName = extractNameFromKey(oldPath)
@ -594,6 +609,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
editRef.current.textContent = extractNameFromKey(oldPath)
renamePath(oldPath, newPath)
}
}
setState(prevState => {
return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } }
})

Loading…
Cancel
Save