Sure! Let’s do a detailed line-by-line analysis of the two provided source files: DocDetails.tsx and Interfaces.ts. We'll break down the DocDetails.tsx file first, as it contains the main component logic, followed by the Interfaces.ts file, which defines the data structures used.
DocDetails.tsx
This file defines a React functional component that displays detailed information about a document in a modal. It utilizes several libraries like Chakra UI for UI components, react-icons for icon representation, and react-markdown for rendering Markdown content.
Imports
tsx Copy code
import {
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
[Redacted Person],
ModalFooter,
[Redacted Person],
Text,
Box,
[Redacted Person],
[Redacted Person],
[Redacted Person],
[Redacted Person],
[Redacted Person],
ListItem,
IconButton,
Tooltip,
useColorMode,
} from "@chakra-ui/react";
Imports UI components from the Chakra UI library for building the modal and other UI elements.
tsx Copy code
import rehypeRaw from 'rehype-raw';
import { useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import {
FaInfoCircle,
FaCalendarAlt,
[Redacted Person],
FaFileAlt,
FaCopy,
FaPrint,
[Redacted Person],
FaRobot,
[Redacted Person],
} from "react-icons/fa";
import ReactMarkdown from 'react-markdown';
import FileViewer from "../FileViewer";
import { Document } from "../utils/interfaces";
import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';
rehypeRaw : A plugin for react-markdown that allows raw HTML to be rendered.
useRef, useState : React hooks for managing state and referencing DOM elements.
useTranslation : A hook from the react-i18next library for internationalization.
react-icons : Includes icons for various functionalities (info, calendar, etc.).
ReactMarkdown : Component to render Markdown content.
FileViewer : A local component presumably used for displaying files.
Document : TypeScript interface imported to define the document structure.
jsPDF and html2canvas : Libraries used for creating PDFs from HTML content.
Interface Definition
tsx Copy code
interface DocDetailsProps {
doc: Document | null;
isOpen: boolean;
onClose: () => void;
}
DocDetailsProps : Defines the expected props for the DocDetails component. It expects a doc object of type Document, a boolean isOpen to indicate if the modal is open, and an onClose function to handle closing the modal.
Component Definition
tsx Copy code
const DocDetails: React.FC<DocDetailsProps> = ({ doc, isOpen, onClose }) => {
DocDetails : A functional component that takes props defined by DocDetailsProps.
tsx Copy code
const [showDoc, setShowDoc] = useState<boolean>(false);
const [showMarkdown, setShowMarkdown] = useState<boolean>(false);
const [markdownContent, setMarkdownContent] = useState<string>("");
State Variables :
showDoc: Controls the visibility of the document viewer.
showMarkdown: Controls the visibility of the markdown viewer.
markdownContent: Stores the content fetched from a markdown file.
tsx Copy code
const { t } = useTranslation("common");
const { colorMode } = useColorMode();
const markdownRef = useRef<HTMLDivElement>(null);
Translation and Color Mode : t is used for translations, and colorMode allows for theme adjustments (light/dark mode).
markdownRef : A reference for the markdown viewer's DOM element.
Toggle Functions
tsx Copy code
const toggleShowDoc = () => setShowDoc((prev) => !prev);
toggleShowDoc : A function to toggle the visibility of the document viewer.
tsx Copy code
const toggleMarkdown = async () => {
if (!showMarkdown) {
try {
const files = ["output.md", "output2.md", "output3.md"];
const randomFile = files[Math.floor(Math.random() * files.length)];
const response = await fetch(`public/documents/output3.md`);
const content = await response.text();
setMarkdownContent(content);
} catch (error) {
console.error('Error loading markdown:', error);
}
}
setShowMarkdown(!showMarkdown);
};
toggleMarkdown :
Fetches markdown content from a file when opening the viewer.
Uses a try-catch block for error handling.
Sets markdownContent with the fetched content.
Copy and Export Functions
tsx Copy code
const handleCopy = () => {
navigator.clipboard.writeText(doc?.description || "");
alert("Contenu copié !");
};
handleCopy : Copies the document's description to the clipboard and alerts the user.
tsx Copy code
const handlePdfExport = async () => {
if (!markdownRef.current) return;
const contentDiv = markdownRef.current.cloneNode(true) as HTMLElement;
document.body.appendChild(contentDiv);
contentDiv.style.position = 'absolute';
contentDiv.style.left = '-9999px';
contentDiv.style.transform = 'none';
const canvas = await html2canvas(contentDiv, {
scale: 2,
useCORS: true,
[Redacted Person]: true,
backgroundColor: '#ffffff'
});
document.body.removeChild(contentDiv);
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF('p', 'mm', 'a4');
const pageWidth = 210;
const pageHeight = 297;
const margin = 10;
const imgWidth = pageWidth - (margin * 2);
const imgHeight = (canvas.height * imgWidth) / canvas.width;
let heightLeft = imgHeight;
let position = margin;
let page = 1;
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pageHeight - (margin * 2));
while (heightLeft >= 0) {
pdf.addPage();
position = heightLeft - imgHeight + margin;
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pageHeight - (margin * 2));
page++;
}
const logo = new Image();
logo.src = '/logo.png';
await new Promise(resolve => logo.onload = resolve);
for (let i = 1; i <= page; i++) {
pdf.setPage(i);
const gState = { opacity: 0.2 };
(pdf as any).setGState(new (pdf as any).GState(gState));
pdf.addImage(logo, 'PNG', 65, 100, 80, 80);
}
pdf.save('legomnia-document.pdf');
};
handlePdfExport :
Exports the markdown viewer content as a PDF.
Uses html2canvas to capture the content as an image.
Creates a new PDF with the captured image and adds a watermark logo.
Handles multiple pages if content exceeds one page height.
tsx Copy code
const handlePrint = () => {
window.print();
};
handlePrint : Triggers the browser's print dialog.
tsx Copy code
const handlePdfDownload = () => {
if (showMarkdown) {
handlePdfExport();
} else {
alert("Veuillez d'abord ouvrir le document");
}
};
handlePdfDownload : Calls handlePdfExport if the markdown viewer is open; otherwise, alerts the user.
tsx Copy code
const handleIaAction = () => {
alert("Fonctionnalité IA à venir !");
};
handleIaAction : Placeholder function for a future AI feature.
JSX Return
tsx Copy code
return (
<>
{doc && (
<Modal isOpen={isOpen} onClose={onClose} size="full" isCentered>
<ModalOverlay />
<ModalContent
maxW="80vw"
maxH="95vh"
p={8}
borderRadius="md"
boxShadow="xl"
bg={colorMode === "dark" ? "#1A202C" : "#F7F6F2"}
>
<ModalHeader
p={0}
mb={4}
display="flex"
justifyContent="space-between"
alignItems="center"
>
<Box>
<Heading as="h1" size="lg" color="#4E4AC6" display="flex" alignItems="center">
Cour d'appel d'Orléans, 25 avril 2019, [Redacted PhoneNumber]
</Heading>
<Text fontSize="sm" color="#4E4AC6" mt={1}>
N°: {doc?.numero} - Date: {new Date(doc.date).toLocaleDateString()} - {doc?.pays_organisation}
</Text>
</Box>
<Flex gap={3}>
<Tooltip label="Copier" aria-label="Copier">
<Icon