diff --git a/libs/remix-ui/workspace/src/lib/actions/gist-handler.ts b/libs/remix-ui/workspace/src/lib/actions/gist-handler.ts index 733ffede34..5df4ad76c7 100644 --- a/libs/remix-ui/workspace/src/lib/actions/gist-handler.ts +++ b/libs/remix-ui/workspace/src/lib/actions/gist-handler.ts @@ -1,73 +1,55 @@ // var modalDialogCustom = require('../app/ui/modal-dialog-custom') -var request = require('request') +import * as request from 'request' -// Allowing window to be overriden for testing -function GistHandler (_window) { - if (_window !== undefined) { - // modalDialogCustom = _window - } +export class GistHandler { + handleLoad (params) { + let loadingFromGist = false + let gistId - this.handleLoad = function (params, cb) { - if (!cb) cb = () => {} - var loadingFromGist = false - var gistId - if (params.gist === '') { + if (params.gist) { loadingFromGist = true - // modalDialogCustom.prompt('Load a Gist', 'Enter the ID of the Gist or URL you would like to load.', null, (target) => { - if (target !== '') { - gistId = getGistId(target) - if (gistId) { - cb(gistId) - } else { - // modalDialogCustom.alert('Gist load error', 'Error while loading gist. Please provide a valid Gist ID or URL.') - } - } - }) - return loadingFromGist } else { gistId = params.gist loadingFromGist = !!gistId } - if (loadingFromGist) { - cb(gistId) - } - return loadingFromGist + + return gistId } - function getGistId (str) { - var idr = /[0-9A-Fa-f]{8,}/ - var match = idr.exec(str) + getGistId (str: string) { + const idr = /[0-9A-Fa-f]{8,}/ + const match = idr.exec(str) + return match ? match[0] : null } - this.loadFromGist = (params, fileManager) => { - const self = this - return self.handleLoad(params, function (gistId) { - request.get({ - url: `https://api.github.com/gists/${gistId}`, - json: true - }, async (error, response, data = {}) => { - if (error || !data.files) { - // modalDialogCustom.alert('Gist load error', error || data.message) - return - } - const obj = {} - Object.keys(data.files).forEach((element) => { - const path = element.replace(/\.\.\./g, '/') + loadFromGist (params, fileManager) { + const gistId = this.handleLoad(params) - obj['/' + 'gist-' + gistId + '/' + path] = data.files[element] - }) - fileManager.setBatchFiles(obj, 'workspace', true, (errorLoadingFile) => { - if (!errorLoadingFile) { - const provider = fileManager.getProvider('workspace') - provider.lastLoadedGistId = gistId - } else { - // modalDialogCustom.alert('Gist load error', errorLoadingFile.message || errorLoadingFile) - } - }) + request.get({ + url: `https://api.github.com/gists/${gistId}`, + json: true + }, async (error, response, data = {}) => { + if (error || !data.files) { + // modalDialogCustom.alert('Gist load error', error || data.message) + return + } + const obj = {} + + Object.keys(data.files).forEach((element) => { + const path = element.replace(/\.\.\./g, '/') + + obj['/' + 'gist-' + gistId + '/' + path] = data.files[element] + }) + fileManager.setBatchFiles(obj, 'workspace', true, (errorLoadingFile) => { + if (!errorLoadingFile) { + const provider = fileManager.getProvider('workspace') + + provider.lastLoadedGistId = gistId + } else { + // modalDialogCustom.alert('Gist load error', errorLoadingFile.message || errorLoadingFile) + } }) }) } } - -module.exports = GistHandler diff --git a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx index df685d448b..ab26239d8c 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -35,7 +35,6 @@ export const Workspace = (props: WorkspaceProps) => { const [state, setState] = useState({ workspaces: [], reset: false, - currentWorkspace: NO_WORKSPACE, hideRemixdExplorer: true, displayNewFile: false, externalUploads: null, @@ -53,7 +52,7 @@ export const Workspace = (props: WorkspaceProps) => { loadingLocalhost: false, toasterMsg: '' }) - const [currentWorkspace, setCurrentWorkspace] = useState('') + const [currentWorkspace, setCurrentWorkspace] = useState(NO_WORKSPACE) const [fs, dispatch] = useReducer(browserReducer, browserInitialState) useEffect(() => { @@ -102,14 +101,14 @@ export const Workspace = (props: WorkspaceProps) => { } props.request.getCurrentWorkspace = () => { - return { name: state.currentWorkspace, isLocalhost: state.currentWorkspace === LOCALHOST, absolutePath: `${props.workspace.workspacesPath}/${state.currentWorkspace}` } + return { name: currentWorkspace, isLocalhost: currentWorkspace === LOCALHOST, absolutePath: `${props.workspace.workspacesPath}/${currentWorkspace}` } } const localhostDisconnect = () => { - if (state.currentWorkspace === LOCALHOST) setWorkspace(props.workspaces.length > 0 ? props.workspaces[0] : NO_WORKSPACE) + if (currentWorkspace === LOCALHOST) setWorkspace(props.workspaces.length > 0 ? props.workspaces[0] : NO_WORKSPACE) // This should be removed some time after refactoring: https://github.com/ethereum/remix-project/issues/1197 else { - setWorkspace(state.currentWorkspace) // Useful to switch to last selcted workspace when remixd is disconnected + setWorkspace(currentWorkspace) // Useful to switch to last selcted workspace when remixd is disconnected props.fileManager.setMode('browser') } } @@ -161,7 +160,7 @@ export const Workspace = (props: WorkspaceProps) => { const workspaceName = workspaceRenameInput.current.value try { - await props.renameWorkspace(state.currentWorkspace, workspaceName) + await props.renameWorkspace(currentWorkspace, workspaceName) setWorkspace(workspaceName) props.workspaceRenamed({ name: workspaceName }) } catch (e) { @@ -188,8 +187,8 @@ export const Workspace = (props: WorkspaceProps) => { const onFinishDeleteWorkspace = async () => { await props.fileManager.closeAllFiles() const workspacesPath = props.workspace.workspacesPath - props.browser.remove(workspacesPath + '/' + state.currentWorkspace) - const name = state.currentWorkspace + props.browser.remove(workspacesPath + '/' + currentWorkspace) + const name = currentWorkspace setWorkspace(NO_WORKSPACE) props.workspaceDeleted({ name }) } @@ -284,7 +283,7 @@ export const Workspace = (props: WorkspaceProps) => { return ( <> { state.modal.message } - + ) } @@ -324,7 +323,7 @@ export const Workspace = (props: WorkspaceProps) => { title='Create'>