All files / src/features/Minibuilders/Conversations/utils functions.ts

82.05% Statements 32/39
76% Branches 19/25
84.61% Functions 11/13
83.33% Lines 30/36

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                                  117x           21x   21x         117x 30x 9x               21x 21x               21x   30x 9x   21x           21x 21x   21x             117x                                         4x   4x 1x         4x     117x     1867x 226x 678x   678x           1x   3x 3x 3x           3x    
import { TComment, TThreadInfo } from "../types";
import { TConversationTopic } from "../hooks/useListTopics";
import { addSizeToImage } from "@components/UploadAvatar/UploadAvatar";
import AvatarPlaceholder from "@assets/images/avatar-placeholder.png";
 
export type TMentionedUser = {
  user: string;
  authorAccID: number | undefined;
};
 
export type ThreadInfos = {
  comment: TComment;
  userAvatars: string[];
  commentCreatedAt: number;
  threadUpdatedAt: number;
};
 
export const objectSlicer = <T extends TComment>(
  objects: T[],
  start: number,
  end: number,
  property?: keyof T,
): T[keyof T][] | T[] => {
  const sliced = objects?.slice(start, end);
 
  Eif (!property) return sliced;
 
  return sliced?.map((obj) => obj[property]);
};
 
export const getThreadInfos = (thread: TThreadInfo) => {
  if (!thread.messages || thread.messages.length === 0) {
    return {
      comment: undefined,
      userAvatars: [],
      commentCreatedAt: 0,
      threadUpdatedAt: 0,
    };
  }
 
  const comment = objectSlicer(thread.messages, 0, 1)[0] as TComment;
  Iif (!comment)
    return {
      comment: undefined,
      userAvatars: [],
      commentCreatedAt: 0,
      threadUpdatedAt: 0,
    };
 
  const userAvatars = thread.messages.reduce(
    (acc: string[], message: TComment, _, arr: TComment[]) => {
      if (acc.includes(message.authorAvatarImageURL)) {
        return acc;
      } else {
        return [...acc, message.authorAvatarImageURL];
      }
    },
    [],
  );
 
  const threadUpdatedAt = new Date(thread.lastMessageAt || 0).getTime();
  const commentCreatedAt = Math.floor(new Date(comment?.createdAt).getTime());
 
  return {
    comment,
    userAvatars,
    commentCreatedAt,
    threadUpdatedAt,
  };
};
export const getRepliesInfos = (thread: TThreadInfo) => {
  const comments = objectSlicer(
    thread.messages,
    0,
    thread.messages.length,
  ) as TComment[];
  if (comments.length === 0)
    return {
      comment: undefined,
      userAvatars: [],
      commentCreatedAt: 0,
      threadUpdatedAt: 0,
    };
 
  return { comments };
};
 
export function parseInput(
  input: string,
  { isUrl }: { isUrl?: boolean } = {},
): string {
  const hashtagPattern = /#([^\s#]+)/g;
 
  const result = input.replace(hashtagPattern, (match, hashtagName) => {
    return !isUrl
      ? `#[__${hashtagName}__](hashtag:__${hashtagName}__)`
      : `hashtag ${hashtagName}`;
  });
 
  return result;
}
 
export const calculateTotalUnreadSum = (
  arrayOfObjects: TConversationTopic[],
): { totalUnread: number; hasUnreadActivity: boolean } => {
  if (!arrayOfObjects) return { totalUnread: 0, hasUnreadActivity: false };
  return {
    totalUnread: arrayOfObjects.reduce((sum, obj) => sum + obj.totalUnread, 0),
    hasUnreadActivity: arrayOfObjects.some(
      (obj: any) => obj.typeName === "activity" && obj.totalUnread > 0,
    ),
  };
};
 
export function getComputedMentions(data: any, currentUserId: number) {
  return data?.data
    ?.map((obj: any) => {
      const { firstName, lastName, accID, imageURL, email } = obj.user;
      const nameBuilder = `${firstName} ${lastName}`;
      return {
        id: `${accID}`,
        display: firstName ? nameBuilder : email,
        image: imageURL ? addSizeToImage(imageURL, "small") : AvatarPlaceholder,
      };
    })
    .filter(({ id }: { id: string }) => id !== `${currentUserId}`);
}