diff --git a/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts b/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts new file mode 100644 index 0000000000..ab3b6b45dd --- /dev/null +++ b/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts @@ -0,0 +1,86 @@ +import React from 'react' +import { File } from '../types' +import { extractNameFromKey, extractParentFromKey } from '../utils' + +const globalRegistry = require('../../../../../../apps/remix-ide/src/global/registry') +const fileProviders = globalRegistry.get('fileproviders').api +const browser = fileProviders.browser // eslint-disable-line +const workspace = fileProviders.workspace +const localhost = fileProviders.localhost // eslint-disable-line + +export const fetchDirectoryError = (error: any) => { + return { + type: 'FETCH_DIRECTORY_ERROR', + payload: error + } +} + +export const fetchDirectoryRequest = (promise: Promise) => { + return { + type: 'FETCH_DIRECTORY_REQUEST', + payload: promise + } +} + +export const fetchDirectorySuccess = (path: string, files: File[]) => { + return { + type: 'FETCH_DIRECTORY_SUCCESS', + payload: { path, files } + } +} + +export const fileSystemReset = () => { + return { + type: 'FILESYSTEM_RESET' + } +} + +const normalize = (filesList): File[] => { + const folders = [] + const files = [] + + Object.keys(filesList || {}).forEach(key => { + key = key.replace(/^\/|\/$/g, '') // remove first and last slash + let path = key + path = path.replace(/^\/|\/$/g, '') // remove first and last slash + + if (filesList[key].isDirectory) { + folders.push({ + path, + name: extractNameFromKey(path), + isDirectory: filesList[key].isDirectory + }) + } else { + files.push({ + path, + name: extractNameFromKey(path), + isDirectory: filesList[key].isDirectory + }) + } + }) + + return [...folders, ...files] +} + +const fetchDirectoryContent = async (folderPath: string): Promise => { + return new Promise((resolve) => { + workspace.resolveDirectory(folderPath, (error, fileTree) => { + if (error) console.error(error) + const files = normalize(fileTree) + + resolve(files) + }) + }) +} + +export const fetchDirectory = (path: string) => (dispatch: React.Dispatch) => { + const promise = fetchDirectoryContent(path) + + dispatch(fetchDirectoryRequest(promise)) + promise.then((files) => { + dispatch(fetchDirectorySuccess(path, files)) + }).catch((error) => { + dispatch(fetchDirectoryError({ error })) + }) + return promise +} diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx index 17f78d79fc..b5be9b0025 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -260,10 +260,12 @@ export const FileExplorer = (props: FileExplorerProps) => { } const fetchDirectoryContent = async (folderPath: string): Promise => { + console.log('folderPath: ', folderPath) return new Promise((resolve) => { filesProvider.resolveDirectory(folderPath, (_error, fileTree) => { const files = normalize(fileTree) + console.log('files: ', files) resolve(files) }) }) diff --git a/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts b/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts index b1a81ee35a..c6086ff59f 100644 --- a/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts +++ b/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts @@ -1,10 +1,12 @@ +import { extractNameFromKey, extractParentFromKey } from '../utils' interface Action { type: string; - payload: Record; + payload: Record; } export const initialState = { files: [], + expandPath: [], isRequesting: false, isSuccessful: false, hasError: null @@ -22,7 +24,8 @@ export const reducer = (state = initialState, action: Action) => { } case 'FETCH_DIRECTORY_SUCCESS': { return { - files: [], + files: action.payload.files, + expandPath: [...action.payload.path], isRequesting: false, isSuccessful: true, hasError: null diff --git a/libs/remix-ui/file-explorer/src/lib/reducers/notification.ts b/libs/remix-ui/file-explorer/src/lib/reducers/notification.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libs/remix-ui/file-explorer/src/lib/utils/index.ts b/libs/remix-ui/file-explorer/src/lib/utils/index.ts new file mode 100644 index 0000000000..3db8f00902 --- /dev/null +++ b/libs/remix-ui/file-explorer/src/lib/utils/index.ts @@ -0,0 +1,13 @@ +export const extractNameFromKey = (key: string): string => { + const keyPath = key.split('/') + + return keyPath[keyPath.length - 1] +} + +export const extractParentFromKey = (key: string):string => { + if (!key) return + const keyPath = key.split('/') + keyPath.pop() + + return keyPath.join('/') +}