-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add document content viewer to Sources page
Implement a modal that displays full document details when clicking the view icon in the Sources table. This allows to inspect all fields of individual documents fetched from Elasticsearch, providing deeper insights into the indexed content.
- Loading branch information
Showing
4 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import React from "react"; | ||
import { | ||
Modal, | ||
ModalOverlay, | ||
ModalContent, | ||
ModalHeader, | ||
ModalBody, | ||
ModalCloseButton, | ||
Text, | ||
Link, | ||
UnorderedList, | ||
ListItem, | ||
Box, | ||
VStack, | ||
} from "@chakra-ui/react"; | ||
|
||
interface DocumentModalProps { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
document: Record<string, any> | null; | ||
isLoading: boolean; | ||
isError: boolean; | ||
error?: string; | ||
} | ||
|
||
const formatValue = (value: any): string => { | ||
if (typeof value === "string") { | ||
return value; | ||
} else if (typeof value === "number" || typeof value === "boolean") { | ||
return value.toString(); | ||
} else if (value instanceof Date) { | ||
return value.toISOString(); | ||
} else if (Array.isArray(value)) { | ||
return "[Array]"; | ||
} else if (typeof value === "object" && value !== null) { | ||
return "[Object]"; | ||
} | ||
return ""; | ||
}; | ||
|
||
const RenderField = ({ name, value }: { name: string; value: any }) => { | ||
if (Array.isArray(value)) { | ||
return ( | ||
<Box mb={2}> | ||
<Text fontWeight="bold">{name}:</Text> | ||
<UnorderedList ml={5}> | ||
{value.map((item, index) => ( | ||
<ListItem key={index}> | ||
{typeof item === "object" ? ( | ||
<RenderObject object={item} /> | ||
) : ( | ||
formatValue(item) | ||
)} | ||
</ListItem> | ||
))} | ||
</UnorderedList> | ||
</Box> | ||
); | ||
} else if (typeof value === "object" && value !== null) { | ||
return ( | ||
<Box mb={2}> | ||
<Text fontWeight="bold">{name}:</Text> | ||
<Box ml={4}> | ||
<RenderObject object={value} /> | ||
</Box> | ||
</Box> | ||
); | ||
} else { | ||
return ( | ||
<Text mb={2}> | ||
<Text as="span" fontWeight="bold"> | ||
{name}: | ||
</Text>{" "} | ||
{formatValue(value)} | ||
</Text> | ||
); | ||
} | ||
}; | ||
|
||
const RenderObject = ({ object }: { object: Record<string, any> }) => { | ||
return ( | ||
<VStack align="stretch" spacing={2}> | ||
{Object.entries(object).map(([key, value]) => ( | ||
<RenderField key={key} name={key} value={value} /> | ||
))} | ||
</VStack> | ||
); | ||
}; | ||
|
||
const DocumentModal: React.FC<DocumentModalProps> = ({ | ||
isOpen, | ||
onClose, | ||
document, | ||
isLoading, | ||
isError, | ||
error, | ||
}) => { | ||
return ( | ||
<Modal isOpen={isOpen} onClose={onClose} size="xl" scrollBehavior="inside"> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader>{document?.title || "Document Details"}</ModalHeader> | ||
<ModalCloseButton /> | ||
<ModalBody> | ||
{isLoading && <Text>Loading document content...</Text>} | ||
{isError && ( | ||
<Text color="red.500">Error loading document: {error}</Text> | ||
)} | ||
{!isLoading && !isError && document && ( | ||
<VStack align="stretch" spacing={4}> | ||
{document.url && ( | ||
<Link | ||
href={document.url} | ||
isExternal | ||
color="blue.500" | ||
_hover={{ textDecoration: "underline" }} | ||
> | ||
{document.url} | ||
</Link> | ||
)} | ||
<RenderObject object={document} /> | ||
</VStack> | ||
)} | ||
</ModalBody> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default DocumentModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
interface DocumentContent { | ||
title: string; | ||
url: string; | ||
content: string; | ||
indexed_at: string; | ||
} | ||
|
||
const fetchDocumentContent = async (url: string): Promise<DocumentContent> => { | ||
const response = await fetch("/api/elasticSearchProxy/getDocumentContent", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ url }), | ||
}); | ||
const data = await response.json(); | ||
if (!data.success) throw new Error(data.message); | ||
return data.data; | ||
}; | ||
|
||
export const useDocumentContent = (url: string) => { | ||
const { data, isLoading, isError, error } = useQuery<DocumentContent, Error>({ | ||
queryKey: ["documentContent", url], | ||
queryFn: () => fetchDocumentContent(url), | ||
enabled: !!url, | ||
cacheTime: Infinity, | ||
staleTime: Infinity, | ||
refetchOnWindowFocus: false, | ||
}); | ||
|
||
return { | ||
documentContent: data, | ||
isLoading, | ||
isError, | ||
error, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { client } from "@/config/elasticsearch"; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (req.method !== "POST") { | ||
return res.status(405).json({ | ||
error: | ||
"Invalid request method. This endpoint only supports POST requests.", | ||
}); | ||
} | ||
|
||
const { url } = req.body; | ||
|
||
if (!url) { | ||
return res.status(400).json({ | ||
error: "URL is required", | ||
}); | ||
} | ||
|
||
try { | ||
const result = await client.search({ | ||
index: process.env.INDEX, | ||
body: { | ||
query: { | ||
term: { "url.keyword": url }, | ||
}, | ||
size: 1, | ||
}, | ||
}); | ||
|
||
if (result.hits.hits.length === 0) { | ||
return res.status(404).json({ | ||
success: false, | ||
message: "Document not found", | ||
}); | ||
} | ||
|
||
const document = result.hits.hits[0]._source; | ||
|
||
return res.status(200).json({ | ||
success: true, | ||
data: document, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(400).json({ | ||
success: false, | ||
message: | ||
error.message || "An error occurred while fetching document content", | ||
}); | ||
} | ||
} |