From 9af3def127503d26889aa740625f2da1c6a6c36d Mon Sep 17 00:00:00 2001 From: PhilReact Date: Thu, 19 Dec 2024 05:51:23 +0200 Subject: [PATCH] change attach download --- src/App.tsx | 2 +- src/common/useFetchResources.tsx | 32 ++++++++++++++++++++--- src/components/Embeds/AttachmentEmbed.tsx | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index d78713c..86cf663 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1567,7 +1567,7 @@ function App() { showTutorial('qapps', true) } else { - showTutorial('create-account', true) + showTutorial('getting-started', true) } }} > diff --git a/src/common/useFetchResources.tsx b/src/common/useFetchResources.tsx index 2180966..02123c5 100644 --- a/src/common/useFetchResources.tsx +++ b/src/common/useFetchResources.tsx @@ -1,10 +1,11 @@ -import React, { useCallback } from 'react'; +import React, { useCallback, useRef } from 'react'; import { useRecoilState } from 'recoil'; import { resourceDownloadControllerAtom } from '../atoms/global'; import { getBaseApiReact } from '../App'; export const useFetchResources = () => { const [resources, setResources] = useRecoilState(resourceDownloadControllerAtom); + const intervalId = useRef(null) const downloadResource = useCallback(({ service, name, identifier }, build) => { setResources((prev) => ({ @@ -21,9 +22,11 @@ export const useFetchResources = () => { let isCalling = false; let percentLoaded = 0; let timer = 24; + let tries = 26; let calledFirstTime = false - const intervalId = setInterval(async () => { + const callFunction = async ()=> { + console.log('calledFirstTime', calledFirstTime) if (isCalling) return; isCalling = true; @@ -40,6 +43,22 @@ export const useFetchResources = () => { }, }); res = await resCall.json() + if(tries === 5 && intervalId?.current){ + clearInterval(intervalId?.current) + setResources((prev) => ({ + ...prev, + [`${service}-${name}-${identifier}`]: { + ...(prev[`${service}-${name}-${identifier}`] || {}), + status: { + ...res, + status: 'FAILED_TO_DOWNLOAD', + }, + }, + })); + return + } + tries = tries + 1 + } @@ -103,7 +122,7 @@ export const useFetchResources = () => { // Check if progress is 100% and clear interval if true if (res?.status === 'READY') { - clearInterval(intervalId); + clearInterval(intervalId.current); // Update Recoil state for completion setResources((prev) => ({ @@ -114,7 +133,12 @@ export const useFetchResources = () => { }, })); } - }, !calledFirstTime ? 100 :5000); + } + callFunction() + intervalId.current = setInterval(async () => { + callFunction() + }, 5000); + } catch (error) { console.error('Error during resource fetch:', error); } diff --git a/src/components/Embeds/AttachmentEmbed.tsx b/src/components/Embeds/AttachmentEmbed.tsx index c068d3b..3de63e3 100644 --- a/src/components/Embeds/AttachmentEmbed.tsx +++ b/src/components/Embeds/AttachmentEmbed.tsx @@ -271,7 +271,7 @@ export const AttachmentCard = ({ )} - {resourceDetails && resourceDetails?.status?.status !== 'READY' && ( + {resourceDetails && resourceDetails?.status?.status !== 'READY' && resourceDetails?.status?.status !== 'FAILED_TO_DOWNLOAD' && ( <>