From 66c53572cc3b3c290b350a24ab0914228914e13c Mon Sep 17 00:00:00 2001 From: bunsenstraat Date: Tue, 15 Oct 2024 07:13:34 +0200 Subject: [PATCH] fixes for git init and status --- apps/remix-ide/src/app/plugins/git.tsx | 6 +- .../src/lib/plugins/dgitplugin-api.ts | 11 +++ libs/remix-api/src/lib/remix-api.ts | 2 + .../app/src/lib/remix-app/actions/app.ts | 10 ++- .../app/src/lib/remix-app/interface/index.ts | 5 +- .../app/src/lib/remix-app/reducer/app.ts | 28 ++++++-- .../app/src/lib/remix-app/state/app.ts | 3 + libs/remix-ui/git/src/components/gitui.tsx | 10 ++- libs/remix-ui/git/src/lib/gitactions.ts | 2 +- libs/remix-ui/git/src/lib/listeners.ts | 7 +- .../src/lib/components/gitStatus.tsx | 69 +++++-------------- .../workspace/src/lib/actions/workspace.ts | 2 +- .../src/lib/components/file-explorer.tsx | 2 +- 13 files changed, 88 insertions(+), 69 deletions(-) create mode 100644 libs/remix-api/src/lib/plugins/dgitplugin-api.ts diff --git a/apps/remix-ide/src/app/plugins/git.tsx b/apps/remix-ide/src/app/plugins/git.tsx index 6ffa0f0835..b3fe6738d0 100644 --- a/apps/remix-ide/src/app/plugins/git.tsx +++ b/apps/remix-ide/src/app/plugins/git.tsx @@ -8,7 +8,7 @@ const profile = { name: 'dgit', displayName: 'Git', desciption: 'Git plugin for Remix', - methods: ['pull', 'track', 'diff', 'clone', 'open'], + methods: ['open', 'init'], events: [''], version: packageJson.version, maintainedBy: 'Remix', @@ -34,6 +34,10 @@ export class GitPlugin extends ViewPlugin { this.emit('openPanel', panel) } + async init() { + this.emit('init') + } + render() { return
} diff --git a/libs/remix-api/src/lib/plugins/dgitplugin-api.ts b/libs/remix-api/src/lib/plugins/dgitplugin-api.ts new file mode 100644 index 0000000000..092e9c7f32 --- /dev/null +++ b/libs/remix-api/src/lib/plugins/dgitplugin-api.ts @@ -0,0 +1,11 @@ +import { StatusEvents } from "@remixproject/plugin-utils" + +export interface IDgitPlugin { + events: { + } & StatusEvents, + methods: { + open(panel: string): Promise, + init(): Promise + } +} + diff --git a/libs/remix-api/src/lib/remix-api.ts b/libs/remix-api/src/lib/remix-api.ts index 12e29f62f5..75e9e6476d 100644 --- a/libs/remix-api/src/lib/remix-api.ts +++ b/libs/remix-api/src/lib/remix-api.ts @@ -15,9 +15,11 @@ import { ILayoutApi } from "./plugins/layout-api" import { IMatomoApi } from "./plugins/matomo-api" import { IRemixAI } from "./plugins/remixai-api" import { IRemixAID } from "./plugins/remixAIDesktop-api" +import { IDgitPlugin } from "./plugins/dgitplugin-api" export interface ICustomRemixApi extends IRemixApi { dgitApi: IGitApi + dgit: IDgitPlugin config: IConfigApi notification: INotificationApi settings: ISettings diff --git a/libs/remix-ui/app/src/lib/remix-app/actions/app.ts b/libs/remix-ui/app/src/lib/remix-app/actions/app.ts index 9822635e1e..716d351e94 100644 --- a/libs/remix-ui/app/src/lib/remix-app/actions/app.ts +++ b/libs/remix-ui/app/src/lib/remix-app/actions/app.ts @@ -1,4 +1,4 @@ -import { GitHubUser } from '@remix-api'; +import { branch, GitHubUser } from '@remix-api'; import { AppModal } from '../interface' type ActionMap = { @@ -14,10 +14,16 @@ type ActionMap = { export const enum appActionTypes { setGitHubUser = 'SET_GITHUB_USER', + setCurrentBranch = 'SET_CURRENT_BRANCH', + setNeedsGitInit = 'SET_NEEDS_GIT_INIT', + setCanUseGit = 'SET_CAN_USE_GIT', } type AppPayload = { - [appActionTypes.setGitHubUser]: GitHubUser + [appActionTypes.setGitHubUser]: GitHubUser, + [appActionTypes.setCurrentBranch]: branch, + [appActionTypes.setNeedsGitInit]: boolean, + [appActionTypes.setCanUseGit]: boolean, } export type AppAction = ActionMap[keyof ActionMap< diff --git a/libs/remix-ui/app/src/lib/remix-app/interface/index.ts b/libs/remix-ui/app/src/lib/remix-app/interface/index.ts index adefcf2a2b..0c146c98f7 100644 --- a/libs/remix-ui/app/src/lib/remix-app/interface/index.ts +++ b/libs/remix-ui/app/src/lib/remix-app/interface/index.ts @@ -1,4 +1,4 @@ -import { GitHubUser } from '@remix-api' +import { branch, GitHubUser } from '@remix-api' import { AppModalCancelTypes, ModalTypes } from '../types' export type ValidationResult = { @@ -49,5 +49,8 @@ export interface forceChoiceModal { export interface AppState { gitHubUser: GitHubUser + currentBranch: branch + needsGitInit: boolean + canUseGit: boolean } diff --git a/libs/remix-ui/app/src/lib/remix-app/reducer/app.ts b/libs/remix-ui/app/src/lib/remix-app/reducer/app.ts index 2fce1a56cf..3201aa6ade 100644 --- a/libs/remix-ui/app/src/lib/remix-app/reducer/app.ts +++ b/libs/remix-ui/app/src/lib/remix-app/reducer/app.ts @@ -3,11 +3,29 @@ import { AppState } from "../interface"; export const appReducer = (state: AppState, action: AppAction): AppState => { switch (action.type) { - case appActionTypes.setGitHubUser:{ - return { - ...state, - gitHubUser: action.payload + case appActionTypes.setGitHubUser: { + return { + ...state, + gitHubUser: action.payload + } + } + case appActionTypes.setCurrentBranch: { + return { + ...state, + currentBranch: action.payload + } + } + case appActionTypes.setNeedsGitInit: { + return { + ...state, + needsGitInit: action.payload + } + } + case appActionTypes.setCanUseGit: { + return { + ...state, + canUseGit: action.payload + } } - } } } \ No newline at end of file diff --git a/libs/remix-ui/app/src/lib/remix-app/state/app.ts b/libs/remix-ui/app/src/lib/remix-app/state/app.ts index 1e76098bdc..8086b75c9c 100644 --- a/libs/remix-ui/app/src/lib/remix-app/state/app.ts +++ b/libs/remix-ui/app/src/lib/remix-app/state/app.ts @@ -3,4 +3,7 @@ import { AppState } from "../interface"; export const appInitialState: AppState = { gitHubUser: {} as GitHubUser, + currentBranch: null, + needsGitInit: true, + canUseGit: false } \ No newline at end of file diff --git a/libs/remix-ui/git/src/components/gitui.tsx b/libs/remix-ui/git/src/components/gitui.tsx index b7444e0b8d..7bf1af9632 100644 --- a/libs/remix-ui/git/src/components/gitui.tsx +++ b/libs/remix-ui/git/src/components/gitui.tsx @@ -31,7 +31,7 @@ import { GitHubCredentials } from './panels/githubcredentials' import { Setup } from './panels/setup' import { Init } from './panels/init' import { Disabled } from './disabled' -import { AppContext, appPlatformTypes, platformContext } from '@remix-ui/app' +import { appActionTypes, AppContext, appPlatformTypes, platformContext } from '@remix-ui/app' import { Version } from './panels/version' export const gitPluginContext = React.createContext(defaultGitState) @@ -113,14 +113,20 @@ export const GitUI = (props: IGitUi) => { }) let needsInit = false - if (!(gitState.currentBranch && gitState.currentBranch.name !== '') && gitState.currentHead === '') { + if (!(gitState.currentBranch && gitState.currentBranch.name !== '') && (!gitState.currentHead || gitState.currentHead === '')) { needsInit = true } setNeedsInit(needsInit) + appContext.appStateDispatch({ type: appActionTypes.setNeedsGitInit, payload: needsInit }) + appContext.appStateDispatch({ type: appActionTypes.setCurrentBranch, payload: gitState.currentBranch || null }) }, [gitState.gitHubUser, gitState.currentBranch, gitState.remotes, gitState.gitHubAccessToken, gitState.currentHead]) + useEffect(() => { + appContext.appStateDispatch({ type: appActionTypes.setCanUseGit, payload: gitState.canUseApp }) + },[gitState.canUseApp]) + useEffect(() => { const panelName = Object.keys(gitUIPanels) .filter(k => gitUIPanels[k] === activePanel); diff --git a/libs/remix-ui/git/src/lib/gitactions.ts b/libs/remix-ui/git/src/lib/gitactions.ts index 62c23412e3..2568696180 100644 --- a/libs/remix-ui/git/src/lib/gitactions.ts +++ b/libs/remix-ui/git/src/lib/gitactions.ts @@ -41,7 +41,7 @@ export const setPlugin = (p: Plugin, dispatcher: React.Dispatch { await sendToMatomo(gitMatomoEventTypes.INIT) - await plugin.call('dgitApi', "init"); + await plugin.call('dgitApi', 'init'); dispatch(setTimestamp(Date.now())) await getBranches(); } diff --git a/libs/remix-ui/git/src/lib/listeners.ts b/libs/remix-ui/git/src/lib/listeners.ts index 42f450c058..028cfa6a7b 100644 --- a/libs/remix-ui/git/src/lib/listeners.ts +++ b/libs/remix-ui/git/src/lib/listeners.ts @@ -3,7 +3,7 @@ import React from "react"; import { setCanUseApp, setLoading, setRepoName, setGItHubToken, setLog, setGitHubUser, setUserEmails, setTimestamp, setDesktopWorkingDir, setVersion } from "../state/gitpayload"; import { gitActionDispatch, gitUIPanels, storage } from "../types"; import { Plugin } from "@remixproject/engine"; -import { getBranches, getFileStatusMatrix, loadGitHubUserFromToken, getRemotes, gitlog, setPlugin, setStorage } from "./gitactions"; +import { getBranches, getFileStatusMatrix, loadGitHubUserFromToken, getRemotes, gitlog, setPlugin, setStorage, init } from "./gitactions"; import { Profile } from "@remixproject/plugin-utils"; import { CustomRemixApi } from "@remix-api"; import { statusChanged } from "./pluginActions"; @@ -200,10 +200,13 @@ export const setCallBacks = (viewPlugin: Plugin, gitDispatcher: React.Dispatch { - setAtivePanel(panel) }) + plugin.on('dgit' as any, 'init', async () => { + init() + }) + callBackEnabled = true; } diff --git a/libs/remix-ui/statusbar/src/lib/components/gitStatus.tsx b/libs/remix-ui/statusbar/src/lib/components/gitStatus.tsx index 79f207806c..fd4a9e5c63 100644 --- a/libs/remix-ui/statusbar/src/lib/components/gitStatus.tsx +++ b/libs/remix-ui/statusbar/src/lib/components/gitStatus.tsx @@ -1,8 +1,9 @@ -import React, { useEffect, Dispatch, useState } from 'react' +import React, { useEffect, Dispatch, useState, useContext } from 'react' // eslint-disable-next-line @nrwl/nx/enforce-module-boundaries import { StatusBar } from 'apps/remix-ide/src/app/components/status-bar' import '../../css/statusbar.css' import { CustomTooltip } from '@remix-ui/helper' +import { AppContext } from '@remix-ui/app' export interface GitStatusProps { plugin: StatusBar @@ -11,70 +12,32 @@ export interface GitStatusProps { } export default function GitStatus({ plugin, gitBranchName, setGitBranchName }: GitStatusProps) { - const [isLocalHost, setIsLocalHost] = useState(false) + const appContext = useContext(AppContext) - useEffect(() => { - plugin.on('filePanel', 'setWorkspace', async (workspace) => { - const isGit = await plugin.call('fileManager', 'isGitRepo') - setIsLocalHost(workspace.isLocalhost) - if (isGit) { - setGitBranchName(workspace.name) - } else { - setGitBranchName('Not a git repo') - } - }) - plugin.on('filePanel', 'workspaceInitializationCompleted', async () => { - const isGit = await plugin.call('fileManager', 'isGitRepo') - if (isGit) { - const workspace = localStorage.getItem('currentWorkspace') - setGitBranchName(workspace) - } else { - setGitBranchName('Not a git repo') - } - }) - plugin.on('dgitApi', 'init', async () => { - const isGit = await plugin.call('fileManager', 'isGitRepo') - if (isGit) { - const workspace = localStorage.getItem('currentWorkspace') - setGitBranchName(workspace) - } - }) - - }, []) - - const lightDgitUp = async () => { - const isActive = await plugin.call('manager', 'isActive', 'dgit') - const isGit = await plugin.call('fileManager', 'isGitRepo') - if (!isActive) await plugin.call('manager', 'activatePlugin', 'dgit') - if (gitBranchName.length > 0 && isGit) { - plugin.verticalIcons.select('dgit') - } + const openDgit = async () => { + plugin.verticalIcons.select('dgit') } const initializeNewGitRepo = async () => { - if (isLocalHost === false) { - const isActive = await plugin.call('manager', 'isActive', 'dgit') - if (!isActive) await plugin.call('manager', 'activatePlugin', 'dgit') - await plugin.call('dgitApi', 'init') - ;(window as any)._paq.push('trackEvent', 'statusbar', 'initNewRepo') - } + await plugin.call('dgit', 'init') + await plugin.call('matomo', 'track', ['trackEvent', 'statusBar', 'initNewRepo']); } - const checkBranchName = ()=> { - return gitBranchName && gitBranchName !== 'Not a git repo' && gitBranchName.length > 0 - } + if (!appContext.appState.canUseGit) return null + return (
await lightDgitUp()} + onClick={async () => await openDgit()} > - {checkBranchName() && isLocalHost === false ? - : Initialize as git repo} - {checkBranchName() && isLocalHost === false && {gitBranchName}} - {checkBranchName() && isLocalHost === false && } + {!appContext.appState.needsGitInit ? + : Initialize as git repo} + {!appContext.appState.needsGitInit && appContext.appState.currentBranch && + await openDgit()} className="ml-1">{appContext.appState.currentBranch.name} + }
) diff --git a/libs/remix-ui/workspace/src/lib/actions/workspace.ts b/libs/remix-ui/workspace/src/lib/actions/workspace.ts index dbd6ba738f..e60869e61d 100644 --- a/libs/remix-ui/workspace/src/lib/actions/workspace.ts +++ b/libs/remix-ui/workspace/src/lib/actions/workspace.ts @@ -167,7 +167,7 @@ export const createWorkspace = async ( // commit the template as first commit plugin.call('notification', 'toast', 'Creating initial git commit ...') - await dgitPlugin.call('dgitApi', 'init') + await dgitPlugin.call('dgit', 'init') if (!isEmpty) await loadWorkspacePreset(workspaceTemplateName, opts) const status = await dgitPlugin.call('dgitApi', 'status', { ref: 'HEAD' }) 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 00b9db4b52..9853cf3739 100644 --- a/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx +++ b/libs/remix-ui/workspace/src/lib/components/file-explorer.tsx @@ -584,7 +584,7 @@ export const FileExplorer = (props: FileExplorerProps) => { const handleGitInit = async () => { const isActive = await plugin.call('manager', 'isActive', 'dgit') if (!isActive) await plugin.call('manager', 'activatePlugin', 'dgit') - await plugin.call('dgitApi', 'init') + await plugin.call('dgit', 'init') } return (