# All files / src/components sequence-submission.tsx

67.24% Statements 39/58
61.9% Branches 39/63
60% Functions 6/10
67.24% Lines 39/58

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

    8x

    13x

    13x

    6x
    6x

    6x

    13x
    7x
    4x
    3x

    13x
    13x
    13x

    5x

    5x

    5x

    5x
    7x
    7x
    2x
    2x
    2x

    7x
    7x
    7x

    7x
    7x
    7x
    7x

    5x
    2x

    2x

    2x

    5x

    5x
    7x

    13x

    13x

    2x

     import { useState, useEffect, useCallback, Fragment } from 'react';

    import sequenceProcessor, {
      type SequenceObject,
    } from '../sequence-utils/sequence-processor';
    import Message from './message';

    import '../styles/components/sequence-submission.scss';

    type Props = {
      /**
       * The value, if needed.
       */
      value?: string;
      /**
       * The default value, if needed.
       */
      defaultValue?: string;
      /**
       * Triggers when the value is changed.
       */
      onChange?: (processed: SequenceObject[]) => void;
      /**
       * Display text when the textarea is empty.
       */
      placeholder?: string;
      /**
       * Minimum acceptable length for a sequence
       */
      minimumLength?: number;
      /**
       * Minimum acceptable number of sequences
       */
      minimumSequences?: number;
      /**
       * Maximum acceptable number of sequences
       */
      maximumSequences?: number;
      /**
       * Error if the same ID is used twice
       */
      noDuplicateID?: boolean;
    };

    const SequenceSubmission = ({
      value,
      onChange,
      placeholder,
      defaultValue,
      minimumLength,
      minimumSequences,
      maximumSequences,
      noDuplicateID,
    }: Props) => {
      const [processed, setProcessed] = useState<SequenceObject[]>([]);

      const onChangeWithValidation = useCallback(
        (newValue: string) => {
          const processed = sequenceProcessor(newValue, minimumLength);
          setProcessed(processed);

          // Calling the custom 'onChange' callback first
          onChange?.(processed);
        },
        [onChange, minimumLength]
      );

      useEffect(() => {
        if (value || value === '') {
          onChangeWithValidation(value);
        } else Iif (defaultValue || defaultValue === '') {
          onChangeWithValidation(defaultValue);
        }
      }, [value, defaultValue, onChangeWithValidation]);

      const errorMessages = [];
      const warningMessages = [];
      if (
        processed.length > 1 ||
        (processed.length === 1 && processed[0].sequence.length > 10)
      ) {
        // loop and match all the errors together, only once
        const uniqueErrors = new Map<
          string,
          Array<{ sequenceIndex: number; sequenceObject: SequenceObject }>
        >();
        // loop and match all the identifiers together, only once
        const uniqueIdentifiers = new Map<
          string,
          Array<{ sequenceIndex: number; sequenceObject: SequenceObject }>
        >();
        // loop and match all the sequences together, only once
        const uniqueSequences = new Map<
          string,
          Array<{ sequenceIndex: number; sequenceObject: SequenceObject }>
        >();
        for (const [index, sequenceObject] of processed.entries()) {
          const { valid, message, name } = sequenceObject;
          if (!valid && message) {
            const itemList = uniqueErrors.get(message) ?? [];
            itemList.push({ sequenceIndex: index + 1, sequenceObject });
            uniqueErrors.set(message, itemList);
          }

          const itemListID = uniqueIdentifiers.get(name) ?? [];
          itemListID.push({ sequenceIndex: index + 1, sequenceObject });
          uniqueIdentifiers.set(name, itemListID);

          const sequence = sequenceObject.sequence.toLowerCase();
          const itemListSeq = uniqueSequences.get(sequence) ?? [];
          itemListSeq.push({ sequenceIndex: index + 1, sequenceObject });
          uniqueSequences.set(sequence, itemListSeq);
        }

        for (const [errorMessage, sameErrors] of uniqueErrors.entries()) {
          errorMessages.push(
            <Message
              level="failure"
              data-testid="sequence-submission-error"
              key={sameErrors.map(({ sequenceIndex }) => sequenceIndex).join('-')}
            >
              Error: {errorMessage}. Sequence{sameErrors.length === 1 ? ' ' : 's '}
              {sameErrors.map(
                ({ sequenceIndex, sequenceObject: { name } }, index) => (
                  <Fragment key={sequenceIndex}>
                    {index ? ', ' : ''}
                    {sequenceIndex}{' '}
                    {name ? (
                      <>
                        (<code>{name}</code>)
                      </>
                    ) : (
                      ''
                    )}
                  </Fragment>
                )
              )}
            </Message>
          );
        }

        Iif (noDuplicateID) {
          for (const [identifier, sameIdentifiers] of uniqueIdentifiers.entries()) {
            if (sameIdentifiers.length > 1) {
              warningMessages.push(
                <Message
                  level="failure"
                  key={sameIdentifiers
                    .map(({ sequenceIndex }) => sequenceIndex)
                    .join('-id-')}
                >
                  Sequences{' '}
                  {sameIdentifiers.map(({ sequenceIndex }, index) => {
                    let conjunction = '';
                    if (index) {
                      if (sameIdentifiers.length === 2) {
                        conjunction = ' and ';
                      } else {
                        conjunction =
                          index + 1 === sameIdentifiers.length ? ', and ' : ', ';
                      }
                    }
                    return (
                      <Fragment key={sequenceIndex}>
                        {conjunction}
                        {sequenceIndex}
                      </Fragment>
                    );
                  })}{' '}
                  have the same identifier (<code>{identifier}</code>), this is
                  invalid.
                </Message>
              );
            }
          }
        }

        for (const sameSequences of uniqueSequences.values()) {
          Iif (sameSequences.length > 1) {
            warningMessages.push(
              <Message
                level="info"
                key={sameSequences
                  .map(({ sequenceIndex }) => sequenceIndex)
                  .join('-seq-')}
              >
                Sequences{' '}
                {sameSequences.map(
                  ({ sequenceIndex, sequenceObject: { name } }, index) => {
                    let conjunction = '';
                    if (index) {
                      if (sameSequences.length === 2) {
                        conjunction = ' and ';
                      } else {
                        conjunction =
                          index + 1 === sameSequences.length ? ', and ' : ', ';
                      }
                    }
                    return (
                      <Fragment key={sequenceIndex}>
                        {conjunction}
                        {sequenceIndex}{' '}
                        {name ? (
                          <>
                            (<code>{name}</code>)
                          </>
                        ) : (
                          ''
                        )}
                      </Fragment>
                    );
                  }
                )}{' '}
                are identical, this might be unintended.
              </Message>
            );
          }
        }
      }

      const sequenceCountMessage =
        (maximumSequences && processed.length > maximumSequences && (
          <Message level="failure">
            Your input contains {processed.length} sequences which exceeds the
            submission limit of {maximumSequences}.
          </Message>
        )) ||
        (minimumSequences &&
          processed.length &&
          processed.length < minimumSequences && (
            <Message level="failure">
              Your input contains {processed.length} sequence
              {processed.length === 1 ? '' : 's'}. At least {minimumSequences}{' '}
              sequences are required.
            </Message>
          )) ||
        (processed[0] && processed[0].sequence.length > 10 && (
          <Message level="info">
            Your input contains {processed.length} sequence
            {processed.length === 1 ? '' : 's'}.
          </Message>
        ));

      return (
        <>
          <textarea
            className="sequence-submission-input"
            value={value}
            onChange={(e) => onChangeWithValidation(e.target.value)}
            placeholder={placeholder}
            data-testid="sequence-submission-input"
            defaultValue={defaultValue}
            spellCheck="false"
          />
          {sequenceCountMessage}
          {errorMessages}
          {warningMessages}
        </>
      );
    };

    export default SequenceSubmission;