From 018a324daf8081732c34b3b11a91a2f5ecd154f7 Mon Sep 17 00:00:00 2001 From: David Disu Date: Wed, 7 Sep 2022 09:22:26 +0100 Subject: [PATCH] Basic checkout implementation --- .../workspace/src/lib/actions/index.ts | 2 +- .../workspace/src/lib/actions/workspace.ts | 37 +++++++++++++++++++ .../workspace/src/lib/contexts/index.ts | 3 +- .../src/lib/providers/FileSystemProvider.tsx | 10 ++++- .../workspace/src/lib/remix-ui-workspace.tsx | 12 +++++- 5 files changed, 59 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 66d7ec7bc7..3469c671e6 100644 --- a/libs/remix-ui/workspace/src/lib/actions/index.ts +++ b/libs/remix-ui/workspace/src/lib/actions/index.ts @@ -495,4 +495,4 @@ export const showAllBranches = async () => { if (!isActive) await plugin.call('manager', 'activatePlugin', 'dgit') plugin.call('menuicons', 'select', 'dgit') -} \ No newline at end of file +} diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index 8800314dc5..5ee0e9ce28 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -439,3 +439,40 @@ export const getGitRepoCurrentBranch = async (workspaceName: string) => { return currentBranch } + +export const showAllBranches = async () => { + const isActive = await plugin.call('manager', 'isActive', 'dgit') + + if (!isActive) await plugin.call('manager', 'activatePlugin', 'dgit') + plugin.call('menuicons', 'select', 'dgit') +} + +export const switchToBranch = async (branch: string) => { + const gitConfig = { + ref: branch + } + const promise = plugin.call('dGitProvider', 'checkout', gitConfig) + + dispatch(cloneRepositoryRequest()) + promise.then(async () => { + await fetchWorkspaceDirectory(ROOT_PATH) + dispatch(cloneRepositorySuccess()) + }).catch((e) => { + const checkoutModal = { + id: 'checkoutGitBranch', + title: 'Checkout Git Branch', + message: 'An error occurred: ' + e, + modalType: 'modal', + okLabel: 'OK', + okFn: async () => { + // await deleteWorkspace(repoName) + dispatch(cloneRepositoryFailed()) + }, + hideFn: async () => { + // await deleteWorkspace(repoName) + dispatch(cloneRepositoryFailed()) + } + } + plugin.call('notification', 'modal', checkoutModal) + }) +} diff --git a/libs/remix-ui/workspace/src/lib/contexts/index.ts b/libs/remix-ui/workspace/src/lib/contexts/index.ts index 5f4cdaee33..ac4e6c456d 100644 --- a/libs/remix-ui/workspace/src/lib/contexts/index.ts +++ b/libs/remix-ui/workspace/src/lib/contexts/index.ts @@ -33,6 +33,7 @@ export const FileSystemContext = createContext<{ dispatchCloneRepository: (url: string) => Promise, dispatchMoveFile: (src: string, dest: string) => Promise, dispatchMoveFolder: (src: string, dest: string) => Promise, - dispatchShowAllBranches: () => Promise + dispatchShowAllBranches: () => Promise, + dispatchSwitchToBranch: (branch: string) => Promise }>(null) \ No newline at end of file diff --git a/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx b/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx index ddc12cdfdf..22c844a48a 100644 --- a/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx +++ b/libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx @@ -8,7 +8,8 @@ import { browserReducer, browserInitialState } from '../reducers/workspace' import { initWorkspace, fetchDirectory, removeInputField, deleteWorkspace, clearPopUp, publishToGist, createNewFile, setFocusElement, createNewFolder, deletePath, renamePath, copyFile, copyFolder, runScript, emitContextMenuEvent, handleClickFile, handleExpandPath, addInputField, createWorkspace, fetchWorkspaceDirectory, renameWorkspace, switchToWorkspace, uploadFile, handleDownloadFiles, restoreBackupZip, cloneRepository, moveFile, moveFolder, - showAllBranches + showAllBranches, + switchToBranch } from '../actions' import { Modal, WorkspaceProps, WorkspaceTemplate } from '../types' // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -143,6 +144,10 @@ export const FileSystemProvider = (props: WorkspaceProps) => { await showAllBranches() } + const dispatchSwitchToBranch = async (branch: string) => { + await switchToBranch(branch) + } + useEffect(() => { dispatchInitWorkspace() }, []) @@ -248,7 +253,8 @@ export const FileSystemProvider = (props: WorkspaceProps) => { dispatchCloneRepository, dispatchMoveFile, dispatchMoveFolder, - dispatchShowAllBranches + dispatchShowAllBranches, + dispatchSwitchToBranch } return ( 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 d100f7ac0f..71416a3337 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -213,6 +213,10 @@ export function Workspace () { global.dispatchShowAllBranches() } + const switchToBranch = (branch: string) => { + global.dispatchSwitchToBranch(branch) + } + const createModalMessage = () => { return ( <> @@ -717,7 +721,13 @@ export function Workspace () { { (selectedWorkspace.branches || []).filter(branch => branch.name.includes(branchFilter) && branch.remote).slice(0, 4).map((branch, index) => { return ( - { selectedWorkspace.currentBranch === branch.name ? ✓ { branch.name } : { branch.name } } + + { + selectedWorkspace.currentBranch === branch.name ? + { switchToBranch(branch.name) }}>✓ { branch.name } : + { switchToBranch(branch.name) }}>{ branch.name } + } + ) }) }