# All files / src/FileToken FileToken.tsx

100% Statements 57/57
96.29% Branches 26/27
100% Functions 7/7
100% Lines 48/48

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
    1535x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x

    5x

    5x

    61x
    61x
    61x
    61x
    61x
    61x
    61x

    61x
    61x

    61x

    61x

    61x

    61x

    61x

    61x
    61x

    61x
    19x

    3x

    19x
    15x

    19x
    5x

    61x
    19x
    19x

    1x

    19x

    19x
    17x

    19x
    5x

    61x

    61x
    1x

    60x

    5x
     import { useId } from '@react-aria/utils'
    import { VisuallyHidden } from '@react-aria/visually-hidden'
    import React from 'react'
    import { Loader } from '../Loader/Loader'
    import { loaderTransitionDuration } from '../Loader/Loader.styles'
    import { Token } from '../Token/Token'
    import { Tooltip } from '../Tooltip/Tooltip'
    import { useI18nContext } from '../_hooks/I18n'
    import { parseFilename } from '../_utils/filename'
    import { DivAttributes } from '../_utils/types'
    import {
      pulseAnimationDuration,
      StyledExtension,
      StyledFilename,
      StyledFilenameShortcut,
      StyledFileToken,
    } from './FileToken.styles'
    import { FileTokenProps } from './FileToken.types'

    /**
     * @deprecatedSince 11
     * @deprecated Intended for internal library development.
     */
    export const FileToken = React.forwardRef<
      HTMLDivElement,
      FileTokenProps & DivAttributes
    >(function FileToken(
      {
        fileName,
        progressValue = 0,
        errorMessage = '',
        onLabelClick,
        onClose,
        qa,
        ...props
      },
      ref
    ) {
      const i18n = useI18nContext()
      const isError = Boolean(errorMessage)

      const isProgressValueComplete = !isError && progressValue >= 100

      const [isProgressComplete, setProgressComplete] = React.useState(
        isProgressValueComplete
      )

      const [isFinalized, setFinalized] = React.useState(false)

      const didMountRef = React.useRef(false)

      const { filename, extension, isFilename } = parseFilename(fileName)

      const progressBarLabelId = useId()
      const name = isFilename ? `${filename}.` : fileName

      React.useEffect(() => {
        const timerId: number | null = isProgressValueComplete
          ? window.setTimeout(() => {
              setProgressComplete(true)
            }, loaderTransitionDuration)
          : null

        if (!isProgressValueComplete) {
          setProgressComplete(false)
        }

        return () => {
          timerId && clearTimeout(timerId)
        }
      }, [isProgressValueComplete])

      React.useEffect(() => {
        const isFinalized = didMountRef.current && isProgressComplete
        const timerId: number | null = isFinalized
          ? window.setTimeout(() => {
              setFinalized(false)
            }, pulseAnimationDuration)
          : null

        setFinalized(didMountRef.current && isProgressComplete)

        if (!didMountRef.current) {
          didMountRef.current = true
        }

        return () => {
          timerId && clearTimeout(timerId)
        }
      }, [isProgressComplete])

      const token = (
        <Token>
          <Token.Label onClick={onLabelClick}>
            <StyledFilenameShortcut>
              <StyledFilename>{name}</StyledFilename>
              <StyledExtension>{extension}</StyledExtension>
            </StyledFilenameShortcut>
          </Token.Label>
          <Token.Remove onClick={onClose} data-qa={qa?.closeButton} />
        </Token>
      )

      // prevent empty tooltip blinks when navigating via the keyboard by wrapping in tooltip conditionally
      if (isError) {
        return (
          <Tooltip ref={ref} trigger="hover" overlay={errorMessage}>
            <StyledFileToken
              {...props}
              $error={isError}
              $finalized={isFinalized}
              $progressComplete={isProgressComplete}
            >
              {token}
              <Loader value={progressValue} animated>
                {token}
              </Loader>
            </StyledFileToken>
          </Tooltip>
        )
      }

      return (
        <StyledFileToken
          {...props}
          $error={isError}
          $finalized={isFinalized}
          $progressComplete={isProgressComplete}
          ref={ref}
        >
          {token}
          <Loader value={progressValue} animated aria-hidden>
            {token}
          </Loader>
          <VisuallyHidden>
            <span id={progressBarLabelId}>
              {i18n.t('core.fileToken.uploading')}
            </span>
            <div
              aria-labelledby={progressBarLabelId}
              role="progressbar"
              aria-valuenow={progressValue}
              aria-valuemin={0}
              aria-valuemax={100}
              aria-valuetext={`${progressValue}%`}
            />
          </VisuallyHidden>
        </StyledFileToken>
      )
    })

    FileToken.displayName = 'FileToken'