diff --git a/app/frontend/src/components/AnalysisPanel/AnalysisPanel.tsx b/app/frontend/src/components/AnalysisPanel/AnalysisPanel.tsx index 99fa65ae2..2df1674ef 100644 --- a/app/frontend/src/components/AnalysisPanel/AnalysisPanel.tsx +++ b/app/frontend/src/components/AnalysisPanel/AnalysisPanel.tsx @@ -78,82 +78,115 @@ export const AnalysisPanel = ({ answer, activeTab, activeCitation, sourceFile, p if (sourceFileBlob === undefined) { if (!isFetchingSourceFileBlob) { setIsFetchingSourceFileBlob(true); - sourceFileBlobPromise = fetchCitationSourceFile().finally(() => { + if (sourceFileUrl !== undefined) { + sourceFileBlobPromise = fetchCitationSourceFile().finally(() => { + setIsFetchingSourceFileBlob(false); + }); + } else { + console.error("sourceFileUrl is undefined"); setIsFetchingSourceFileBlob(false); - }); + } } await sourceFileBlobPromise; } }; - fetchSourceFileBlob(); - return sourceFileUrl; + + if (sourceFileBlob !== undefined) { + fetchSourceFileBlob(); + + } + return sourceFileUrl ?? ''; } - async function fetchActiveCitationObj() { +async function fetchActiveCitationObj() { + try { + if (!activeCitation) { + console.warn('Active citation is undefined'); + setActiveCitationObj(undefined); + return; + } + const citationObj = await getCitationObj(activeCitation); + setActiveCitationObj(citationObj); + console.log(citationObj); + } catch (error) { + // Handle the error here + console.log(error); + } +} + +useEffect(() => { + const fetchMarkdownContent = async () => { try { - const citationObj = await getCitationObj(activeCitation as string); - setActiveCitationObj(citationObj); - console.log(citationObj); + if (!sourceFile) { + console.error('Source file is undefined'); + return; + } + const citationURL = getCitationURL(); + if (!citationURL) { + throw new Error('Citation URL is undefined'); + } + const response = await fetch(citationURL); + const content = await response.text(); + setMarkdownContent(content); } catch (error) { - // Handle the error here - console.log(error); + console.error('Error fetching Markdown content:', error); } - } + }; + fetchMarkdownContent(); +}, [sourceFileBlob, sourceFileExt]); - useEffect(() => { - if (!sourceFile) { - return; - } - const fetchMarkdownContent = async () => { - try { - const response = await fetch(getCitationURL()); - const content = await response.text(); - setMarkdownContent(content); - } catch (error) { - console.error('Error fetching Markdown content:', error); +useEffect(() => { + const fetchPlainTextContent = async () => { + try { + if (!sourceFile) { + console.error('Source file is undefined'); + return; } - }; - fetchMarkdownContent(); - }, [sourceFileBlob, sourceFileExt]); - - useEffect(() => { - const fetchPlainTextContent = async () => { - try { - const response = await fetch(getCitationURL()); - const content = await response.text(); - setPlainTextContent(content); - } catch (error) { - console.error('Error fetching plain text content:', error); + const citationURL = getCitationURL(); + if (!citationURL) { + throw new Error('Citation URL is undefined'); } - }; - - if (["json", "txt", "xml"].includes(sourceFileExt)) { - fetchPlainTextContent(); + const response = await fetch(citationURL); + const content = await response.text(); + setPlainTextContent(content); + } catch (error) { + console.error('Error fetching plain text content:', error); } - }, [sourceFileBlob, sourceFileExt]); + }; + + if (["json", "txt", "xml"].includes(sourceFileExt)) { + fetchPlainTextContent(); + } +}, [sourceFileBlob, sourceFileExt]); + +useEffect(() => { + if (activeCitation) { + setInnerPivotTab('indexedFile'); + } else { + console.warn('Active citation is undefined'); + setActiveCitationObj(undefined); + } - useEffect(() => { - if (activeCitation) { - setInnerPivotTab('indexedFile'); + fetchActiveCitationObj(); + + const fetchSourceFileBlob = async () => { + if (!sourceFile) { + console.error('Source file is undefined'); + return; } - fetchActiveCitationObj(); - const fetchSourceFileBlob = async () => { - - if (!isFetchingSourceFileBlob) { - setIsFetchingSourceFileBlob(true); - sourceFileBlobPromise = fetchCitationSourceFile().finally(() => { - setIsFetchingSourceFileBlob(false); - }); - } - await sourceFileBlobPromise; - - }; - fetchSourceFileBlob(); - - }, [activeCitation]); + if (!isFetchingSourceFileBlob) { + setIsFetchingSourceFileBlob(true); + sourceFileBlobPromise = fetchCitationSourceFile().finally(() => { + setIsFetchingSourceFileBlob(false); + }); + } + await sourceFileBlobPromise; + }; + fetchSourceFileBlob(); +}, [activeCitation]); return ( - {getCitationURL() === '' ? ( + {getCitationURL() === '' ? ( Loading... ) : ["docx", "xlsx", "pptx"].includes(sourceFileExt) ? ( // Treat other Office formats like "xlsx" for the Office Online Viewer -