All files / src/features/Minibuilders/Conversations/hooks useUndeletableWord.tsx

30.76% Statements 4/13
0% Branches 0/16
50% Functions 1/2
30.76% Lines 4/13

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 4021x         60x   60x                                                     60x          
export const useUndeletableWord = (
  element: HTMLTextAreaElement,
  word: string | undefined,
  isHashtag?: boolean,
) => {
  const autoFocus = Boolean(word);
 
  const keyDownExtender = (event: any) => {
    const prevLength = element.selectionEnd;
    const prevLengthCondition =
      word &&
      (event.key === "Backspace" || event.key === "Delete") &&
      (isHashtag
        ? prevLength <= word.trim()?.length + 1
        : prevLength <= word.trim()?.length);
    if (prevLengthCondition) {
      event.preventDefault();
    }
 
    const prevSelectionCondition =
      word &&
      (isHashtag
        ? element.selectionStart < word.length + 1
        : element.selectionStart < word.length);
 
    if (prevSelectionCondition) {
      if (isHashtag) {
        element.selectionStart = word.length
      } else {
        element.selectionStart = word.length - 1
      }
    }
  };
 
  return {
    keyDownExtender,
    autoFocus,
  };
};