|
| 1 | +import { MUIDataTableColumn, MUIDataTableMeta } from 'mui-datatables'; |
| 2 | +import { PLAYGROUND_MODES } from '../../constants/constants'; |
| 3 | +import { ChainIcon, CopyIcon, KanvasIcon, PublishIcon } from '../../icons'; |
| 4 | +import Download from '../../icons/Download/Download'; |
| 5 | +import { CHARCOAL } from '../../theme'; |
| 6 | +import { downloadYaml, slugify } from '../CatalogDetail/helper'; |
| 7 | +import { RESOURCE_TYPES } from '../CatalogDetail/types'; |
| 8 | +import { Pattern } from '../CustomCatalog/CustomCard'; |
| 9 | +import { ConditionalTooltip } from '../Helpers/CondtionalTooltip'; |
| 10 | +import { ColView } from '../Helpers/ResponsiveColumns/responsive-coulmns.tsx'; |
| 11 | +import { DataTableEllipsisMenu } from '../ResponsiveDataTable'; |
| 12 | +import AuthorCell from './AuthorCell'; |
| 13 | +import { getColumnValue } from './helper'; |
| 14 | +import { L5DeleteIcon, NameDiv } from './style'; |
| 15 | + |
| 16 | +interface TableMeta extends MUIDataTableMeta { |
| 17 | + rowIndex: number; |
| 18 | + tableData: Pattern[]; |
| 19 | +} |
| 20 | + |
| 21 | +interface ColumnConfigProps { |
| 22 | + handleDeleteModal: (data: Pattern) => () => void; |
| 23 | + handlePublishModal: (data: Pattern) => void; |
| 24 | + handleUnpublishModal: (data: Pattern) => () => void; |
| 25 | + handleCopyUrl: (type: string, name: string, id: string) => void; |
| 26 | + handleClone: (name: string, id: string) => void; |
| 27 | + handleShowDetails: (designId: string, designName: string) => void; |
| 28 | + isDownloadAllowed: boolean; |
| 29 | + isCopyLinkAllowed: boolean; |
| 30 | + isDeleteAllowed: boolean; |
| 31 | + isPublishAllowed: boolean; |
| 32 | + isUnpublishAllowed: boolean; |
| 33 | + // for workspace designs table page only |
| 34 | + isFromWorkspaceTable?: boolean; |
| 35 | + isRemoveAllowed?: boolean; |
| 36 | +} |
| 37 | + |
| 38 | +export const colViews: ColView[] = [ |
| 39 | + ['id', 'na'], |
| 40 | + ['name', 'xs'], |
| 41 | + ['first_name', 'xs'], |
| 42 | + ['created_at', 'na'], |
| 43 | + ['updated_at', 'l'], |
| 44 | + ['visibility', 'l'], |
| 45 | + ['user_id', 'na'], |
| 46 | + ['actions', 'xs'] |
| 47 | +]; |
| 48 | + |
| 49 | +export const createDesignsColumnsConfig = ({ |
| 50 | + handleDeleteModal, |
| 51 | + handlePublishModal, |
| 52 | + handleUnpublishModal, |
| 53 | + handleCopyUrl, |
| 54 | + handleClone, |
| 55 | + handleShowDetails, |
| 56 | + isUnpublishAllowed, |
| 57 | + isCopyLinkAllowed, |
| 58 | + isDeleteAllowed, |
| 59 | + isPublishAllowed, |
| 60 | + isDownloadAllowed, |
| 61 | + isRemoveAllowed, |
| 62 | + isFromWorkspaceTable = false |
| 63 | +}: ColumnConfigProps): MUIDataTableColumn[] => { |
| 64 | + return [ |
| 65 | + { |
| 66 | + name: 'id', |
| 67 | + label: 'ID', |
| 68 | + options: { |
| 69 | + filter: false, |
| 70 | + customBodyRender: (value: string) => <ConditionalTooltip value={value} maxLength={10} /> |
| 71 | + } |
| 72 | + }, |
| 73 | + { |
| 74 | + name: 'name', |
| 75 | + label: 'Pattern Name', |
| 76 | + options: { |
| 77 | + filter: false, |
| 78 | + sort: true, |
| 79 | + searchable: true, |
| 80 | + customBodyRender: (value: string, tableMeta: MUIDataTableMeta) => { |
| 81 | + const designId = (tableMeta as TableMeta).tableData[tableMeta.rowIndex]?.id ?? ''; |
| 82 | + const designName = (tableMeta as TableMeta).tableData[tableMeta.rowIndex]?.name ?? ''; |
| 83 | + |
| 84 | + return <NameDiv onClick={() => handleShowDetails(designId, designName)}>{value}</NameDiv>; |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + name: 'first_name', |
| 90 | + label: 'Author', |
| 91 | + options: { |
| 92 | + filter: false, |
| 93 | + sort: true, |
| 94 | + searchable: true, |
| 95 | + customBodyRender: (_, tableMeta: MUIDataTableMeta) => { |
| 96 | + const firstName = getColumnValue(tableMeta as TableMeta, 'first_name'); |
| 97 | + const lastName = getColumnValue(tableMeta as TableMeta, 'last_name'); |
| 98 | + const avatar_url = getColumnValue(tableMeta as TableMeta, 'avatar_url'); |
| 99 | + const user_id = getColumnValue(tableMeta as TableMeta, 'user_id'); |
| 100 | + |
| 101 | + return ( |
| 102 | + <AuthorCell |
| 103 | + firstName={firstName} |
| 104 | + lastName={lastName} |
| 105 | + avatarUrl={avatar_url} |
| 106 | + userId={user_id} |
| 107 | + /> |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + }, |
| 112 | + { |
| 113 | + name: 'created_at', |
| 114 | + label: 'Created At', |
| 115 | + options: { |
| 116 | + filter: false, |
| 117 | + sort: true, |
| 118 | + searchable: true, |
| 119 | + setCellHeaderProps: () => { |
| 120 | + return { align: 'center' }; |
| 121 | + } |
| 122 | + } |
| 123 | + }, |
| 124 | + { |
| 125 | + name: 'updated_at', |
| 126 | + label: 'Updated At', |
| 127 | + options: { |
| 128 | + filter: false, |
| 129 | + sort: true, |
| 130 | + searchable: true, |
| 131 | + setCellHeaderProps: () => { |
| 132 | + return { align: 'center' }; |
| 133 | + } |
| 134 | + } |
| 135 | + }, |
| 136 | + { |
| 137 | + name: 'visibility', |
| 138 | + label: 'Visibility', |
| 139 | + options: { |
| 140 | + filter: false, |
| 141 | + sort: false, |
| 142 | + searchable: true |
| 143 | + } |
| 144 | + }, |
| 145 | + { |
| 146 | + name: 'user_id', |
| 147 | + label: 'User ID', |
| 148 | + options: { |
| 149 | + filter: false, |
| 150 | + sort: false, |
| 151 | + searchable: false |
| 152 | + } |
| 153 | + }, |
| 154 | + { |
| 155 | + name: 'actions', |
| 156 | + label: 'Actions', |
| 157 | + options: { |
| 158 | + filter: false, |
| 159 | + sort: false, |
| 160 | + searchable: false, |
| 161 | + setCellHeaderProps: () => ({ align: 'center' as const }), |
| 162 | + setCellProps: () => ({ align: 'center' as const }), |
| 163 | + customBodyRender: function CustomBody(_, tableMeta: MUIDataTableMeta) { |
| 164 | + const rowIndex = (tableMeta as TableMeta).rowIndex; |
| 165 | + const rowData = (tableMeta as TableMeta).tableData[rowIndex]; |
| 166 | + |
| 167 | + const actionsList = [ |
| 168 | + { |
| 169 | + title: 'Download', |
| 170 | + onClick: () => downloadYaml(rowData?.pattern_file, rowData?.name), |
| 171 | + disabled: !isDownloadAllowed, |
| 172 | + icon: <Download width={24} height={24} fill={CHARCOAL} /> |
| 173 | + }, |
| 174 | + { |
| 175 | + title: 'Copy Link', |
| 176 | + disabled: rowData.visibility === 'private' || !isCopyLinkAllowed, |
| 177 | + onClick: () => { |
| 178 | + handleCopyUrl(RESOURCE_TYPES.DESIGNS, rowData?.name, rowData?.id); |
| 179 | + }, |
| 180 | + icon: <ChainIcon width={'24'} height={'24'} fill={CHARCOAL} /> |
| 181 | + }, |
| 182 | + { |
| 183 | + title: 'Open in playground', |
| 184 | + onClick: () => { |
| 185 | + window.open( |
| 186 | + `https://playground.meshery.io/extension/meshmap?mode=${ |
| 187 | + PLAYGROUND_MODES.DESIGNER |
| 188 | + }&type=${RESOURCE_TYPES.DESIGNS}&id=${rowData?.id}&name=${slugify( |
| 189 | + rowData?.name |
| 190 | + )}`, |
| 191 | + '_blank' |
| 192 | + ); |
| 193 | + }, |
| 194 | + icon: <KanvasIcon width={24} height={24} primaryFill={CHARCOAL} /> |
| 195 | + }, |
| 196 | + { |
| 197 | + title: isFromWorkspaceTable ? 'Remove Design' : 'Delete', |
| 198 | + disabled: isFromWorkspaceTable ? !isRemoveAllowed : !isDeleteAllowed, |
| 199 | + onClick: () => handleDeleteModal(rowData)(), |
| 200 | + icon: <L5DeleteIcon /> |
| 201 | + } |
| 202 | + ]; |
| 203 | + |
| 204 | + const publishAction = { |
| 205 | + title: 'Publish', |
| 206 | + disabled: !isPublishAllowed, |
| 207 | + onClick: () => handlePublishModal(rowData), |
| 208 | + icon: <PublishIcon width={24} height={24} fill={CHARCOAL} /> |
| 209 | + }; |
| 210 | + |
| 211 | + const unpublishAction = { |
| 212 | + title: 'Unpublish', |
| 213 | + onClick: () => handleUnpublishModal(rowData)(), |
| 214 | + disabled: !isUnpublishAllowed, |
| 215 | + icon: <PublishIcon width={24} height={24} fill={CHARCOAL} /> |
| 216 | + }; |
| 217 | + |
| 218 | + const cloneAction = { |
| 219 | + title: 'Clone', |
| 220 | + onClick: () => handleClone(rowData?.name, rowData?.id), |
| 221 | + icon: <CopyIcon width={24} height={24} fill={CHARCOAL} /> |
| 222 | + }; |
| 223 | + |
| 224 | + if (rowData.visibility === 'published') { |
| 225 | + actionsList.splice(0, 0, cloneAction); |
| 226 | + actionsList.splice(2, 0, unpublishAction); |
| 227 | + } else { |
| 228 | + actionsList.splice(1, 0, publishAction); |
| 229 | + } |
| 230 | + |
| 231 | + return <DataTableEllipsisMenu actionsList={actionsList} />; |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + ]; |
| 236 | +}; |
0 commit comments