|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; |
| 3 | +import { MUIDataTableColumn, MUIDataTableMeta } from 'mui-datatables'; |
| 4 | +import React, { useState } from 'react'; |
| 5 | +import { Accordion, AccordionDetails, AccordionSummary, Typography } from '../../base'; |
| 6 | +import { DeleteIcon, EnvironmentIcon } from '../../icons'; |
| 7 | +import { CHARCOAL, SistentThemeProvider } from '../../theme'; |
| 8 | +import { NameDiv } from '../CatalogDesignTable/style'; |
| 9 | +import { RESOURCE_TYPES } from '../CatalogDetail/types'; |
| 10 | +import { CustomColumnVisibilityControl } from '../CustomColumnVisibilityControl'; |
| 11 | +import { CustomTooltip } from '../CustomTooltip'; |
| 12 | +import { ConditionalTooltip } from '../Helpers/CondtionalTooltip'; |
| 13 | +import { useWindowDimensions } from '../Helpers/Dimension'; |
| 14 | +import { |
| 15 | + ColView, |
| 16 | + updateVisibleColumns |
| 17 | +} from '../Helpers/ResponsiveColumns/responsive-coulmns.tsx/responsive-column'; |
| 18 | +import ResponsiveDataTable, { IconWrapper } from '../ResponsiveDataTable'; |
| 19 | +import SearchBar from '../SearchBar'; |
| 20 | +import { TooltipIcon } from '../TooltipIconButton'; |
| 21 | +import AssignmentModal from './AssignmentModal'; |
| 22 | +import EditButton from './EditButton'; |
| 23 | +import useViewAssignment from './hooks/useViewsAssignment'; |
| 24 | +import { CellStyle, CustomBodyRenderStyle, TableHeader, TableRightActionHeader } from './styles'; |
| 25 | + |
| 26 | +interface ViewsTableProps { |
| 27 | + workspaceId: string; |
| 28 | + workspaceName: string; |
| 29 | + useGetViewsOfWorkspaceQuery: any; |
| 30 | + useUnassignViewFromWorkspaceMutation: any; |
| 31 | + useAssignViewToWorkspaceMutation: any; |
| 32 | + isRemoveAllowed: boolean; |
| 33 | + isAssignAllowed: boolean; |
| 34 | + handleShowDetails: (viewId: string, viewName: string, filterType: string) => void; |
| 35 | +} |
| 36 | + |
| 37 | +const colViews: ColView[] = [ |
| 38 | + ['id', 'na'], |
| 39 | + ['name', 'xs'], |
| 40 | + ['description', 'm'], |
| 41 | + ['organization_id', 'l'], |
| 42 | + ['created_at', 'xl'], |
| 43 | + ['updated_at', 'xl'], |
| 44 | + ['visibility', 'l'], |
| 45 | + ['actions', 'xs'] |
| 46 | +]; |
| 47 | + |
| 48 | +export const ResizableDescriptionCell = ({ value }: { value: string }) => ( |
| 49 | + <div style={{ position: 'relative', height: '20px' }}> |
| 50 | + <CustomBodyRenderStyle> |
| 51 | + <CellStyle> |
| 52 | + <CustomTooltip title={value} placement="top-start"> |
| 53 | + <span style={{ cursor: 'pointer' }}>{value}</span> |
| 54 | + </CustomTooltip> |
| 55 | + </CellStyle> |
| 56 | + </CustomBodyRenderStyle> |
| 57 | + </div> |
| 58 | +); |
| 59 | + |
| 60 | +const WorkspaceViewsTable: React.FC<ViewsTableProps> = ({ |
| 61 | + workspaceId, |
| 62 | + workspaceName, |
| 63 | + isRemoveAllowed, |
| 64 | + useGetViewsOfWorkspaceQuery, |
| 65 | + useUnassignViewFromWorkspaceMutation, |
| 66 | + useAssignViewToWorkspaceMutation, |
| 67 | + isAssignAllowed, |
| 68 | + handleShowDetails |
| 69 | +}) => { |
| 70 | + const [expanded, setExpanded] = useState<boolean>(true); |
| 71 | + const handleAccordionChange = () => { |
| 72 | + setExpanded(!expanded); |
| 73 | + }; |
| 74 | + const [search, setSearch] = useState(''); |
| 75 | + const [isSearchExpanded, setIsSearchExpanded] = useState(false); |
| 76 | + const [page, setPage] = useState<number>(0); |
| 77 | + const [pageSize, setPageSize] = useState<number>(10); |
| 78 | + const [sortOrder, setSortOrder] = useState<string>('updated_at desc'); |
| 79 | + const { data: viewsOfWorkspace } = useGetViewsOfWorkspaceQuery({ |
| 80 | + workspaceId, |
| 81 | + page: page, |
| 82 | + pageSize: pageSize, |
| 83 | + search: search, |
| 84 | + order: sortOrder |
| 85 | + }); |
| 86 | + const { width } = useWindowDimensions(); |
| 87 | + const [unassignviewFromWorkspace] = useUnassignViewFromWorkspaceMutation(); |
| 88 | + const columns: MUIDataTableColumn[] = [ |
| 89 | + { |
| 90 | + name: 'id', |
| 91 | + label: 'ID', |
| 92 | + options: { |
| 93 | + filter: false, |
| 94 | + customBodyRender: (value) => <ConditionalTooltip value={value} maxLength={10} /> |
| 95 | + } |
| 96 | + }, |
| 97 | + { |
| 98 | + name: 'name', |
| 99 | + label: 'Name', |
| 100 | + options: { |
| 101 | + filter: false, |
| 102 | + sort: true, |
| 103 | + searchable: true, |
| 104 | + customBodyRender: (value, tableMeta) => { |
| 105 | + const viewId = tableMeta.tableData[tableMeta.rowIndex]?.id ?? ''; |
| 106 | + const viewName = tableMeta.tableData[tableMeta.rowIndex]?.name ?? ''; |
| 107 | + return ( |
| 108 | + <NameDiv onClick={() => handleShowDetails(viewId, viewName, RESOURCE_TYPES.VIEW)}> |
| 109 | + {value} |
| 110 | + </NameDiv> |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + { |
| 116 | + name: 'created_at', |
| 117 | + label: 'Created At', |
| 118 | + options: { |
| 119 | + filter: false, |
| 120 | + sort: true, |
| 121 | + searchable: true, |
| 122 | + setCellHeaderProps: () => { |
| 123 | + return { align: 'center' }; |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | + { |
| 128 | + name: 'updated_at', |
| 129 | + label: 'Updated At', |
| 130 | + options: { |
| 131 | + filter: false, |
| 132 | + sort: true, |
| 133 | + searchable: true, |
| 134 | + setCellHeaderProps: () => { |
| 135 | + return { align: 'center' }; |
| 136 | + } |
| 137 | + } |
| 138 | + }, |
| 139 | + { |
| 140 | + name: 'visibility', |
| 141 | + label: 'Visibility', |
| 142 | + options: { |
| 143 | + filter: false, |
| 144 | + sort: false, |
| 145 | + searchable: true, |
| 146 | + setCellHeaderProps: () => { |
| 147 | + return { align: 'center' }; |
| 148 | + } |
| 149 | + } |
| 150 | + }, |
| 151 | + { |
| 152 | + name: 'actions', |
| 153 | + label: 'Actions', |
| 154 | + options: { |
| 155 | + filter: false, |
| 156 | + sort: false, |
| 157 | + searchable: false, |
| 158 | + customBodyRender: (_: string, tableMeta: MUIDataTableMeta) => ( |
| 159 | + <IconWrapper disabled={!isRemoveAllowed}> |
| 160 | + <TooltipIcon |
| 161 | + id={`delete_team-${tableMeta.rowIndex}`} |
| 162 | + title="Remove View" |
| 163 | + onClick={() => { |
| 164 | + isRemoveAllowed && |
| 165 | + unassignviewFromWorkspace({ |
| 166 | + workspaceId, |
| 167 | + viewId: tableMeta.rowData[0] |
| 168 | + }); |
| 169 | + }} |
| 170 | + iconType="delete" |
| 171 | + > |
| 172 | + <DeleteIcon height={28} width={28} fill={CHARCOAL} /> |
| 173 | + </TooltipIcon> |
| 174 | + </IconWrapper> |
| 175 | + ) |
| 176 | + } |
| 177 | + } |
| 178 | + ]; |
| 179 | + |
| 180 | + const viewAssignment = useViewAssignment({ |
| 181 | + workspaceId, |
| 182 | + useGetViewsOfWorkspaceQuery, |
| 183 | + useUnassignViewFromWorkspaceMutation, |
| 184 | + useAssignViewToWorkspaceMutation |
| 185 | + }); |
| 186 | + |
| 187 | + const [columnVisibility, setColumnVisibility] = useState<Record<string, boolean>>(() => { |
| 188 | + const showCols = updateVisibleColumns(colViews, width); |
| 189 | + const initialVisibility: Record<string, boolean> = {}; |
| 190 | + columns.forEach((col) => { |
| 191 | + initialVisibility[col.name] = showCols[col.name]; |
| 192 | + }); |
| 193 | + return initialVisibility; |
| 194 | + }); |
| 195 | + |
| 196 | + const options = { |
| 197 | + filter: false, |
| 198 | + responsive: 'standard', |
| 199 | + selectableRows: 'none', |
| 200 | + count: viewsOfWorkspace?.total_count, |
| 201 | + rowsPerPage: pageSize, |
| 202 | + page, |
| 203 | + elevation: 0, |
| 204 | + sortOrder: { |
| 205 | + name: sortOrder.split(' ')[0], |
| 206 | + direction: sortOrder.split(' ')[1] |
| 207 | + }, |
| 208 | + onTableChange: (action: string, tableState: any) => { |
| 209 | + const sortInfo = tableState.announceText ? tableState.announceText.split(' : ') : []; |
| 210 | + let order = ''; |
| 211 | + if (tableState.activeColumn) { |
| 212 | + order = `${columns[tableState.activeColumn].name} desc`; |
| 213 | + } |
| 214 | + switch (action) { |
| 215 | + case 'changePage': |
| 216 | + setPage(tableState.page); |
| 217 | + break; |
| 218 | + case 'changeRowsPerPage': |
| 219 | + setPageSize(tableState.rowsPerPage); |
| 220 | + break; |
| 221 | + case 'sort': |
| 222 | + if (sortInfo.length == 2) { |
| 223 | + if (sortInfo[1] === 'ascending') { |
| 224 | + order = `${columns[tableState.activeColumn].name} asc`; |
| 225 | + } else { |
| 226 | + order = `${columns[tableState.activeColumn].name} desc`; |
| 227 | + } |
| 228 | + } |
| 229 | + if (order !== sortOrder) { |
| 230 | + setSortOrder(order); |
| 231 | + } |
| 232 | + break; |
| 233 | + } |
| 234 | + } |
| 235 | + }; |
| 236 | + const [tableCols, updateCols] = useState(columns); |
| 237 | + |
| 238 | + return ( |
| 239 | + <SistentThemeProvider> |
| 240 | + <Accordion expanded={expanded} onChange={handleAccordionChange} style={{ margin: 0 }}> |
| 241 | + <AccordionSummary |
| 242 | + expandIcon={<ExpandMoreIcon />} |
| 243 | + sx={{ |
| 244 | + backgroundColor: 'background.paper' |
| 245 | + }} |
| 246 | + > |
| 247 | + <TableHeader> |
| 248 | + <Typography variant="h6" fontWeight={'bold'}> |
| 249 | + Assigned Views |
| 250 | + </Typography> |
| 251 | + <TableRightActionHeader> |
| 252 | + <SearchBar |
| 253 | + onSearch={(value) => { |
| 254 | + setSearch(value); |
| 255 | + }} |
| 256 | + onClear={() => { |
| 257 | + setSearch(''); |
| 258 | + }} |
| 259 | + expanded={isSearchExpanded} |
| 260 | + setExpanded={setIsSearchExpanded} |
| 261 | + placeholder="Search workspaces..." |
| 262 | + /> |
| 263 | + <CustomColumnVisibilityControl |
| 264 | + columns={columns} |
| 265 | + customToolsProps={{ |
| 266 | + columnVisibility, |
| 267 | + setColumnVisibility |
| 268 | + }} |
| 269 | + id={'views-table'} |
| 270 | + /> |
| 271 | + <EditButton onClick={viewAssignment.handleAssignModal} disabled={!isAssignAllowed} /> |
| 272 | + </TableRightActionHeader> |
| 273 | + </TableHeader> |
| 274 | + </AccordionSummary> |
| 275 | + <AccordionDetails style={{ padding: 0 }}> |
| 276 | + <ResponsiveDataTable |
| 277 | + columns={columns} |
| 278 | + data={viewsOfWorkspace?.views} |
| 279 | + options={options} |
| 280 | + colViews={colViews} |
| 281 | + tableCols={tableCols} |
| 282 | + updateCols={updateCols} |
| 283 | + columnVisibility={columnVisibility} |
| 284 | + /> |
| 285 | + </AccordionDetails> |
| 286 | + </Accordion> |
| 287 | + |
| 288 | + <AssignmentModal |
| 289 | + open={viewAssignment.assignModal} |
| 290 | + onClose={viewAssignment.handleAssignModalClose} |
| 291 | + title={`Assign Views to ${workspaceName}`} |
| 292 | + headerIcon={<EnvironmentIcon height="40" width="40" fill={'white'} />} |
| 293 | + name="Views" |
| 294 | + assignableData={viewAssignment.data} |
| 295 | + handleAssignedData={viewAssignment.handleAssignData} |
| 296 | + originalAssignedData={viewAssignment.workspaceData} |
| 297 | + emptyStateIcon={<EnvironmentIcon height="5rem" width="5rem" fill={'#808080'} />} |
| 298 | + handleAssignablePage={viewAssignment.handleAssignablePage} |
| 299 | + handleAssignedPage={viewAssignment.handleAssignedPage} |
| 300 | + originalLeftCount={viewAssignment.data?.length || 0} |
| 301 | + originalRightCount={viewsOfWorkspace?.total_count || 0} |
| 302 | + onAssign={viewAssignment.handleAssign} |
| 303 | + disableTransfer={viewAssignment.disableTransferButton} |
| 304 | + helpText={`Assign Views to ${workspaceName}`} |
| 305 | + isAssignAllowed={isAssignAllowed} |
| 306 | + isRemoveAllowed={isRemoveAllowed} |
| 307 | + /> |
| 308 | + </SistentThemeProvider> |
| 309 | + ); |
| 310 | +}; |
| 311 | + |
| 312 | +export default WorkspaceViewsTable; |
0 commit comments