2024-02-12 00:23:19 +05:30
|
|
|
import './useTableView.styles.scss';
|
|
|
|
|
|
2023-07-06 14:22:44 +03:00
|
|
|
import Convert from 'ansi-to-html';
|
2024-02-12 00:23:19 +05:30
|
|
|
import { Typography } from 'antd';
|
2023-07-06 14:22:44 +03:00
|
|
|
import { ColumnsType } from 'antd/es/table';
|
2024-08-22 23:56:51 +05:30
|
|
|
import cx from 'classnames';
|
2024-08-26 10:41:52 +05:30
|
|
|
import { unescapeString } from 'container/LogDetailedView/utils';
|
2023-07-06 14:22:44 +03:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import dompurify from 'dompurify';
|
2024-02-12 00:23:19 +05:30
|
|
|
import { useIsDarkMode } from 'hooks/useDarkMode';
|
2023-07-06 14:22:44 +03:00
|
|
|
import { FlatLogData } from 'lib/logs/flatLogData';
|
2024-02-12 00:23:19 +05:30
|
|
|
import { useMemo } from 'react';
|
2024-05-20 19:41:44 +05:30
|
|
|
import { FORBID_DOM_PURIFY_TAGS } from 'utils/app';
|
2023-07-06 14:22:44 +03:00
|
|
|
|
2024-03-07 12:25:00 +05:30
|
|
|
import LogStateIndicator from '../LogStateIndicator/LogStateIndicator';
|
|
|
|
|
import { getLogIndicatorTypeForTable } from '../LogStateIndicator/utils';
|
2024-02-20 16:21:07 +05:30
|
|
|
import {
|
|
|
|
|
defaultListViewPanelStyle,
|
|
|
|
|
defaultTableStyle,
|
|
|
|
|
getDefaultCellStyle,
|
|
|
|
|
} from './config';
|
2023-07-06 14:22:44 +03:00
|
|
|
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,
|
2024-08-22 23:56:51 +05:30
|
|
|
fontSize,
|
2023-07-06 18:56:29 +03:00
|
|
|
appendTo = 'center',
|
2024-02-12 00:23:19 +05:30
|
|
|
activeContextLog,
|
|
|
|
|
activeLog,
|
2024-02-20 16:21:07 +05:30
|
|
|
isListViewPanel,
|
2023-07-06 18:56:29 +03:00
|
|
|
} = props;
|
2024-02-12 00:23:19 +05:30
|
|
|
|
|
|
|
|
const isDarkMode = useIsDarkMode();
|
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: {
|
2024-02-20 16:21:07 +05:30
|
|
|
style: isListViewPanel
|
|
|
|
|
? defaultListViewPanelStyle
|
|
|
|
|
: getDefaultCellStyle(isDarkMode),
|
2023-07-06 14:22:44 +03:00
|
|
|
},
|
|
|
|
|
children: (
|
2024-08-22 23:56:51 +05:30
|
|
|
<Typography.Paragraph
|
|
|
|
|
ellipsis={{ rows: linesPerRow }}
|
|
|
|
|
className={cx('paragraph', fontSize)}
|
|
|
|
|
>
|
2023-07-06 14:22:44 +03:00
|
|
|
{field}
|
|
|
|
|
</Typography.Paragraph>
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
2024-02-20 16:21:07 +05:30
|
|
|
if (isListViewPanel) {
|
|
|
|
|
return [...fieldColumns];
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-06 14:22:44 +03:00
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
title: 'timestamp',
|
|
|
|
|
dataIndex: 'timestamp',
|
|
|
|
|
key: 'timestamp',
|
|
|
|
|
// https://github.com/ant-design/ant-design/discussions/36886
|
2024-02-12 00:23:19 +05:30
|
|
|
render: (field, item): ColumnTypeRender<Record<string, unknown>> => {
|
2023-07-06 14:22:44 +03:00
|
|
|
const date =
|
|
|
|
|
typeof field === 'string'
|
2024-05-01 15:27:48 +05:30
|
|
|
? dayjs(field).format('YYYY-MM-DD HH:mm:ss.SSS')
|
|
|
|
|
: dayjs(field / 1e6).format('YYYY-MM-DD HH:mm:ss.SSS');
|
2023-07-06 14:22:44 +03:00
|
|
|
return {
|
2024-02-12 00:23:19 +05:30
|
|
|
children: (
|
|
|
|
|
<div className="table-timestamp">
|
|
|
|
|
<LogStateIndicator
|
2024-03-07 12:25:00 +05:30
|
|
|
type={getLogIndicatorTypeForTable(item)}
|
2024-02-12 00:23:19 +05:30
|
|
|
isActive={
|
|
|
|
|
activeLog?.id === item.id || activeContextLog?.id === item.id
|
|
|
|
|
}
|
2024-08-22 23:56:51 +05:30
|
|
|
fontSize={fontSize}
|
2024-02-12 00:23:19 +05:30
|
|
|
/>
|
2024-08-22 23:56:51 +05:30
|
|
|
<Typography.Paragraph ellipsis className={cx('text', fontSize)}>
|
2024-02-12 00:23:19 +05:30
|
|
|
{date}
|
|
|
|
|
</Typography.Paragraph>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
2023-07-06 14:22:44 +03:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
},
|
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={{
|
2024-05-20 19:41:44 +05:30
|
|
|
__html: convert.toHtml(
|
2024-08-26 10:41:52 +05:30
|
|
|
dompurify.sanitize(unescapeString(field), {
|
2024-05-20 19:41:44 +05:30
|
|
|
FORBID_TAGS: [...FORBID_DOM_PURIFY_TAGS],
|
|
|
|
|
}),
|
|
|
|
|
),
|
2023-07-06 14:22:44 +03:00
|
|
|
}}
|
2024-08-22 23:56:51 +05:30
|
|
|
fontSize={fontSize}
|
2023-07-06 14:22:44 +03:00
|
|
|
linesPerRow={linesPerRow}
|
2024-02-12 00:23:19 +05:30
|
|
|
isDarkMode={isDarkMode}
|
2023-07-06 14:22:44 +03:00
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
},
|
2023-07-06 18:56:29 +03:00
|
|
|
...(appendTo === 'end' ? fieldColumns : []),
|
2023-07-06 14:22:44 +03:00
|
|
|
];
|
2023-07-30 14:02:18 +03:00
|
|
|
}, [
|
|
|
|
|
fields,
|
2024-02-20 16:21:07 +05:30
|
|
|
isListViewPanel,
|
2023-07-30 14:02:18 +03:00
|
|
|
appendTo,
|
2024-02-12 00:23:19 +05:30
|
|
|
isDarkMode,
|
2023-07-30 14:02:18 +03:00
|
|
|
linesPerRow,
|
2024-02-12 00:23:19 +05:30
|
|
|
activeLog?.id,
|
|
|
|
|
activeContextLog?.id,
|
2024-08-22 23:56:51 +05:30
|
|
|
fontSize,
|
2023-07-30 14:02:18 +03:00
|
|
|
]);
|
2023-07-06 14:22:44 +03:00
|
|
|
|
|
|
|
|
return { columns, dataSource: flattenLogData };
|
|
|
|
|
};
|