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 { ILog } from 'types/api/logs/log'; import { ExpandIconWrapper } from '../RawLogView/styles'; import { defaultCellStyle, defaultTableStyle } from './config'; import { TableBodyContent } from './styles'; import { ColumnTypeRender, UseTableViewProps, UseTableViewResult, } from './types'; const convert = new Convert(); export const useTableView = (props: UseTableViewProps): UseTableViewResult => { const { logs, fields, linesPerRow, onClickExpand, appendTo = 'center', } = props; const flattenLogData = useMemo(() => logs.map((log) => FlatLogData(log)), [ logs, ]); const columns: ColumnsType> = useMemo(() => { const fieldColumns: ColumnsType> = fields .filter((e) => e.name !== 'id') .map(({ name }) => ({ title: name, dataIndex: name, key: name, render: (field): ColumnTypeRender> => ({ props: { style: defaultCellStyle, }, children: ( {field} ), }), })); return [ { title: '', dataIndex: 'id', key: 'expand', // https://github.com/ant-design/ant-design/discussions/36886 render: (_, item): ColumnTypeRender> => ({ props: { style: defaultCellStyle, }, children: ( { onClickExpand((item as unknown) as ILog); }} > ), }), }, { title: 'timestamp', dataIndex: 'timestamp', key: 'timestamp', // https://github.com/ant-design/ant-design/discussions/36886 render: (field): ColumnTypeRender> => { const date = typeof field === 'string' ? dayjs(field).format() : dayjs(field / 1e6).format(); return { children: {date}, }; }, }, ...(appendTo === 'center' ? fieldColumns : []), { title: 'body', dataIndex: 'body', key: 'body', render: (field): ColumnTypeRender> => ({ props: { style: defaultTableStyle, }, children: ( ), }), }, ...(appendTo === 'end' ? fieldColumns : []), ]; }, [fields, linesPerRow, appendTo, onClickExpand]); return { columns, dataSource: flattenLogData }; };