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';
|
|
|
|
|
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';
|
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,
|
|
|
|
|
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: (
|
|
|
|
|
<Typography.Paragraph ellipsis={{ rows: linesPerRow }}>
|
|
|
|
|
{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'
|
|
|
|
|
? dayjs(field).format()
|
|
|
|
|
: dayjs(field / 1e6).format();
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<Typography.Paragraph ellipsis className="text">
|
|
|
|
|
{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={{
|
|
|
|
|
__html: convert.toHtml(dompurify.sanitize(field)),
|
|
|
|
|
}}
|
|
|
|
|
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,
|
2023-07-30 14:02:18 +03:00
|
|
|
]);
|
2023-07-06 14:22:44 +03:00
|
|
|
|
|
|
|
|
return { columns, dataSource: flattenLogData };
|
|
|
|
|
};
|