Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to document viewer for more document types #473

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"classnames": "^2.3.1",
"nanoid": "3.3.4",
"prop-types": "15.8.1",
"react-bootstrap": "^2.7.4"
"react-bootstrap": "^2.7.4",
"react-markdown": "^9.0.1",
"remark-gfm": "^3.0.0"
},
"devDependencies": {
"@types/dompurify": "^2.4.0",
Expand Down
72 changes: 57 additions & 15 deletions app/frontend/src/components/AnalysisPanel/AnalysisPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Pivot, PivotItem, Text } from "@fluentui/react";
import { Label } from '@fluentui/react/lib/Label';
import { Separator } from '@fluentui/react/lib/Separator';
import DOMPurify from "dompurify";
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm'

import styles from "./AnalysisPanel.module.css";

Expand All @@ -28,6 +30,8 @@ const pivotItemDisabledStyle = { disabled: true, style: { color: "grey" } };

export const AnalysisPanel = ({ answer, activeTab, activeCitation, sourceFile, pageNumber, citationHeight, className, onActiveTabChanged }: Props) => {
const [activeCitationObj, setActiveCitationObj] = useState<ActiveCitation>();
const [markdownContent, setMarkdownContent] = useState('');
const [plainTextContent, setPlainTextContent] = useState('');

const isDisabledThoughtProcessTab: boolean = !answer.thoughts;
const isDisabledSupportingContentTab: boolean = !answer.data_points.length;
Expand All @@ -51,6 +55,36 @@ export const AnalysisPanel = ({ answer, activeTab, activeCitation, sourceFile, p
fetchActiveCitationObj();
}, [activeCitation]);

useEffect(() => {
const fetchMarkdownContent = async () => {
try {
const response = await fetch(sourceFile!);
const content = await response.text();
setMarkdownContent(content);
} catch (error) {
console.error('Error fetching Markdown content:', error);
}
};

fetchMarkdownContent();
}, [sourceFile]);

useEffect(() => {
const fetchPlainTextContent = async () => {
try {
const response = await fetch(sourceFile!);
const content = await response.text();
setPlainTextContent(content);
} catch (error) {
console.error('Error fetching plain text content:', error);
}
};

if (["json", "txt", "xml"].includes(sourceFileExt)) {
fetchPlainTextContent();
}
}, [sourceFile, sourceFileExt]);

return (
<Pivot
className={className}
Expand Down Expand Up @@ -78,31 +112,39 @@ export const AnalysisPanel = ({ answer, activeTab, activeCitation, sourceFile, p
>
<Pivot className={className}>
<PivotItem itemKey="indexedFile" headerText="Document Section">
{ activeCitationObj === undefined ? (
{activeCitationObj === undefined ? (
<Text>Loading...</Text>
) : (
<div>
<Separator>Metadata</Separator>
<Label>File Name</Label><Text>{activeCitationObj.file_name}</Text>
<Label>File URI</Label><Text>{activeCitationObj.file_uri}</Text>
<Label>Title</Label><Text>{activeCitationObj.title}</Text>
<Label>Section</Label><Text>{activeCitationObj.section}</Text>
<Label>Page Number(s)</Label><Text>{activeCitationObj.pages?.join(",")}</Text>
<Label>Token Count</Label><Text>{activeCitationObj.token_count}</Text>
<Separator>Content</Separator>
<Label>Content</Label><Text>{activeCitationObj.content}</Text>
<Separator>Metadata</Separator>
<Label>File Name</Label><Text>{activeCitationObj.file_name}</Text>
<Label>File URI</Label><Text>{activeCitationObj.file_uri}</Text>
<Label>Title</Label><Text>{activeCitationObj.title}</Text>
<Label>Section</Label><Text>{activeCitationObj.section}</Text>
<Label>Page Number(s)</Label><Text>{activeCitationObj.pages?.join(",")}</Text>
<Label>Token Count</Label><Text>{activeCitationObj.token_count}</Text>
<Separator>Content</Separator>
<Label>Content</Label><Text>{activeCitationObj.content}</Text>
</div>
)}
</PivotItem>
<PivotItem itemKey="rawFile" headerText="Document">
{ sourceFileExt === "pdf" ? (
//use object tag for pdfs because iframe does not support page numbers
{["docx", "xlsx", "pptx"].includes(sourceFileExt) ? (
// Treat other Office formats like "xlsx" for the Office Online Viewer
<iframe title="Source File" src={'https://view.officeapps.live.com/op/view.aspx?src=' + encodeURIComponent(sourceFile as string) + "&action=embedview&wdStartOn=" + pageNumber} width="100%" height={citationHeight} />
) : sourceFileExt === "pdf" ? (
// Use object tag for PDFs because iframe does not support page numbers
<object data={sourceFile + "#page=" + pageNumber} type="application/pdf" width="100%" height={citationHeight} />
) : ( sourceFileExt === "docx" ? (
<iframe title="Source File" src={'https://view.officeapps.live.com/op/view.aspx?src='+encodeURIComponent(sourceFile as string)+"&action=embedview&wdStartOn="+pageNumber} width="100%" height={citationHeight} />
) : sourceFileExt === "md" ? (
// Render Markdown content using react-markdown
<ReactMarkdown>{markdownContent}</ReactMarkdown>
) : ["json", "txt", "xml"].includes(sourceFileExt) ? (
// Render plain text content
<pre>{plainTextContent}</pre>
) : (
// Default to iframe for other file types
<iframe title="Source File" src={sourceFile} width="100%" height={citationHeight} />
)) }
)}
</PivotItem>
</Pivot>
</PivotItem>
Expand Down
Loading