All files / modules/45-projects-orgs/components/ProjectSelector ProjectSelector.tsx

87.5% Statements 35/40
80.77% Branches 63/78
55.56% Functions 5/9
87.18% Lines 34/39

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              55x 55x 55x 55x 55x                     55x 55x 55x 55x 55x   55x 55x 55x 55x             55x 8x 8x 8x 8x 8x 8x   8x                   8x                                                                                                                                       7x             1x                                           55x 8x 8x 8x 8x   8x     7x 6x       8x                                                                                        
/*
 * 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, { useEffect, useState } from 'react'
import { Position, PopoverInteractionKind, Classes } from '@blueprintjs/core'
import { useParams, useHistory } from 'react-router-dom'
import cx from 'classnames'
import {
  Text,
  Layout,
  Container,
  Popover,
  Pagination,
  Button,
  ButtonVariation,
  ExpandingSearchInput,
  NoDataCard
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import routes from '@common/RouteDefinitions'
import { Project, useGetProjectAggregateDTOList } from 'services/cd-ng'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useStrings } from 'framework/strings'
import type { AccountPathProps, ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import ProjectCard from '@projects-orgs/components/ProjectCard/ProjectCard'
import { PageSpinner } from '@common/components'
import pointerImage from './pointer.svg'
import css from './ProjectSelector.module.scss'
 
export interface ProjectSelectorProps {
  onSelect: (project: Project) => void
  moduleFilter?: Required<Project>['modules'][0]
}
 
const ProjectSelect: React.FC<ProjectSelectorProps> = ({ onSelect }) => {
  const { accountId } = useParams<AccountPathProps>()
  const history = useHistory()
  const { selectedProject } = useAppStore()
  const [page, setPage] = useState(0)
  const [searchTerm, setSearchTerm] = useState<string>()
  const { getString } = useStrings()
 
  const { data, loading } = useGetProjectAggregateDTOList({
    queryParams: {
      accountIdentifier: accountId,
      searchTerm,
      pageIndex: page,
      pageSize: 50
    },
    debounce: 300
  })
 
  return (
    <Popover
      interactionKind={PopoverInteractionKind.CLICK}
      position={Position.RIGHT}
      modifiers={{ offset: { offset: -50 } }}
      minimal
      fill={true}
      popoverClassName={css.popover}
    >
      {selectedProject ? (
        <Button
          minimal
          withoutBoxShadow={true}
          icon="double-chevron-right"
          tooltipProps={{
            isDark: true,
            usePortal: true,
            fill: true
          }}
          tooltip={
            <Text padding="small" color={Color.WHITE}>
              {getString('selectProject')}
            </Text>
          }
          data-testid="project-select-dropdown"
          className={css.popoverTarget}
        />
      ) : (
        <Button
          minimal
          text={
            <Text color={Color.GREY_400} font={{ size: 'normal' }} padding="xsmall">
              {getString('selectProject')}
            </Text>
          }
          rightIcon="chevron-right"
          data-testid="project-select-button"
          className={cx(css.popoverTarget, css.selectButton)}
        />
      )}
 
      <Container width={600} padding="xlarge" className={css.selectContainer}>
        <Layout.Horizontal flex padding={{ bottom: 'large' }}>
          <Text font={{ size: 'medium', weight: 'bold' }} color={Color.BLACK}>
            {getString('selectProject')}
          </Text>
          <Button
            variation={ButtonVariation.LINK}
            text={getString('projectsOrgs.viewAllProjects')}
            onClick={() => history.push(routes.toProjects({ accountId }))}
          />
        </Layout.Horizontal>
        <Layout.Vertical>
          <ExpandingSearchInput
            defaultValue={searchTerm}
            placeholder={getString('projectsOrgs.searchProjectPlaceHolder')}
            alwaysExpanded
            onChange={text => {
              setSearchTerm(text.trim())
              setPage(0)
            }}
          />
        </Layout.Vertical>
        {loading && <PageSpinner />}
        {data?.data?.content?.length ? (
          <Layout.Vertical className={css.projectContainerWrapper}>
            <div className={css.projectContainer}>
              {data.data.content.map(projectAggregate => (
                <ProjectCard
                  key={projectAggregate.projectResponse.project.identifier}
                  data={projectAggregate}
                  minimal={true}
                  selected={projectAggregate.projectResponse.project.identifier === selectedProject?.identifier}
                  className={cx(css.projectCard, Classes.POPOVER_DISMISS)}
                  onClick={() => {
                    onSelect(projectAggregate.projectResponse.project)
                  }}
                />
              ))}
            </div>
            <Pagination
              className={css.pagination}
              itemCount={data?.data?.totalItems || 0}
              pageSize={data?.data?.pageSize || 10}
              pageCount={data?.data?.totalPages || 0}
              pageIndex={data?.data?.pageIndex || 0}
              gotoPage={pageNumber => setPage(pageNumber)}
              hidePageNumbers
            />
          </Layout.Vertical>
        ) : (
          <NoDataCard icon="nav-project" message={getString('noProjects')} />
        )}
      </Container>
    </Popover>
  )
}
export const ProjectSelector: React.FC<ProjectSelectorProps> = ({ onSelect, moduleFilter }) => {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { selectedProject, updateAppStore } = useAppStore()
  const { getString } = useStrings()
  const history = useHistory()
 
  useEffect(() => {
    // deselect current project if user switches module
    // and the new module isn't added on selected project
    if (moduleFilter && !selectedProject?.modules?.includes(moduleFilter)) {
      updateAppStore({ selectedProject: undefined })
    }
  }, [moduleFilter])
 
  return (
    <>
      <Layout.Vertical padding={{ left: 'large', right: 'large', top: 'large', bottom: 'small' }}>
        <Text margin={{ bottom: 'xsmall' }} font={{ size: 'small' }} color={Color.GREY_500}>
          {getString('projectLabel')}
        </Text>
        <div className={cx(css.projectSelector, { [css.selectProjectDisplay]: !selectedProject })}>
          {selectedProject && (
            <Button
              minimal
              tooltipProps={{
                isDark: true,
                fill: true
              }}
              tooltip={
                <Layout.Vertical padding="medium" spacing="small">
                  <Text color={Color.GREY_300}>{getString('projectsOrgs.manageAProject')}</Text>
                  <Text color={Color.WHITE}>{selectedProject.name}</Text>
                </Layout.Vertical>
              }
              onClick={() => {
                history.push(routes.toProjectDetails({ accountId, orgIdentifier, projectIdentifier }))
              }}
              className={cx(css.popoverTarget, css.selectedProject)}
            >
              <Text color={Color.WHITE} padding="xsmall" className={css.projectText}>
                {selectedProject.name}
              </Text>
            </Button>
          )}
          <ProjectSelect onSelect={onSelect} />
        </div>
      </Layout.Vertical>
 
      {selectedProject ? null : (
        <div style={{ backgroundImage: `url(${pointerImage})` }} className={css.pickProjectHelp}>
          <Text color={Color.GREY_200} padding="small">
            {getString('pickProject')}
          </Text>
        </div>
      )}
    </>
  )
}