From cbc0650cbbba540a4d6cb9bc581d1cdb33f8466d Mon Sep 17 00:00:00 2001 From: vipinpaul Date: Mon, 9 Oct 2023 16:57:48 +0530 Subject: [PATCH] Relocating the Sync related files to components --- renderer/pages/sync.js | 2 +- .../components/Sync/Ag/SyncToGitea-backup.js | 107 ------------------ .../Sync/Gitea/EditorSync/EditorSync.js | 4 +- .../Sync/Gitea/EditorSync/EditorSyncUtils.js | 2 +- renderer/src/components/Sync/Gitea/Gitea.js | 2 +- .../Gitea/ProjectMerge/ProjectMergePopUp.js | 2 +- .../Gitea/ProjectMerge/ProjectMergeUtils.js | 2 +- .../Sync/PopUpModal.js | 0 .../Sync/{Ag => Scribe}/ProjectFileBrowser.js | 0 .../Sync/{Ag => Scribe}/SyncToGitea.js | 0 .../Sync/{Ag => Scribe}/SyncToGiteaUtils.js | 0 .../projects => components/Sync}/Sync.js | 18 ++- .../components/Sync/SyncContextProvider.js | 2 +- .../components/Sync/{ => hooks}/useSync.js | 6 +- 14 files changed, 19 insertions(+), 128 deletions(-) delete mode 100644 renderer/src/components/Sync/Ag/SyncToGitea-backup.js rename renderer/src/{layouts => components}/Sync/PopUpModal.js (100%) rename renderer/src/components/Sync/{Ag => Scribe}/ProjectFileBrowser.js (100%) rename renderer/src/components/Sync/{Ag => Scribe}/SyncToGitea.js (100%) rename renderer/src/components/Sync/{Ag => Scribe}/SyncToGiteaUtils.js (100%) rename renderer/src/{modules/projects => components/Sync}/Sync.js (94%) rename renderer/src/components/Sync/{ => hooks}/useSync.js (93%) diff --git a/renderer/pages/sync.js b/renderer/pages/sync.js index aaddb4a6f..ca59b328e 100644 --- a/renderer/pages/sync.js +++ b/renderer/pages/sync.js @@ -1,4 +1,4 @@ -import Sync from '@/modules/projects/Sync'; +import Sync from '@/components/Sync/Sync'; import AuthenticationContextProvider from '@/components/Login/AuthenticationContextProvider'; import AutographaContextProvider from '@/components/context/AutographaContext'; import SyncContextProvider from '@/components/Sync/SyncContextProvider'; diff --git a/renderer/src/components/Sync/Ag/SyncToGitea-backup.js b/renderer/src/components/Sync/Ag/SyncToGitea-backup.js deleted file mode 100644 index 7a08dd2b1..000000000 --- a/renderer/src/components/Sync/Ag/SyncToGitea-backup.js +++ /dev/null @@ -1,107 +0,0 @@ -import * as localForage from 'localforage'; -import * as logger from '../../../logger'; -import { - handleCreateRepo, createFiletoServer, updateFiletoServer, getOrPutLastSyncInAgSettings, -} from './SyncToGiteaUtils'; -import packageInfo from '../../../../../package.json'; - -// upload project to gitea main function -export async function uploadToGitea(projectDataAg, auth, setSyncProgress, notifyStatus, addNotification) { - logger.debug('ToGiteaUtils.js', 'in uploadTOGitea'); - const projectData = projectDataAg.projectMeta; - const projectId = Object.keys(projectData.identification.primary[packageInfo.name])[0]; - const projectName = projectData.identification.name.en; - const ingredientsObj = projectData.ingredients; - const projectCreated = projectData.meta.dateCreated.split('T')[0]; - const repoName = `${packageInfo.name}-${projectData.languages[0].tag}-${projectData.type.flavorType.flavor.name}-${projectName.replace(/[\s+ -]/g, '_')}`; - - localForage.getItem('userProfile').then(async (user) => { - const newpath = localStorage.getItem('userPath'); - const fs = window.require('fs'); - const path = require('path'); - const projectsMetaPath = path.join(newpath, packageInfo.name, 'users', user?.username, 'projects', `${projectName}_${projectId}`); - setSyncProgress((prev) => ( - { - ...prev, - totalFiles: Object.keys(ingredientsObj).length, - } - )); - // Create A REPO for the project - try { - const createResult = await handleCreateRepo(repoName.toLowerCase(), auth); - if (createResult.id) { - logger.debug('SyncToGitea.js', 'repo created success : starting sync'); - setSyncProgress((prev) => ({ - ...prev, - syncStarted: true, - })); - // read metadata - const Metadata = await fs.readFileSync(path.join(projectsMetaPath, 'metadata.json')); - await createFiletoServer(JSON.stringify(Metadata), 'metadata.json', `${user?.username}/${projectCreated}.1`, createResult.name, auth); - logger.debug('SyncToGitea.js', 'read and uploaded metaData to repo'); - // Read ingredients - /* eslint-disable no-await-in-loop */ - /* eslint-disable no-restricted-syntax */ - for (const key in ingredientsObj) { - if (Object.prototype.hasOwnProperty.call(ingredientsObj, key)) { - setSyncProgress((prev) => ({ - ...prev, - completedFiles: prev.completedFiles + 1, - })); - // setTotalUploadedAg((prev) => prev + 1); - const Metadata1 = fs.readFileSync(path.join(projectsMetaPath, key), 'utf8'); - await createFiletoServer(Metadata1, key, `${user?.username}/${projectCreated}.1`, createResult.name, auth); - logger.debug('SyncToGitea.js', `Read and uploaded ${key} to repo`); - } - } - // update the scribe-settings - sync - await getOrPutLastSyncInAgSettings('put', projectData, auth?.user?.username); - notifyStatus('success', 'Sync completed successfully !!'); - await addNotification('Sync', 'Project Sync Successfull', 'success'); - logger.debug('SyncToGitea.js', 'sync successfull - first time'); - } else if (createResult.message.includes('409')) { - logger.debug('SyncToGitea.js', 'repo exist update section : 409 error'); - setSyncProgress((prev) => ({ - ...prev, - syncStarted: true, - })); - const metadataContent = fs.readFileSync(path.join(projectsMetaPath, 'metadata.json')); - await updateFiletoServer(JSON.stringify(metadataContent), 'metadata.json', `${user?.username}/${projectCreated}.1`, repoName, auth); - logger.debug('SyncToGitea.js', 'read and updated metaData to repo'); - // Read ingredients and update - for (const key in ingredientsObj) { - if (Object.prototype.hasOwnProperty.call(ingredientsObj, key)) { - setSyncProgress((prev) => ({ - ...prev, - completedFiles: prev.completedFiles + 1, - })); - const metadata1 = await fs.readFileSync(path.join(projectsMetaPath, key), 'utf8'); - // setTotalUploadedAg((prev) => prev + 1); - await updateFiletoServer(metadata1, key, `${user?.username}/${projectCreated}.1`, repoName, auth); - logger.debug('SyncToGitea.js', `Read and updated ${key} to repo`); - } - } - // update the scribe-settings - sync update - await getOrPutLastSyncInAgSettings('put', projectData, auth?.user?.username); - logger.debug('SyncToGitea.js', 'Updated last Sync data'); - notifyStatus('success', 'Sync completed successfully !!'); - await addNotification('Sync', 'Project Sync Successfull', 'success'); - logger.debug('SyncToGitea.js', 'sync successfull - update sync'); - } else { - logger.debug('SyncToGitea.js', 'create/update not success on sync : ', createResult.message); - notifyStatus('failure', 'Something went wrong, sync failed, check internet'); - logger.debug('SyncToGitea.js', 'sync failed - may be due to internet'); - } - } catch (err) { - logger.debug('SyncToGitea.js', `Error on Sync create/update : ${err}`); - notifyStatus('failure', `Sync failed : ${err}`); - await addNotification('Sync', err?.message || err, 'failure'); - } finally { - setSyncProgress({ - syncStarted: false, - totalFiles: 0, - completedFiles: 0, - }); - } - }); -} diff --git a/renderer/src/components/Sync/Gitea/EditorSync/EditorSync.js b/renderer/src/components/Sync/Gitea/EditorSync/EditorSync.js index 3cce09dcf..e8eac5781 100644 --- a/renderer/src/components/Sync/Gitea/EditorSync/EditorSync.js +++ b/renderer/src/components/Sync/Gitea/EditorSync/EditorSync.js @@ -7,9 +7,9 @@ import Link from 'next/link'; import { SnackBar } from '@/components/SnackBar'; import menuStyles from '@/layouts/editor/MenuBar.module.css'; import useAddNotification from '@/components/hooks/useAddNotification'; -import PopUpModal from '@/layouts/Sync/PopUpModal'; import ConfirmationModal from '@/layouts/editor/ConfirmationModal'; import { ReferenceContext } from '@/components/context/ReferenceContext'; +import PopUpModal from '../../PopUpModal'; import * as logger from '../../../../logger'; import CloudUploadIcon from '@/icons/basil/Outline/Files/Cloud-upload.svg'; import CloudCheckIcon from '@/icons/basil/Solid/Files/Cloud-check.svg'; @@ -17,7 +17,7 @@ import ProgressCircle from '../../ProgressCircle'; import Door43Logo from '@/icons/door43.svg'; import { getGiteaUsersList, handleEditorSync } from './EditorSyncUtils'; import useGetCurrentProjectMeta from '../../hooks/useGetCurrentProjectMeta'; -import { getOrPutLastSyncInAgSettings } from '../../Ag/SyncToGiteaUtils'; +import { getOrPutLastSyncInAgSettings } from '../../Scribe/SyncToGiteaUtils'; function EditorSync({ selectedProject }) { const [usersList, setUsersList] = useState([]); diff --git a/renderer/src/components/Sync/Gitea/EditorSync/EditorSyncUtils.js b/renderer/src/components/Sync/Gitea/EditorSync/EditorSyncUtils.js index dae9345ab..0e32b184e 100644 --- a/renderer/src/components/Sync/Gitea/EditorSync/EditorSyncUtils.js +++ b/renderer/src/components/Sync/Gitea/EditorSync/EditorSyncUtils.js @@ -2,7 +2,7 @@ import * as localForage from 'localforage'; import { environment } from '../../../../../environment'; import * as logger from '../../../../logger'; import packageInfo from '../../../../../../package.json'; -import { uploadToGitea } from '../../Ag/SyncToGitea'; +import { uploadToGitea } from '../../Scribe/SyncToGitea'; export async function getGiteaUsersList() { let usersList = []; diff --git a/renderer/src/components/Sync/Gitea/Gitea.js b/renderer/src/components/Sync/Gitea/Gitea.js index bee23608c..de029713d 100644 --- a/renderer/src/components/Sync/Gitea/Gitea.js +++ b/renderer/src/components/Sync/Gitea/Gitea.js @@ -5,7 +5,7 @@ import { } from 'gitea-react-toolkit'; import GiteaFileBrowser from './GiteaFileBrowser'; import { environment } from '../../../../environment'; -import { createSyncProfile } from '../Ag/SyncToGiteaUtils'; +import { createSyncProfile } from '../Scribe/SyncToGiteaUtils'; const Gitea = ({ setAuth, setRepo }) => { const [authentication, setAuthentication] = useState(); diff --git a/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergePopUp.js b/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergePopUp.js index db5190522..1072bfeda 100644 --- a/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergePopUp.js +++ b/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergePopUp.js @@ -3,9 +3,9 @@ import React, { useCallback, useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import ConfirmationModal from '@/layouts/editor/ConfirmationModal'; import PropTypes from 'prop-types'; -import PopUpModal from '@/layouts/Sync/PopUpModal'; import { SnackBar } from '@/components/SnackBar'; import useAddNotification from '@/components/hooks/useAddNotification'; +import PopUpModal from '../../PopUpModal'; import { VerticalLinearStepper } from '../../VerticalStepperProgress'; import { mergeProgressSteps } from './ProjectMergeConst'; import LoadingSpinner from '../../LoadingSpinner'; diff --git a/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergeUtils.js b/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergeUtils.js index c37c2bdcc..cb25d7afd 100644 --- a/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergeUtils.js +++ b/renderer/src/components/Sync/Gitea/ProjectMerge/ProjectMergeUtils.js @@ -1,4 +1,4 @@ -import { createFiletoServer } from '../../Ag/SyncToGiteaUtils'; +import { createFiletoServer } from '../../Scribe/SyncToGiteaUtils'; import * as logger from '../../../../logger'; import packageInfo from '../../../../../../package.json'; diff --git a/renderer/src/layouts/Sync/PopUpModal.js b/renderer/src/components/Sync/PopUpModal.js similarity index 100% rename from renderer/src/layouts/Sync/PopUpModal.js rename to renderer/src/components/Sync/PopUpModal.js diff --git a/renderer/src/components/Sync/Ag/ProjectFileBrowser.js b/renderer/src/components/Sync/Scribe/ProjectFileBrowser.js similarity index 100% rename from renderer/src/components/Sync/Ag/ProjectFileBrowser.js rename to renderer/src/components/Sync/Scribe/ProjectFileBrowser.js diff --git a/renderer/src/components/Sync/Ag/SyncToGitea.js b/renderer/src/components/Sync/Scribe/SyncToGitea.js similarity index 100% rename from renderer/src/components/Sync/Ag/SyncToGitea.js rename to renderer/src/components/Sync/Scribe/SyncToGitea.js diff --git a/renderer/src/components/Sync/Ag/SyncToGiteaUtils.js b/renderer/src/components/Sync/Scribe/SyncToGiteaUtils.js similarity index 100% rename from renderer/src/components/Sync/Ag/SyncToGiteaUtils.js rename to renderer/src/components/Sync/Scribe/SyncToGiteaUtils.js diff --git a/renderer/src/modules/projects/Sync.js b/renderer/src/components/Sync/Sync.js similarity index 94% rename from renderer/src/modules/projects/Sync.js rename to renderer/src/components/Sync/Sync.js index 4ab5ba642..6518afe1d 100644 --- a/renderer/src/modules/projects/Sync.js +++ b/renderer/src/components/Sync/Sync.js @@ -1,8 +1,6 @@ import { useState, useContext } from 'react'; import { useTranslation } from 'react-i18next'; - import ProjectsLayout from '@/layouts/projects/Layout'; -// import Gitea from '@/components/Sync/Gitea/Gitea'; import AuthenticationContextProvider from '@/components/Login/AuthenticationContextProvider'; import ProjectContextProvider from '@/components/context/ProjectContext'; import ReferenceContextProvider from '@/components/context/ReferenceContext'; @@ -10,16 +8,16 @@ import { CloudArrowDownIcon, CloudArrowUpIcon, } from '@heroicons/react/24/outline'; -import ProjectFileBrowser from '@/components/Sync/Ag/ProjectFileBrowser'; -import Gitea from '@/components/Sync/Gitea/Gitea'; -import { SyncContext } from '@/components/Sync/SyncContextProvider'; import { SnackBar } from '@/components/SnackBar'; -import { uploadToGitea } from '@/components/Sync/Ag/SyncToGitea'; -import { downloadFromGitea } from '@/components/Sync/Gitea/SyncFromGitea'; import useAddNotification from '@/components/hooks/useAddNotification'; import ConfirmationModal from '@/layouts/editor/ConfirmationModal'; -import { cloneAndSetProject, updateSettingsFiles } from '@/components/Sync/Gitea/SyncFromGiteaUtils'; -import { checkoutJsonFiles, pullProject } from '@/components/Sync/Isomorphic/utils'; +import { SyncContext } from './SyncContextProvider'; +import { uploadToGitea } from './Scribe/SyncToGitea'; +import { downloadFromGitea } from './Gitea/SyncFromGitea'; +import { cloneAndSetProject, updateSettingsFiles } from './Gitea/SyncFromGiteaUtils'; +import { checkoutJsonFiles, pullProject } from './Isomorphic/utils'; +import Gitea from './Gitea/Gitea'; +import ProjectFileBrowser from './Scribe/ProjectFileBrowser'; import Door43Logo from '@/icons/door43.svg'; import * as logger from '../../logger'; @@ -81,7 +79,7 @@ export default function Sync() { // for pull without conflict const checkoutFIles = await checkoutJsonFiles(pullData.fs, pullData.gitprojectDir, pullData.checkoutBranch); const pullStatus = checkoutFIles && await pullProject(pullData.fs, pullData.gitprojectDir, pullData.userBranch, auth.token.sha1, pullData.checkoutBranch); - pullStatus?.status && await updateSettingsFiles( + pullStatus?.status && await updateSettingsFiles( pullData.fs, pullData.sbDataObject, pullData.projectDir, diff --git a/renderer/src/components/Sync/SyncContextProvider.js b/renderer/src/components/Sync/SyncContextProvider.js index 955e4a583..a05a787cf 100644 --- a/renderer/src/components/Sync/SyncContextProvider.js +++ b/renderer/src/components/Sync/SyncContextProvider.js @@ -1,7 +1,7 @@ /* eslint-disable react/jsx-no-constructed-context-values */ import React, { createContext } from 'react'; import PropTypes from 'prop-types'; -import useSync from './useSync'; +import useSync from './hooks/useSync'; export const SyncContext = createContext(); diff --git a/renderer/src/components/Sync/useSync.js b/renderer/src/components/Sync/hooks/useSync.js similarity index 93% rename from renderer/src/components/Sync/useSync.js rename to renderer/src/components/Sync/hooks/useSync.js index 761b6fbaa..5300854c1 100644 --- a/renderer/src/components/Sync/useSync.js +++ b/renderer/src/components/Sync/hooks/useSync.js @@ -1,8 +1,8 @@ import { useState, useEffect } from 'react'; import * as localForage from 'localforage'; -import * as logger from '../../logger'; -import fetchProjectsMeta from '../../core/projects/fetchProjectsMeta'; -import { getOrPutLastSyncInAgSettings } from './Ag/SyncToGiteaUtils'; +import * as logger from '../../../logger'; +import fetchProjectsMeta from '../../../core/projects/fetchProjectsMeta'; +import { getOrPutLastSyncInAgSettings } from '../Scribe/SyncToGiteaUtils'; function useSync() { const projectList = [];