All files / app/components/Navigation ReportDialog.tsx

95.23% Statements 40/42
75% Branches 6/8
91.66% Functions 11/12
97.36% Lines 37/38

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 1633x 3x 3x 3x 3x 3x 3x   3x 3x   3x 3x 3x 3x 3x 3x               39x             161x     3x   20x               32x 55x   55x 55x             55x 4x   2x       55x 1x 1x 1x 1x 1x     55x 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 TextField from "components/TextField";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { GLOBAL } from "i18n/namespaces";
import { ReportBugRes } from "proto/bugs_pb";
import { ComponentPropsWithRef, forwardRef, useState } from "react";
import { useForm } from "react-hook-form";
import { Trans } from "react-i18next";
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),
  },
}));
 
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">
          <Trans
            i18nKey="report.bug.success_message2"
            t={t}
            components={{
              id: <span>{bug.bugId}</span>,
            }}
          />
        </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>
              <Button onClick={() => handleClose({}, "button")} variant="outlined">
                {t("cancel")}
              </Button>
            </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 onClick={() => handleClose({}, "button")} variant="outlined">
                {t("cancel")}
              </Button>
              <Button type="submit" loading={isPending} onClick={onSubmit}>
                {t("submit")}
              </Button>
            </DialogActions>
          </form>
        ) : null}
      </Dialog>
    </>
  );
}