# All files / src/Thumbnail ThumbnailCaption.tsx

73.58% Statements 39/53
70.58% Branches 12/17
75% Functions 3/4
78.04% Lines 32/41

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
    1478x
    8x
    8x
    8x

    8x

    8x
    8x

    8x
    8x
    8x
    8x
    8x
    8x

    8x
    8x

    8x
    69x
    69x
    69x
    69x
    69x
    69x
    69x
    69x

    69x
    59x

    59x
    1x

    58x

    10x
    9x

    1x

     import React from 'react'
    import { Tooltip } from '../Tooltip/Tooltip'
    import { useOverflowObserver } from '../_hooks/OverflowObserver'
    import { parseFilename } from '../_utils/filename'
    import { Props } from '../_utils/types'
    import {
      StyledCaptionText,
      StyledMultilineCaption,
      StyledPlaceholderCaption,
      StyledSingleLineCaption,
      StyledSinglelineCaptionBody,
      StyledSinglelineCaptionEnding,
    } from './Thumbnail.styles'
    import {
      CaptionProps,
      CaptionTooltipProps,
      MultilineCaptionProps,
      SinglelineCaptionProps,
    } from './Thumbnail.types'

    function CaptionTooltip({ children, overlay, isVisible }: CaptionTooltipProps) {
      return (
        <Tooltip
          overlay={<Tooltip.Content>{overlay}</Tooltip.Content>}
          placement="bottom"
          trigger={isVisible ? 'hover' : 'none'}
        >
          {children}
        </Tooltip>
      )
    }

    function MultilineCaption({
      disabled,
      caption,
      error,
      layout,
      qa,
      ...props
    }: MultilineCaptionProps & Props) {
      const { ref, isOverflowing } = useOverflowObserver()
      return (
        <CaptionTooltip overlay={caption} isVisible={isOverflowing}>
          {/* additional container is needed because Tooltip overrides the passed `ref` */}
          <span data-qa={qa}>
            <StyledMultilineCaption
              ref={ref}
              disabled={disabled}
              error={error}
              layout={layout}
              {...props}
            >
              <StyledCaptionText>{caption}</StyledCaptionText>
            </StyledMultilineCaption>
          </span>
        </CaptionTooltip>
      )
    }

    function SinglelineCaption({
      caption,
      error,
      disabled,
      layout,
      qa,
      ...props
    }: SinglelineCaptionProps & Props) {
      const { ref, isOverflowing } = useOverflowObserver()
      const { filename, extension, isFilename } = parseFilename(caption)

      return (
        <CaptionTooltip overlay={caption} isVisible={isOverflowing}>
          <StyledSingleLineCaption
            error={error}
            disabled={disabled}
            layout={layout}
            data-qa={qa}
            {...props}
          >
            <StyledSinglelineCaptionBody ref={ref}>
              {isFilename ? `${filename}.` : caption}
            </StyledSinglelineCaptionBody>
            {isFilename && (
              <StyledSinglelineCaptionEnding>
                {extension}
              </StyledSinglelineCaptionEnding>
            )}
          </StyledSingleLineCaption>
        </CaptionTooltip>
      )
    }

    export function ThumbnailCaption({
      layout,
      disabled = false,
      error = false,
      caption,
      hasCaptionPlaceholder,
      multiline,
      qa = '',
      ...props
    }: CaptionProps) {
      if (!caption) {
        const captionPlaceholder = hasCaptionPlaceholder && '- -'

        if (captionPlaceholder) {
          return (
            <StyledPlaceholderCaption
              {...props}
              disabled={disabled}
              error={error}
              layout={layout}
              data-qa={qa}
            >
              <StyledCaptionText>{captionPlaceholder}</StyledCaptionText>
            </StyledPlaceholderCaption>
          )
        }

        return null
      }

      if (multiline) {
        return (
          <MultilineCaption
            disabled={disabled}
            caption={caption}
            error={error}
            layout={layout}
            qa={qa}
            {...props}
          />
        )
      }

      return (
        <SinglelineCaption
          caption={caption}
          disabled={disabled}
          error={error}
          layout={layout}
          qa={qa}
          {...props}
        />
      )
    }