All files / modules/70-pipeline/components/Notifications NotificationTable.tsx

85.71% Statements 66/77
53.95% Branches 41/76
81.48% Functions 22/27
85.33% Lines 64/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325              25x 25x                         25x   25x 25x 25x 25x   25x   25x 25x 25x 25x 25x                                                     25x 40x   30x                           10x       25x 30x 30x                               25x 30x 30x               25x 40x 30x 30x 30x     40x                                             25x 30x 30x                 25x 36x 36x 36x   36x 1x 1x 1x     36x 1x 1x 1x   36x         4x                 2x 2x                                                         2x   9x 9x   9x               9x 9x         21x                     21x                 21x                 21x               21x                         9x   9x                 1x         17x                                                     25x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useState, useMemo } from 'react'
import {
  Text,
  Layout,
  Button,
  Popover,
  Switch,
  Container,
  Icon,
  Select,
  MultiSelectOption,
  ButtonVariation,
  TableV2
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { CellProps, Renderer, Column } from 'react-table'
import { Classes, Menu, PopoverInteractionKind, Position, Tag } from '@blueprintjs/core'
import { startCase } from 'lodash-es'
import produce from 'immer'
import { useStrings } from 'framework/strings'
import type { NotificationRules, PipelineEvent } from 'services/pipeline-ng'
import { getIconByNotificationMethod } from '@notifications/Utils/Utils'
import type { NotificationType } from '@notifications/interfaces/Notifications'
import { useNotificationModal } from './useNotificationModal'
import { PipelineEventType } from './Steps/PipelineEvents'
import { Actions } from './NotificationUtils'
import { getAllNotificationTypeSelectOption, NotificationTypeSelectOptions } from './NotificationTypeOptions'
import css from './NotificationTable.module.scss'
 
export interface NotificationRulesItem {
  index: number
  notificationRules: NotificationRules
}
 
export interface NotificationTableProps {
  data: NotificationRulesItem[]
  onUpdate?: (data?: NotificationRulesItem, action?: Actions, closeModal?: () => void) => void
  onFilterType: (type: string | undefined) => void
  filterType: string | undefined
  gotoPage: (index: number) => void
  totalPages: number
  totalItems: number
  pageItemCount?: number
  pageSize: number
  pageIndex: number
  stagesOptions?: MultiSelectOption[]
  getExistingNotificationNames?: (skipIndex?: number) => string[]
  isReadonly?: boolean
}
 
type CustomColumn<T extends Record<string, any>> = Column<T> & {
  onUpdate?: (data: NotificationRulesItem) => void
}
 
export const getPipelineEventColor = (option?: Required<PipelineEvent>['type']): Color => {
  switch (option) {
    case PipelineEventType.ALL_EVENTS:
      return Color.BLUE_500
    case PipelineEventType.PipelineSuccess:
      return Color.BLUE_300
    case PipelineEventType.StageSuccess:
      return Color.BLUE_300
    case PipelineEventType.StageFailed:
      return Color.BLUE_300
    case PipelineEventType.StageStart:
      return Color.BLUE_300
    case PipelineEventType.PipelineEnd:
      return Color.BLUE_300
    case PipelineEventType.PipelineStart:
      return Color.BLUE_300
  }
  return Color.GREY_200
}
 
// eslint-disable-next-line react/function-component-definition
const RenderColumnEnabled: Renderer<CellProps<NotificationRulesItem>> = ({ row, column }) => {
  const data = row.original
  return (
    <Switch
      checked={data.notificationRules.enabled}
      onChange={e => {
        ;(column as any).onUpdate?.(
          produce(data, draft => {
            draft.notificationRules.enabled = e.currentTarget.checked
          }),
          Actions.Update
        )
      }}
      disabled={(column as any).disabled}
    />
  )
}
// eslint-disable-next-line react/function-component-definition
const RenderColumnName: Renderer<CellProps<NotificationRulesItem>> = ({ row }) => {
  const data = row.original
  return (
    <Text color={Color.BLACK} lineClamp={1}>
      {data.notificationRules.name}
    </Text>
  )
}
 
// eslint-disable-next-line react/function-component-definition
const RenderColumnEvents: Renderer<CellProps<NotificationRulesItem>> = ({ row }) => {
  const data = row.original.notificationRules.pipelineEvents?.map(event => event.type)
  const baseData = data?.slice(0, 3)
  const popoverData = data?.slice(3, data.length)
  return (
    <Layout.Horizontal spacing="xsmall">
      {baseData?.map(event => (
        <Tag round={true} key={event} style={{ backgroundColor: getPipelineEventColor(event) }}>
          {startCase(event)}
        </Tag>
      ))}
      {popoverData?.length ? (
        <Popover interactionKind={PopoverInteractionKind.HOVER}>
          <Layout.Horizontal flex={{ align: 'center-center' }} spacing="xsmall">
            <Icon name="main-tags" size={15} />
          </Layout.Horizontal>
          <Layout.Vertical padding="small" spacing="small">
            {popoverData?.map(event => (
              <Tag round={true} key={event} style={{ backgroundColor: getPipelineEventColor(event) }}>
                {startCase(event)}
              </Tag>
            ))}
          </Layout.Vertical>
        </Popover>
      ) : null}
    </Layout.Horizontal>
  )
}
 
// eslint-disable-next-line react/function-component-definition
const RenderColumnMethod: Renderer<CellProps<NotificationRulesItem>> = ({ row }) => {
  const data = row.original.notificationRules.notificationMethod?.type
  return (
    <Layout.Horizontal spacing="small">
      <Icon name={getIconByNotificationMethod(data as NotificationType)} />
      <Text>{data}</Text>
    </Layout.Horizontal>
  )
}
 
// eslint-disable-next-line react/function-component-definition
const RenderColumnMenu: Renderer<CellProps<NotificationRulesItem>> = ({ row, column }) => {
  const [menuOpen, setMenuOpen] = useState(false)
  const { getString } = useStrings()
  const data = row.original
 
  const handleEdit = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    ;(column as any).openNotificationModal?.(data.notificationRules, data.index)
  }
 
  const handleDelete = (event: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    event.stopPropagation()
    setMenuOpen?.(false)
    ;(column as any).onUpdate?.(row.original, Actions.Delete)
  }
  return (
    <Layout.Horizontal>
      <Popover
        isOpen={menuOpen}
        onInteraction={nextOpenState => {
          setMenuOpen(nextOpenState)
        }}
        className={Classes.DARK}
        position={Position.BOTTOM_RIGHT}
      >
        <Button
          minimal
          icon="Options"
          onClick={e => {
            e.stopPropagation()
            setMenuOpen(true)
          }}
        />
        <Menu>
          <Menu.Item icon="edit" text={getString('edit')} onClick={handleEdit} disabled={(column as any).disabled} />
          <Menu.Item
            icon="trash"
            text={getString('delete')}
            onClick={handleDelete}
            disabled={(column as any).disabled}
          />
        </Menu>
      </Popover>
    </Layout.Horizontal>
  )
}
 
function NotificationTable(props: NotificationTableProps): React.ReactElement {
  const {
    data,
    onUpdate,
    gotoPage,
    filterType,
    onFilterType,
    totalPages,
    totalItems,
    pageSize,
    pageIndex,
    stagesOptions = [],
    getExistingNotificationNames = (_skipIndex?: number) => [],
    isReadonly = false
  } = props
  const { getString } = useStrings()
 
  const { openNotificationModal, closeNotificationModal } = useNotificationModal({
    onCreateOrUpdate: (_data?: NotificationRules, _index?: number, _action?: Actions) => {
      onUpdate?.({ notificationRules: _data!, index: _index! }, _action, closeNotificationModal)
    },
    stagesOptions,
    getExistingNotificationNames
  })
 
  const columns: CustomColumn<NotificationRulesItem>[] = useMemo(
    () => [
      {
        Header: getString('enabledLabel').toUpperCase(),
        id: 'enabled',
        className: css.notificationTableHeader,
        accessor: row => row.notificationRules.enabled,
        onUpdate: onUpdate,
        width: '15%',
        Cell: RenderColumnEnabled,
        disableSortBy: true,
        disabled: isReadonly
      },
      {
        Header: getString('notifications.nameOftheRule').toUpperCase(),
        id: 'name',
        className: css.notificationTableHeader,
        accessor: row => row.notificationRules.name,
        width: '20%',
        Cell: RenderColumnName,
        disableSortBy: true
      },
      {
        Header: getString('notifications.pipelineEvents').toUpperCase(),
        id: 'events',
        className: css.notificationTableHeader,
        accessor: row => row.notificationRules.pipelineEvents,
        width: '35%',
        Cell: RenderColumnEvents,
        disableSortBy: true
      },
      {
        Header: getString('notifications.notificationMethod').toUpperCase(),
        id: 'methods',
        className: css.notificationTableHeader,
        accessor: row => row.notificationRules.notificationMethod?.type,
        width: '28%',
        Cell: RenderColumnMethod,
        disableSortBy: true
      },
      {
        Header: '',
        id: 'menu',
        accessor: row => row.notificationRules.notificationMethod?.spec,
        className: css.notificationTableHeader,
        width: '2%',
        Cell: RenderColumnMenu,
        onUpdate: onUpdate,
        openNotificationModal: openNotificationModal,
        disableSortBy: true,
        disabled: isReadonly
      }
    ],
    [onUpdate, openNotificationModal, data]
  )
 
  const filterOptions = [getAllNotificationTypeSelectOption(getString), ...NotificationTypeSelectOptions]
 
  return (
    <>
      <Container>
        <Layout.Horizontal flex className={css.headerActions}>
          <Button
            variation={ButtonVariation.PRIMARY}
            text={getString('notifications.name')}
            icon="plus"
            id="newNotificationBtn"
            onClick={() => openNotificationModal()}
            disabled={isReadonly}
          />
 
          <Select
            value={filterOptions.find(item => item.value === filterType)}
            items={filterOptions}
            onChange={value => {
              onFilterType?.(value.value as string)
            }}
            className={css.filterDropdown}
          />
        </Layout.Horizontal>
      </Container>
      <Container padding={{ bottom: 'huge' }} className={css.content}>
        <TableV2<NotificationRulesItem>
          columns={columns}
          data={data}
          className={css.notificationTable}
          pagination={{
            itemCount: totalItems,
            pageSize: pageSize,
            pageCount: totalPages,
            pageIndex: pageIndex,
            gotoPage: gotoPage
          }}
        />
      </Container>
    </>
  )
}
 
export default NotificationTable