File size: 5,997 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 |
import React from "react";
import {
Box,
Divider,
Flex,
Heading,
HStack,
Image,
Text,
useColorModeValue,
Wrap,
WrapItem,
} from "@chakra-ui/react";
import CustomAvatar from "../../utils/CustomAvatar";
import ManangePost from "../post/ManangePost";
import { nanoid } from "@reduxjs/toolkit";
import LangTag from "../../utils/LangTag";
import { htmlToJsx } from "../../helper/htmlToJsx";
import converter from "../../helper/converter";
import Discussion from "../discussion/Discussion";
import { useNavigate } from "react-router-dom";
import { useAuth } from "../../context/auth";
import AllComment from "../comment/AllComment";
import { dateFormat, showEditedDate } from "../../helper/calcTimestamp";
import { useRef } from "react";
import { useEffect } from "react";
import { useSelector } from "react-redux";
import useClickTag from "../../hooks/useClickTag";
import hljs from "highlight.js";
const MainContent = ({ postDetail }) => {
const navigate = useNavigate();
const user = useAuth();
const discussionBoxRef = useRef();
const { clickComment } = useSelector((state) => state.scrollDiscussion);
const handleClickTag = useClickTag();
// scroll to
useEffect(() => {
const scrollHeight =
window.pageYOffset +
discussionBoxRef.current?.getBoundingClientRect().top -
60;
if (clickComment) {
setTimeout(() => window.scrollTo({ top: scrollHeight }), 0);
} else {
window.scrollTo(0, 0);
}
}, [clickComment, postDetail.id]);
// syntax highlighting
useEffect(() => {
hljs.highlightAll();
}, [postDetail.id]);
const isAuthor = user?.userId === postDetail?.userId;
const dividerColor = useColorModeValue("light.cardBorder", "dark.cardBorder");
const headingHover = useColorModeValue(
"light.headingHover",
"dark.headingHover"
);
const ghostColor = useColorModeValue("light.ghostColor", "dark.ghostColor");
const colorTertiary = useColorModeValue(
"light.colorTertiary",
"dark.colorTertiary"
);
return (
<Box
m={{ base: "0", md: "1px" }}
className="shadow"
borderRadius={{ base: "0", md: "5px" }}
bg={useColorModeValue("light.cardBg", "dark.cardBg")}
>
{/* coverImgae */}
{postDetail.cvImg && (
<Image
src={postDetail.cvImg}
alt="cover_image"
maxH="335px"
width="100%"
borderTopLeftRadius={{ base: "none", md: "5px" }}
borderTopRightRadius={{ base: "none", md: "5px" }}
objectFit="cover"
/>
)}
{/* content */}
<Box px={{ base: ".7rem", md: "2.5rem" }} pb="1rem" pt={3}>
<Box className="mde-preview">
<Flex align="center" justify="space-between" gap=".5rem">
<HStack align="flex-start">
<CustomAvatar
profile={postDetail.profile}
size="40px"
onClick={() => navigate(`/${postDetail.username}`)}
/>
<Box flex="1" pt="3px">
<Text
fontWeight={600}
cursor="pointer"
lineHeight={1.25}
color={ghostColor}
_hover={{ color: headingHover }}
onClick={() => navigate(`/${postDetail.username}`)}
>
{postDetail.name}
</Text>
{postDetail.draft && (
<Text
bg="#FCD34D"
color="dark.cardColor"
px="5px"
fontSize="12px"
rounded="sm"
display="inline-block"
>
Draft
</Text>
)}
{postDetail.createdAt && (
<Text fontSize="12px" color={colorTertiary}>
Posted on {dateFormat(postDetail.createdAt)}{" "}
{postDetail.updatedAt && (
<Text as="span">
{showEditedDate(
postDetail.createdAt,
postDetail.updatedAt
)
? `• Updated on ${dateFormat(postDetail.updatedAt)}`
: "• Updated"}
</Text>
)}
</Text>
)}
</Box>
</HStack>
{/* manage post */}
{isAuthor && postDetail && (
<ManangePost postId={postDetail.id} m="0 0 0 auto" />
)}
</Flex>
<Heading my={2}>{postDetail.title}</Heading>
{postDetail.tags.length > 0 && (
<Wrap pt=".3rem" pb="1.5rem" spacing=".3rem">
{postDetail.tags.map((tag) => (
<WrapItem
key={nanoid()}
onClick={(e) =>
postDetail.draft
? () => {} // don't allow view tag if it's a draft
: handleClickTag(e, tag.tagName)
}
>
<LangTag tag={tag} />
</WrapItem>
))}
</Wrap>
)}
<Box
className="mde-preview-content"
fontSize={["16px", "17px", "19px"]}
fontFamily="monospace"
>
{htmlToJsx(converter().makeHtml(postDetail.MDEValue))}
</Box>
{!postDetail.draft && (
<Divider mt={7} h="1px" bg={dividerColor} mx="auto" />
)}
{!postDetail.draft && (
<Discussion
discussionBoxRef={discussionBoxRef}
postId={postDetail.id}
comments={postDetail.comments}
/>
)}
{postDetail.comments.length !== 0 && (
<AllComment postDetail={postDetail} />
)}
</Box>
</Box>
</Box>
);
};
export default MainContent;
|