add storage state

pull/5000/head
bunsenstraat 5 months ago committed by Aniket
parent bed41f3b07
commit ff75a668db
  1. 36
      libs/remix-ui/git/src/components/branchHeader.tsx
  2. 11
      libs/remix-ui/git/src/lib/gitactions.ts
  3. 55
      libs/remix-ui/git/src/lib/listeners.ts
  4. 9
      libs/remix-ui/git/src/lib/pluginActions.ts
  5. 3
      libs/remix-ui/git/src/state/actions.ts
  6. 9
      libs/remix-ui/git/src/state/gitpayload.ts
  7. 398
      libs/remix-ui/git/src/state/gitreducer.tsx
  8. 18
      libs/remix-ui/git/src/types/index.ts

@ -1,6 +1,5 @@
import React, { useEffect, useState } from 'react'
import { gitActionsContext, pluginActionsContext } from '../state/context'
import { storageStatus } from '../lib/pluginActions'
import { ReadCommitResult } from "isomorphic-git"
import { gitPluginContext } from './gitui'
export const BranchHeader = () => {
@ -11,7 +10,6 @@ export const BranchHeader = () => {
const [changed, setChanged] = useState(false)
const [isDetached, setIsDetached] = useState(false)
const [latestCommit, setLatestCommit] = useState<ReadCommitResult>(null)
const [storageStats, setStorageStats] = useState<string>('')
useEffect(() => {
if (context.currentBranch) {
@ -45,18 +43,6 @@ export const BranchHeader = () => {
}
}, [context.fileStatusResult, context.modified, context.allchangesnotstaged, context.untracked, context.deleted])
useEffect(() => {
const run = async () => {
const stats = await storageStatus()
setStorageStats(stats)
}
run()
return () => {
run()
}
}, [])
const getName = () => {
const url = context.currentBranch?.remote?.url
if (!url) return
@ -80,7 +66,7 @@ export const BranchHeader = () => {
<div className="pr-1 m-0">
<span className="">Repository Name:</span>
<span className="text-secondary text-truncate overflow-hidden whitespace-nowrap" style={{ width: '15rem' }}>
<span className={`${ changed ? 'text-danger pl-2' : "pl-2" }`}>
<span className={`${changed ? 'text-danger pl-2' : "pl-2"}`}>
{getName() ?? ''}
</span>
</span>
@ -88,20 +74,22 @@ export const BranchHeader = () => {
<div className="pr-1 m-0">
<span className="">Branch Name:</span>
<span className="pl-2 text-secondary text-truncate overflow-hidden whitespace-nowrap">
<span className={`${ changed ? 'text-danger pl-2' : "pl-2" }`}>
<span className={`${changed ? 'text-danger pl-2' : "pl-2"}`}>
<i className="fa fa-code-branch mr-1 pl-2"></i>
{context.currentBranch && context.currentBranch.name}
</span>
</span>
</div>
<div className="d-flex flex-column">
<span className="d-flex justify-between align-items-center">
<span className="">Storage : </span>
<span className="text-secondary text-sm text-truncate overflow-hidden whitespace-nowrap ml-4">
{storageStats} MB
{context.storage.enabled ?
<div className="d-flex flex-column">
<span className="d-flex justify-between align-items-center">
<span className="">Storage : </span>
<span className="text-secondary text-sm text-truncate overflow-hidden whitespace-nowrap ml-4">
{context.storage.used} MB used
({context.storage.percentUsed} %)
</span>
</span>
</span>
</div>
</div> : null}
<div className="d-flex flex-row">
<span className="d-flex justify-between align-items-center">
<span className="">Messages :</span>
@ -109,7 +97,7 @@ export const BranchHeader = () => {
{latestCommit ?
latestCommit.commit && latestCommit.commit.message ? latestCommit.commit.message : '' : null}
{isDetached ?
<>You are in a detached state<i onClick={showDetachedWarningText} className="btn fa fa-info-circle mr-1 pl-2"></i></>: null}
<>You are in a detached state<i onClick={showDetachedWarningText} className="btn fa fa-info-circle mr-1 pl-2"></i></> : null}
</span>
</span>
</div>

@ -1,7 +1,7 @@
import { ReadBlobResult, ReadCommitResult } from "isomorphic-git";
import React from "react";
import { fileStatus, fileStatusMerge, setRemoteBranchCommits, resetRemoteBranchCommits, setBranches, setCanCommit, setCommitChanges, setCommits, setCurrentBranch, setGitHubUser, setLoading, setRemoteBranches, setRemotes, setRepos, setUpstream, setLocalBranchCommits, setBranchDifferences, setRemoteAsDefault, setScopes, setLog, clearLog, setUserEmails, setCurrenHead } from "../state/gitpayload";
import { GitHubUser, branch, commitChange, gitActionDispatch, statusMatrixType, gitState, branchDifference, remote, gitLog, fileStatusResult, customGitApi, IGitApi, cloneInputType, fetchInputType, pullInputType, pushInputType, checkoutInput, rmInput, addInput, repository, userEmails } from '../types';
import { fileStatus, fileStatusMerge, setRemoteBranchCommits, resetRemoteBranchCommits, setBranches, setCanCommit, setCommitChanges, setCommits, setCurrentBranch, setGitHubUser, setLoading, setRemoteBranches, setRemotes, setRepos, setUpstream, setLocalBranchCommits, setBranchDifferences, setRemoteAsDefault, setScopes, setLog, clearLog, setUserEmails, setCurrenHead, setStoragePayload } from "../state/gitpayload";
import { GitHubUser, branch, commitChange, gitActionDispatch, statusMatrixType, gitState, branchDifference, remote, gitLog, fileStatusResult, customGitApi, IGitApi, cloneInputType, fetchInputType, pullInputType, pushInputType, checkoutInput, rmInput, addInput, repository, userEmails, storage } from '../types';
import { removeSlash } from "../utils";
import { disableCallBacks, enableCallBacks } from "./listeners";
import { ModalTypes } from "@remix-ui/app";
@ -849,4 +849,9 @@ export const sendToGitLog = async (message: gitLog) => {
export const clearGitLog = async () => {
dispatch(clearLog())
}
}
export const setStorage = async (storage: storage) => {
console.log(storage)
dispatch(setStoragePayload(storage))
}

@ -1,9 +1,9 @@
import React from "react";
import { setCanUseApp, setLoading, setRepoName, setGItHubToken, setLog, setGitHubUser, setUserEmails } from "../state/gitpayload";
import { gitActionDispatch } from "../types";
import { gitActionDispatch, storage } from "../types";
import { Plugin } from "@remixproject/engine";
import { getBranches, getFileStatusMatrix, loadGitHubUserFromToken, getRemotes, gitlog, setPlugin } from "./gitactions";
import { getBranches, getFileStatusMatrix, loadGitHubUserFromToken, getRemotes, gitlog, setPlugin, setStorage } from "./gitactions";
import { Profile } from "@remixproject/plugin-utils";
import { CustomRemixApi } from "@remix-api";
import { statusChanged } from "./pluginActions";
@ -20,7 +20,7 @@ class AsyncDebouncedQueue {
this.queues = new Map();
}
enqueue(callback: AsyncCallback, customDelay?:number): void {
enqueue(callback: AsyncCallback, customDelay?: number): void {
if (this.queues.has(callback)) {
clearTimeout(this.queues.get(callback)!.timer);
}
@ -180,6 +180,7 @@ export const getGitConfig = async () => {
export const loadFiles = async (filepaths: string[] = null) => {
try {
await calculateLocalStorage()
const branch = await plugin.call('dgitApi', "currentbranch")
if (branch) {
await getFileStatusMatrix(filepaths);
@ -199,3 +200,51 @@ export const enableCallBacks = async () => {
callBackEnabled = true;
}
const calculateLocalStorage = async () => {
function bytesToMB(bytes) {
return parseFloat((bytes / (1024 * 1024)).toFixed(2));
}
function calculatePercentage(used, quota) {
return parseFloat(((used / quota) * 100).toFixed(2));
}
let storage: storage = {
used: 0,
total: 0,
available: 0,
percentUsed: 0,
enabled: false
}
if ('storage' in navigator && 'estimate' in navigator.storage) {
navigator.storage.estimate().then(estimate => {
const usedMB = bytesToMB(estimate.usage);
const quotaMB = bytesToMB(estimate.quota);
const availableMB = bytesToMB(estimate.quota - estimate.usage);
const percentageUsed = calculatePercentage(estimate.usage, estimate.quota);
console.log(`Used storage: ${usedMB} MB`);
console.log(`Total quota: ${quotaMB} MB`);
console.log(`Available storage: ${availableMB} MB`);
console.log(`Percentage used: ${percentageUsed}%`);
storage = {
used: usedMB,
total: quotaMB,
available: availableMB,
percentUsed: percentageUsed,
enabled: true
}
setStorage(storage);
});
} else {
console.log('Storage API not supported in this browser.');
setStorage(storage);
}
}

@ -23,15 +23,6 @@ export const statusChanged = (badges: number) => {
})
}
export const storageStatus = async () => {
try {
const status = await plugin?.call('dgitApi' as any, 'localStorageUsed')
return status
} catch (error) {
console.log('An error occured while getting storage use summary', error)
}
}
export const openFile = async (path: string) => {
if (!plugin) return
await plugin.call('fileManager', 'open', path)

@ -1,5 +1,5 @@
import { ReadCommitResult } from "isomorphic-git"
import { branch, branchDifference, commitChange, fileStatusResult, GitHubUser, gitLog, pagedCommits, remote, remoteBranch, repository, userEmails } from "../types"
import { branch, branchDifference, commitChange, fileStatusResult, GitHubUser, gitLog, pagedCommits, remote, remoteBranch, repository, storage, userEmails } from "../types"
export interface ActionPayloadTypes {
FILE_STATUS: fileStatusResult[],
@ -42,6 +42,7 @@ export interface ActionPayloadTypes {
SET_LOG: gitLog
CLEAR_LOG: void
SET_USER_EMAILS: userEmails
SET_STORAGE: storage
}
export interface Action<T extends keyof ActionPayloadTypes> {

@ -1,5 +1,5 @@
import { ReadCommitResult } from "isomorphic-git"
import { GitHubUser, branch, commitChange, fileStatusResult, remote, pagedCommits, branchDifference, gitLog, repository, userEmails } from "../types"
import { GitHubUser, branch, commitChange, fileStatusResult, remote, pagedCommits, branchDifference, gitLog, repository, userEmails, storage } from "../types"
import { Endpoints } from "@octokit/types"
export const fileStatus = (files: fileStatusResult[]) => {
@ -218,3 +218,10 @@ export const clearLog = () => {
type: 'CLEAR_LOG'
}
}
export const setStoragePayload = (storage: storage) => {
return {
type: 'SET_STORAGE',
payload: storage
}
}

@ -6,203 +6,209 @@ import { Actions } from "./actions"
export const gitReducer = (state: gitState = defaultGitState, action: Actions): gitState => {
switch (action.type) {
case 'FILE_STATUS':
return {
...state,
fileStatusResult: action.payload,
staged: getFilesByStatus("staged", action.payload),
modified: getFilesByStatus("modified", action.payload),
untracked: getFilesByStatus("untracked", action.payload),
deleted: getFilesByStatus("deleted", action.payload),
allchangesnotstaged: allChangedButNotStagedFiles(action.payload)
}
case 'FILE_STATUS_MERGE':
action.payload.map((fileStatusResult: fileStatusResult) => {
const file = state.fileStatusResult.find(stateFile => {
return stateFile.filename === fileStatusResult.filename
case 'FILE_STATUS':
return {
...state,
fileStatusResult: action.payload,
staged: getFilesByStatus("staged", action.payload),
modified: getFilesByStatus("modified", action.payload),
untracked: getFilesByStatus("untracked", action.payload),
deleted: getFilesByStatus("deleted", action.payload),
allchangesnotstaged: allChangedButNotStagedFiles(action.payload)
}
case 'FILE_STATUS_MERGE':
action.payload.map((fileStatusResult: fileStatusResult) => {
const file = state.fileStatusResult.find(stateFile => {
return stateFile.filename === fileStatusResult.filename
})
if (file) {
file.status = fileStatusResult.status
file.statusNames = fileStatusResult.statusNames
}
})
return {
...state,
staged: getFilesByStatus("staged", state.fileStatusResult),
modified: getFilesByStatus("modified", state.fileStatusResult),
untracked: getFilesByStatus("untracked", state.fileStatusResult),
deleted: getFilesByStatus("deleted", state.fileStatusResult),
allchangesnotstaged: allChangedButNotStagedFiles(state.fileStatusResult)
}
case 'SET_COMMITS':
return {
...state,
commits: action.payload,
localCommitCount: action.payload.length
}
case 'SET_BRANCHES':
return {
...state,
branches: action.payload
}
case 'SET_CURRENT_BRANCH':
return {
...state,
currentBranch: action.payload
}
case 'SET_CURRENT_HEAD':
return {
...state,
currentHead: action.payload
}
case 'SET_CAN_USE_APP':
return {
...state,
canUseApp: action.payload
}
case 'SET_REPO_NAME':
return {
...state,
reponame: action.payload
}
case 'SET_LOADING':
return {
...state,
loading: action.payload
}
case 'SET_REPOS':
return {
...state,
repositories: action.payload
}
case 'SET_REMOTE_BRANCHES':
return {
...state,
remoteBranches: action.payload
}
case 'SET_CAN_COMMIT':
return {
...state,
canCommit: action.payload
}
case 'SET_REMOTES':
return {
...state,
remotes: action.payload
}
case 'SET_UPSTREAM':
return {
...state,
upstream: action.payload
}
case 'SET_COMMIT_CHANGES':
action.payload.forEach((change: commitChange) => {
state.commitChanges.find((c) => c.hashModified === change.hashModified && c.hashOriginal === change.hashOriginal && c.path === change.path) ? null : state.commitChanges.push(change)
})
if (file) {
file.status = fileStatusResult.status
file.statusNames = fileStatusResult.statusNames
}
})
return {
...state,
staged: getFilesByStatus("staged", state.fileStatusResult),
modified: getFilesByStatus("modified", state.fileStatusResult),
untracked: getFilesByStatus("untracked", state.fileStatusResult),
deleted: getFilesByStatus("deleted", state.fileStatusResult),
allchangesnotstaged: allChangedButNotStagedFiles(state.fileStatusResult)
}
case 'SET_COMMITS':
return {
...state,
commits: action.payload,
localCommitCount: action.payload.length
}
case 'SET_BRANCHES':
return {
...state,
branches: action.payload
}
case 'SET_CURRENT_BRANCH':
return {
...state,
currentBranch: action.payload
}
case 'SET_CURRENT_HEAD':
return {
...state,
currentHead: action.payload
}
case 'SET_CAN_USE_APP':
return {
...state,
canUseApp: action.payload
}
case 'SET_REPO_NAME':
return {
...state,
reponame: action.payload
}
case 'SET_LOADING':
return {
...state,
loading: action.payload
}
case 'SET_REPOS':
return {
...state,
repositories: action.payload
}
case 'SET_REMOTE_BRANCHES':
return {
...state,
remoteBranches: action.payload
}
case 'SET_CAN_COMMIT':
return {
...state,
canCommit: action.payload
}
case 'SET_REMOTES':
return {
...state,
remotes: action.payload
}
case 'SET_UPSTREAM':
return {
...state,
upstream: action.payload
}
case 'SET_COMMIT_CHANGES':
action.payload.forEach((change: commitChange) => {
state.commitChanges.find((c) => c.hashModified === change.hashModified && c.hashOriginal === change.hashOriginal && c.path === change.path) ? null : state.commitChanges.push(change)
})
return {
...state,
commitChanges: [...state.commitChanges]
}
case 'RESET_REMOTE_BRANCH_COMMITS':
if (state.remoteBranchCommits[action.payload.branch.name]) {
delete state.remoteBranchCommits[action.payload.branch.name]
}
return {
...state,
remoteBranchCommits: { ...state.remoteBranchCommits }
}
case 'SET_REMOTE_BRANCH_COMMITS':
if (state.remoteBranchCommits[action.payload.branch.name]) {
state.remoteBranchCommits[action.payload.branch.name].push(...action.payload.commits)
} else {
state.remoteBranchCommits[action.payload.branch.name] = action.payload.commits
}
return {
...state,
remoteBranchCommits: { ...state.remoteBranchCommits }
}
case 'SET_LOCAL_BRANCH_COMMITS':
state.localBranchCommits[action.payload.branch.name] = action.payload.commits
return {
...state,
localBranchCommits: { ...state.localBranchCommits }
}
case 'SET_BRANCH_DIFFERENCES':
state.branchDifferences[`${action.payload.remote.name}/${action.payload.branch.name}`] = action.payload.branchDifference
return {
...state,
branchDifferences: { ...state.branchDifferences }
}
case 'SET_GITHUB_USER':
return {
...state,
gitHubUser: action.payload
}
case 'SET_GITHUB_ACCESS_TOKEN':
return {
...state,
gitHubAccessToken: action.payload
}
case 'SET_SCOPES':
return {
...state,
gitHubScopes: action.payload
}
case 'SET_USER_EMAILS':
return {
...state,
userEmails: action.payload
}
case 'SET_DEFAULT_REMOTE':
return {
...state,
defaultRemote: action.payload
}
case 'SET_LOG':
if (state.log.length > 0 && state.log[[...state.log].length - 1].message === action.payload.message) {
return {
...state,
log: [...state.log]
}
}
return {
...state,
log: [...state.log, action.payload]
}
case 'CLEAR_LOG':
return {
...state,
log: []
}
return {
...state,
commitChanges: [...state.commitChanges]
}
case 'RESET_REMOTE_BRANCH_COMMITS':
if (state.remoteBranchCommits[action.payload.branch.name]) {
delete state.remoteBranchCommits[action.payload.branch.name]
}
return {
...state,
remoteBranchCommits: { ...state.remoteBranchCommits }
}
case 'SET_REMOTE_BRANCH_COMMITS':
if (state.remoteBranchCommits[action.payload.branch.name]) {
state.remoteBranchCommits[action.payload.branch.name].push(...action.payload.commits)
} else {
state.remoteBranchCommits[action.payload.branch.name] = action.payload.commits
}
return {
...state,
remoteBranchCommits: { ...state.remoteBranchCommits }
}
case 'SET_LOCAL_BRANCH_COMMITS':
state.localBranchCommits[action.payload.branch.name] = action.payload.commits
return {
...state,
localBranchCommits: { ...state.localBranchCommits }
}
case 'SET_BRANCH_DIFFERENCES':
state.branchDifferences[`${action.payload.remote.name}/${action.payload.branch.name}`] = action.payload.branchDifference
return {
...state,
branchDifferences: { ...state.branchDifferences }
}
case 'SET_GITHUB_USER':
return {
...state,
gitHubUser: action.payload
}
case 'SET_GITHUB_ACCESS_TOKEN':
return {
...state,
gitHubAccessToken: action.payload
}
case 'SET_SCOPES':
return {
...state,
gitHubScopes: action.payload
}
case 'SET_USER_EMAILS':
return {
...state,
userEmails: action.payload
}
case 'SET_DEFAULT_REMOTE':
return {
...state,
defaultRemote: action.payload
}
case 'SET_LOG':
if (state.log.length > 0 && state.log[[...state.log].length - 1].message === action.payload.message) {
return {
...state,
log: [...state.log]
}
}
return {
...state,
log: [...state.log, action.payload]
}
case 'CLEAR_LOG':
return {
...state,
log: []
}
case 'SET_STORAGE':
return {
...state,
storage: action.payload
}
}
}

@ -163,7 +163,7 @@ export type gitState = {
fileStatusResult: fileStatusResult[]
canUseApp: boolean
loading: boolean
storageUsed: any
storage: storage
reponame: string
staged: fileStatusResult[]
untracked: fileStatusResult[]
@ -282,7 +282,13 @@ export const defaultGitState: gitState = {
allchangesnotstaged: [],
canUseApp: true,
loading: false,
storageUsed: {},
storage: {
used: 0,
total: 0,
available: 0,
percentUsed: 0,
enabled: false
},
reponame: "",
repositories: [],
remoteBranches: [],
@ -324,6 +330,14 @@ export type sourceControlGroup = {
name: string
}
export type storage = {
used: number,
total: number
available: number
percentUsed: number
enabled: boolean
}
export interface fileStatusAction {
type: string,
payload: fileStatusResult[]

Loading…
Cancel
Save