import { ArrowLeftOutlined, ArrowRightOutlined, ReloadOutlined, } from "@ant-design/icons"; import { Card, Checkbox, Popover, Space, Spin, Tooltip } from "antd"; import { useContext, useEffect, useState } from "react"; import { Constants, FormatDatetime, WebSocketContext, getUserSessionFromLocalStorage, handleUnauthorizedStatus, } from "../../utils"; import { Link } from "react-router-dom"; export default function LogCard({ type }) { const webSocketContext = useContext(WebSocketContext); const [checkboxInfoChecked, setCheckboxInfoChecked] = useState(true); const [checkboxErrorChecked, setCheckboxErrorChecked] = useState(true); const getDate = () => { const today = new Date(); return `${today.getDate()}-${today.getMonth() + 1}-${today.getFullYear()}`; }; const [logData, setLogData] = useState({ Logs: [], Dates: [] }); const [selectedDate, setSelectedDate] = useState(getDate()); const [loadingSpinner, setLoadingSpinner] = useState(true); useEffect(() => { loadLogs(selectedDate); }, [selectedDate]); const getColorCode = (index) => { const colorCodes = ["#3498db", "#9b59b6", "#1abc9c"]; const colorIndex = index % colorCodes.length; return colorCodes[colorIndex]; }; /* const getLogDataValue = (logData, splittedMessage) => { const logDataIndex = logData.findIndex( (data) => data.Type === splittedMessage.replace(new RegExp("%", "g"), "") ); return logData[logDataIndex].Value; };*/ const ColoredSpanItem = ({ dataLogs, splittedMessage, children }) => { const logDataIndex = dataLogs.LogData.findIndex( (data) => data.Type === splittedMessage.replace(new RegExp("%", "g"), "") ); if (logDataIndex !== -1) { return ( {children === undefined ? ( dataLogs.LogData[logDataIndex].Value ) : ( <>{children} )}{" "} ); } return <>; }; const getGroupTaskStepName = (groupTaskStep) => { const foundName = webSocketContext.CategoryGroups.find( (categoryGroup) => categoryGroup.category === webSocketContext.GroupTasks.find( (groupTask) => groupTask.Id === groupTaskStep.GroupTasksId ).Category ).groups.find( (groups) => groups.id === webSocketContext.GroupTasks.find( (groupTask) => groupTask.Id === groupTaskStep.GroupTasksId ).GroupId ).tasks[groupTaskStep.Step - 1]; return foundName !== undefined ? foundName.name : groupTaskStep.Id; }; const loadLogs = (date) => { setLoadingSpinner(true); fetch( `${Constants.API_ADDRESS}/log?type=${ type === "grouptasks" ? "g" : "s" }&date=${date}`, { method: "GET", headers: { "Content-Type": "application/json", "X-Authorization": getUserSessionFromLocalStorage(), }, } ) .then((res) => { handleUnauthorizedStatus(res.status); return res.json(); }) .then((data) => { setLoadingSpinner(false); if (data.Logs === null) { data.Logs = []; } else { for (let i = 0; i < data.Logs.length; i++) { let items = []; if (data.Logs[i].Message === "") { items.push( Message not found.{" "} {data.Logs[i].LogData.length > 0 && ( <> Given Args:{" "} {data.Logs[i].LogData.map((data) => ( {data.Type}:{data.Value} ))} )} ); } let splittedMessage = data.Logs[i].Message.split(" "); for (let s = 0; s < splittedMessage.length; s++) { if (splittedMessage[s].includes("%")) { if (splittedMessage[s] === "%userId%") { const foundData = data.Logs[i].LogData.find( (data) => data.Type === "userId" ); if (foundData !== undefined) { const foundUser = webSocketContext.AllUsers.find( (user) => user.Id === foundData.Value ); items.push( {foundUser !== undefined ? foundUser.Username : foundData.Value}{" "} ); } else { items.push( ); } } else if (splittedMessage[s] === "%groupTaskId%") { const foundData = data.Logs[i].LogData.find( (data) => data.Type === "groupTaskId" ); if (foundData !== undefined) { const foundGroupTask = webSocketContext.GroupTasks.find( (groupTask) => groupTask.Id === foundData.Value ); items.push( ID: {foundData.Value} } title="Group Task" trigger="click" > {foundGroupTask !== undefined ? foundGroupTask.GroupName : foundData.Value} } /> ); } else { items.push( ); } } else if (splittedMessage[s] === "%taskStepId%") { const foundData = data.Logs[i].LogData.find( (data) => data.Type === "taskStepId" ); if (foundData !== undefined) { const foundGroupTaskStep = webSocketContext.GroupTasksSteps.find( (step) => step.Id === foundData.Value ); items.push( ID: {foundGroupTaskStep !== undefined ? foundGroupTaskStep.Id : foundData.Value} } title="Group Task Step" trigger="click" > {" "} {foundGroupTaskStep !== undefined ? getGroupTaskStepName(foundGroupTaskStep) : foundData.Value} } /> ); } else { items.push( ); } } else if (splittedMessage[s] === "%roleId%") { const foundData = data.Logs[i].LogData.find( (data) => data.Type === "roleId" ); if (foundData !== undefined) { const foundRole = webSocketContext.AllRoles.find( (role) => role.Id === foundData.Value ); items.push( ID: {foundData.Value} } title="Role" trigger="click" > {" "} {foundRole !== undefined ? foundRole.DisplayName : foundData.Value} } /> ); } else { items.push( ); } } else { items.push( ); } } else { items.push({splittedMessage[s]} ); } } data.Logs[i].Message = items; } } setLogData(data); }) .catch((errStatus) => console.log("error", errStatus)); }; const inBrackets = (content, contentColor) => { return ( <> [ {content} ] ); }; const getMessageType = (logType) => { if (logType === Constants.SYSTEM_LOG_TYPE.INFO) { return inBrackets("INFO", "#27ae60"); } if (logType === Constants.SYSTEM_LOG_TYPE.DEBUG) { return inBrackets("DEB", "#e67e22"); } if (logType === Constants.SYSTEM_LOG_TYPE.ERROR) { return inBrackets("ERR", "#e74c3c"); } return ""; }; return ( setCheckboxInfoChecked(e.target.checked)} > INFO setCheckboxErrorChecked(e.target.checked)} > ERROR {logData.Dates.findIndex((date) => date === selectedDate) > 0 ? ( setSelectedDate( logData.Dates[ logData.Dates.findIndex((date) => date === selectedDate) - 1 ] ) } /> ) : ( )} {logData.Dates.findIndex((date) => date === selectedDate) + 1 < logData.Dates.length ? ( setSelectedDate( logData.Dates[ logData.Dates.findIndex((date) => date === selectedDate) + 1 ] ) } /> ) : ( )} loadLogs(selectedDate)} /> } style={{ height: "97vh", overflow: "hidden", overflowWrap: "break-word", width: "100%", }} > {!loadingSpinner ? (
{logData.Logs.filter((log) => { return ( (log.Type === 0 && checkboxInfoChecked) || (log.Type === 1 && checkboxErrorChecked) ); }).map((log, i) => { if ( (log.Type === 0 && !checkboxInfoChecked) || (log.Type === 1 && !checkboxErrorChecked) ) { return ""; } return (
{inBrackets(FormatDatetime(log.Time), "#7f8c8d")}{" "} {getMessageType(log.Type)} {log.Message}
); })}
) : (
)}
); }