|
| 1 | +import React from 'react'; |
| 2 | +import { formatRelative } from "date-fns"; |
| 3 | +import { |
| 4 | + FiSend, |
| 5 | + } from "react-icons/fi"; |
| 6 | + |
| 7 | +function Reply(props) { |
| 8 | + const {email, name, photoURL, reply, time} = props.reply; |
| 9 | + return ( |
| 10 | + <div className="flex items-center duration-200 mt-2"> |
| 11 | + <img |
| 12 | + src={ |
| 13 | + photoURL |
| 14 | + ? photoURL |
| 15 | + : `https://unavatar.vercel.app/${email}` |
| 16 | + } |
| 17 | + alt={email} |
| 18 | + className="h-[30px] w-[30px] rounded-md pixelated white-light-shadow" |
| 19 | + /> |
| 20 | + <div className="ml-2"> |
| 21 | + <h2 className="font-semibold text-xs text-[#222] dark:text-[#fafafa]"> |
| 22 | + {reply} |
| 23 | + </h2> |
| 24 | + <h4 className="text-xs font-semibold text-[#666] capitalize dark:text-[#aaa]"> |
| 25 | + {formatRelative(time, new Date())} •{" "} |
| 26 | + {name && name} |
| 27 | + </h4> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +function Replies({toggleReplies, setToggleReplies, replies}) { |
| 34 | + return ( |
| 35 | + <div className="rounded-md border border-[#ccc] dark:bg-[#2f2f2f] dark:border-[#555] hover:bg-white p-2 mt-2"> |
| 36 | + <div className="flex items-center justify-between w-full"> |
| 37 | + <h1 className="font-semibold text-sm lg:text-base xl:text-lg text-[#555] dark:text-[#ccc]"> |
| 38 | + Replies ({replies && replies.length}) |
| 39 | + </h1> |
| 40 | + <span className="text-xs text-[#3d5eff] font-semibold continuous-line cursor-pointer" onClick={() => setToggleReplies(!toggleReplies)}> |
| 41 | + {!toggleReplies && <>Show</>} |
| 42 | + {toggleReplies && <>Hide</>} |
| 43 | + </span> |
| 44 | + </div> |
| 45 | + {toggleReplies && replies.map((reply, index) => ( |
| 46 | + <Reply key={`reply-${index}`} reply={reply} /> |
| 47 | + ))} |
| 48 | + </div> |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +function ReplyButton({toggleReplyBox, setToggleReplyBox}) { |
| 53 | + return ( |
| 54 | + <span className="text-xs text-[#3d5eff] font-semibold continuous-line cursor-pointer" onClick={() => setToggleReplyBox(!toggleReplyBox)}> |
| 55 | + {toggleReplyBox && <>Close Reply</>} |
| 56 | + {!toggleReplyBox && <>Reply</>} |
| 57 | + </span> |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +function ReplyBox({id, toggleReplyBox, user, comments, comment, review, index, fetchAgain, setFetchAgain, setOpen}) { |
| 62 | + const [reply, setReply] = React.useState(''); |
| 63 | + const sendReply = () => { |
| 64 | + const url = `/api/POST/${review ? `comment-review-cheatsheet` : `comment-cheatsheet`}`; |
| 65 | + if(user?.email){ |
| 66 | + if(reply.trim()) { |
| 67 | + const temp = [...comments]; |
| 68 | + temp[index].replies = comments[index].replies ? [...comments[index].replies] : []; |
| 69 | + temp[index].replies.unshift({ |
| 70 | + name: user.displayName ? user.displayName : "", |
| 71 | + photoURL: user.photoURL ? user.photoURL : "", |
| 72 | + reply: reply, |
| 73 | + email: user.email, |
| 74 | + time: new Date().getTime(), |
| 75 | + }); |
| 76 | + fetch(url, { |
| 77 | + method: "POST", |
| 78 | + body: JSON.stringify({ |
| 79 | + id: id, |
| 80 | + comments: [...temp], |
| 81 | + }), |
| 82 | + }); |
| 83 | + |
| 84 | + setFetchAgain(fetchAgain + 1); |
| 85 | + setReply(''); |
| 86 | + } |
| 87 | + } else setOpen(true); |
| 88 | + } |
| 89 | + return ( |
| 90 | + <> |
| 91 | + {toggleReplyBox && <div className="reply-box"> |
| 92 | + <div className="flex items-center justify-between rounded-md border border-[#3d5eff] dark:bg-[#1F1F1F] mt-2 p-1"> |
| 93 | + <input |
| 94 | + type="text" |
| 95 | + value={reply} |
| 96 | + onChange={e => setReply(e.target.value)} |
| 97 | + placeholder={`Replying to ${comment.name}...`} |
| 98 | + className="h-full py-1 pl-2 w-full bg-transparent" |
| 99 | + onKeyDown={e => {if(e.keyCode === 13) {sendReply()}}} |
| 100 | + /> |
| 101 | + <div |
| 102 | + className="bg-[#3d5eff] p-3 pr-4 cursor-pointer shine rounded-lg" |
| 103 | + onClick={sendReply} |
| 104 | + > |
| 105 | + <FiSend |
| 106 | + className="text-white" |
| 107 | + style={{ transform: "rotate(45deg)" }} |
| 108 | + /> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div>} |
| 112 | + </> |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +export {Reply, Replies, ReplyButton, ReplyBox}; |
0 commit comments