import { DialogFooter } from '@blueprintjs/core'; import type { LogEntry } from 'fifo-logger'; import { useMemo } from 'react'; import { FaRegTrashAlt } from 'react-icons/fa'; import { IoBugOutline } from 'react-icons/io5'; import { ObjectInspector } from 'react-inspector'; import { useOnOff } from 'react-science/ui'; import { useLogger } from '../context/LoggerContext.js'; import Button from '../elements/Button.js'; import { ColumnWrapper } from '../elements/ColumnWrapper.js'; import { StandardDialog } from '../elements/StandardDialog.tsx'; import { StyledDialogBody } from '../elements/StyledDialogBody.js'; import type { TanStackTableRow } from '../elements/tanstack_table/index.ts'; import { TanStackTable, createTanStackColumnHelper, } from '../elements/tanstack_table/index.ts'; const logsDataFormat = new Intl.DateTimeFormat('default', { hour: 'numeric', minute: 'numeric', second: 'numeric', }); function handleRowStyle(row: TanStackTableRow) { const level = row.original.level; let backgroundColor = 'lightgreen'; if (level > 40) { backgroundColor = 'pink'; } else if (level === 40) { backgroundColor = 'lightyellow'; } return { base: { backgroundColor } }; } function getMaxLevelLogs(logs: LogEntry[], lastReadLogId: number) { const logsCounts: Record = {}; for (const log of logs) { if (log.id > lastReadLogId) { logsCounts[log.level] = ++logsCounts[log.level] || 1; } } let maxLevel = 0; for (const levelKey in logsCounts) { const level = Number(levelKey); if (level > maxLevel) { maxLevel = level; } } let backgroundColor = '#2dd36f'; if (maxLevel > 40) { backgroundColor = '#ff0000'; } else if (maxLevel === 40) { backgroundColor = '#ffc409'; } return { backgroundColor, count: logsCounts[maxLevel] }; } interface LogsHistoryModalProps { autoOpen?: boolean; onClose?: () => void; } export function LogsHistoryModal(props: LogsHistoryModalProps) { const { autoOpen = false, onClose } = props; const { logsHistory, logger, markAsRead, lastReadLogId } = useLogger(); const [isOpenDialog, openDialog, closeDialog] = useOnOff(autoOpen); const { count, backgroundColor } = getMaxLevelLogs( logsHistory, lastReadLogId, ); const COLUMNS = useMemo(() => { const columnHelper = createTanStackColumnHelper(); return columnHelper.columns([ { header: '#', accessorFn: (_, index) => index + 1, cell: ({ row }) => ( {row.original.id > lastReadLogId && (
)} {row.index + 1} ), meta: { style: { width: '40px' } }, }, { header: 'Time', accessorKey: 'time', cell: ({ getValue }) => logsDataFormat.format(getValue()), meta: { style: { width: '100px' } }, }, { header: 'Label', accessorKey: 'levelLabel', meta: { style: { width: '60px' } }, }, { header: 'Message', accessorKey: 'message', }, { header: 'Error', cell: ({ row }) => { const { error } = row.original; return ( ); }, }, ]); }, [lastReadLogId]); const sortedLogs = useMemo( () => logsHistory.toSorted((a, b) => b.time - a.time), [logsHistory], ); return ( <> {!autoOpen && ( { openDialog(); }} >
{count ? ( {count} ) : null}
)} { markAsRead(); closeDialog(); onClose?.(); }} title="Logs history" style={{ width: '50vw', height: '50vh' }} > {/* TODO: logger should be in a ref. */} {/* eslint-disable-next-line @eslint-react/immutability */} logger.clear()} fill="outline">
Clear Logs
); }