Commit 03c194dc authored by ioedeveloper's avatar ioedeveloper

FileAdded and Folder added event

parent 349f6113
...@@ -64,12 +64,16 @@ export const FileExplorer = (props: FileExplorerProps) => { ...@@ -64,12 +64,16 @@ export const FileExplorer = (props: FileExplorerProps) => {
}, [fileSystem.notification.message]) }, [fileSystem.notification.message])
useEffect(() => { useEffect(() => {
if (fileSystem.files.expandPath.length > 0) { if (global.fs.mode === 'browser') {
setState(prevState => { setState(prevState => {
return { ...prevState, expandPath: [...new Set([...prevState.expandPath, ...fileSystem.files.expandPath])] } return { ...prevState, expandPath: [...new Set([...prevState.expandPath, ...global.fs.browser.expandPath])] }
})
} else if (global.fs.mode === 'localhost') {
setState(prevState => {
return { ...prevState, expandPath: [...new Set([...prevState.expandPath, ...global.fs.localhost.expandPath])] }
}) })
} }
}, [fileSystem.files.expandPath]) }, [global.fs.browser.expandPath, global.fs.localhost.expandPath])
useEffect(() => { useEffect(() => {
if (state.focusEdit.element) { if (state.focusEdit.element) {
......
...@@ -5,8 +5,9 @@ import { checkSpecialChars, checkSlash } from '@remix-ui/helper' ...@@ -5,8 +5,9 @@ import { checkSpecialChars, checkSlash } from '@remix-ui/helper'
const QueryParams = require('../../../../../../apps/remix-ide/src/lib/query-params') const QueryParams = require('../../../../../../apps/remix-ide/src/lib/query-params')
const examples = require('../../../../../../apps/remix-ide/src/app/editor/examples') const examples = require('../../../../../../apps/remix-ide/src/app/editor/examples')
// const queuedEvents = [] const queuedEvents = []
// const pendingEvents = {} const pendingEvents = {}
let plugin, dispatch: React.Dispatch<any> let plugin, dispatch: React.Dispatch<any>
const setCurrentWorkspace = (workspace: string) => { const setCurrentWorkspace = (workspace: string) => {
...@@ -51,36 +52,31 @@ const fetchDirectorySuccess = (path: string, files) => { ...@@ -51,36 +52,31 @@ const fetchDirectorySuccess = (path: string, files) => {
} }
} }
export const displayNotification = (title: string, message: string, labelOk: string, labelCancel: string, actionOk?: (...args) => void, actionCancel?: (...args) => void) => { const displayNotification = (title: string, message: string, labelOk: string, labelCancel: string, actionOk?: (...args) => void, actionCancel?: (...args) => void) => {
return { return {
type: 'DISPLAY_NOTIFICATION', type: 'DISPLAY_NOTIFICATION',
payload: { title, message, labelOk, labelCancel, actionOk, actionCancel } payload: { title, message, labelOk, labelCancel, actionOk, actionCancel }
} }
} }
export const hideNotification = () => { const hideNotification = () => {
return { return {
type: 'HIDE_NOTIFICATION' type: 'HIDE_NOTIFICATION'
} }
} }
export const fetchDirectory = (mode: 'browser' | 'localhost', path: string) => (dispatch: React.Dispatch<any>) => { const fileAddedSuccess = (filePath: string) => {
const provider = mode === 'browser' ? plugin.fileProviders.workspace : plugin.fileProviders.localhost return {
const promise = new Promise((resolve) => { type: 'FILE_ADDED_SUCCESS',
provider.resolveDirectory(path, (error, fileTree) => { payload: filePath
if (error) console.error(error) }
}
resolve(fileTree)
})
})
dispatch(fetchDirectoryRequest(promise)) const folderAddedSuccess = (folderPath: string) => {
promise.then((fileTree) => { return {
dispatch(fetchDirectorySuccess(path, fileTree)) type: 'FOLDER_ADDED_SUCCESS',
}).catch((error) => { payload: folderPath
dispatch(fetchDirectoryError({ error })) }
})
return promise
} }
const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true, template: 'gist-template' | 'code-template' | 'default-template' = 'default-template') => { const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true, template: 'gist-template' | 'code-template' | 'default-template' = 'default-template') => {
...@@ -129,7 +125,7 @@ const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true ...@@ -129,7 +125,7 @@ const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true
const data = response.data const data = response.data
if (!data.files) { if (!data.files) {
return dispatch(displayNotification('Gist load error', 'No files found', 'OK', null, () => {}, null)) return dispatch(displayNotification('Gist load error', 'No files found', 'OK', null, () => { dispatch(hideNotification()) }, null))
} }
const obj = {} const obj = {}
...@@ -148,7 +144,7 @@ const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true ...@@ -148,7 +144,7 @@ const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true
} }
}) })
} catch (e) { } catch (e) {
dispatch(displayNotification('Gist load error', e.message, 'OK', null, () => {}, null)) dispatch(displayNotification('Gist load error', e.message, 'OK', null, () => { dispatch(hideNotification()) }, null))
console.error(e) console.error(e)
} }
break break
...@@ -200,6 +196,16 @@ const getWorkspaces = async (): Promise<string[]> | undefined => { ...@@ -200,6 +196,16 @@ const getWorkspaces = async (): Promise<string[]> | undefined => {
} }
} }
const listenOnEvents = (provider) => {
provider.event.on('fileAdded', async (filePath) => {
await executeEvent('fileAdded', filePath)
})
provider.event.on('folderAdded', async (folderPath) => {
await executeEvent('folderAdded', folderPath)
})
}
export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.Dispatch<any>) => { export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.Dispatch<any>) => {
if (filePanelPlugin) { if (filePanelPlugin) {
plugin = filePanelPlugin plugin = filePanelPlugin
...@@ -226,12 +232,9 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React. ...@@ -226,12 +232,9 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
} }
} }
} }
// provider.event.on('fileAdded', async (filePath) => {
// await executeEvent('fileAdded', filePath) listenOnEvents(provider)
// })
// provider.event.on('folderAdded', async (folderPath) => {
// await executeEvent('folderAdded', folderPath)
// })
// provider.event.on('fileRemoved', async (removePath) => { // provider.event.on('fileRemoved', async (removePath) => {
// await executeEvent('fileRemoved', removePath) // await executeEvent('fileRemoved', removePath)
// }) // })
...@@ -290,16 +293,35 @@ export const initLocalhost = (filePanelPlugin) => async (dispatch: React.Dispatc ...@@ -290,16 +293,35 @@ export const initLocalhost = (filePanelPlugin) => async (dispatch: React.Dispatc
} }
} }
// const fileAdded = async (filePath: string) => { export const fetchDirectory = (path: string) => (dispatch: React.Dispatch<any>) => {
// if (extractParentFromKey(filePath) === '/.workspaces') return const provider = plugin.fileManager.currentFileProvider()
// const path = extractParentFromKey(filePath) || provider.workspace || provider.type || '' const promise = new Promise((resolve) => {
// const data = await fetchDirectoryContent(provider, path) provider.resolveDirectory(path, (error, fileTree) => {
if (error) console.error(error)
// await dispatch(fileAddedSuccess(path, data)) resolve(fileTree)
// if (filePath.includes('_test.sol')) { })
// plugin.emit('newTestFileCreated', filePath) })
// }
// } dispatch(fetchDirectoryRequest(promise))
promise.then((fileTree) => {
dispatch(fetchDirectorySuccess(path, fileTree))
}).catch((error) => {
dispatch(fetchDirectoryError({ error }))
})
return promise
}
const fileAdded = async (filePath: string) => {
await dispatch(fileAddedSuccess(filePath))
if (filePath.includes('_test.sol')) {
plugin.emit('newTestFileCreated', filePath)
}
}
const folderAdded = async (folderPath: string) => {
await dispatch(folderAddedSuccess(folderPath))
}
// const folderAdded = async (folderPath: string) => { // const folderAdded = async (folderPath: string) => {
// if (extractParentFromKey(folderPath) === '/.workspaces') return // if (extractParentFromKey(folderPath) === '/.workspaces') return
...@@ -328,60 +350,60 @@ export const initLocalhost = (filePanelPlugin) => async (dispatch: React.Dispatc ...@@ -328,60 +350,60 @@ export const initLocalhost = (filePanelPlugin) => async (dispatch: React.Dispatc
// await fetchDirectory(provider, workspaceName)(dispatch) // await fetchDirectory(provider, workspaceName)(dispatch)
// } // }
// const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemoved' | 'fileRenamed' | 'rootFolderChanged', path?: string) => { const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemoved' | 'fileRenamed' | 'rootFolderChanged', path?: string) => {
// if (Object.keys(pendingEvents).length) { if (Object.keys(pendingEvents).length) {
// return queuedEvents.push({ eventName, path }) return queuedEvents.push({ eventName, path })
// } }
// pendingEvents[eventName + path] = { eventName, path } pendingEvents[eventName + path] = { eventName, path }
// switch (eventName) { switch (eventName) {
// case 'fileAdded': case 'fileAdded':
// await fileAdded(path) await fileAdded(path)
// delete pendingEvents[eventName + path] delete pendingEvents[eventName + path]
// if (queuedEvents.length) { if (queuedEvents.length) {
// const next = queuedEvents.pop() const next = queuedEvents.pop()
// await executeEvent(next.eventName, next.path) await executeEvent(next.eventName, next.path)
// } }
// break break
// case 'folderAdded': case 'folderAdded':
// await folderAdded(path) await folderAdded(path)
// delete pendingEvents[eventName + path] delete pendingEvents[eventName + path]
// if (queuedEvents.length) { if (queuedEvents.length) {
// const next = queuedEvents.pop() const next = queuedEvents.pop()
// await executeEvent(next.eventName, next.path) await executeEvent(next.eventName, next.path)
// } }
// break break
// case 'fileRemoved': // case 'fileRemoved':
// await fileRemoved(path) // await fileRemoved(path)
// delete pendingEvents[eventName + path] // delete pendingEvents[eventName + path]
// if (queuedEvents.length) { // if (queuedEvents.length) {
// const next = queuedEvents.pop() // const next = queuedEvents.pop()
// await executeEvent(next.eventName, next.path) // await executeEvent(next.eventName, next.path)
// } // }
// break // break
// case 'fileRenamed': // case 'fileRenamed':
// await fileRenamed(path) // await fileRenamed(path)
// delete pendingEvents[eventName + path] // delete pendingEvents[eventName + path]
// if (queuedEvents.length) { // if (queuedEvents.length) {
// const next = queuedEvents.pop() // const next = queuedEvents.pop()
// await executeEvent(next.eventName, next.path) // await executeEvent(next.eventName, next.path)
// } // }
// break // break
// case 'rootFolderChanged': // case 'rootFolderChanged':
// await rootFolderChanged() // await rootFolderChanged()
// delete pendingEvents[eventName + path] // delete pendingEvents[eventName + path]
// if (queuedEvents.length) { // if (queuedEvents.length) {
// const next = queuedEvents.pop() // const next = queuedEvents.pop()
// await executeEvent(next.eventName, next.path) // await executeEvent(next.eventName, next.path)
// } // }
// break // break
// } }
// } }
...@@ -32,7 +32,7 @@ export const FileSystemProvider = (props: WorkspaceProps) => { ...@@ -32,7 +32,7 @@ export const FileSystemProvider = (props: WorkspaceProps) => {
} }
const dispatchFetchDirectory = async (path: string) => { const dispatchFetchDirectory = async (path: string) => {
await fetchDirectory(fs.mode, path)(fsDispatch) await fetchDirectory(path)(fsDispatch)
} }
useEffect(() => { useEffect(() => {
......
import { extractNameFromKey, File } from '@remix-ui/file-explorer' import { extractNameFromKey, File } from '@remix-ui/file-explorer'
import * as _ from 'lodash'
interface Action { interface Action {
type: string type: string
payload: any payload: any
...@@ -7,13 +8,15 @@ export interface BrowserState { ...@@ -7,13 +8,15 @@ export interface BrowserState {
browser: { browser: {
currentWorkspace: string, currentWorkspace: string,
workspaces: string[], workspaces: string[],
files: { [x: string]: Record<string, File> } files: { [x: string]: Record<string, File> },
expandPath: string[]
isRequesting: boolean, isRequesting: boolean,
isSuccessful: boolean, isSuccessful: boolean,
error: string error: string
}, },
localhost: { localhost: {
files: { [x: string]: Record<string, File> }, files: { [x: string]: Record<string, File> },
expandPath: string[],
isRequesting: boolean, isRequesting: boolean,
isSuccessful: boolean, isSuccessful: boolean,
error: string error: string
...@@ -34,12 +37,14 @@ export const browserInitialState: BrowserState = { ...@@ -34,12 +37,14 @@ export const browserInitialState: BrowserState = {
currentWorkspace: '', currentWorkspace: '',
workspaces: [], workspaces: [],
files: {}, files: {},
expandPath: [],
isRequesting: false, isRequesting: false,
isSuccessful: false, isSuccessful: false,
error: null error: null
}, },
localhost: { localhost: {
files: {}, files: {},
expandPath: [],
isRequesting: false, isRequesting: false,
isSuccessful: false, isSuccessful: false,
error: null error: null
...@@ -152,6 +157,43 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...@@ -152,6 +157,43 @@ export const browserReducer = (state = browserInitialState, action: Action) => {
notification: browserInitialState.notification notification: browserInitialState.notification
} }
} }
case 'FILE_ADDED_SUCCESS': {
const payload = action.payload as string
return {
...state,
browser: {
...state.browser,
files: fileAdded(state, payload),
expandPath: [...new Set([...state.browser.expandPath, payload])]
},
localhost: {
...state.localhost,
files: fileAdded(state, payload),
expandPath: [...new Set([...state.localhost.expandPath, payload])]
}
}
}
case 'FOLDER_ADDED_SUCCESS': {
const payload = action.payload as string
return {
...state,
browser: {
...state.browser,
files: folderAdded(state, payload),
expandPath: [...new Set([...state.browser.expandPath, payload])]
},
localhost: {
...state.localhost,
files: folderAdded(state, payload),
expandPath: [...new Set([...state.localhost.expandPath, payload])]
}
}
}
default: default:
throw new Error() throw new Error()
} }
...@@ -211,3 +253,32 @@ const normalize = (filesList): Record<string, File> => { ...@@ -211,3 +253,32 @@ const normalize = (filesList): Record<string, File> => {
return Object.assign({}, folders, files) return Object.assign({}, folders, files)
} }
const fileAdded = (state: BrowserState, path: string): { [x: string]: Record<string, File> } => {
let files = state.mode === 'browser' ? state.browser.files : state.localhost.files
const _path = splitPath(state, path)
files = _.set(files, _path)
return files
}
const folderAdded = (state: BrowserState, path: string): { [x: string]: Record<string, File> } => {
let files = state.mode === 'browser' ? state.browser.files : state.localhost.files
const _path = splitPath(state, path)
files = _.set(files, _path)
return files
}
const splitPath = (state: BrowserState, path: string): string[] | string => {
const root = state.mode === 'browser' ? state.browser.currentWorkspace : 'localhost'
const pathArr: string[] = path.split('/').filter(value => value)
if (pathArr[0] !== root) pathArr.unshift(root)
const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => {
return Array.isArray(cur) ? [...acc, ...cur] : [...acc, cur]
}, [])
return _path
}
...@@ -29,7 +29,7 @@ export function Workspace (props: WorkspaceProps) { ...@@ -29,7 +29,7 @@ export function Workspace (props: WorkspaceProps) {
useEffect(() => { useEffect(() => {
if (global.fs.browser.currentWorkspace) { if (global.fs.browser.currentWorkspace) {
setCurrentWorkspace(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]) }, [global.fs.browser.currentWorkspace])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment