All files / app/components/Comments CommentBox.tsx

0% Statements 0/43
0% Branches 0/12
0% Functions 0/11
0% Lines 0/42

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                                                                                                                                                                                                                                                   
import { Card } from "@material-ui/core";
import Alert from "components/Alert";
import CircularProgress from "components/CircularProgress";
import NewComment from "components/Comments/NewComment";
import Markdown from "components/Markdown";
import { useTranslation } from "next-i18next";
import { Reply } from "proto/threads_pb";
import React, { useEffect, useState } from "react";
import { service } from "service";
import isGrpcError from "service/utils/isGrpcError";
import makeStyles from "utils/makeStyles";
 
const useStyles = makeStyles(() => ({
  card: {
    border: "1px solid",
    marginTop: "1em",
    padding: "1em",
  },
}));
 
interface CommentBoxProps {
  threadId: number;
}
 
// Reply with more Reply objects as children
interface MultiLevelReply extends Reply.AsObject {
  replies: Array<Reply.AsObject>;
  // page token, etc? not sure what's needed for react query
}
 
export default function CommentBox({ threadId }: CommentBoxProps) {
  const { t } = useTranslation();
  const classes = useStyles();
 
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");
 
  const [comments, setComments] = useState<Array<MultiLevelReply>>([]);
 
  useEffect(() => {
    (async () => {
      setLoading(true);
      try {
        const thread = await service.threads.getThread(threadId);
        setComments(
          await Promise.all(
            thread.repliesList.map(async (reply) => {
              return {
                ...reply,
                replies:
                  reply.numReplies > 0
                    ? (await service.threads.getThread(reply.threadId))
                        .repliesList
                    : [],
              };
            })
          )
        );
      } catch (e) {
        console.error(e);
        setError(isGrpcError(e) ? e.message : t("error.fatal_message"));
      }
      setLoading(false);
    })();
  }, [t, threadId]);
 
  const handleComment = async (threadId: number, content: string) => {
    await service.threads.postReply(threadId, content);
    setLoading(true);
    try {
      const thread = await service.threads.getThread(threadId);
      setComments(
        await Promise.all(
          thread.repliesList.map(async (reply) => {
            return {
              ...reply,
              replies:
                reply.numReplies > 0
                  ? (await service.threads.getThread(reply.threadId))
                      .repliesList
                  : [],
            };
          })
        )
      );
    } catch (e) {
      console.error(e);
      setError(isGrpcError(e) ? e.message : t("error.fatal_message"));
    }
    setLoading(false);
  };
  return (
    <>
      {error && <Alert severity="error">{error}</Alert>}
      {loading && <CircularProgress />}
      {comments.map((comment) => (
        <>
          <Card className={classes.card}>
            Comment: by user id {comment.authorUserId}, posted at{" "}
            {comment.createdTime!.seconds}, {comment.numReplies} replies.
            <Markdown source={comment.content} />
            Replies:
            {comment.replies.map((reply) => (
              <>
                <Card className={classes.card}>
                  Reply: by user id {reply.authorUserId}, posted at{" "}
                  {reply.createdTime!.seconds}.
                  <Markdown source={reply.content} />
                </Card>
              </>
            ))}
            <NewComment
              onComment={(content) => handleComment(comment.threadId, content)}
            />
          </Card>
        </>
      ))}
      <NewComment onComment={(content) => handleComment(threadId, content)} />
    </>
  );
}