|
| 1 | +import React, {useCallback, useRef, useState} from "react"; |
| 2 | +import {createPortal} from "react-dom"; |
| 3 | +import {Checkbox} from "@surfnet/sds"; |
| 4 | +import {useLocation} from "react-router-dom"; |
| 5 | +import DOMPurify from "dompurify"; |
| 6 | +import html2canvas from "html2canvas"; |
| 7 | +import I18n from "../locale/I18n.js"; |
| 8 | +import {sendFeedback} from "../api/index.js"; |
| 9 | +import {useAppStore} from "../stores/AppStore.js"; |
| 10 | +import ConfirmationDialog from "./ConfirmationDialog.jsx"; |
| 11 | +import "./UserFeedbackWidget.scss"; |
| 12 | + |
| 13 | +const MAX_SCREENSHOT_BYTES = 5 * 1024 * 1024; |
| 14 | + |
| 15 | +export const UserFeedbackWidget = () => { |
| 16 | + const location = useLocation(); |
| 17 | + const setFlash = useAppStore(state => state.setFlash); |
| 18 | + const [open, setOpen] = useState(false); |
| 19 | + const [message, setMessage] = useState(""); |
| 20 | + const [includeScreenshot, setIncludeScreenshot] = useState(true); |
| 21 | + const [submitting, setSubmitting] = useState(false); |
| 22 | + const inputRef = useRef(null); |
| 23 | + |
| 24 | + const closeModal = () => { |
| 25 | + setOpen(false); |
| 26 | + setMessage(""); |
| 27 | + setIncludeScreenshot(true); |
| 28 | + }; |
| 29 | + |
| 30 | + const captureScreenshot = useCallback(async () => { |
| 31 | + document.body.classList.add("feedback-capture"); |
| 32 | + try { |
| 33 | + const canvas = await html2canvas(document.body, { |
| 34 | + backgroundColor: null, |
| 35 | + useCORS: true, |
| 36 | + scale: 1, |
| 37 | + windowWidth: document.documentElement.clientWidth, |
| 38 | + windowHeight: document.documentElement.clientHeight |
| 39 | + }); |
| 40 | + return canvas.toDataURL("image/png"); |
| 41 | + } finally { |
| 42 | + document.body.classList.remove("feedback-capture"); |
| 43 | + } |
| 44 | + }, []); |
| 45 | + |
| 46 | + const handleSubmit = useCallback(async () => { |
| 47 | + if (!message.trim()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + setSubmitting(true); |
| 51 | + try { |
| 52 | + const payload = { |
| 53 | + message, |
| 54 | + url: `${window.location.origin}${location.pathname}${location.search}${location.hash}`, |
| 55 | + includeScreenshot |
| 56 | + }; |
| 57 | + |
| 58 | + if (includeScreenshot) { |
| 59 | + const dataUrl = await captureScreenshot(); |
| 60 | + const base64 = dataUrl.split(",")[1] || ""; |
| 61 | + const estimatedBytes = Math.ceil((base64.length * 3) / 4); |
| 62 | + if (estimatedBytes > MAX_SCREENSHOT_BYTES) { |
| 63 | + setFlash(I18n.t("feedback.tooLarge")); |
| 64 | + } else if (base64.length > 0) { |
| 65 | + payload.screenshotBase64 = base64; |
| 66 | + payload.screenshotContentType = "image/png"; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + await sendFeedback(payload); |
| 71 | + setFlash(I18n.t("feedback.flash")); |
| 72 | + closeModal(); |
| 73 | + } catch (error) { |
| 74 | + setFlash(I18n.t("forms.error")); |
| 75 | + } finally { |
| 76 | + setSubmitting(false); |
| 77 | + } |
| 78 | + }, [captureScreenshot, includeScreenshot, location.hash, location.pathname, location.search, message, setFlash]); |
| 79 | + |
| 80 | + |
| 81 | + const renderContent = () => ( |
| 82 | + <div className="user-feedback-widget__modal"> |
| 83 | + <p>{I18n.t("feedback.info")}</p> |
| 84 | + <div className="sds--text-area"> |
| 85 | + <textarea |
| 86 | + name="feedback" |
| 87 | + id="feedback" |
| 88 | + value={message} |
| 89 | + rows="6" |
| 90 | + ref={inputRef} |
| 91 | + onChange={e => setMessage(e.target.value)} |
| 92 | + /> |
| 93 | + </div> |
| 94 | + <label className="user-feedback-widget__options"> |
| 95 | + <Checkbox |
| 96 | + value={includeScreenshot} |
| 97 | + onChange={() => setIncludeScreenshot(!includeScreenshot)} |
| 98 | + /> |
| 99 | + <span>{I18n.t("feedback.includeScreenshot")}</span> |
| 100 | + </label> |
| 101 | + <section className="disclaimer"> |
| 102 | + <span |
| 103 | + dangerouslySetInnerHTML={{ |
| 104 | + __html: DOMPurify.sanitize(I18n.t("feedback.disclaimer"), { |
| 105 | + ADD_ATTR: ["target", "rel"] |
| 106 | + }) |
| 107 | + }} |
| 108 | + /> |
| 109 | + </section> |
| 110 | + <section className="help"> |
| 111 | + <h3 |
| 112 | + className="title" |
| 113 | + dangerouslySetInnerHTML={{ |
| 114 | + __html: DOMPurify.sanitize(I18n.t("feedback.help")) |
| 115 | + }} |
| 116 | + /> |
| 117 | + <span |
| 118 | + dangerouslySetInnerHTML={{ |
| 119 | + __html: DOMPurify.sanitize(I18n.t("feedback.helpInfo")) |
| 120 | + }} |
| 121 | + /> |
| 122 | + </section> |
| 123 | + </div> |
| 124 | + ); |
| 125 | + |
| 126 | + return ( |
| 127 | + <div className="user-feedback-widget"> |
| 128 | + <button |
| 129 | + className="user-feedback-widget__trigger" |
| 130 | + onClick={() => { |
| 131 | + setOpen(true); |
| 132 | + setTimeout(() => inputRef.current?.focus(), 0); |
| 133 | + }} |
| 134 | + type="button" |
| 135 | + > |
| 136 | + {I18n.t("feedback.widgetLabel")} |
| 137 | + </button> |
| 138 | + {open && createPortal( |
| 139 | + <ConfirmationDialog |
| 140 | + cancel={closeModal} |
| 141 | + confirm={handleSubmit} |
| 142 | + confirmationHeader={I18n.t("feedback.title")} |
| 143 | + confirmationTxt={I18n.t("forms.submit")} |
| 144 | + disabledConfirm={submitting || !message.trim()} |
| 145 | + children={renderContent()} |
| 146 | + largeWidth={true} |
| 147 | + />, |
| 148 | + document.body |
| 149 | + )} |
| 150 | + </div> |
| 151 | + ); |
| 152 | +}; |
0 commit comments