All files / app/components/Navigation ReportDialog.tsx

95.34% Statements 41/43
71.42% Branches 5/7
92.3% Functions 12/13
97.43% Lines 38/39

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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 2021x 1x 1x 1x             1x 1x 1x 1x   1x   1x 1x 1x 1x 1x               54x                         161x       1x   20x               28x                 23x 50x   50x         50x             50x 4x   2x       50x       1x 1x 1x 1x 1x 1x         50x 4x 4x                                             7x                                                                                                                             1x                        
import { DialogProps, styled } from "@mui/material";
import { useMutation } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
} from "components/Dialog";
import Snackbar from "components/Snackbar";
import StyledLink from "components/StyledLink";
import TextField from "components/TextField";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { ReportBugRes } from "proto/bugs_pb";
import { ComponentPropsWithRef, forwardRef, useState } from "react";
import { useForm } from "react-hook-form";
import { helpCenterReportContentURL } from "routes";
import { service } from "service";
import { theme } from "theme";
 
export interface BugReportFormData {
  subject: string;
  description: string;
  results: string;
}
 
const StyledTextField = styled(TextField)(() => ({
  marginBottom: theme.spacing(2),
}));
 
// If onKeyDown event propagation isn't stopped, rendering inside menu will cause
// focus issues
const ReportDialogTextField = forwardRef<
  HTMLInputElement,
  Omit<ComponentPropsWithRef<typeof TextField>, "onKeyDown">
>((props, ref) => (
  <StyledTextField
    ref={ref}
    {...props}
    onKeyDown={(e) => e.stopPropagation()}
  />
));
 
ReportDialogTextField.displayName = "ReportDialogTextField";
 
const StyledReportTypeButton = styled(Button)(() => ({
  display: "block",
  margin: "0 auto",
  "& + &": {
    marginBlockStart: theme.spacing(2),
  },
}));
 
const StyledCancelButton = styled(Button)(() => ({
  color: theme.palette.common.black,
  borderColor: theme.palette.grey[300],
  "&:hover": {
    borderColor: theme.palette.grey[300],
    backgroundColor: "#3135390A",
  },
}));
 
export default function ReportDialog({ open, onClose }: DialogProps) {
  const { t } = useTranslation("global");
 
  const [type, setType] = useState<"initial" | "bug">("initial");
  const {
    register,
    handleSubmit,
    reset: resetForm,
  } = useForm<BugReportFormData>();
  const {
    data: bug,
    error,
    isPending,
    mutate: reportBug,
    reset: resetMutation,
  } = useMutation<ReportBugRes.AsObject, RpcError, BugReportFormData>({
    mutationFn: (formData) => service.bugs.reportBug(formData),
    onSuccess: () => {
      onClose?.({}, "escapeKeyDown");
    },
  });
 
  const handleClose = (
    event: unknown,
    reason: "backdropClick" | "escapeKeyDown" | "button",
  ) => {
    Iif (reason !== "button") return;
    resetForm();
    resetMutation();
    onClose?.({}, "escapeKeyDown");
    setTimeout(
      () => setType("initial"),
      theme.transitions.duration.leavingScreen,
    );
  };
 
  const onSubmit = handleSubmit((data) => {
    reportBug(data);
    resetForm();
  });
 
  return (
    <>
      {bug && (
        <Snackbar severity="success">
          <>
            {t("report.bug.success_message")}
            <StyledLink variant="body2" href={bug.bugUrl}>
              {bug.bugId}
            </StyledLink>
            .
          </>
        </Snackbar>
      )}
      <Dialog aria-labelledby="bug-reporter" open={open} onClose={handleClose}>
        <DialogTitle id="bug-reporter">{t("report.label")}</DialogTitle>
        {type === "initial" ? (
          <>
            <DialogContent>
              <StyledReportTypeButton
                onClick={() => {
                  setType("bug");
                }}
              >
                {t("report.bug.button_label")}
              </StyledReportTypeButton>
              <StyledReportTypeButton
                href={helpCenterReportContentURL}
                style={{ maxWidth: "fit-content", textAlign: "center" }}
              >
                {t("report.content.button_label")}
              </StyledReportTypeButton>
            </DialogContent>
            <DialogActions>
              <StyledCancelButton
                onClick={() => handleClose({}, "button")}
                variant="outlined"
              >
                {t("cancel")}
              </StyledCancelButton>
            </DialogActions>
          </>
        ) : type === "bug" ? (
          <form onSubmit={onSubmit}>
            <DialogContent>
              {error && <Alert severity="error">{error.message}</Alert>}
              <DialogContentText>
                {t("report.bug.warning_message")}
              </DialogContentText>
              <ReportDialogTextField
                id="bug-report-subject"
                {...register("subject", { required: true })}
                label={t("report.bug.title_label")}
                fullWidth
              />
              <ReportDialogTextField
                id="bug-report-description"
                {...register("description", { required: true })}
                label={t("report.bug.problem_label")}
                helperText={t("report.bug.problem_helper")}
                name="description"
                fullWidth
                multiline
                minRows={4}
                maxRows={6}
              />
              <ReportDialogTextField
                {...register("results")}
                id="bug-report-results"
                defaultValue=""
                label={t("report.bug.expect_label")}
                helperText={t("report.bug.expect_helper")}
                name="results"
                fullWidth
                multiline
                minRows={4}
                maxRows={6}
              />
            </DialogContent>
            <DialogActions>
              <Button type="submit" loading={isPending} onClick={onSubmit}>
                {t("submit")}
              </Button>
              <StyledCancelButton
                onClick={() => handleClose({}, "button")}
                variant="outlined"
              >
                {t("cancel")}
              </StyledCancelButton>
            </DialogActions>
          </form>
        ) : null}
      </Dialog>
    </>
  );
}