import { useDisclosure } from "@mantine/hooks"; import { Button, Box, Drawer, Grid, Table, Text, ScrollArea, Tooltip, TextInput, } from "@mantine/core"; import { DateInput } from "@mantine/dates"; import { useEffect, useState } from "react"; import type { ISystemLog } from "../../untils/types"; import { IconDownload, IconEye, IconInfoCircle, IconX, } from "@tabler/icons-react"; import classes from "../Component.module.css"; import moment from "moment"; import type { Socket } from "socket.io-client"; import ModalLog from "../Modal/ModalLog"; function DrawerLogs({ socket, isLogModalOpen, setIsLogModalOpen, testLogContent, setTestLogContent, }: { socket: Socket | null; isLogModalOpen: boolean; setIsLogModalOpen: (value: React.SetStateAction) => void; testLogContent: string; setTestLogContent: (value: React.SetStateAction) => void; }) { const [opened, { open, close }] = useDisclosure(false); const [systemLogs, setSystemLogs] = useState([]); const [isDownloadLog, setIsDownloadLog] = useState(false); // const [testLogContent, setTestLogContent] = useState(""); // const [isLogModalOpen, setIsLogModalOpen] = useState(false); const [downloadName, setDownloadName] = useState(""); const [searchFileName, setSearchFileName] = useState(""); const [fromDate, setFromDate] = useState(null); const [toDate, setToDate] = useState(null); const [filteredLogs, setFilteredLogs] = useState([]); useEffect(() => { if (opened) { socket?.emit("get_list_logs"); } }, [opened]); useEffect(() => { socket?.on("list_logs", (files: string[]) => { const list: ISystemLog[] = files.map((file) => { const filename = file.replace(/^.*[\\/]/, ""); const createAt = filename.match(/\d{8}/); return { fileName: file.split("/")[3] || file.split("/")[2] || file.split("/")[1], createdAt: createAt ? createAt[0] : "N/A", path: file, }; }); setSystemLogs( list.sort( (a: ISystemLog, b: ISystemLog) => parseInt(b.createdAt) - parseInt(a.createdAt) ) ); }); }, [socket]); useEffect(() => { if (isDownloadLog && testLogContent && downloadName) { const blob = new Blob([testLogContent], { type: "text/plain" }); // Create a temporary link element const link = document.createElement("a"); link.href = URL.createObjectURL(blob); link.download = downloadName; // Trigger the download by clicking the link link.click(); // Clean up URL.revokeObjectURL(link.href); setIsDownloadLog(false); setTestLogContent(""); setDownloadName(""); } }, [testLogContent]); useEffect(() => { // Chuẩn bị trước các giá trị search/date để tránh tính lại trong filter cho từng phần tử const trimmedSearch = searchFileName.trim().toLowerCase(); const hasSearch = trimmedSearch.length > 0; const fromMoment = fromDate ? moment(fromDate).startOf("day") : null; const toMoment = toDate ? moment(toDate).endOf("day") : null; const delayDebounceFn = setTimeout(() => { // Nếu không có filter nào, tránh filter tốn công, gán thẳng if (!hasSearch && !fromMoment && !toMoment) { setFilteredLogs(systemLogs); return; } const next = systemLogs.filter((log) => { if (hasSearch && !log.fileName.toLowerCase().includes(trimmedSearch)) { return false; } const logDate = moment(log.createdAt, "YYYYMMDD"); if (fromMoment && !logDate.isSameOrAfter(fromMoment)) { return false; } if (toMoment && !logDate.isSameOrBefore(toMoment)) { return false; } return true; }); setFilteredLogs(next); }, 500); return () => clearTimeout(delayDebounceFn); }, [searchFileName, fromDate, toDate, systemLogs]); return ( <> Format: YYYYMMDD-AUTO-Session.{`{Station name}`}-{`{Station ID}`}- {`{Station IP}`}-{`{Line number}`} .log } position="right" > List Logs } > setSearchFileName(event.currentTarget.value) } rightSection={ searchFileName ? ( setSearchFileName("")} /> ) : null } rightSectionPointerEvents="auto" size="xs" /> setFromDate(value as Date | null)} placeholder="From date" valueFormat="DD/MM/YYYY" size="xs" clearable /> setToDate(value as Date | null)} placeholder="To date" valueFormat="DD/MM/YYYY" size="xs" clearable /> File name Created at {filteredLogs.map((element) => ( {element.fileName} {moment(element.createdAt).format("DD/MM/YYYY")} { setTestLogContent(""); socket?.emit("get_content_log", { line: { systemLogUrl: element.path }, }); setIsLogModalOpen(true); }} width={20} /> { socket?.emit("get_content_log", { line: { systemLogUrl: element.path }, }); setIsDownloadLog(true); setTestLogContent(""); setDownloadName( element.path.split("/")[3] || element.path.split("/")[2] || element.path.split("/")[1] ); }} width={20} /> ))}
{isLogModalOpen && ( { setIsLogModalOpen(false); }} testLogContent={testLogContent} /> )}
); } export default DrawerLogs;