All files / app/features/messages/groupchats MembersDialog.tsx

80.95% Statements 17/21
42.85% Branches 6/14
40% Functions 2/5
84.21% Lines 16/19

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          1x 1x 1x           1x 1x 1x 1x 1x     1x 1x   1x                                                                 1x       161x 161x   161x                     408x                                                  
import {
  CircularProgress,
  DialogProps,
  List,
  ListItem,
} from "@material-ui/core";
import Avatar from "components/Avatar";
import Button from "components/Button";
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
} from "components/Dialog";
import TextBody from "components/TextBody";
import useUsers from "features/userQueries/useUsers";
import { useTranslation } from "i18n";
import { GLOBAL, MESSAGES } from "i18n/namespaces";
import { User } from "proto/api_pb";
import { GroupChat } from "proto/conversations_pb";
import React from "react";
import makeStyles from "utils/makeStyles";
 
export const useMembersDialogStyles = makeStyles((theme) => ({
  avatar: {
    height: 30,
    marginInlineEnd: theme.spacing(1),
    width: 30,
  },
  memberListItemContainer: {
    alignItems: "center",
    display: "flex",
    justifyContent: "flex-start",
  },
}));
 
function MemberListItem({
  member,
  memberIsAdmin,
}: {
  member: User.AsObject;
  memberIsAdmin: boolean;
}) {
  const classes = useMembersDialogStyles();
 
  return (
    <ListItem dense className={classes.memberListItemContainer}>
      <Avatar user={member} className={classes.avatar} />
      <TextBody noWrap>
        {member.name}
        {memberIsAdmin && " (admin)"}
      </TextBody>
    </ListItem>
  );
}
 
export default function MembersDialog({
  groupChat,
  ...props
}: DialogProps & { groupChat?: GroupChat.AsObject }) {
  const { t } = useTranslation([GLOBAL, MESSAGES]);
  const members = useUsers(groupChat?.memberUserIdsList ?? []);
 
  return (
    <Dialog {...props} aria-labelledby="members-dialog-title">
      <DialogTitle id="members-dialog-title">
        {t("messages:members_dialog.title")}
      </DialogTitle>
      <DialogContent>
        <List>
          {members.isLoading ? (
            <CircularProgress />
          ) : (
            Array.from(members.data?.values() ?? []).map((user) =>
              user ? (
                <MemberListItem
                  key={`members-dialog-${user.userId}`}
                  member={user}
                  memberIsAdmin={
                    groupChat?.adminUserIdsList.includes(user.userId) ?? false
                  }
                />
              ) : null
            )
          )}
        </List>
      </DialogContent>
      <DialogActions>
        <Button
          onClick={() =>
            props.onClose ? props.onClose({}, "escapeKeyDown") : null
          }
        >
          {t("global:ok")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}