From a9e35b697272dae15877df101c23b26a12777f0a Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 19 Aug 2021 17:27:01 +0100 Subject: [PATCH] fileRemoved event listener --- .../workspace/src/lib/actions/workspace.ts | 49 +++++++------- .../workspace/src/lib/reducers/workspace.ts | 67 ++++++++++++++++--- .../workspace/src/lib/remix-ui-workspace.tsx | 2 +- 3 files changed, 80 insertions(+), 38 deletions(-) diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index 2e9fd350de..79842bde73 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -79,6 +79,13 @@ const folderAddedSuccess = (folderPath: string) => { } } +const fileRemovedSuccess = (removePath: string) => { + return { + type: 'FILE_REMOVED_SUCCESS', + payload: removePath + } +} + const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true, template: 'gist-template' | 'code-template' | 'default-template' = 'default-template') => { if (!workspaceName) throw new Error('workspace name cannot be empty') if (checkSpecialChars(workspaceName) || checkSlash(workspaceName)) throw new Error('special characters are not allowed') @@ -191,7 +198,7 @@ const getWorkspaces = async (): Promise | undefined => { return workspaces } catch (e) { - // modalDialogCustom.alert('Workspaces have not been created on your system. Please use "Migrate old filesystem to workspace" on the home page to transfer your files or start by creating a new workspace in the File Explorers.') + dispatch(displayNotification('Workspaces', 'Workspaces have not been created on your system. Please use "Migrate old filesystem to workspace" on the home page to transfer your files or start by creating a new workspace in the File Explorers.', 'OK', null, () => { dispatch(hideNotification()) }, null)) console.log(e) } } @@ -204,6 +211,10 @@ const listenOnEvents = (provider) => { provider.event.on('folderAdded', async (folderPath) => { await executeEvent('folderAdded', folderPath) }) + + provider.event.on('fileRemoved', async (removePath) => { + await executeEvent('fileRemoved', removePath) + }) } export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.Dispatch) => { @@ -234,10 +245,6 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React. } listenOnEvents(provider) - - // provider.event.on('fileRemoved', async (removePath) => { - // await executeEvent('fileRemoved', removePath) - // }) // provider.event.on('fileRenamed', async (oldPath) => { // await executeEvent('fileRenamed', oldPath) // }) @@ -323,19 +330,9 @@ const folderAdded = async (folderPath: string) => { await dispatch(folderAddedSuccess(folderPath)) } -// const folderAdded = async (folderPath: string) => { -// if (extractParentFromKey(folderPath) === '/.workspaces') return -// const path = extractParentFromKey(folderPath) || provider.workspace || provider.type || '' -// const data = await fetchDirectoryContent(provider, path) - -// await dispatch(folderAddedSuccess(path, data)) -// } - -// const fileRemoved = async (removePath: string) => { -// const path = extractParentFromKey(removePath) || provider.workspace || provider.type || '' - -// await dispatch(fileRemovedSuccess(path, removePath)) -// } +const fileRemoved = async (removePath: string) => { + await dispatch(fileRemovedSuccess(removePath)) +} // const fileRenamed = async (oldPath: string) => { // const path = extractParentFromKey(oldPath) || provider.workspace || provider.type || '' @@ -376,15 +373,15 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove } break - // case 'fileRemoved': - // await fileRemoved(path) - // delete pendingEvents[eventName + path] - // if (queuedEvents.length) { - // const next = queuedEvents.pop() + case 'fileRemoved': + await fileRemoved(path) + delete pendingEvents[eventName + path] + if (queuedEvents.length) { + const next = queuedEvents.pop() - // await executeEvent(next.eventName, next.path) - // } - // break + await executeEvent(next.eventName, next.path) + } + break // case 'fileRenamed': // await fileRenamed(path) diff --git a/libs/remix-ui/workspace/src/lib/reducers/workspace.ts b/libs/remix-ui/workspace/src/lib/reducers/workspace.ts index 561386a0b2..d5132402d8 100644 --- a/libs/remix-ui/workspace/src/lib/reducers/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/reducers/workspace.ts @@ -101,7 +101,13 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...state, browser: { ...state.browser, - isRequesting: true, + isRequesting: state.mode === 'browser', + isSuccessful: false, + error: null + }, + localhost: { + ...state.localhost, + isRequesting: state.mode === 'localhost', isSuccessful: false, error: null } @@ -115,7 +121,14 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...state, browser: { ...state.browser, - files: fetchDirectoryContent(payload.files, payload.path), + files: state.mode === 'browser' ? fetchDirectoryContent(payload.files, payload.path) : state.browser.files, + isRequesting: false, + isSuccessful: true, + error: null + }, + localhost: { + ...state.localhost, + files: state.mode === 'localhost' ? fetchDirectoryContent(payload.files, payload.path) : state.localhost.files, isRequesting: false, isSuccessful: true, error: null @@ -130,7 +143,13 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...state.browser, isRequesting: false, isSuccessful: false, - error: action.payload + error: state.mode === 'browser' ? action.payload : null + }, + localhost: { + ...state.localhost, + isRequesting: false, + isSuccessful: false, + error: state.mode === 'localhost' ? action.payload : null } } } @@ -165,13 +184,13 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...state, browser: { ...state.browser, - files: fileAdded(state, payload), - expandPath: [...new Set([...state.browser.expandPath, payload])] + files: state.mode === 'browser' ? fileAdded(state, payload) : state.browser.files, + expandPath: state.mode === 'browser' ? [...new Set([...state.browser.expandPath, payload])] : state.browser.expandPath }, localhost: { ...state.localhost, - files: fileAdded(state, payload), - expandPath: [...new Set([...state.localhost.expandPath, payload])] + files: state.mode === 'localhost' ? fileAdded(state, payload) : state.localhost.files, + expandPath: state.mode === 'localhost' ? [...new Set([...state.localhost.expandPath, payload])] : state.localhost.expandPath } } } @@ -183,13 +202,31 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...state, browser: { ...state.browser, - files: folderAdded(state, payload), - expandPath: [...new Set([...state.browser.expandPath, payload])] + files: state.mode === 'browser' ? folderAdded(state, payload) : state.browser.files, + expandPath: state.mode === 'browser' ? [...new Set([...state.browser.expandPath, payload])] : state.browser.expandPath }, localhost: { ...state.localhost, - files: folderAdded(state, payload), - expandPath: [...new Set([...state.localhost.expandPath, payload])] + files: state.mode === 'localhost' ? folderAdded(state, payload) : state.localhost.files, + expandPath: state.mode === 'localhost' ? [...new Set([...state.localhost.expandPath, payload])] : state.localhost.expandPath + } + } + } + + case 'FILE_REMOVED_SUCCESS': { + const payload = action.payload as string + + return { + ...state, + browser: { + ...state.browser, + files: state.mode === 'browser' ? fileRemoved(state, payload) : state.browser.files, + expandPath: state.mode === 'browser' ? [...new Set([...state.browser.expandPath, payload])] : state.browser.expandPath + }, + localhost: { + ...state.localhost, + files: state.mode === 'localhost' ? fileRemoved(state, payload) : state.localhost.files, + expandPath: state.mode === 'localhost' ? [...new Set([...state.localhost.expandPath, payload])] : state.localhost.expandPath } } } @@ -270,6 +307,14 @@ const folderAdded = (state: BrowserState, path: string): { [x: string]: Record } => { + let files = state.mode === 'browser' ? state.browser.files : state.localhost.files + const _path = splitPath(state, path) + + files = _.unset(files, _path) + return files +} + const splitPath = (state: BrowserState, path: string): string[] | string => { const root = state.mode === 'browser' ? state.browser.currentWorkspace : 'localhost' 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 c1ff44d550..f09e111c0a 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -29,7 +29,7 @@ export function Workspace (props: WorkspaceProps) { useEffect(() => { if (global.fs.browser.currentWorkspace) { setCurrentWorkspace(global.fs.browser.currentWorkspace) - // global.dispatchFetchDirectory(global.fs.browser.currentWorkspace) + global.dispatchFetchDirectory(global.fs.browser.currentWorkspace) } }, [global.fs.browser.currentWorkspace])