All files / app/features/profile/view Overview.tsx

86.36% Statements 38/44
58.82% Branches 10/17
55.55% Functions 5/9
86.04% Lines 37/43

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  2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x             2x   2x 2x 2x   16x             2x 5x     5x                         5x 5x   5x                                                                         16x 16x   16x   16x   16x     16x   16x                                                                                                         14x           14x 14x 14x                                      
import { Edit, OpenInNew } from "@mui/icons-material";
import { styled } from "@mui/material";
import Alert from "components/Alert";
import Button from "components/Button";
import ProfileIncompleteDialog from "components/ProfileIncompleteDialog/ProfileIncompleteDialog";
import { doAntibot } from "features/antibot/antibot";
import { useAuthContext } from "features/auth/AuthProvider";
import useAccountInfo from "features/auth/useAccountInfo";
import FriendActions from "features/profile/actions/FriendActions";
import MessageUserButton from "features/profile/actions/MessageUserButton";
import UserOverview from "features/profile/view/UserOverview";
import { GLOBAL, PROFILE } from "i18n/namespaces";
import Link from "next/link";
import { useTranslation } from "next-i18next";
import { HostingStatus } from "proto/api_pb";
import { useState } from "react";
import {
  connectionsRoute,
  EditUserTab,
  routeToEditProfile,
  routeToUser,
  UserTab,
} from "routes";
 
import { useProfileUser } from "../hooks/useProfileUser";
import AdminPanelUserButton from "./AdminPanelUserButton";
import ProfileReportFlagButton from "./ProfileReportFlagButton";
 
const StyledModButtons = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  alignItems: "center",
  width: "100%",
}));
 
const getEditTab = (tab: UserTab): EditUserTab | undefined => {
  switch (tab) {
    case "about":
    case "home":
      return tab;
    default:
      return undefined;
  }
};
 
function LoggedInUserActions({
  tab,
  isInSheet,
}: {
  tab: UserTab;
  isInSheet: boolean;
}) {
  const { t } = useTranslation([GLOBAL, PROFILE]);
  const user = useProfileUser();
 
  Iif (isInSheet) {
    return (
      <Button
        component={Link}
        color="primary"
        href={routeToUser(user.username)}
        startIcon={<OpenInNew fontSize="small" />}
      >
        {t("profile:open_full_profile")}
      </Button>
    );
  }
 
  return (
    <>
      <Button
        component={Link}
        color="primary"
        href={routeToEditProfile(getEditTab(tab))}
        startIcon={<Edit fontSize="small" />}
      >
        {t("profile:edit")}
      </Button>
      <Button component={Link} variant="outlined" href={connectionsRoute}>
        {t("profile:my_connections")}
      </Button>
    </>
  );
}
 
function DefaultActions({
  setIsRequesting,
  setIsMessaging,
}: {
  setIsRequesting: (value: boolean) => void;
  setIsMessaging: (value: boolean) => void;
}) {
  const { t } = useTranslation([GLOBAL, PROFILE]);
  const user = useProfileUser();
  const disableHosting =
    user.hostingStatus === HostingStatus.HOSTING_STATUS_CANT_HOST;
 
  const [mutationError, setMutationError] = useState("");
  const [showCantRequestDialog, setShowCantRequestDialog] =
    useState<boolean>(false);
 
  const { data: accountInfo, isLoading: isAccountInfoLoading } =
    useAccountInfo();
 
  const requestButton = () => {
    doAntibot("host_request");
    if (!accountInfo?.profileComplete) {
      setShowCantRequestDialog(true);
    } else {
      setIsRequesting(true);
    }
  };
 
  return (
    <>
      <ProfileIncompleteDialog
        open={showCantRequestDialog}
        onClose={() => setShowCantRequestDialog(false)}
        attempted_action="send_request"
      />
      <Button
        onClick={requestButton}
        disabled={isAccountInfoLoading || disableHosting}
      >
        {disableHosting
          ? t("global:hosting_status.cant_host")
          : t("profile:actions.request")}
      </Button>
 
      <MessageUserButton
        user={user}
        setMutationError={setMutationError}
        setIsMessaging={setIsMessaging}
      />
      <FriendActions user={user} setMutationError={setMutationError} />
 
      <StyledModButtons>
        <ProfileReportFlagButton
          contentRef={`profile/${user.userId}`}
          authorUser={user.userId}
          profileUser={user}
        />
        <AdminPanelUserButton username={user.username} />
      </StyledModButtons>
 
      {mutationError && <Alert severity="error">{mutationError}</Alert>}
    </>
  );
}
 
interface OverviewProps {
  setIsRequesting?: (value: boolean) => void;
  setIsMessaging?: (value: boolean) => void;
  tab: UserTab;
  isInSheet?: boolean;
}
 
export default function Overview({
  setIsRequesting,
  setIsMessaging,
  tab,
  isInSheet = false,
}: OverviewProps) {
  const currentUserId = useAuthContext().authState.userId;
  const user = useProfileUser();
  const isOwnProfile = user.userId === currentUserId;
 
  return (
    <UserOverview
      showHostAndMeetAvailability
      isOwnProfile={isOwnProfile}
      actions={
        isOwnProfile ? (
          <LoggedInUserActions tab={tab} isInSheet={isInSheet} />
        ) : (
          <DefaultActions
            setIsRequesting={setIsRequesting ?? (() => {})}
            setIsMessaging={setIsMessaging ?? (() => {})}
          />
        )
      }
    />
  );
}