All files / modules/75-cf/pages/environments EnvironmentsPage.tsx

90.54% Statements 67/74
77.68% Branches 87/112
76.19% Functions 16/21
90.41% Lines 66/73

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              1x 1x   1x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     120x 1x     60x         1x 60x 60x     1x 60x 60x   68x       60x                                                           68x                                 1x 62x 62x 62x               1x       62x         4x               1x                                               1x 6x 6x 6x 6x 6x 6x 6x                         6x     6x             6x 6x 6x   6x 2x                   6x 1x 1x 1x 1x           6x 6x                                                       6x                                                                                           1x                                                   1x  
/*
 * Copyright 2022 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, { useMemo, useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import type { Column } from 'react-table'
import { get } from 'lodash-es'
import { Position } from '@blueprintjs/core'
import { Container, Layout, Pagination, Text, TableV2 } from '@wings-software/uicore'
import {
  EnvironmentResponseDTO,
  GetEnvironmentListForProjectQueryParams,
  useDeleteEnvironmentV2,
  useGetEnvironmentListForProject
} from 'services/cd-ng'
import { useToaster } from '@common/exports'
import { IdentifierText } from '@cf/components/IdentifierText/IdentifierText'
import { CF_DEFAULT_PAGE_SIZE } from '@cf/utils/CFUtils'
import { EnvironmentType } from '@common/constants/EnvironmentType'
import { useConfirmAction } from '@common/hooks/useConfirmAction'
import { useEnvStrings } from '@cf/hooks/environment'
import ListingPageTemplate from '@cf/components/ListingPageTemplate/ListingPageTemplate'
import EnvironmentDialog from '@cf/components/CreateEnvironmentDialog/EnvironmentDialog'
import routes from '@common/RouteDefinitions'
import { NoEnvironment } from '@cf/components/NoEnvironment/NoEnvironment'
import { withTableData } from '@cf/utils/table-utils'
import RbacOptionsMenuButton from '@rbac/components/RbacOptionsMenuButton/RbacOptionsMenuButton'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import css from './EnvironmentsPage.module.scss'
 
type EnvData = { environment: EnvironmentResponseDTO }
const withEnvironment = withTableData<EnvironmentResponseDTO, EnvData>(({ row }) => ({ environment: row.original }))
const withActions = withTableData<
  EnvironmentResponseDTO,
  EnvData & { actions: { [P in 'onEdit' | 'onDelete']?: (id: string) => void } }
>(({ row, column }) => ({
  environment: row.original,
  actions: (column as any).actions as { [P in 'onEdit' | 'onDelete']?: (id: string) => void }
}))
 
export const TypeCell = withEnvironment(({ environment }) => {
  const { getString } = useEnvStrings()
  return <Text>{getString(environment.type === EnvironmentType.PRODUCTION ? 'production' : 'nonProduction')}</Text>
})
 
export const NameCell = withEnvironment(({ environment }) => {
  const { getString } = useEnvStrings()
  const tags = Object.entries(environment.tags ?? {}).reduce(
    (acc: Array<{ name: string; value: string }>, [key, value]: [string, string]) => {
      return [...acc, { name: key, value: value }]
    },
    [] as Array<{ name: string; value: string }>
  )
  return (
    <Layout.Horizontal
      flex={{ distribution: 'space-between', align: 'center-center' }}
      padding={{ left: 'small', right: 'small' }}
      style={{ maxWidth: 'calc(100% - var(--spacing-medium))' }}
    >
      <Layout.Vertical spacing="xsmall" style={{ maxWidth: 'calc(100% - 100px)' }}>
        <Text style={{ color: '#22222A', fontWeight: 500 }}>{environment.name}</Text>
 
        {environment.description && (
          <Container>
            <Text style={{ color: '#6B6D85', fontSize: '12px', lineHeight: '18px' }}>{environment.description}</Text>
          </Container>
        )}
 
        <Container padding={{ top: 'xsmall' }} margin={{ bottom: 'xsmall' }}>
          <IdentifierText identifier={environment.identifier} inline style={{ padding: 'var(--spacing-xsmall)' }} />
        </Container>
      </Layout.Vertical>
      <Layout.Horizontal>
        <Text
          width={100}
          flex
          icon="main-tags"
          style={{ justifyContent: 'center' }}
          tooltip={
            tags.length ? (
              <>
                <Text>{getString('tagsLabel').toUpperCase()}</Text>
                {tags.map((elem, i) => (
                  <Text key={`${elem.value}-${i}`}>{elem.name}</Text>
                ))}
              </>
            ) : undefined
          }
          tooltipProps={{
            portalClassName: css.tagsPopover,
            position: Position.RIGHT
          }}
        >
          {tags.length}
        </Text>
      </Layout.Horizontal>
    </Layout.Horizontal>
  )
})
 
export const ModifiedByCell = withActions(({ environment, actions }) => {
  const { getString } = useEnvStrings()
  const identifier = environment.identifier as string
  const deleteEnvironment = useConfirmAction({
    title: getString('cf.environments.delete.title'),
    message: (
      <span
        dangerouslySetInnerHTML={{ __html: getString('cf.environments.delete.message', { name: environment.name }) }}
      />
    ),
    action: () => {
      actions.onDelete?.(identifier)
    }
  })
 
  return (
    <Layout.Horizontal style={{ alignItems: 'center', justifyContent: 'flex-end' }}>
      {/* TODO: Add user info when BE is ready */}
      <Container
        onClick={(e: React.MouseEvent) => {
          e.stopPropagation()
        }}
      >
        <RbacOptionsMenuButton
          items={[
            {
              icon: 'edit',
              text: getString('edit'),
              onClick: () => actions.onEdit?.(identifier),
              permission: {
                resource: { resourceType: ResourceType.ENVIRONMENT },
                permission: PermissionIdentifier.EDIT_ENVIRONMENT
              }
            },
            {
              icon: 'trash',
              text: getString('delete'),
              onClick: deleteEnvironment,
              permission: {
                resource: { resourceType: ResourceType.ENVIRONMENT },
                permission: PermissionIdentifier.DELETE_ENVIRONMENT
              }
            }
          ]}
        />
      </Container>
    </Layout.Horizontal>
  )
})
 
type CustomColumn<T extends Record<string, any>> = Column<T>
 
const EnvironmentsPage: React.FC = () => {
  const { getString } = useEnvStrings()
  const { showError, showSuccess } = useToaster()
  const history = useHistory()
  const [page, setPage] = useState(0)
  const { accountId: accountIdentifier, projectIdentifier, orgIdentifier } = useParams<Record<string, string>>()
  const queryParams = useMemo<GetEnvironmentListForProjectQueryParams>(() => {
    return {
      accountId: accountIdentifier,
      orgIdentifier,
      projectIdentifier,
      size: CF_DEFAULT_PAGE_SIZE,
      page
    }
  }, [accountIdentifier, orgIdentifier, projectIdentifier, page])
  const {
    data: envData,
    loading,
    error,
    refetch
  } = useGetEnvironmentListForProject({
    queryParams
  })
  const { mutate: deleteEnvironment } = useDeleteEnvironmentV2({
    queryParams: {
      accountIdentifier,
      projectIdentifier,
      orgIdentifier
    }
  })
  const environments = envData?.data?.content
  const hasEnvs = Boolean(!loading && envData?.data?.content?.length)
  const emptyEnvs = Boolean(!loading && envData?.data?.content?.length === 0)
 
  const handleEdit = (id: string) => {
    history.push(
      routes.toCFEnvironmentDetails({
        environmentIdentifier: id,
        projectIdentifier,
        orgIdentifier,
        accountId: accountIdentifier
      })
    )
  }
 
  const handleDeleteEnv = async (id: string) => {
    try {
      await deleteEnvironment(id, { headers: { 'content-type': 'application/json' } })
      showSuccess(`Successfully deleted environment ${id}`)
      refetch()
    } catch (e) {
      showError(get(e, 'data.message', e?.message), 0, 'cf.delete.env.error')
    }
  }
 
  const columns: CustomColumn<EnvironmentResponseDTO>[] = useMemo(
    () => [
      {
        Header: getString('environment').toUpperCase(),
        id: 'name',
        width: '75%',
        accessor: 'name',
        Cell: NameCell
      },
      {
        Header: getString('typeLabel').toUpperCase(),
        id: 'type',
        accessor: 'type',
        width: '15%',
        Cell: TypeCell
      },
      {
        id: 'modifiedBy',
        width: '10%',
        Cell: ModifiedByCell,
        actions: {
          onEdit: handleEdit,
          onDelete: handleDeleteEnv
        }
      }
    ],
    [getString, handleDeleteEnv]
  )
 
  return (
    <ListingPageTemplate
      title={getString('environments')}
      titleTooltipId="ff_env_heading"
      toolbar={
        hasEnvs && (
          <Layout.Horizontal>
            <EnvironmentDialog
              disabled={loading}
              onCreate={response => {
                setTimeout(() => {
                  history.push(
                    routes.toCFEnvironmentDetails({
                      environmentIdentifier: response?.data?.identifier as string,
                      projectIdentifier,
                      orgIdentifier,
                      accountId: accountIdentifier
                    })
                  )
                }, 1000)
              }}
            />
          </Layout.Horizontal>
        )
      }
      pagination={
        <Pagination
          itemCount={envData?.data?.totalItems || 0}
          pageSize={envData?.data?.pageSize || 0}
          pageCount={envData?.data?.totalPages || 0}
          pageIndex={page}
          gotoPage={index => {
            setPage(index)
            refetch({ queryParams: { ...queryParams, page: index } })
          }}
        />
      }
      error={error}
      retryOnError={refetch}
      loading={loading}
    >
      {hasEnvs && (
        <Container padding={{ top: 'medium', right: 'xlarge', left: 'xlarge' }}>
          <TableV2<EnvironmentResponseDTO>
            columns={columns}
            data={(environments as EnvironmentResponseDTO[]) || []}
            onRowClick={({ identifier }) => handleEdit(identifier as string)}
          />
        </Container>
      )}
      {emptyEnvs && (
        <Container flex={{ align: 'center-center' }} height="100%">
          <NoEnvironment
            onCreated={response =>
              setTimeout(() => {
                history.push(
                  routes.toCFEnvironmentDetails({
                    environmentIdentifier: response?.data?.identifier as string,
                    projectIdentifier,
                    orgIdentifier,
                    accountId: accountIdentifier
                  })
                )
              }, 1000)
            }
          />
        </Container>
      )}
    </ListingPageTemplate>
  )
}
 
export default EnvironmentsPage