All files / modules/75-ce/components/PerspectiveTimeRangePicker PerspectiveTimeRangePicker.tsx

81.58% Statements 62/76
60% Branches 18/30
53.33% Functions 8/15
81.58% Lines 62/76

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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449              7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x             7x 7x 7x 7x   7x   7x 50x                             7x                         7x                         7x           7x           7x         7x         7x         7x         7x         7x           7x           7x                       7x                                   7x 39x   39x   39x                                                       7x 11x   11x 11x   11x   11x   11x   11x   11x 11x 121x 121x       9x     11x 9x         2x           11x 11x   11x   11x                                                                               22x                                                                                                                                                               22x                                                             99x                                                                       7x                                       7x                       7x  
/*
 * 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 { Link, useParams } from 'react-router-dom'
import { noop } from 'lodash-es'
import { Layout, Text, Button, Container, Icon } from '@wings-software/uicore'
import { DateRangePicker } from '@blueprintjs/datetime'
import moment from 'moment'
import { Color } from '@harness/design-system'
import cx from 'classnames'
import { Popover, Position, Classes } from '@blueprintjs/core'
import { useStrings, UseStringsReturn } from 'framework/strings'
import routes from '@common/RouteDefinitions'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import {
  DATE_RANGE_SHORTCUTS,
  DATE_RANGE_SHORTCUTS_NAME,
  CE_DATE_FORMAT_INTERNAL,
  getStartDateTime,
  getEndDateTime
} from '@ce/utils/momentUtils'
import { useLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import { ModuleLicenseType } from '@common/constants/SubscriptionTypes'
import { useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import type { setTimeRangeFn } from '@ce/types'
import css from './PerspectiveTimeRangePicker.module.scss'
 
const getDateLabelToDisplayText: (getString: UseStringsReturn['getString']) => Record<string, string> = getString => {
  return {
    [DATE_RANGE_SHORTCUTS_NAME.LAST_7_DAYS]: getString('ce.perspectives.timeRangeConstants.last7Days'),
    [DATE_RANGE_SHORTCUTS_NAME.CURRENT_MONTH]: getString('ce.perspectives.timeRangeConstants.thisMonth'),
    [DATE_RANGE_SHORTCUTS_NAME.LAST_30_DAYS]: getString('projectsOrgs.landingDashboard.last30Days'),
    [DATE_RANGE_SHORTCUTS_NAME.THIS_QUARTER]: getString('ce.perspectives.timeRangeConstants.thisQuarter'),
    [DATE_RANGE_SHORTCUTS_NAME.THIS_YEAR]: getString('ce.perspectives.timeRangeConstants.thisYear'),
    [DATE_RANGE_SHORTCUTS_NAME.LAST_MONTH]: getString('ce.perspectives.timeRangeConstants.lastMonth'),
    [DATE_RANGE_SHORTCUTS_NAME.LAST_QUARTER]: getString('ce.perspectives.timeRangeConstants.lastQuarter'),
    [DATE_RANGE_SHORTCUTS_NAME.LAST_YEAR]: getString('ce.perspectives.timeRangeConstants.lastYear'),
    [DATE_RANGE_SHORTCUTS_NAME.LAST_3_MONTHS]: getString('ce.perspectives.timeRangeConstants.last3Months'),
    [DATE_RANGE_SHORTCUTS_NAME.LAST_6_MONTHS]: getString('ce.perspectives.timeRangeConstants.last6Months'),
    [DATE_RANGE_SHORTCUTS_NAME.LAST_12_MONTHS]: getString('ce.perspectives.timeRangeConstants.last12Months')
  }
}
 
const RECOMMENDED_DATES = [
  {
    label: DATE_RANGE_SHORTCUTS_NAME.LAST_7_DAYS,
    dateRange: DATE_RANGE_SHORTCUTS.LAST_7_DAYS,
    dateFormat: ['MMM D', 'MMM D']
  },
  {
    label: DATE_RANGE_SHORTCUTS_NAME.CURRENT_MONTH,
    dateRange: DATE_RANGE_SHORTCUTS.CURRENT_MONTH,
    dateFormat: ['MMM YYYY']
  }
]
 
const RELATIVE_DATES = [
  {
    label: DATE_RANGE_SHORTCUTS_NAME.LAST_7_DAYS,
    dateRange: DATE_RANGE_SHORTCUTS.LAST_7_DAYS,
    dateFormat: ['MMM D', 'MMM D']
  },
  {
    label: DATE_RANGE_SHORTCUTS_NAME.LAST_30_DAYS,
    dateRange: DATE_RANGE_SHORTCUTS.LAST_30_DAYS,
    dateFormat: ['MMM D', 'MMM D']
  }
]
 
const CURRENT_MONTH = {
  label: DATE_RANGE_SHORTCUTS_NAME.CURRENT_MONTH,
  dateRange: DATE_RANGE_SHORTCUTS.CURRENT_MONTH,
  dateFormat: ['MMM YYYY']
}
 
const THIS_QUARTER = {
  label: DATE_RANGE_SHORTCUTS_NAME.THIS_QUARTER,
  dateRange: DATE_RANGE_SHORTCUTS.THIS_QUARTER,
  dateFormat: ['MMM', 'MMM YYYY']
}
 
const THIS_YEAR = {
  label: DATE_RANGE_SHORTCUTS_NAME.THIS_YEAR,
  dateRange: DATE_RANGE_SHORTCUTS.THIS_YEAR,
  dateFormat: ['YYYY']
}
const LAST_MONTH = {
  label: DATE_RANGE_SHORTCUTS_NAME.LAST_MONTH,
  dateRange: DATE_RANGE_SHORTCUTS.LAST_MONTH,
  dateFormat: ['MMM YYYY']
}
const LAST_YEAR = {
  label: DATE_RANGE_SHORTCUTS_NAME.LAST_YEAR,
  dateRange: DATE_RANGE_SHORTCUTS.LAST_YEAR,
  dateFormat: ['YYYY']
}
const LAST_QUARTER = {
  label: DATE_RANGE_SHORTCUTS_NAME.LAST_QUARTER,
  dateRange: DATE_RANGE_SHORTCUTS.LAST_QUARTER,
  dateFormat: ['MMM', 'MMM YYYY']
}
const LAST_3_MONTHS = {
  label: DATE_RANGE_SHORTCUTS_NAME.LAST_3_MONTHS,
  dateRange: DATE_RANGE_SHORTCUTS.LAST_3_MONTHS,
  dateFormat: ['MMM YYYY', 'MMM YYYY']
}
const LAST_6_MONTHS = {
  label: DATE_RANGE_SHORTCUTS_NAME.LAST_6_MONTHS,
  dateRange: DATE_RANGE_SHORTCUTS.LAST_6_MONTHS,
  dateFormat: ['MMM YYYY', 'MMM YYYY']
}
 
const LAST_12_MONTHS = {
  label: DATE_RANGE_SHORTCUTS_NAME.LAST_12_MONTHS,
  dateRange: DATE_RANGE_SHORTCUTS.LAST_12_MONTHS,
  dateFormat: ['MMM YYYY', 'MMM YYYY']
}
 
const CALENDAR_MONTH_DATES = [
  CURRENT_MONTH,
  THIS_QUARTER,
  THIS_YEAR,
  LAST_MONTH,
  LAST_QUARTER,
  LAST_YEAR,
  LAST_3_MONTHS,
  LAST_6_MONTHS,
  LAST_12_MONTHS
]
 
const RESTRICTED_CALENDAR_MONTH_DATES = [
  THIS_QUARTER,
  THIS_YEAR,
  LAST_QUARTER,
  LAST_YEAR,
  LAST_3_MONTHS,
  LAST_6_MONTHS,
  LAST_12_MONTHS
]
 
interface DateLabelRendererProps {
  text: DATE_RANGE_SHORTCUTS_NAME
  dateRange: moment.Moment[]
  dateFormat: string[]
  onClick: () => void
  disable?: boolean
}
 
const DateLabelRenderer: React.FC<DateLabelRendererProps> = ({ text, dateRange, dateFormat, onClick, disable }) => {
  const { getString } = useStrings()
 
  const labelToTextMapping = getDateLabelToDisplayText(getString)
 
  return (
    <Container onClick={disable ? noop : onClick} className={cx(css.dateLabelContainer, Classes.POPOVER_DISMISS)}>
      <Layout.Horizontal
        style={{
          justifyContent: 'space-between'
        }}
        spacing="large"
      >
        <Text className={css.pointerText} color={disable ? Color.GREY_200 : Color.GREY_600}>
          {labelToTextMapping[text]}
        </Text>
        <Text className={css.pointerText} color={disable ? Color.GREY_200 : Color.GREY_300}>{`${dateRange[0].format(
          dateFormat[0]
        )} ${dateFormat[1] ? '- ' + dateRange[1].format(dateFormat[1]) : ''}`}</Text>
      </Layout.Horizontal>
    </Container>
  )
}
 
interface PerspectiveTimeRangePickerProps {
  setTimeRange: setTimeRangeFn
  timeRange: {
    to: string
    from: string
  }
  featureEnabled?: boolean
}
 
const PerspectiveTimeRangePicker: React.FC<PerspectiveTimeRangePickerProps> = ({ timeRange, setTimeRange }) => {
  const { getString } = useStrings()
 
  const { licenseInformation } = useLicenseStore()
  const isFreeEdition = licenseInformation['CE']?.edition === ModuleLicenseType.FREE
 
  const isFeatureEnforcementEnabled = useFeatureFlag(FeatureFlag.FEATURE_ENFORCEMENT_ENABLED)
 
  const featureEnforced = isFreeEdition && isFeatureEnforcementEnabled
 
  const [isPopoverOpen, setIsPopoverOpen] = useState<boolean | undefined>()
 
  const labelToTextMapping = getDateLabelToDisplayText(getString)
 
  const { dateLabelText } = useMemo(() => {
    const filteredArray = Object.keys(DATE_RANGE_SHORTCUTS).filter(short => {
      const date = DATE_RANGE_SHORTCUTS[short]
      if (
        timeRange.from === date[0].format(CE_DATE_FORMAT_INTERNAL) &&
        timeRange.to === date[1].format(CE_DATE_FORMAT_INTERNAL)
      ) {
        return true
      }
    })
    if (filteredArray.length) {
      return {
        dateLabelKey: filteredArray[0],
        dateLabelText: labelToTextMapping[filteredArray[0]]
      }
    }
    return {
      dateLabelKey: DATE_RANGE_SHORTCUTS_NAME.LAST_7_DAYS,
      dateLabelText: `${timeRange.from} - ${timeRange.to}`
    }
  }, [timeRange])
 
  const fromDate: Date | undefined = new Date(getStartDateTime(timeRange.from))
  const toDate: Date | undefined = new Date(getEndDateTime(timeRange.to))
 
  const maxDate = new Date(moment().add(30, 'day').valueOf())
 
  return (
    <Popover
      position={Position.BOTTOM_RIGHT}
      modifiers={{
        arrow: { enabled: false },
        flip: { enabled: true },
        keepTogether: { enabled: true },
        preventOverflow: { enabled: true }
      }}
      onClose={() => {
        setIsPopoverOpen(undefined)
      }}
      isOpen={isPopoverOpen}
      content={
        <Layout.Vertical
          spacing="large"
          padding={{
            top: 'small',
            bottom: 'small'
          }}
        >
          {featureEnforced && <SubscriptionLimitWarning />}
          <Layout.Vertical
            margin={{
              bottom: 'medium'
            }}
          >
            <Text
              padding={{
                top: 'small',
                bottom: 'small',
                left: 'large',
                right: 'large'
              }}
              font={{ weight: 'semi-bold' }}
              color="grey800"
            >
              {getString('ce.perspectives.timeRange.recommended')}
            </Text>
            {RECOMMENDED_DATES.map(item => {
              return (
                <DateLabelRenderer
                  key={`recommended-${item.label}`}
                  dateFormat={item.dateFormat}
                  dateRange={item.dateRange}
                  text={item.label}
                  onClick={() => {
                    setTimeRange({
                      from: item.dateRange[0].format(CE_DATE_FORMAT_INTERNAL),
                      to: item.dateRange[1].format(CE_DATE_FORMAT_INTERNAL)
                    })
                  }}
                />
              )
            })}
 
            <Popover
              position={Position.LEFT_TOP}
              modifiers={{
                flip: { enabled: true },
                keepTogether: { enabled: true },
                preventOverflow: { enabled: true }
              }}
              isOpen={isPopoverOpen}
              disabled={featureEnforced}
              content={
                <DateRangePicker
                  defaultValue={[fromDate, toDate]}
                  allowSingleDayRange={true}
                  contiguousCalendarMonths={false}
                  shortcuts={false}
                  maxDate={maxDate}
                  onChange={val => {
                    if (val[0] && val[1]) {
                      const from = moment(val[0]).format(CE_DATE_FORMAT_INTERNAL)
                      const to = moment(val[1]).format(CE_DATE_FORMAT_INTERNAL)
 
                      setTimeRange({
                        from: from,
                        to: to
                      })
                      setIsPopoverOpen(false)
                    }
                  }}
                />
              }
            >
              <Text
                padding={{
                  top: 'small',
                  bottom: 'small',
                  left: 'large',
                  right: 'large'
                }}
                color={featureEnforced ? 'grey200' : 'primary7'}
                className={css.pointerText}
              >
                {getString('ce.perspectives.timeRange.selectCustomRange')}
              </Text>
            </Popover>
          </Layout.Vertical>
 
          <Layout.Vertical
            margin={{
              bottom: 'medium'
            }}
          >
            <Text
              padding={{
                top: 'small',
                bottom: 'small',
                left: 'large',
                right: 'large'
              }}
              font={{ weight: 'semi-bold' }}
              color="grey800"
            >
              {getString('ce.perspectives.timeRange.relativeDates')}
            </Text>
            {RELATIVE_DATES.map(item => {
              return (
                <DateLabelRenderer
                  key={`recommended-${item.label}`}
                  dateFormat={item.dateFormat}
                  dateRange={item.dateRange}
                  text={item.label}
                  onClick={() => {
                    setTimeRange({
                      from: item.dateRange[0].format(CE_DATE_FORMAT_INTERNAL),
                      to: item.dateRange[1].format(CE_DATE_FORMAT_INTERNAL)
                    })
                  }}
                />
              )
            })}
          </Layout.Vertical>
 
          <Layout.Vertical>
            <Text
              padding={{
                top: 'small',
                bottom: 'small',
                left: 'large',
                right: 'large'
              }}
              font={{ weight: 'semi-bold' }}
              color="grey800"
            >
              {getString('ce.perspectives.timeRange.calendarMonths')}
            </Text>
            {CALENDAR_MONTH_DATES.map(item => {
              return (
                <DateLabelRenderer
                  disable={featureEnforced && RESTRICTED_CALENDAR_MONTH_DATES.includes(item)}
                  key={`recommended-${item.label}`}
                  dateFormat={item.dateFormat}
                  dateRange={item.dateRange}
                  text={item.label}
                  onClick={() => {
                    setTimeRange({
                      from: item.dateRange[0].format(CE_DATE_FORMAT_INTERNAL),
                      to: item.dateRange[1].format(CE_DATE_FORMAT_INTERNAL)
                    })
                  }}
                />
              )
            })}
          </Layout.Vertical>
        </Layout.Vertical>
      }
    >
      <Button
        intent="primary"
        minimal
        padding="small"
        className={css.timeRangeButton}
        text={dateLabelText}
        icon="calendar"
        iconProps={{
          size: 16
        }}
        rightIcon="caret-down"
      />
    </Popover>
  )
}
 
const SubscriptionLimitWarning = () => {
  const { getString } = useStrings()
  return (
    <Container className={css.subscriptionLimitCtn}>
      <Layout.Horizontal spacing="medium" style={{ alignItems: 'center' }}>
        <Icon name="info-message" size={24} style={{ color: Color.BLUE_700 }} />
        <Container>
          <Text inline color={Color.GREY_800}>
            {getString('ce.perspectives.timeRangeLimitWarning.currentPlanOffer')}
          </Text>
          <Upgrade />
          <Text inline color={Color.GREY_800}>
            {getString('ce.perspectives.timeRangeLimitWarning.upgradeOffer')}
          </Text>
        </Container>
      </Layout.Horizontal>
    </Container>
  )
}
 
const Upgrade = () => {
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  return (
    <Link to={routes.toSubscriptions({ accountId, moduleCard: 'ce', tab: 'PLANS' })}>
      <Text inline margin={{ top: 'small' }} color={Color.PRIMARY_7} style={{ padding: '0px 5px' }}>
        {getString('common.upgrade')}
      </Text>
    </Link>
  )
}
 
export default PerspectiveTimeRangePicker