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

78.94% Statements 30/38
66.66% Branches 4/6
50% Functions 4/8
77.14% Lines 27/35

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 1032x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   36x         24x       24x           14x 36x 36x 36x 36x             36x   36x                         36x   36x                                                                                      
import { CardActions, Skeleton, styled, Typography } from "@mui/material";
import { useMutation } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import TextField from "components/TextField";
import { useProfileUser } from "features/profile/hooks/useProfileUser";
import { useProfileSheet } from "features/profile/ProfileSheetContext";
import { useLiteUser } from "features/userQueries/useLiteUsers";
import { Trans, useTranslation } from "i18n";
import { GLOBAL, PROFILE } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useForm } from "react-hook-form";
import { routeToGroupChat } from "routes";
import { service } from "service";
import { theme } from "theme";
 
const StyledTitle = styled(Typography)(() => ({
  marginTop: theme.spacing(1),
  marginBottom: theme.spacing(1),
}));
 
const StyledMessageField = styled(TextField)(() => ({
  marginTop: theme.spacing(2),
}));
 
const StyledSendActions = styled(CardActions)(() => ({
  display: "flex",
  justifyContent: "flex-end",
  marginTop: theme.spacing(2),
}));
 
export default function NewMessage({ setIsMessaging }: { setIsMessaging: (value: boolean) => void }) {
  const { t } = useTranslation([GLOBAL, PROFILE]);
  const user = useProfileUser();
  const router = useRouter();
  const { openGroupChat, openProfileUserId } = useProfileSheet();
 
  const {
    handleSubmit,
    register,
    reset,
    formState: { errors },
  } = useForm<{ text: string }>();
 
  const { error, mutate, isPending } = useMutation<number, Error, { text: string }>({
    mutationFn: (data) => service.conversations.sendDirectMessage(user.userId, data.text),
    onSuccess: (groupChatId) => {
      reset();
      setIsMessaging(false);
      if (openProfileUserId !== null) {
        openGroupChat(groupChatId);
      } else {
        router.push(routeToGroupChat(groupChatId));
      }
    },
  });
 
  const { isLoading: userLoading, error: userError } = useLiteUser(user.userId);
 
  const onSubmit = handleSubmit((data) => {
    mutate(data);
  });
 
  return (
    <>
      <StyledTitle variant="h1">
        {userLoading ? <Skeleton width="100" /> : t("profile:message_form.send_message", { name: user.name })}
      </StyledTitle>
      {error && <Alert severity="error">{error.message}</Alert>}
      {userError ? (
        <Alert severity={"error"}>{userError?.message}</Alert>
      ) : (
        <form onSubmit={onSubmit}>
          <StyledMessageField
            id="text"
            {...register("text", {
              required: t("profile:message_form.message_empty"),
            })}
            label={t("profile:message_form.message")}
            minRows={6}
            multiline
            fullWidth
            placeholder={t("profile:message_form.message_description")}
            error={!!errors.text}
            helperText={errors.text?.message}
          />
          <StyledSendActions>
            <Button onClick={() => setIsMessaging(false)} variant="outlined">
              {t("global:cancel")}
            </Button>
            <Button type="submit" onClick={onSubmit} loading={isPending}>
              {t("global:send")}
            </Button>
          </StyledSendActions>
          <Typography variant="body2" sx={{ textAlign: "center" }}>
            <Trans i18nKey="profile:message_form.hosting_request_hint" components={{ request: <strong /> }} />
          </Typography>
        </form>
      )}
    </>
  );
}