File size: 6,530 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { useEffect } from "react";
import {
  Box,
  Flex,
  HStack,
  Text,
  useColorModeValue,
  VStack,
} from "@chakra-ui/react";
import { ReactionButton } from "../../utils/Buttons";
import {
  HeartIcon,
  RedHeart,
  CommentIcon,
  AuthorIcon,
} from "../../assets/icons";
import { dateFormat, showEditedDate } from "../../helper/calcTimestamp";
import { htmlToJsx } from "../../helper/htmlToJsx";
import converter from "../../helper/converter";
import CustomAvatar from "../../utils/CustomAvatar";
import { useNavigate } from "react-router-dom";
import { useState } from "react";
import DiscussionBox from "../discussion/DiscussionBox";
import useClickLikeToComment from "../../hooks/useClickLikeToComment";
import ManageComment from "./ManageComment";
import { FiCornerLeftUp } from "react-icons/fi";
import { useDispatch } from "react-redux";
import { setLoginAlert } from "../../store/loginAlert";

const CommentItem = ({
  text,
  createdAt,
  currentUserProfile,
  userId,
  postId,
  commentId,
  comments,
  likes,
  authorId,
  currentUserId,
  ps,
  footerPs,
  avatarSize,
  edited,
  editedAt,
  reply,
  repliedUserName,
}) => {
  const navigate = useNavigate();
  const dispatch = useDispatch();

  const [showDiscussionBox, setShowDiscussionbox] = useState(false);

  const { handleClickLike, updatingLike } = useClickLikeToComment(
    currentUserId,
    postId
  );

  const handleViewProfile = (username) => {
    navigate(`/${username}`);
  };

  const totalLike = likes.length;
  const alreadyLiked = likes.includes(currentUserId);

  const handleshowDiscussionBox = () => {
    if (!currentUserId) {
      dispatch(setLoginAlert(true));
      return;
    }

    setShowDiscussionbox((prev) => !prev);
  };

  // auto focus when click reply
  // give commentId for each comment item and when showDiscussionBox is true , select editor inside this specific id and focus it
  useEffect(() => {
    if (showDiscussionBox) {
      document.querySelector(`#comment${commentId} .mde-text`).focus();
    }
  }, [showDiscussionBox, commentId]);

  const reactionIconColor = useColorModeValue("#3d3d3d", "#d6d6d7");
  const ghostColor = useColorModeValue("light.ghostColor", "dark.ghostColor");
  const colorHover = useColorModeValue("light.colorHover", "dark.colorHover");
  const colorTertiary = useColorModeValue(
    "light.colorTertiary",
    "dark.colorTertiary"
  );
  const replyToColor = useColorModeValue("#8f8f8f", "dark.colorTertiary");

  return (
    <VStack mb={[".7rem", "1rem"]} ps={ps} id={`comment${commentId}`}>
      <Flex align="flex-start" w="100%">
        <CustomAvatar
          size={avatarSize}
          profile={currentUserProfile?.profile}
          onClick={() => handleViewProfile(currentUserProfile.username)}
        />

        <Box
          boxShadow={useColorModeValue(
            "0 0 0 1px rgb(23 23 23 / 13%)",
            "0 0 0 1px rgb(255 255 255 / 15%)"
          )}
          p={{ base: ".5rem .7rem", sm: ".5rem 1rem" }}
          borderRadius="5px"
          _hover={{
            ".more-icon": { fill: reactionIconColor },
            ".arrow-up": { color: reactionIconColor },
          }}
          w="100%"
          flex="1"
          ms=".5rem"
          overflow="hidden"
        >
          <Flex justify="space-between" mb={2}>
            <HStack align="center" spacing="2px">
              <Text
                fontSize="15px"
                fontWeight="900"
                cursor="pointer"
                color={ghostColor}
                _hover={{ color: colorHover }}
                onClick={() => handleViewProfile(currentUserProfile.username)}
              >
                {currentUserProfile.name}
              </Text>

              {authorId === userId && <AuthorIcon fill={reactionIconColor} />}

              {/* show Date */}
              <Text fontSize="12px" color={colorTertiary}>
                {" "}
                • {dateFormat(createdAt)}{" "}
                {edited && (
                  <Text as="span">
                    {showEditedDate(createdAt, editedAt)
                      ? `• Edited on ${dateFormat(editedAt)}`
                      : "• Edited"}
                  </Text>
                )}
              </Text>
            </HStack>

            {/* option menu */}
            {currentUserId === userId && (
              <ManageComment
                commentId={commentId}
                postId={postId}
                comments={comments}
              />
            )}
          </Flex>

          <Box
            fontSize={{ base: "14px", sm: "16px" }}
            fontFamily="monospace"
            sx={{ p: { marginBottom: "5px !important" } }}
          >
            {reply && repliedUserName !== currentUserProfile.name && (
              <Text
                as="div"
                fontSize="13px"
                color={replyToColor}
                mt="-7px !important"
                mb=".5rem !important"
                fontFamily="sans-serif"
              >
                <FiCornerLeftUp
                  className="arrow-up"
                  style={{ display: "inline-block" }}
                />{" "}
                reply to {repliedUserName}
              </Text>
            )}

            <Box className="mde-preview-content">
              {htmlToJsx(converter().makeHtml(text))}
            </Box>
          </Box>
        </Box>
      </Flex>

      <Box w="100%" ps={footerPs} mt=".3rem !important">
        {!showDiscussionBox && (
          <HStack justify="flex-start">
            <ReactionButton
              value={totalLike < 1 ? "" : totalLike}
              text={totalLike < 1 ? "" : totalLike > 1 ? "likes" : "like"}
              disabled={updatingLike}
              onClick={() => handleClickLike(comments, commentId)}
            >
              {alreadyLiked ? (
                <RedHeart />
              ) : (
                <HeartIcon fill={reactionIconColor} />
              )}
            </ReactionButton>

            <ReactionButton text="reply" onClick={handleshowDiscussionBox}>
              <CommentIcon fill={reactionIconColor} />
            </ReactionButton>
          </HStack>
        )}

        {showDiscussionBox && (
          <DiscussionBox
            postId={postId}
            showDismiss={true}
            onDismiss={handleshowDiscussionBox}
            commentId={commentId}
            repliedUserId={userId}
          />
        )}
      </Box>
    </VStack>
  );
};

export default CommentItem;