From 9edfd398c88b29393eba6f86a5e04f9f6c45a5be Mon Sep 17 00:00:00 2001 From: David Disu Date: Wed, 29 Jun 2022 11:45:01 +0100 Subject: [PATCH] Clone cleanup --- apps/remix-ide/src/app/files/dgitProvider.js | 13 ++-- .../workspace/src/lib/actions/index.ts | 5 +- .../workspace/src/lib/actions/payload.ts | 18 +++++ .../workspace/src/lib/actions/workspace.ts | 26 +++++-- .../workspace/src/lib/reducers/workspace.ts | 58 ++++++++++++++-- .../workspace/src/lib/remix-ui-workspace.tsx | 68 ++++++++++--------- 6 files changed, 135 insertions(+), 53 deletions(-) diff --git a/apps/remix-ide/src/app/files/dgitProvider.js b/apps/remix-ide/src/app/files/dgitProvider.js index d4a1a26c8c..8b5ce925c5 100644 --- a/apps/remix-ide/src/app/files/dgitProvider.js +++ b/apps/remix-ide/src/app/files/dgitProvider.js @@ -233,12 +233,11 @@ class DGitProvider extends Plugin { return this.calculateLocalStorage() } - async clone (input, workspaceName) { + async clone (input, workspaceName, workspaceExists = false) { const permission = await this.askUserPermission('clone', 'Import multiple files into your workspaces.') if (!permission) return false if (this.calculateLocalStorage() > 10000) throw new Error('The local storage of the browser is full.') - await this.call('filePanel', 'createWorkspace', workspaceName || `workspace_${Date.now()}`, true) - + if (!workspaceExists) await this.call('filePanel', 'createWorkspace', workspaceName || `workspace_${Date.now()}`, true) const cmd = { url: input.url, singleBranch: input.singleBranch, @@ -249,9 +248,11 @@ class DGitProvider extends Plugin { } const result = await git.clone(cmd) - setTimeout(async () => { - await this.call('fileManager', 'refresh') - }, 1000) + if (!workspaceExists) { + setTimeout(async () => { + await this.call('fileManager', 'refresh') + }, 1000) + } return result } diff --git a/libs/remix-ui/workspace/src/lib/actions/index.ts b/libs/remix-ui/workspace/src/lib/actions/index.ts index 460aa1d1f8..7a5387474b 100644 --- a/libs/remix-ui/workspace/src/lib/actions/index.ts +++ b/libs/remix-ui/workspace/src/lib/actions/index.ts @@ -33,9 +33,10 @@ const basicWorkspaceInit = async (workspaces: { name: string; isGitRepo: boolean } else { if (workspaces.length > 0) { const workspace = workspaces[workspaces.length - 1] + const workspaceName = (workspace || {}).name - workspaceProvider.setWorkspace(workspace.name) - plugin.setWorkspace({ name: (workspace || {}).name, isLocalhost: false }) + workspaceProvider.setWorkspace(workspaceName) + plugin.setWorkspace({ name: workspaceName, isLocalhost: false }) dispatch(setCurrentWorkspace(workspace)) } } diff --git a/libs/remix-ui/workspace/src/lib/actions/payload.ts b/libs/remix-ui/workspace/src/lib/actions/payload.ts index 67b45d01f5..b663f60cfe 100644 --- a/libs/remix-ui/workspace/src/lib/actions/payload.ts +++ b/libs/remix-ui/workspace/src/lib/actions/payload.ts @@ -239,3 +239,21 @@ export const fsInitializationCompleted = () => { type: 'FS_INITIALIZATION_COMPLETED' } } + +export const cloneRepositoryRequest = () => { + return { + type: 'CLONE_REPOSITORY_REQUEST' + } +} + +export const cloneRepositorySuccess = () => { + return { + type: 'CLONE_REPOSITORY_SUCCESS' + } +} + +export const cloneRepositoryFailed = () => { + return { + type: 'CLONE_REPOSITORY_FAILED' + } +} diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index b0a3531f1d..ee8ade4f2b 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -1,7 +1,7 @@ import React from 'react' import { bufferToHex, keccakFromString } from 'ethereumjs-util' import axios, { AxiosResponse } from 'axios' -import { addInputFieldSuccess, createWorkspaceError, createWorkspaceRequest, createWorkspaceSuccess, displayNotification, displayPopUp, fetchWorkspaceDirectoryError, fetchWorkspaceDirectoryRequest, fetchWorkspaceDirectorySuccess, hideNotification, setCurrentWorkspace, setDeleteWorkspace, setMode, setReadOnlyMode, setRenameWorkspace } from './payload' +import { addInputFieldSuccess, cloneRepositoryFailed, cloneRepositoryRequest, cloneRepositorySuccess, createWorkspaceError, createWorkspaceRequest, createWorkspaceSuccess, displayNotification, displayPopUp, fetchWorkspaceDirectoryError, fetchWorkspaceDirectoryRequest, fetchWorkspaceDirectorySuccess, hideNotification, setCurrentWorkspace, setDeleteWorkspace, setMode, setReadOnlyMode, setRenameWorkspace } from './payload' import { checkSlash, checkSpecialChars, createNonClashingTitle } from '@remix-ui/helper' import { JSONStandardInput, WorkspaceTemplate } from '../types' @@ -42,13 +42,13 @@ export const addInputField = async (type: 'file' | 'folder', path: string, cb?: return promise } -export const createWorkspace = async (workspaceName: string, workspaceTemplateName: WorkspaceTemplate, isEmpty = false, cb?: (err: Error, result?: string | number | boolean | Record) => void) => { +export const createWorkspace = async (workspaceName: string, workspaceTemplateName: WorkspaceTemplate, isEmpty = false, cb?: (err: Error, result?: string | number | boolean | Record) => void, isGitRepo: boolean = false) => { await plugin.fileManager.closeAllFiles() const promise = createWorkspaceTemplate(workspaceName, workspaceTemplateName) dispatch(createWorkspaceRequest(promise)) promise.then(async () => { - dispatch(createWorkspaceSuccess({ name: workspaceName, isGitRepo: false })) + dispatch(createWorkspaceSuccess({ name: workspaceName, isGitRepo })) await plugin.setWorkspace({ name: workspaceName, isLocalhost: false }) await plugin.setWorkspaces(await getWorkspaces()) await plugin.workspaceCreated(workspaceName) @@ -334,12 +334,26 @@ export const cloneRepository = async (url: string) => { const token = config.get('currentFile') const repoConfig = { url, token } const urlArray = url.split('/') - let repoName = urlArray[urlArray.length - 1] + let repoName = urlArray.length > 0 ? urlArray[urlArray.length - 1] : '' try { repoName = await createNonClashingTitle(repoName, plugin.fileManager) - await plugin.call('dGitProvider', 'clone', repoConfig, repoName) - await plugin.call('manager', 'activatePlugin', 'dgit') + await createWorkspace(repoName, 'blank', true, null, true) + const promise = plugin.call('dGitProvider', 'clone', repoConfig, repoName, true) + + dispatch(cloneRepositoryRequest()) + promise.then(async () => { + const isActive = await plugin.call('manager', 'isActive', 'dgit') + + if (!isActive) await plugin.call('manager', 'activatePlugin', 'dgit') + await fetchWorkspaceDirectory(repoName) + dispatch(cloneRepositorySuccess()) + }).catch((e) => { + dispatch(displayNotification('Clone Git Repository', 'An error occured: ' + e, 'Ok', null, async () => { + await deleteWorkspace(repoName) + dispatch(cloneRepositoryFailed()) + })) + }) } catch (e) { dispatch(displayPopUp('An error occured: ' + e)) } diff --git a/libs/remix-ui/workspace/src/lib/reducers/workspace.ts b/libs/remix-ui/workspace/src/lib/reducers/workspace.ts index 36b8497f70..39f56a202f 100644 --- a/libs/remix-ui/workspace/src/lib/reducers/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/reducers/workspace.ts @@ -18,6 +18,8 @@ export interface BrowserState { isSuccessfulDirectory: boolean, isRequestingWorkspace: boolean, isSuccessfulWorkspace: boolean, + isRequestingCloning: boolean, + isSuccessfulCloning: boolean, error: string, contextMenu: { registeredMenuItems: action[], @@ -53,8 +55,7 @@ export interface BrowserState { popup: string, focusEdit: string, focusElement: { key: string, type: 'file' | 'folder' | 'gist' }[], - initializingFS: boolean, - isGitRepo: boolean + initializingFS: boolean } export const browserInitialState: BrowserState = { @@ -67,6 +68,8 @@ export const browserInitialState: BrowserState = { isSuccessfulDirectory: false, isRequestingWorkspace: false, isSuccessfulWorkspace: false, + isRequestingCloning: false, + isSuccessfulCloning: false, error: null, contextMenu: { registeredMenuItems: [], @@ -102,8 +105,7 @@ export const browserInitialState: BrowserState = { popup: '', focusEdit: '', focusElement: [], - initializingFS: true, - isGitRepo: false + initializingFS: true } export const browserReducer = (state = browserInitialState, action: Action) => { @@ -451,14 +453,25 @@ export const browserReducer = (state = browserInitialState, action: Action) => { case 'RENAME_WORKSPACE': { const payload = action.payload as { oldName: string, workspaceName: string } - const workspaces = state.browser.workspaces.filter(({ name }) => name && (name !== payload.oldName)) + let renamedWorkspace + const workspaces = state.browser.workspaces.filter(({ name, isGitRepo }) => { + if (name && (name !== payload.oldName)) { + return true + } else { + renamedWorkspace = { + name: payload.workspaceName, + isGitRepo + } + return false + } + }) return { ...state, browser: { ...state.browser, currentWorkspace: payload.workspaceName, - workspaces: [...workspaces, payload.workspaceName], + workspaces: [...workspaces, renamedWorkspace], expandPath: [] } } @@ -597,6 +610,39 @@ export const browserReducer = (state = browserInitialState, action: Action) => { } } + case 'CLONE_REPOSITORY_REQUEST': { + return { + ...state, + browser: { + ...state.browser, + isRequestingCloning: true, + isSuccessfulCloning: false + } + } + } + + case 'CLONE_REPOSITORY_SUCCESS': { + return { + ...state, + browser: { + ...state.browser, + isRequestingCloning: false, + isSuccessfulCloning: true + } + } + } + + case 'CLONE_REPOSITORY_FAILED': { + return { + ...state, + browser: { + ...state.browser, + isRequestingCloning: false, + isSuccessfulCloning: false + } + } + } + case 'FS_INITIALIZATION_COMPLETED': { return { ...state, 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 0df9b8b53c..e911d9decf 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -290,39 +290,41 @@ export function Workspace () {
-
- { (global.fs.mode === 'browser') && (currentWorkspace !== NO_WORKSPACE) && - - } -
+ { global.fs.browser.isRequestingWorkspace || global.fs.browser.isRequestingCloning ?
+ :
+ { (global.fs.mode === 'browser') && (currentWorkspace !== NO_WORKSPACE) && + + } +
+ } { global.fs.localhost.isRequestingLocalhost ?
: