owncast/web/components/log-table.tsx

89 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-02-04 18:19:16 +01:00
import React from 'react';
import { Table, Tag, Typography } from 'antd';
import Linkify from 'react-linkify';
import { SortOrder } from 'antd/lib/table/interface';
import format from 'date-fns/format';
2020-11-01 07:17:44 +01:00
2020-11-13 12:57:57 +01:00
const { Title } = Typography;
2020-11-01 07:17:44 +01:00
function renderColumnLevel(text, entry) {
let color = 'black';
2021-02-04 18:19:16 +01:00
if (entry.level === 'warning') {
color = 'orange';
2020-11-01 07:17:44 +01:00
} else if (entry.level === 'error') {
2021-02-04 18:19:16 +01:00
color = 'red';
2020-11-01 07:17:44 +01:00
}
2020-11-03 02:23:32 +01:00
return <Tag color={color}>{text}</Tag>;
2020-11-01 07:17:44 +01:00
}
2020-11-01 08:01:37 +01:00
function renderMessage(text) {
2021-02-04 18:19:16 +01:00
return <Linkify>{text}</Linkify>;
2020-11-01 07:17:44 +01:00
}
2020-10-30 02:01:38 +01:00
2020-11-01 08:01:37 +01:00
interface Props {
2021-02-04 18:19:16 +01:00
logs: object[];
pageSize: number;
2020-11-01 08:01:37 +01:00
}
export default function LogTable({ logs, pageSize }: Props) {
2020-11-16 23:32:37 +01:00
if (!logs?.length) {
2020-11-13 12:57:57 +01:00
return null;
}
2020-10-30 02:01:38 +01:00
const columns = [
{
2021-02-04 18:19:16 +01:00
title: 'Level',
dataIndex: 'level',
key: 'level',
2020-10-30 02:01:38 +01:00
filters: [
{
2021-02-04 18:19:16 +01:00
text: 'Info',
value: 'info',
2020-10-30 02:01:38 +01:00
},
{
2021-02-04 18:19:16 +01:00
text: 'Warning',
value: 'warning',
2020-10-30 02:01:38 +01:00
},
{
2021-02-04 18:19:16 +01:00
text: 'Error',
value: 'Error',
2020-10-30 02:01:38 +01:00
},
],
onFilter: (level, row) => row.level.indexOf(level) === 0,
render: renderColumnLevel,
},
{
2021-02-04 18:19:16 +01:00
title: 'Timestamp',
dataIndex: 'time',
key: 'time',
render: timestamp => {
const dateObject = new Date(timestamp);
2020-12-06 09:49:55 +01:00
return format(dateObject, 'p P');
},
2020-10-30 02:01:38 +01:00
sorter: (a, b) => new Date(a.time).getTime() - new Date(b.time).getTime(),
2021-02-04 18:19:16 +01:00
sortDirections: ['descend', 'ascend'] as SortOrder[],
defaultSortOrder: 'descend' as SortOrder,
2020-10-30 02:01:38 +01:00
},
{
2021-02-04 18:19:16 +01:00
title: 'Message',
dataIndex: 'message',
key: 'message',
2020-10-30 02:01:38 +01:00
render: renderMessage,
},
];
return (
2020-11-13 12:57:57 +01:00
<div className="logs-section">
<Title>Logs</Title>
2020-10-30 02:01:38 +01:00
<Table
2020-11-01 03:29:06 +01:00
size="middle"
2020-10-30 02:01:38 +01:00
dataSource={logs}
columns={columns}
2021-02-04 18:19:16 +01:00
rowKey={row => row.time}
2020-10-30 02:01:38 +01:00
pagination={{ pageSize: pageSize || 20 }}
/>
</div>
);
}