2023-07-06 14:22:44 +03:00
|
|
|
import { ExpandAltOutlined } from '@ant-design/icons';
|
|
|
|
|
import Convert from 'ansi-to-html';
|
|
|
|
|
import { Typography } from 'antd';
|
|
|
|
|
import { ColumnsType } from 'antd/es/table';
|
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import dompurify from 'dompurify';
|
|
|
|
|
import { FlatLogData } from 'lib/logs/flatLogData';
|
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
|
|
|
|
|
import { ExpandIconWrapper } from '../RawLogView/styles';
|
|
|
|
|
import { defaultCellStyle, defaultTableStyle } from './config';
|
|
|
|
|
import { TableBodyContent } from './styles';
|
2023-07-06 18:56:29 +03:00
|
|
|
import {
|
|
|
|
|
ColumnTypeRender,
|
|
|
|
|
UseTableViewProps,
|
|
|
|
|
UseTableViewResult,
|
|
|
|
|
} from './types';
|
2023-07-06 14:22:44 +03:00
|
|
|
|
|
|
|
|
const convert = new Convert();
|
|
|
|
|
|
2023-07-06 18:56:29 +03:00
|
|
|
export const useTableView = (props: UseTableViewProps): UseTableViewResult => {
|
|
|
|
|
const {
|
|
|
|
|
logs,
|
|
|
|
|
fields,
|
|
|
|
|
linesPerRow,
|
|
|
|
|
onClickExpand,
|
|
|
|
|
appendTo = 'center',
|
|
|
|
|
} = props;
|
2023-07-06 14:22:44 +03:00
|
|
|
|
|
|
|
|
const flattenLogData = useMemo(() => logs.map((log) => FlatLogData(log)), [
|
|
|
|
|
logs,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const columns: ColumnsType<Record<string, unknown>> = useMemo(() => {
|
|
|
|
|
const fieldColumns: ColumnsType<Record<string, unknown>> = fields
|
|
|
|
|
.filter((e) => e.name !== 'id')
|
|
|
|
|
.map(({ name }) => ({
|
|
|
|
|
title: name,
|
|
|
|
|
dataIndex: name,
|
|
|
|
|
key: name,
|
|
|
|
|
render: (field): ColumnTypeRender<Record<string, unknown>> => ({
|
|
|
|
|
props: {
|
|
|
|
|
style: defaultCellStyle,
|
|
|
|
|
},
|
|
|
|
|
children: (
|
|
|
|
|
<Typography.Paragraph ellipsis={{ rows: linesPerRow }}>
|
|
|
|
|
{field}
|
|
|
|
|
</Typography.Paragraph>
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
title: '',
|
|
|
|
|
dataIndex: 'id',
|
|
|
|
|
key: 'expand',
|
|
|
|
|
// https://github.com/ant-design/ant-design/discussions/36886
|
2023-07-17 18:25:55 +05:30
|
|
|
render: (_, item, index): ColumnTypeRender<Record<string, unknown>> => ({
|
2023-07-06 14:22:44 +03:00
|
|
|
props: {
|
|
|
|
|
style: defaultCellStyle,
|
|
|
|
|
},
|
|
|
|
|
children: (
|
|
|
|
|
<ExpandIconWrapper
|
|
|
|
|
onClick={(): void => {
|
2023-07-17 18:25:55 +05:30
|
|
|
onClickExpand(logs[index]);
|
2023-07-06 14:22:44 +03:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<ExpandAltOutlined />
|
|
|
|
|
</ExpandIconWrapper>
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
title: 'timestamp',
|
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
|
key: 'timestamp',
|
|
|
|
|
// https://github.com/ant-design/ant-design/discussions/36886
|
|
|
|
|
render: (field): ColumnTypeRender<Record<string, unknown>> => {
|
|
|
|
|
const date =
|
|
|
|
|
typeof field === 'string'
|
|
|
|
|
? dayjs(field).format()
|
|
|
|
|
: dayjs(field / 1e6).format();
|
|
|
|
|
return {
|
|
|
|
|
children: <Typography.Paragraph ellipsis>{date}</Typography.Paragraph>,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-07-06 18:56:29 +03:00
|
|
|
...(appendTo === 'center' ? fieldColumns : []),
|
2023-07-06 14:22:44 +03:00
|
|
|
{
|
|
|
|
|
title: 'body',
|
|
|
|
|
dataIndex: 'body',
|
|
|
|
|
key: 'body',
|
|
|
|
|
render: (field): ColumnTypeRender<Record<string, unknown>> => ({
|
|
|
|
|
props: {
|
|
|
|
|
style: defaultTableStyle,
|
|
|
|
|
},
|
|
|
|
|
children: (
|
|
|
|
|
<TableBodyContent
|
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
|
__html: convert.toHtml(dompurify.sanitize(field)),
|
|
|
|
|
}}
|
|
|
|
|
linesPerRow={linesPerRow}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
},
|
2023-07-06 18:56:29 +03:00
|
|
|
...(appendTo === 'end' ? fieldColumns : []),
|
2023-07-06 14:22:44 +03:00
|
|
|
];
|
2023-07-17 18:25:55 +05:30
|
|
|
}, [fields, appendTo, linesPerRow, onClickExpand, logs]);
|
2023-07-06 14:22:44 +03:00
|
|
|
|
|
|
|
|
return { columns, dataSource: flattenLogData };
|
|
|
|
|
};
|