Files
element-call/src/room/FeedbackModal.tsx

115 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
Copyright 2022 Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-02-04 16:55:57 -08:00
import React, { useCallback, useEffect } from "react";
2022-08-02 00:46:16 +02:00
import { randomString } from "matrix-js-sdk/src/randomstring";
2022-10-10 09:19:10 -04:00
import { useTranslation } from "react-i18next";
2022-08-02 00:46:16 +02:00
2022-02-04 16:55:57 -08:00
import { Modal, ModalContent } from "../Modal";
import { Button } from "../button";
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
2022-04-07 14:22:36 -07:00
import {
useSubmitRageshake,
useRageshakeRequest,
} from "../settings/submit-rageshake";
2022-02-04 16:55:57 -08:00
import { Body } from "../typography/Typography";
2022-10-10 09:19:10 -04:00
2022-08-02 00:46:16 +02:00
interface Props {
inCall: boolean;
roomId: string;
onClose?: () => void;
// TODO: add all props for for <Modal>
[index: string]: unknown;
}
2022-10-10 09:19:10 -04:00
2022-08-02 00:46:16 +02:00
export function FeedbackModal({ inCall, roomId, onClose, ...rest }: Props) {
2022-10-10 09:19:10 -04:00
const { t } = useTranslation();
2022-02-04 16:55:57 -08:00
const { submitRageshake, sending, sent, error } = useSubmitRageshake();
const sendRageshakeRequest = useRageshakeRequest();
const onSubmitFeedback = useCallback(
(e) => {
e.preventDefault();
const data = new FormData(e.target);
2022-08-02 00:46:16 +02:00
const descriptionData = data.get("description");
const description =
typeof descriptionData === "string" ? descriptionData : "";
const sendLogs = Boolean(data.get("sendLogs"));
const rageshakeRequestId = randomString(16);
submitRageshake({
description,
sendLogs,
rageshakeRequestId,
roomId,
});
2022-02-04 16:55:57 -08:00
if (inCall && sendLogs) {
sendRageshakeRequest(roomId, rageshakeRequestId);
2022-02-04 16:55:57 -08:00
}
},
[inCall, submitRageshake, roomId, sendRageshakeRequest]
);
useEffect(() => {
if (sent) {
2022-08-02 00:46:16 +02:00
onClose();
2022-02-04 16:55:57 -08:00
}
2022-08-02 00:46:16 +02:00
}, [sent, onClose]);
2022-02-04 16:55:57 -08:00
return (
2022-10-10 09:19:10 -04:00
<Modal
title={t("Submit feedback")}
isDismissable
onClose={onClose}
{...rest}
>
2022-02-04 16:55:57 -08:00
<ModalContent>
2022-10-10 09:19:10 -04:00
<Body>{t("Having trouble? Help us fix it.")}</Body>
2022-02-04 16:55:57 -08:00
<form onSubmit={onSubmitFeedback}>
<FieldRow>
<InputField
id="description"
name="description"
2022-10-10 09:19:10 -04:00
label={t("Description (optional)")}
2022-02-23 16:07:14 -08:00
type="textarea"
2022-02-04 16:55:57 -08:00
/>
</FieldRow>
<FieldRow>
<InputField
id="sendLogs"
name="sendLogs"
2022-10-10 09:19:10 -04:00
label={t("Include debug logs")}
2022-02-04 16:55:57 -08:00
type="checkbox"
defaultChecked
/>
</FieldRow>
{error && (
<FieldRow>
2022-10-10 09:19:10 -04:00
<ErrorMessage error={error} />
2022-02-04 16:55:57 -08:00
</FieldRow>
)}
<FieldRow>
<Button type="submit" disabled={sending}>
2022-10-10 09:19:10 -04:00
{sending ? t("Submitting feedback…") : t("Submit feedback")}
2022-02-04 16:55:57 -08:00
</Button>
</FieldRow>
</form>
</ModalContent>
</Modal>
);
}