All files / app/features/messages utils.ts

38.23% Statements 13/34
15.62% Branches 5/32
50% Functions 4/8
38.23% Lines 13/34

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      1x   1x   1x 745x     1x 151x                 1x                     139x 139x 139x                                                               1x         161x                     1x                            
import useUsers from "features/userQueries/useUsers";
import { GroupChat, Message } from "proto/conversations_pb";
import { TFunction } from "react-i18next";
import { firstName } from "utils/names";
 
import { requestStatusToTransKey } from "./constants";
 
export function isControlMessage(message: Message.AsObject) {
  return !message.text;
}
 
export function messageTargetId(message: Message.AsObject) {
  return message.userInvited
    ? message.userInvited.targetUserId
    : message.userMadeAdmin
    ? message.userMadeAdmin.targetUserId
    : message.userRemovedAdmin
    ? message.userRemovedAdmin.targetUserId
    : undefined;
}
 
export function controlMessage({
  user,
  target_user,
  message,
  t,
}: {
  user: string;
  target_user?: string;
  message: Message.AsObject;
  t: TFunction<"messages", undefined>;
}) {
  const userCap = user.charAt(0).toUpperCase() + user.slice(1);
  if (message.chatCreated) {
    return t("control_message.created_chat_text", { user: userCap });
  } else Eif (message.chatEdited) {
    return t("control_message.edited_chat_text", { user: userCap });
  } else if (message.userInvited) {
    return t("control_message.invite_user_text", {
      user: userCap,
      target_user,
    });
  } else if (message.userLeft) {
    return t("control_message.user_left_chat_text", { user: userCap });
  } else if (message.userMadeAdmin) {
    return t("control_message.admin_assignment_text", {
      user: userCap,
      target_user,
    });
  } else if (message.userRemovedAdmin) {
    return t("control_message.admin_removal_text", {
      user: userCap,
      target_user,
    });
  } else if (message.hostRequestStatusChanged) {
    return t("control_message.host_request_status_changed_text", {
      user,
      status: t(
        requestStatusToTransKey[message.hostRequestStatusChanged.status]
      ),
    });
  } else {
    throw Error(t("control_message.unknown_message_text"));
  }
}
 
export function groupChatTitleText(
  groupChat: GroupChat.AsObject,
  groupChatMembersQuery: ReturnType<typeof useUsers>,
  currentUserId: number
) {
  return groupChat.title
    ? groupChat.title
    : groupChatMembersQuery.isLoading
    ? "Chat"
    : Array.from(groupChatMembersQuery.data?.values() ?? [])
        .filter((user) => user?.userId !== currentUserId)
        .map((user) => firstName(user?.name))
        .join(", ");
}
 
/** Returns the other user's username, or null if there are more than 2 users. */
export function getDmUsername(
  groupChatMembersQuery: ReturnType<typeof useUsers>,
  currentUserId: number
) {
  const users = Array.from(groupChatMembersQuery.data?.values() ?? []);
  if (users.length === 2) {
    const username = users.find(
      (user) => user?.userId !== currentUserId
    )?.username;
    return username ?? null;
  } else {
    return null;
  }
}