import React, { useEffect, useState } from "react"; import { Close, Comment as CommentIcon } from "@mui/icons-material"; import { Box, Button, CircularProgress, DialogTitle, IconButton, Snackbar, } from "@mui/material"; import ReCAPTCHA from "react-google-recaptcha"; import { api } from "../lib/api"; import Comment from "./conversation/comment"; import { useEditor, useFinalPage, useThread, useThreadId, } from "./conversation/ConversationContext"; import { useUpdate } from "./conversation/functions/update"; import TextEditor from "./textEditor"; import { useIsSmallScreen, useNotification, useReCaptchaSiteKey, } from "./ContextProvider"; import useChangePage from "./conversation/functions/changePage"; import { roundup } from "../lib/common"; import { parseError } from "../lib/parseError"; declare const grecaptcha: { reset: () => void }; export default function FloatingEditor() { const threadId = useThreadId(); const [editor, setEditor] = useEditor(); const [comment, setComment] = useState(""); const [rtoken, setRtoken] = useState(""); const [creating, setCreating] = useState(false); const [fold, setFold] = useState(false); const [, setNotification] = useNotification(); const [thread] = useThread(); const isSmallScreen = useIsSmallScreen(); const update = useUpdate(); const changePage = useChangePage(); const [finalPage] = useFinalPage(); const [shouldUpdate, setShouldUpdate] = useState(false); const [newCommentId, setNewCommentId] = useState(0); const reCaptchaSiteKey = useReCaptchaSiteKey(); useEffect(() => { if (shouldUpdate && newCommentId) { setShouldUpdate(false); setNewCommentId(0); update({ scrollToComment: newCommentId }); } }, [newCommentId, shouldUpdate, update]); function clearState() { setComment(""); setRtoken(""); setCreating(false); setFold(false); } const handleClose = () => { setEditor({ ...editor, open: false }); clearState(); }; function CreateComment() { setCreating(true); api.commentCreate(threadId, { comment, quote: editor.quote?.id, rtoken, }) .then((data) => { setNewCommentId(data.id); const numOfPages = roundup((data.id || 0) / 25); setEditor({ ...editor, open: false }); if (numOfPages !== finalPage) changePage(numOfPages, () => { setShouldUpdate(true); }); else update({ scrollToComment: data.id }); setCreating(false); clearState(); }) .catch((err) => { setNotification({ open: true, text: parseError(err), }); setCreating(false); grecaptcha.reset(); }); } return (

{editor.quote ? "Reply" : "Comment"}

{ setFold(!fold); }} > {fold ? "Expand" : "Fold"}

{editor.quote && ( div": { borderRadius: 2 } }} maxHeight={200} /> )} { setComment(e); }} initText={ editor.edit && /*html*/ `
${editor.edit}

` } autoresize noMenuBar noStatusBar toolbarBottom className="ml10 mr10" /> { setRtoken(token || ""); }} /> {creating ? ( ) : ( )}
); }