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

83.33% Statements 30/36
52.38% Branches 11/21
83.33% Functions 5/6
96.55% Lines 28/29

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 821x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x       37x 40x 40x 40x     2x 2x 2x 2x 2x 2x             1x 1x     1x       40x   40x                     2x                                                    
import { DialogProps, FormControl, FormControlLabel, Radio, RadioGroup } from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import { Dialog, DialogActions, DialogContent, DialogTitle } from "components/Dialog";
import { groupChatKey, groupChatsListKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { GLOBAL, MESSAGES } from "i18n/namespaces";
import React, { useState } from "react";
import { service } from "service";
import { Temporal } from "temporal-polyfill";
import { approxTimeDuration } from "utils/date";
 
type DurationChoice = "1h" | "8h" | "1d" | "1w" | "1m" | "forever";
 
export default function MuteDialog({ groupChatId, ...props }: DialogProps & { groupChatId: number }) {
  const { t } = useTranslation([GLOBAL, MESSAGES]);
  const queryClient = useQueryClient();
  const muteMutation = useMutation<void, RpcError, DurationChoice>({
    mutationFn: async (duration) => {
      let d;
      Iif (duration === "1h") d = Temporal.Duration.from({ hours: 1 });
      else Iif (duration === "8h") d = Temporal.Duration.from({ hours: 8 });
      else Iif (duration === "1d") d = Temporal.Duration.from({ days: 1 });
      else Iif (duration === "1w") d = Temporal.Duration.from({ weeks: 1 });
      else Iif (duration === "1m") d = Temporal.Duration.from({ months: 1 });
      await service.conversations.muteChat({
        groupChatId,
        forDuration: d ? approxTimeDuration(d) : undefined,
        forever: !d,
      });
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [groupChatsListKey] });
      queryClient.invalidateQueries({
        queryKey: [groupChatKey(groupChatId)],
      });
      Eif (props.onClose) props.onClose({}, "escapeKeyDown");
    },
  });
 
  const [selected, setSelected] = useState<DurationChoice | undefined>(undefined);
 
  const handleSubmit = () => (selected ? muteMutation.mutate(selected) : null);
 
  return (
    <Dialog {...props} aria-labelledby="mute-dialog-title">
      <DialogTitle id="mute-dialog-title">{t("messages:chat_view.mute.dialog_title")}</DialogTitle>
      <DialogContent>
        {muteMutation.error && <Alert severity="error">{muteMutation.error.message}</Alert>}
        <FormControl variant="standard" component="fieldset">
          <RadioGroup
            aria-labelledby="mute-dialog-title"
            value={selected ?? null}
            onChange={(e, val) => setSelected(val as DurationChoice)}
          >
            <FormControlLabel value="1h" control={<Radio />} label={t("messages:chat_view.mute.1_hour_label")} />
            <FormControlLabel value="8h" control={<Radio />} label={t("messages:chat_view.mute.8_hours_label")} />
            <FormControlLabel value="1d" control={<Radio />} label={t("messages:chat_view.mute.1_day_label")} />
            <FormControlLabel value="1w" control={<Radio />} label={t("messages:chat_view.mute.1_week_label")} />
            <FormControlLabel value="1m" control={<Radio />} label={t("messages:chat_view.mute.1_month_label")} />
            <FormControlLabel value="forever" control={<Radio />} label={t("messages:chat_view.mute.forever_label")} />
          </RadioGroup>
        </FormControl>
      </DialogContent>
      <DialogActions>
        <Button
          variant="outlined"
          onClick={() => (props.onClose ? props.onClose({}, "escapeKeyDown") : null)}
          loading={muteMutation.isPending}
        >
          {t("global:cancel")}
        </Button>
        <Button onClick={handleSubmit} loading={muteMutation.isPending} disabled={!selected}>
          {t("messages:chat_view.mute.button_label")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}