From 3663320776ebb438b058a96e05b27ac0fb5b1417 Mon Sep 17 00:00:00 2001 From: Joseph Izang Date: Thu, 1 Aug 2024 15:31:44 +0100 Subject: [PATCH] implement multiple files gist publish --- .../workspace/src/lib/actions/index.ts | 2 ++ .../components/file-explorer-context-menu.tsx | 5 +++ .../src/lib/components/file-explorer.tsx | 8 ++--- .../workspace/src/lib/remix-ui-workspace.tsx | 35 ++++++++++++++++++- .../remix-ui/workspace/src/lib/types/index.ts | 7 ++++ .../remix-ui/workspace/src/lib/utils/index.ts | 9 +++++ 6 files changed, 61 insertions(+), 5 deletions(-) diff --git a/libs/remix-ui/workspace/src/lib/actions/index.ts b/libs/remix-ui/workspace/src/lib/actions/index.ts index 5814bb9092..50ddf496cb 100644 --- a/libs/remix-ui/workspace/src/lib/actions/index.ts +++ b/libs/remix-ui/workspace/src/lib/actions/index.ts @@ -328,6 +328,8 @@ export const publishToGist = async (path?: string) => { handleGistResponse(error, result) }) } + // fire event for cleanup of temp folder + plugin.emit('FinishedGistPublish', folder) } } catch (error) { console.log(error) diff --git a/libs/remix-ui/workspace/src/lib/components/file-explorer-context-menu.tsx b/libs/remix-ui/workspace/src/lib/components/file-explorer-context-menu.tsx index 1024821340..b3a39f974c 100644 --- a/libs/remix-ui/workspace/src/lib/components/file-explorer-context-menu.tsx +++ b/libs/remix-ui/workspace/src/lib/components/file-explorer-context-menu.tsx @@ -40,6 +40,7 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) => focus, downloadPath, uploadFile, + publishManyFilesToGist, ...otherProps } = props const contextMenuRef = useRef(null) @@ -197,6 +198,10 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) => _paq.push(['trackEvent', 'fileExplorer', 'contextMenu', 'publishFileToGist']) publishFileToGist(path) break + case 'Publish files to gist': + _paq.push(['trackEvent', 'fileExplorer', 'contextMenu', 'publishFilesToGist']) + publishManyFilesToGist() + break case 'Run': _paq.push(['trackEvent', 'fileExplorer', 'contextMenu', 'runScript']) runScript(path) diff --git a/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx b/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx index a30f06ec9b..04cc1af2d7 100644 --- a/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx +++ b/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx @@ -9,7 +9,7 @@ import '../css/file-explorer.css' import { checkSpecialChars, extractNameFromKey, extractParentFromKey, getPathIcon, joinPath } from '@remix-ui/helper' // eslint-disable-next-line @typescript-eslint/no-unused-vars import { ROOT_PATH } from '../utils/constants' -import { moveFileIsAllowed, moveFilesIsAllowed, moveFolderIsAllowed, moveFoldersIsAllowed } from '../actions' +import { copyFile, moveFileIsAllowed, moveFilesIsAllowed, moveFolderIsAllowed, moveFoldersIsAllowed } from '../actions' import { FlatTree } from './flat-tree' import { FileSystemContext } from '../contexts' @@ -35,7 +35,9 @@ export const FileExplorer = (props: FileExplorerProps) => { uploadFile, uploadFolder, fileState, - canPaste + canPaste, + feTarget, + setFeTarget } = props const [state, setState] = useState(workspaceState) // const [isPending, startTransition] = useTransition(); @@ -43,7 +45,6 @@ export const FileExplorer = (props: FileExplorerProps) => { const [cutActivated, setCutActivated] = useState(false) const { plugin } = useContext(FileSystemContext) - const [feTarget, setFeTarget] = useState<{ key: string, type: 'file' | 'folder' }[]>({} as { key: string, type: 'file' | 'folder' }[]) const [filesSelected, setFilesSelected] = useState([]) const feWindow = (window as any) @@ -589,7 +590,6 @@ export const FileExplorer = (props: FileExplorerProps) => {
({ valid: true, message: '' }) + const [feTarget, setFeTarget] = useState<{ key: string, type: 'file' | 'folder' }[]>({} as { key: string, type: 'file' | 'folder' }[]) const loadingInitialState = { tooltip: '', @@ -154,6 +155,31 @@ export function Workspace() { }) } + /** + * Void action to ensure multiselected files are published + * folders are not handled + */ + const handlePublishingMultiSelectedFilesToGist = async () => { + const tempFolderName = '.gistTemp' + // first create a temporary folder to populate selected files + try { + await global.dispatchCreateNewFolder(tempFolderName, ROOT_PATH) + feTarget.filter(feObject => feObject.type !== 'folder') + .forEach(async feFile => { + await copyFile(feFile.key, tempFolderName) + }) + await global.dispatchPublishToGist(tempFolderName) + global.plugin.on('finishedGistPublish', async (folderName) => { + console.log('name of folder', folderName) + if (folderName === tempFolderName) + await global.dispatchDeletePath(folderName) + }) + } catch (error) { + await global.plugin.call('notification', 'toast', 'Could not publish files to gist. There was an error') + await global.plugin.call('notification', 'toast', typeof(error) === 'string' ? error : `${console.log(error)} check the console for more details`) + } + } + const showFullMessage = async (title: string, loadItem: string, examples: Array, prefix = '') => { setModalState((prevState) => { return { @@ -1023,6 +1049,9 @@ export function Workspace() { files={global.fs.browser.files} flatTree={global.fs.browser.flatTree} workspaceState={state} + feTarget={feTarget} + setFeTarget={setFeTarget} + publishManyFilesToGist={handlePublishingMultiSelectedFilesToGist} expandPath={global.fs.browser.expandPath} focusEdit={global.fs.focusEdit} focusElement={global.fs.focusElement} @@ -1094,6 +1123,9 @@ export function Workspace() { fileState={[]} canPaste={canPaste} workspaceState={state} + feTarget={feTarget} + setFeTarget={setFeTarget} + publishManyFilesToGist={handlePublishingMultiSelectedFilesToGist} expandPath={global.fs.localhost.expandPath} focusEdit={global.fs.focusEdit} focusElement={global.fs.focusElement} @@ -1269,7 +1301,7 @@ export function Workspace() { )} {state.showContextMenu && ( 1 ? state.actions.filter((item) => item.multiselect) : state.actions.filter((item) => !item.multiselect)} + actions={(global.fs.focusElement.length > 1 || feTarget.length > 1) ? state.actions.filter((item) => item.multiselect) : state.actions.filter((item) => !item.multiselect)} hideContextMenu={hideContextMenu} createNewFile={handleNewFileInput} createNewFolder={handleNewFolderInput} @@ -1292,6 +1324,7 @@ export function Workspace() { publishFileToGist={publishFileToGist} uploadFile={uploadFile} downloadPath={downloadPath} + publishManyFilesToGist={handlePublishingMultiSelectedFilesToGist} /> )} diff --git a/libs/remix-ui/workspace/src/lib/types/index.ts b/libs/remix-ui/workspace/src/lib/types/index.ts index 79c9b4afef..17a1db53d7 100644 --- a/libs/remix-ui/workspace/src/lib/types/index.ts +++ b/libs/remix-ui/workspace/src/lib/types/index.ts @@ -160,6 +160,12 @@ export interface FileExplorerProps { importFromIpfs: any importFromHttps: any handleMultiCopies: any + feTarget: { key: string, type: 'file' | 'folder' }[] + setFeTarget: Dispatch> + publishManyFilesToGist: () => Promise } export interface FileExplorerMenuProps { @@ -201,6 +207,7 @@ export interface FileExplorerContextMenuProps { copyPath?: (path: string, type: string) => void generateUml?: (path: string) => Promise uploadFile?: (target: EventTarget & HTMLInputElement) => void + publishManyFilesToGist: () => Promise } export interface WorkSpaceState { diff --git a/libs/remix-ui/workspace/src/lib/utils/index.ts b/libs/remix-ui/workspace/src/lib/utils/index.ts index d29b524163..a0dfc8df6a 100644 --- a/libs/remix-ui/workspace/src/lib/utils/index.ts +++ b/libs/remix-ui/workspace/src/lib/utils/index.ts @@ -97,6 +97,15 @@ export const contextMenuActions: MenuItems = [{ group: 4, platform: appPlatformTypes.web }, { + id: 'publishFilesToGist', + name: 'Publish files to gist', + type: ['file'], + multiselect: true, + label: '', + group: 4, + platform: appPlatformTypes.web +} +, { id: 'uploadFile', name: 'Load a Local File', type: ['folder', 'workspace'],