|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */
|
2 | 2 | import _ from 'lodash';
|
3 |
| -import { useEffect, useRef, useState } from 'react'; |
| 3 | +import { useCallback, useMemo, useRef } from 'react'; |
4 | 4 | import { PublishIcon } from '../../icons';
|
5 | 5 | import { CHARCOAL, useTheme } from '../../theme';
|
6 | 6 | import { Pattern } from '../CustomCatalog/CustomCard';
|
@@ -47,28 +47,54 @@ export const CatalogDesignsTable: React.FC<CatalogDesignsTableProps> = ({
|
47 | 47 | handleBulkDeleteModal,
|
48 | 48 | handleBulkpatternsDataUnpublishModal
|
49 | 49 | }) => {
|
50 |
| - const [tableCols, updateCols] = useState<Array<any>>([]); |
51 | 50 | const { width } = useWindowDimensions();
|
52 | 51 | const smallScreen = width <= 360;
|
53 | 52 | const theme = useTheme();
|
54 | 53 | const modalRef = useRef<PromptRef>(null);
|
55 | 54 |
|
56 |
| - useEffect(() => { |
57 |
| - if (Array.isArray(columns) && columns.length > 0) { |
58 |
| - updateCols(columns); |
59 |
| - } |
60 |
| - }, [columns]); |
| 55 | + const formatDate = useCallback((date: string | Date): string => { |
| 56 | + const dateOptions: Intl.DateTimeFormatOptions = { |
| 57 | + weekday: 'short', |
| 58 | + day: 'numeric', |
| 59 | + month: 'long', |
| 60 | + year: 'numeric' |
| 61 | + }; |
| 62 | + return new Date(date).toLocaleDateString('en-US', dateOptions); |
| 63 | + }, []); |
| 64 | + |
| 65 | + const processedColumns = useMemo(() => { |
| 66 | + return columns.map((col) => { |
| 67 | + const newCol = { ...col }; |
| 68 | + if (!newCol.options) newCol.options = {}; |
| 69 | + newCol.options.display = columnVisibility[col.name]; |
| 70 | + if ( |
| 71 | + [ |
| 72 | + 'updated_at', |
| 73 | + 'created_at', |
| 74 | + 'deleted_at', |
| 75 | + 'last_login_time', |
| 76 | + 'joined_at', |
| 77 | + 'last_run', |
| 78 | + 'next_run' |
| 79 | + ].includes(col.name) |
| 80 | + ) { |
| 81 | + newCol.options.customBodyRender = (value: any) => { |
| 82 | + if (!value || value === 'NA') return <>NA</>; |
| 83 | + if (typeof value === 'object' && 'Valid' in value) { |
| 84 | + if (value.Valid && value.Time) { |
| 85 | + return <>{formatDate(value.Time)}</>; |
| 86 | + } |
| 87 | + return <>NA</>; |
| 88 | + } |
| 89 | + return <>{formatDate(value)}</>; |
| 90 | + }; |
| 91 | + } |
| 92 | + return newCol; |
| 93 | + }); |
| 94 | + }, [columns, columnVisibility, formatDate]); |
61 | 95 |
|
62 |
| - const options: any = { |
63 |
| - selectableRows: _.isNil(filter) ? 'none' : 'multiple', |
64 |
| - serverSide: true, |
65 |
| - filterType: 'multiselect', |
66 |
| - responsive: smallScreen ? 'vertical' : 'standard', |
67 |
| - count: totalCount, |
68 |
| - rowsPerPage: pageSize, |
69 |
| - page, |
70 |
| - elevation: 0, |
71 |
| - onTableChange: (action: string, tableState: any) => { |
| 96 | + const handleTableChange = useCallback( |
| 97 | + (action: string, tableState: any) => { |
72 | 98 | const sortInfo = tableState.announceText ? tableState.announceText.split(' : ') : [];
|
73 | 99 | let order = '';
|
74 | 100 | if (tableState.activeColumn) {
|
@@ -98,44 +124,71 @@ export const CatalogDesignsTable: React.FC<CatalogDesignsTableProps> = ({
|
98 | 124 | }
|
99 | 125 | break;
|
100 | 126 | }
|
101 |
| - } |
102 |
| - }; |
| 127 | + }, |
| 128 | + [columns, setPage, setPageSize, setSortOrder, sortOrder] |
| 129 | + ); |
103 | 130 |
|
104 |
| - if (_.isNil(filter)) { |
105 |
| - options.customToolbarSelect = (selected: any) => ( |
106 |
| - <UnpublishTooltipIcon |
107 |
| - title="Unpublish" |
108 |
| - onClick={() => handleBulkpatternsDataUnpublishModal(selected, patterns, modalRef)} |
109 |
| - iconType="publish" |
110 |
| - id={'unpublish-button'} |
111 |
| - > |
112 |
| - <PublishIcon width={28.8} height={28.8} fill={CHARCOAL} /> |
113 |
| - </UnpublishTooltipIcon> |
114 |
| - ); |
115 |
| - } else { |
116 |
| - options.onRowsDelete = (rowsDeleted: any) => { |
117 |
| - const selectedPatterns = rowsDeleted.data.map(({ dataIndex }: any) => patterns[dataIndex]); |
118 |
| - handleBulkDeleteModal(selectedPatterns, modalRef); |
119 |
| - return false; |
120 |
| - }; |
121 |
| - } |
| 131 | + const options = useMemo( |
| 132 | + () => ({ |
| 133 | + selectableRows: _.isNil(filter) ? 'none' : 'multiple', |
| 134 | + serverSide: true, |
| 135 | + filterType: 'multiselect', |
| 136 | + responsive: smallScreen ? 'vertical' : 'standard', |
| 137 | + count: totalCount, |
| 138 | + rowsPerPage: pageSize, |
| 139 | + page, |
| 140 | + elevation: 0, |
| 141 | + onTableChange: handleTableChange, |
| 142 | + customToolbarSelect: _.isNil(filter) |
| 143 | + ? (selected: any) => ( |
| 144 | + <UnpublishTooltipIcon |
| 145 | + title="Unpublish" |
| 146 | + onClick={() => handleBulkpatternsDataUnpublishModal(selected, patterns, modalRef)} |
| 147 | + iconType="publish" |
| 148 | + id={'unpublish-button'} |
| 149 | + > |
| 150 | + <PublishIcon width={28.8} height={28.8} fill={CHARCOAL} /> |
| 151 | + </UnpublishTooltipIcon> |
| 152 | + ) |
| 153 | + : undefined, |
| 154 | + onRowsDelete: !_.isNil(filter) |
| 155 | + ? (rowsDeleted: any) => { |
| 156 | + const selectedPatterns = rowsDeleted.data.map( |
| 157 | + ({ dataIndex }: any) => patterns[dataIndex] |
| 158 | + ); |
| 159 | + handleBulkDeleteModal(selectedPatterns, modalRef); |
| 160 | + return false; |
| 161 | + } |
| 162 | + : undefined |
| 163 | + }), |
| 164 | + [ |
| 165 | + filter, |
| 166 | + smallScreen, |
| 167 | + totalCount, |
| 168 | + pageSize, |
| 169 | + page, |
| 170 | + handleTableChange, |
| 171 | + patterns, |
| 172 | + handleBulkDeleteModal, |
| 173 | + handleBulkpatternsDataUnpublishModal |
| 174 | + ] |
| 175 | + ); |
122 | 176 |
|
123 |
| - if (!Array.isArray(tableCols) || tableCols.length === 0) { |
| 177 | + if (!processedColumns.length) { |
124 | 178 | return null;
|
125 | 179 | }
|
126 | 180 |
|
127 | 181 | return (
|
128 | 182 | <>
|
129 | 183 | <PromptComponent ref={modalRef} />
|
130 | 184 | <ResponsiveDataTable
|
131 |
| - columns={columns} |
| 185 | + columns={processedColumns} |
132 | 186 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
133 | 187 | //@ts-ignore
|
134 | 188 | data={patterns || []}
|
135 | 189 | options={options}
|
136 | 190 | colViews={colViews}
|
137 |
| - tableCols={tableCols} |
138 |
| - updateCols={updateCols} |
| 191 | + tableCols={processedColumns} |
139 | 192 | columnVisibility={columnVisibility}
|
140 | 193 | backgroundColor={
|
141 | 194 | theme.palette.mode === 'light'
|
|
0 commit comments