From b06844d0302568ffb32040403c00aa90c00b9cb1 Mon Sep 17 00:00:00 2001 From: VIPIN PAUL <37212471+vipinpaul@users.noreply.github.com> Date: Tue, 21 Nov 2023 18:05:20 +0530 Subject: [PATCH] fixing star/unstar issue and cleaning up the code base (#250) * fixing star/unstar issue and cleaning up the code base * font-save fix * font-save for editor fix * debugging * fixing editor font save issue --- .../components/EditorPage/Scribex/Scribex.jsx | 201 -------- .../src/components/Login/LeftLogin copy 2.js | 435 ------------------ .../src/components/Login/LeftLogin org.js | 432 ----------------- .../src/core/projects/updateAgSettings.js | 9 +- 4 files changed, 4 insertions(+), 1073 deletions(-) delete mode 100644 renderer/src/components/EditorPage/Scribex/Scribex.jsx delete mode 100644 renderer/src/components/Login/LeftLogin copy 2.js delete mode 100644 renderer/src/components/Login/LeftLogin org.js diff --git a/renderer/src/components/EditorPage/Scribex/Scribex.jsx b/renderer/src/components/EditorPage/Scribex/Scribex.jsx deleted file mode 100644 index d410f6254..000000000 --- a/renderer/src/components/EditorPage/Scribex/Scribex.jsx +++ /dev/null @@ -1,201 +0,0 @@ -import { - useEffect, useState, useContext, Fragment, -} from 'react'; -import { useProskomma, useImport, useCatalog } from 'proskomma-react-hooks'; -import { useDeepCompareEffect } from 'use-deep-compare'; -import { LockClosedIcon, BookmarkIcon, LockOpenIcon } from '@heroicons/react/24/outline'; -import BibleNavigationX from '@/modules/biblenavigation/BibleNavigationX'; -import usePerf from '@/components/hooks/scribex/usePerf'; -import htmlMap from '@/components/hooks/scribex/htmlmap'; -import { ScribexContext } from '@/components/context/ScribexContext'; -import { ReferenceContext } from '@/components/context/ReferenceContext'; -import { ProjectContext } from '@/components/context/ProjectContext'; -import EditorSideBar from '@/modules/editorsidebar/EditorSideBar'; -import MenuDropdown from '@/components/MenuDropdown/MenuDropdown'; -import Buttons from './Buttons'; -import Editor from './Editor'; -import InsertMenu from './InsertMenu'; - -export default function Scribex(props) { - const { state, actions } = useContext(ScribexContext); - const { verbose } = state; - const { usfmData, bookAvailable } = props; - const [selectedBook, setSelectedBook] = useState(); - const [bookChange, setBookChange] = useState(false); - const [chapterNumber, setChapterNumber] = useState(1); - const [verseNumber, setVerseNumber] = useState(1); - const [triggerVerseInsert, setTriggerVerseInsert] = useState(false); - const [newVerChapNumber, setInsertNumber] = useState(''); - const [insertVerseRChapter, setInsertVerseRChapter] = useState(''); - const [selectedText, setSelectedText] = useState(); - - const handleClick = (number, title) => { - setInsertNumber(number); - setInsertVerseRChapter(title); - setTriggerVerseInsert(!triggerVerseInsert); - }; - - let selectedDocument; - - const { proskomma, stateId, newStateId } = useProskomma({ verbose }); - const { done } = useImport({ - proskomma, - stateId, - newStateId, - documents: usfmData, - }); - - const { - state: { - bookId, selectedFont, fontSize, projectScriptureDir, - }, - actions: { handleSelectedFont }, - } = useContext(ReferenceContext); - - const { - states: { scrollLock }, - actions: { setScrollLock }, - } = useContext(ProjectContext); - - const { - states: { openSideBar }, - actions: { setOpenSideBar }, - } = useContext(ProjectContext); - - function closeSideBar(open) { - setOpenSideBar(open); - } - - useEffect(() => { - setSelectedBook(bookId.toUpperCase()); - setBookChange(true); - }, [bookId]); - - const { catalog } = useCatalog({ proskomma, stateId, verbose }); - const { id: docSetId, documents } = (done && catalog.docSets[0]) || {}; - if (done) { - selectedDocument = documents?.find( - (doc) => doc.bookCode === selectedBook, - ); - } - - const { bookCode, h: bookName } = selectedDocument || {}; - const ready = (docSetId && bookCode) || false; - const isLoading = !done || !ready; - const { state: perfState, actions: perfActions } = usePerf({ - proskomma, - ready, - docSetId, - bookCode, - verbose, - htmlMap, - }); - const { htmlPerf } = perfState; - - useDeepCompareEffect(() => { - if (htmlPerf && htmlPerf.mainSequenceId !== state.sequenceIds[0]) { - actions.setSequenceIds([htmlPerf?.mainSequenceId]); - } - }, [htmlPerf, state.sequenceIds, perfState]); - const _props = { - ...state, - ...perfState, - ...actions, - ...perfActions, - triggerVerseInsert, - chapterNumber, - verseNumber, - isLoading, - bookName, - bookChange, - bookAvailable, - setBookChange, - setChapterNumber, - setVerseNumber, - newVerChapNumber, - insertVerseRChapter, - selectedText, - setSelectedText, - }; - return ( - <> - -
-
-
- -
- Editor -
-
-
- {scrollLock === true ? ( -
-
-
-
-
-
-
- -
-
- - -
-
-
- -
1.3) ? 1.5 : '', - direction: `${projectScriptureDir === 'RTL' ? 'rtl' : 'auto'}`, - }} - className="border-l-2 border-r-2 border-secondary pb-16 overflow-auto h-full scrollbars-width leading-8" - > - -
-
- - ); -} diff --git a/renderer/src/components/Login/LeftLogin copy 2.js b/renderer/src/components/Login/LeftLogin copy 2.js deleted file mode 100644 index 2d6950d00..000000000 --- a/renderer/src/components/Login/LeftLogin copy 2.js +++ /dev/null @@ -1,435 +0,0 @@ -import { Dialog, Tab, Transition } from '@headlessui/react'; -import React, { - Fragment, useContext, useEffect, useState, -} from 'react'; -import * as localForage from 'localforage'; -import { TrashIcon } from '@heroicons/react/24/outline'; -import { Restore } from '@material-ui/icons'; -import { createUser, handleLogin, writeToFile } from '../../core/Login/handleLogin'; -import { isElectron } from '../../core/handleElectron'; -import * as logger from '../../logger'; -import { AuthenticationContext } from './AuthenticationContextProvider'; - -const LeftLogin = () => { - const [isOpen, setIsOpen] = useState(false); - const [open, setOpen] = useState(false); - const [values, setValues] = useState({}); - const [text, setText] = useState(''); - const [newOpen, setNewOpen] = useState(false); - const [users, setUsers] = useState([]); - const { - action: { generateToken }, - } = useContext(AuthenticationContext); - // eslint-disable-next-line no-unused-vars - const [valid, setValid] = useState({ - username: false, - password: false, - }); - const [showArchived, setShowArchived] = useState(false); - const [userNameError, setUserNameError] = useState(false); - - /* Checking if the users array is empty, if it is, it is getting the users from localForage and - setting the users array to the users from localForage. */ - useEffect(() => { - const checkUsers = async () => { - if (users.length === 0) { - const user = await localForage.getItem('users'); - if (user) { - setUsers(user); - } - } - }; - checkUsers(); - }, [users]); - - function closeModal() { - setIsOpen(false); - setShowArchived(false); - } - function openModal() { - setIsOpen(true); - } - function closeAccountModal() { - setOpen(false); - setValues({}); - } - function openAccountModal() { - setOpen(true); - } - const handleChange = (event) => { - setValues({ ...values, username: event.target.value }); - setUserNameError(false); - }; - - /** - * If the username is not empty, set the username error to false. If the username is empty, set the - * username error to true. - * @param values - the values of the form - * @returns The return value is a boolean. - */ - const handleValidation = (values) => { - let user; - if (values.username) { - user = true; - setUserNameError(false); - setNewOpen(false); - } else if (values.username === '') { - user = false; - setUserNameError(true); - } else { - user = false; - setUserNameError(true); - } - return user; - }; - /* Sorting the users array by the lastSeen property. */ - const sortedUsers = [...users].sort((a, b) => Date.parse(b.lastSeen) - Date.parse(a.lastSeen)); - /** - * Checks if the user is existing or not, if not then it creates a new user and generates a token - * for the user. - * @param values - { - */ - const handleSubmit = async (values) => { - localForage.setItem('appMode', 'offline'); - logger.debug('Login.js', 'In handleSubmit'); - if (isElectron()) { - // router.push('/main'); - // The below code is commented for UI dev purpose. - if (handleValidation(values)) { - const fs = window.require('fs'); - logger.debug( - 'LeftLogin.js', - 'Triggers handleLogin to check whether the user is existing or not', - ); - const user = await handleLogin(users, values); - if (user) { - logger.debug( - 'LeftLogin.js', - 'Triggers generateToken to generate a Token for the user', - ); - generateToken(user); - } else { - logger.debug( - 'LeftLogin.js', - 'Triggers createUser for creating a new user', - ); - const user = await createUser(values, fs); - logger.debug( - 'LeftLogin.js', - 'Triggers generateToken to generate a Token for the user', - ); - generateToken(user); - } - } - } - }; - /** - * When the form is submitted, prevent the default action, then call the handleSubmit function with - * the values from the form, then reset the form values. - * @param event - the event object - */ - const displayError = (errorText) => { - setNewOpen(true); - setTimeout(() => { - setNewOpen(false); - }, 2000); - setText(errorText); - }; - function formSubmit(event) { - event.preventDefault(); - if (values.username.length < 3 || values.username.length > 15) { - displayError('The input has to be between 3 and 15 characters long'); - } else if (users.length > 0 && users.find((item) => (item.username.toLowerCase() === values.username.toLowerCase().trim()))) { - displayError('User exists, Check archived and active tab by click on view more.'); - } else { - handleSubmit(values); - setValues({}); - } - } - function classNames(...classes) { - return classes.filter(Boolean).join(' '); - } - - function archiveUser(users, selectedUser) { - const archivedUsers = users.map((user) => { - if (user.username === selectedUser.username) { - return { ...user, isArchived: true }; - } - return user; - }); - - setUsers(archivedUsers); - localForage.setItem('users', archivedUsers); - writeToFile(archivedUsers); - } - function restoreUser(users, selectedUser) { - const activeUsers = users.map((user) => { - if (user.username === selectedUser.username) { - return { ...user, isArchived: false }; - } - return user; - }); - setUsers(activeUsers); - localForage.setItem('users', activeUsers); - writeToFile(activeUsers); - } - const filterUsers = (user) => { - if (user.isArchived === showArchived || (user.isArchived === undefined && showArchived === false)) { - return true; - } - return false; - }; - return ( - //
-
-

- {sortedUsers.length === 0 ? 'Welcome' : 'Welcome Back!'} -

- {/*

- Welcome back! Login to access Scribe Scripture -

*/} -
-
- {sortedUsers?.filter(filterUsers).slice(0, 5).map((user) => ( -
{ - handleSubmit({ username: user?.username }); - }} - > - {user.username} -
- ))} -
- {sortedUsers.length === 0 ? (
) : ( -
- -
-)} - - - -
- -
-
- - - setShowArchived((value) => !value)}> - - classNames( - 'w-full text-md items-center justify-center outline-none font-bold py-4 leading-5 rounded-t-lg', - '', - selected - ? 'text-primary bg-gray-200' - : 'text-gray-400 hover:text-gray-500 border-b bg-white', - )} - > - Active - - classNames( - 'w-full text-md items-center justify-center outline-none font-bold py-4 leading-5 rounded-t-lg', - selected - ? ' text-error bg-gray-200 ' - : 'text-gray-400 hover:text-gray-500 border-b bg-white ', - )} - > - Archived - - - - - -
- {sortedUsers.filter(filterUsers).map((user) => ( -
-
{ handleSubmit({ username: user.username }); }} - className="w-full p-4 py-3 text-sm rounded-lg cursor-pointer bg-[#F9F9F9] hover:bg-primary hover:text-white border border-[#E3E3E3] font-semibold" - > - -

- {user.username} -

-
- -
- ))} -
-
- -
- {sortedUsers.filter(filterUsers).map((user) => ( -
-
-

- {user.username} -

-
- - -
- ))} -
-
-
-
-
-
-
-
-
-
-
- - - - -
- - -
-
- - - - Create New User* - - -
- - {userNameError && ( - - Required - - )} -
- {newOpen && ( - {text} - )} -
- - -
-
-
-
-
-
-
-
- {/* */} -
-
- ); -}; - -export default LeftLogin; diff --git a/renderer/src/components/Login/LeftLogin org.js b/renderer/src/components/Login/LeftLogin org.js deleted file mode 100644 index c05dacc54..000000000 --- a/renderer/src/components/Login/LeftLogin org.js +++ /dev/null @@ -1,432 +0,0 @@ -import { Dialog, Tab, Transition } from '@headlessui/react'; -import React, { - Fragment, useContext, useEffect, useState, -} from 'react'; -import * as localForage from 'localforage'; -import { TrashIcon } from '@heroicons/react/24/outline'; -import { Restore } from '@material-ui/icons'; -import { createUser, handleLogin, writeToFile } from '../../core/Login/handleLogin'; -import { isElectron } from '../../core/handleElectron'; -import * as logger from '../../logger'; -import { AuthenticationContext } from './AuthenticationContextProvider'; - -const LeftLogin = () => { - const [isOpen, setIsOpen] = useState(false); - const [open, setOpen] = useState(false); - const [values, setValues] = useState({}); - const [text, setText] = useState(''); - const [newOpen, setNewOpen] = useState(false); - const [users, setUsers] = useState([]); - const { - action: { generateToken }, - } = useContext(AuthenticationContext); - // eslint-disable-next-line no-unused-vars - const [valid, setValid] = useState({ - username: false, - password: false, - }); - const [showArchived, setShowArchived] = useState(false); - const [userNameError, setUserNameError] = useState(false); - - /* Checking if the users array is empty, if it is, it is getting the users from localForage and - setting the users array to the users from localForage. */ - useEffect(() => { - const checkUsers = async () => { - if (users.length === 0) { - const user = await localForage.getItem('users'); - if (user) { - setUsers(user); - } - } - }; - checkUsers(); - }, [users]); - - function closeModal() { - setIsOpen(false); - setShowArchived(false); - } - function openModal() { - setIsOpen(true); - } - function closeAccountModal() { - setOpen(false); - setValues({}); - } - function openAccountModal() { - setOpen(true); - } - const handleChange = (event) => { - setValues({ ...values, username: event.target.value }); - setUserNameError(false); - }; - - /** - * If the username is not empty, set the username error to false. If the username is empty, set the - * username error to true. - * @param values - the values of the form - * @returns The return value is a boolean. - */ - const handleValidation = (values) => { - let user; - if (values.username) { - user = true; - setUserNameError(false); - setNewOpen(false); - } else if (values.username === '') { - user = false; - setUserNameError(true); - } else { - user = false; - setUserNameError(true); - } - return user; - }; - /* Sorting the users array by the lastSeen property. */ - const sortedUsers = [...users].sort((a, b) => Date.parse(b.lastSeen) - Date.parse(a.lastSeen)); - /** - * Checks if the user is existing or not, if not then it creates a new user and generates a token - * for the user. - * @param values - { - */ - const handleSubmit = async (values) => { - localForage.setItem('appMode', 'offline'); - logger.debug('Login.js', 'In handleSubmit'); - if (isElectron()) { - // router.push('/main'); - // The below code is commented for UI dev purpose. - if (handleValidation(values)) { - const fs = window.require('fs'); - logger.debug( - 'LeftLogin.js', - 'Triggers handleLogin to check whether the user is existing or not', - ); - const user = await handleLogin(users, values); - if (user) { - logger.debug( - 'LeftLogin.js', - 'Triggers generateToken to generate a Token for the user', - ); - generateToken(user); - } else { - logger.debug( - 'LeftLogin.js', - 'Triggers createUser for creating a new user', - ); - const user = await createUser(values, fs); - logger.debug( - 'LeftLogin.js', - 'Triggers generateToken to generate a Token for the user', - ); - generateToken(user); - } - } - } - }; - /** - * When the form is submitted, prevent the default action, then call the handleSubmit function with - * the values from the form, then reset the form values. - * @param event - the event object - */ - const displayError = (errorText) => { - setNewOpen(true); - setTimeout(() => { - setNewOpen(false); - }, 2000); - setText(errorText); - }; - function formSubmit(event) { - event.preventDefault(); - if (values.username.length < 3 || values.username.length > 15) { - displayError('The input has to be between 3 and 15 characters long'); - } else if (users.length > 0 && users.find((item) => (item.username.toLowerCase() === values.username.toLowerCase().trim()))) { - displayError('User exists, Check archived and active tab by click on view more.'); - } else { - handleSubmit(values); - setValues({}); - } - } - function classNames(...classes) { - return classes.filter(Boolean).join(' '); - } - - function archiveUser(users, selectedUser) { - const archivedUsers = users.map((user) => { - if (user.username === selectedUser.username) { - return { ...user, isArchived: true }; - } - return user; - }); - - setUsers(archivedUsers); - localForage.setItem('users', archivedUsers); - writeToFile(archivedUsers); - } - function restoreUser(users, selectedUser) { - const activeUsers = users.map((user) => { - if (user.username === selectedUser.username) { - return { ...user, isArchived: false }; - } - return user; - }); - setUsers(activeUsers); - localForage.setItem('users', activeUsers); - writeToFile(activeUsers); - } - const filterUsers = (user) => { - if (user.isArchived === showArchived || (user.isArchived === undefined && showArchived === false)) { - return true; - } - return false; - }; - return ( -
-

Welcome!

-

- Welcome back! Login to access Scribe Scripture -

-
-
- {sortedUsers?.filter(filterUsers).slice(0, 5).map((user) => ( -
{ - handleSubmit({ username: user?.username }); - }} - > - {user.username} -
- ))} -
- {sortedUsers.length === 0 ? (
) : ( -
- -
-)} - - - -
- -
-
- - - setShowArchived((value) => !value)}> - - classNames( - 'w-full text-md items-center justify-center outline-none font-bold py-4 leading-5 rounded-t-lg', - '', - selected - ? 'text-primary bg-gray-200' - : 'text-gray-400 hover:text-gray-500 border-b bg-white', - )} - > - Active - - classNames( - 'w-full text-md items-center justify-center outline-none font-bold py-4 leading-5 rounded-t-lg', - selected - ? ' text-error bg-gray-200 ' - : 'text-gray-400 hover:text-gray-500 border-b bg-white ', - )} - > - Archived - - - - - -
- {sortedUsers.filter(filterUsers).map((user) => ( -
-
{ handleSubmit({ username: user.username }); }} - className="w-full p-4 py-3 text-sm rounded-lg cursor-pointer bg-[#F9F9F9] hover:bg-primary hover:text-white border border-[#E3E3E3] font-semibold" - > - -

- {user.username} -

-
- -
- ))} -
-
- -
- {sortedUsers.filter(filterUsers).map((user) => ( -
-
-

- {user.username} -

-
- - -
- ))} -
-
-
-
-
-
-
-
-
-
-
- - - - -
- - -
-
- - - - Create New User* - - -
- - {userNameError && ( - - Required - - )} -
- {newOpen && ( - {text} - )} -
- - -
-
-
-
-
-
-
-
- {/* */} -
-
- ); -}; - -export default LeftLogin; diff --git a/renderer/src/core/projects/updateAgSettings.js b/renderer/src/core/projects/updateAgSettings.js index 42894c9c3..0c27d716f 100644 --- a/renderer/src/core/projects/updateAgSettings.js +++ b/renderer/src/core/projects/updateAgSettings.js @@ -21,7 +21,7 @@ export const updateAgSettings = async (username, projectName, data, font) => { const folder = path.join(newpath, packageInfo.name, 'users', username, 'projects', projectName, result[0]); const settings = await fs.readFileSync(folder, 'utf8'); const setting = JSON.parse(settings); - if (settings.version !== environment.AG_SETTING_VERSION) { + if (setting.version !== environment.AG_SETTING_VERSION) { setting.version = environment.AG_SETTING_VERSION; if (!setting.sync && !setting.sync?.services) { setting.sync = { services: { door43: [] } }; @@ -30,11 +30,11 @@ export const updateAgSettings = async (username, projectName, data, font) => { } if (!setting.project[data.type.flavorType.flavor.name].font) { setting.project[data.type.flavorType.flavor.name].font = font || ''; - } else { - setting.project[data.type.flavorType.flavor.name].font = font || setting.project[data.type.flavorType.flavor.name].font; } } - setting.project[data.type.flavorType.flavor.name].refResources = data.project[data.type.flavorType.flavor.name].refResources; + const savedFont = JSON.stringify(setting.project[data.type.flavorType.flavor.name].font); + setting.project[data.type.flavorType.flavor.name] = data.project[data.type.flavorType.flavor.name]; + setting.project[data.type.flavorType.flavor.name].font = font || JSON.parse(savedFont); logger.debug('updateAgSettings.js', `Updating the ${environment.PROJECT_SETTING_FILE}`); await fs.writeFileSync(folder, JSON.stringify(setting)); }; @@ -58,7 +58,6 @@ export const updateWebAgSettings = async (username, projectName, data) => { setting.project[data.type.flavorType.flavor.name].font = (setting.project[data.type.flavorType.flavor.name].font) ? (setting.project[data.type.flavorType.flavor.name].font) : ''; } } - setting.project[data.type.flavorType.flavor.name] = data.project[data.type.flavorType.flavor.name]; await sbStorageUpload(folder, JSON.stringify(setting), { // cacheControl: '3600', upsert: true,