From d29c0e694cc4705d3e78655971a9fa7832cf219e Mon Sep 17 00:00:00 2001 From: Siju Moncy <72241997+sijumoncy@users.noreply.github.com> Date: Thu, 4 Jan 2024 16:51:15 +0530 Subject: [PATCH] rewrite the logics for loading online and offline Twords (#285) --- .../EditorPage/Reference/TW/TwNavigation.js | 126 ++++++++++-------- 1 file changed, 71 insertions(+), 55 deletions(-) diff --git a/renderer/src/components/EditorPage/Reference/TW/TwNavigation.js b/renderer/src/components/EditorPage/Reference/TW/TwNavigation.js index 4dee6c222..d75c4bc3c 100644 --- a/renderer/src/components/EditorPage/Reference/TW/TwNavigation.js +++ b/renderer/src/components/EditorPage/Reference/TW/TwNavigation.js @@ -17,34 +17,30 @@ export default function TwNavigation({ languageId, referenceResources, setRefere const [openSnackBar, setOpenSnackBar] = useState(false); const [snackText, setSnackText] = useState(''); const [error, setError] = useState(''); + const [onlineMeta, setOnlineMeta] = useState(); - // const baseUrl = 'https://git.door43.org/api/v1/repos'; const owner = referenceResources?.owner; useEffect(() => { if (referenceResources && referenceResources?.offlineResource?.offline) { - // offline - // console.log('offline : ', { referenceResources }); - const taArrayOffline = []; const { offlineResource } = referenceResources; localForage.getItem('userProfile').then(async (user) => { - logger.debug('TwNavigation.js', 'reading offline helps ', offlineResource.data?.projectDir); const fs = window.require('fs'); const path = require('path'); const newpath = localStorage.getItem('userPath'); const currentUser = user?.username; const folder = path.join(newpath, packageInfo.name, 'users', `${currentUser}`, 'resources'); const projectName = `${offlineResource?.data?.value?.meta?.name}_${offlineResource?.data?.value?.meta?.owner}_${offlineResource?.data?.value?.meta?.release?.tag_name}`; - // set Options + const optionsDir = path.join(folder, projectName, 'bible'); const tempOptions = []; + if (fs.existsSync(optionsDir)) { fs.readdir(optionsDir, async (err, optionsNames) => { if (err) { // console.log(`Unable to scan directory: ${ err}`); logger.debug('TwNavigation.js', `Unable to scan directory: ${ err}`); } - // console.log({ optionsNames }); let optionsCount = 0; await optionsNames.forEach(async (folderName) => { if (fs.lstatSync(path.join(optionsDir, folderName)).isDirectory()) { @@ -53,36 +49,14 @@ export default function TwNavigation({ languageId, referenceResources, setRefere } }); if (tempOptions.length === optionsCount) { - setoptions(tempOptions); + setoptions(tempOptions); + setselectedOption(tempOptions[0]); } - // fetch contents of selected folder - if (selectedOption && selectedOption?.length > 0 && fs.existsSync(path.join(optionsDir, selectedOption))) { - fs.readdir(path.join(optionsDir, selectedOption), async (err, folderContents) => { - let contentsCount = 0; - folderContents.forEach((content) => { - if (fs.lstatSync(path.join(optionsDir, selectedOption, content)).isFile()) { - contentsCount += 1; - taArrayOffline.push( - { - folder: path.join(selectedOption, content), - title: content.replace('.md', ''), - subTitle: '', - }, - ); - } - }); - if (taArrayOffline.length === contentsCount) { - setTwList(taArrayOffline); - } - }); - } - }); + }); } }); } else { // online - // get options - // fetch(`https://git.door43.org/api/catalog/v5/search?subject=Translation%20Words&lang=${languageId}&owner=${owner}`) fetch(`${environment.GITEA_API_ENDPOINT}/catalog/search?metadataType=rc&subject=Translation%20Words&lang=${languageId}&owner=${owner}`) .then((res) => res.json()) .then((meta) => { @@ -90,6 +64,7 @@ export default function TwNavigation({ languageId, referenceResources, setRefere fetch(meta?.data[0]?.contents_url) .then((response) => response.json()) .then((contents) => { + setOnlineMeta(meta); const bibleDirUlr = contents.filter((content) => content?.name === 'bible' && content?.type === 'dir'); // console.log({ bibleDirUlr }); if (bibleDirUlr?.length > 0) { @@ -107,45 +82,86 @@ export default function TwNavigation({ languageId, referenceResources, setRefere }); if (folderCount === tempOptions?.length) { setoptions(tempOptions); + setselectedOption(tempOptions[0]); } } }); } }); + }); + } + }, [owner, languageId]); - const fetchData = async () => { - await fetch(`${environment.GITEA_API_ENDPOINT}/repos/${owner}/${languageId}_tw/contents/bible/${selectedOption}?ref=${meta?.data[0]?.release?.tag_name}`) - .then((response) => response.json()) - .then((twData) => { - twData && twData?.forEach((data) => { - data.folder = data?.path.replace('bible/', ''); - data.title = data?.name.replace('.md', ''); - data.subTitle = ''; + useEffect(() => { + if (referenceResources && referenceResources?.offlineResource?.offline) { + const taArrayOffline = []; + const { offlineResource } = referenceResources; + localForage.getItem('userProfile').then(async (user) => { + const fs = window.require('fs'); + const path = require('path'); + const newpath = localStorage.getItem('userPath'); + const currentUser = user?.username; + const folder = path.join(newpath, packageInfo.name, 'users', `${currentUser}`, 'resources'); + const projectName = `${offlineResource?.data?.value?.meta?.name}_${offlineResource?.data?.value?.meta?.owner}_${offlineResource?.data?.value?.meta?.release?.tag_name}`; + // set Options + const optionsDir = path.join(folder, projectName, 'bible'); + if (selectedOption && selectedOption?.length > 0 && fs.existsSync(path.join(optionsDir, selectedOption))) { + fs.readdir(path.join(optionsDir, selectedOption), async (err, folderContents) => { + let contentsCount = 0; + folderContents.forEach((content) => { + if (fs.lstatSync(path.join(optionsDir, selectedOption, content)).isFile()) { + contentsCount += 1; + taArrayOffline.push( + { + folder: path.join(selectedOption, content), + title: content.replace('.md', ''), + subTitle: '', + }, + ); + } }); - setTwList(twData); - }).catch((err) => { - logger.debug('TwNavigation.js', 'reading offline helps ', err); - setOpenSnackBar(true); - setError('failure'); - setSnackText('Can not load content'); + if (taArrayOffline.length === contentsCount) { + setTwList(taArrayOffline); + setSelected(taArrayOffline[0]); + } }); - }; + } + }); + } else { + // online data fetch + const fetchData = async () => { + await fetch(`${environment.GITEA_API_ENDPOINT}/repos/${owner}/${languageId}_tw/contents/bible/${selectedOption}?ref=${onlineMeta?.data[0]?.release?.tag_name}`) + .then((response) => response.json()) + .then((twData) => { + twData && twData?.forEach((data) => { + data.folder = data?.path.replace('bible/', ''); + data.title = data?.name.replace('.md', ''); + data.subTitle = ''; + }); + setTwList(twData); + setSelected(twData[0]); + }).catch((err) => { + logger.debug('TwNavigation.js', 'reading offline helps ', err); + setOpenSnackBar(true); + setError('failure'); + setSnackText('Can not load content'); + }); + }; - const getData = async () => { - await fetchData(); - }; - // console.log({ selectedOption }); - selectedOption && getData(); - }); + const getData = async () => { + await fetchData(); + }; + + selectedOption && getData(); } - }, [languageId, referenceResources, owner, selectedOption]); + }, [options, selectedOption]); useEffect(() => { selected && setReferenceResources((current) => ({ ...current, offlineResource: { ...current.offlineResource, twSelected: selected }, })); - }, [selected, selectedOption, setReferenceResources]); + }, [selected]); return ( <>