Skip to content

Commit

Permalink
fix(client): post content truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 18, 2024
1 parent 56936f2 commit c43ea9c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions client/components/app/Post/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@ import MarkdownContent from '@/components/app/Markdown';
export default function Post({ post: initialPost, onDelete, onNewComment }) {
const [post, setPost] = useState(initialPost);
const isPostPage = usePathname().includes('/app/posts/');

const [isExpanded, setIsExpanded] = useState(false);

const lines = post.content.split('\n');
const isLong = lines.length > 5 || post.content.length > 200;
const content =
isLong && !isPostPage
? isExpanded
? post.content
: lines.slice(0, 5).join('\n')
const contentLines = post.content.split('\n');
const contentLength = post.content.length;
const lineCount = contentLines.length;

const isLong = lineCount > 5 || contentLength > 500;

const truncatedContent =
lineCount > 5
? contentLines.slice(0, 5).join('\n') + '...'
: contentLength > 500
? post.content.substring(0, 500) + '...'
: post.content;

const contentToShow =
isExpanded || isPostPage ? post.content : truncatedContent;

return (
<div className='p-5 bg-zinc-900 rounded-lg'>
<div className='flex items-center justify-between mb-3'>
<UserInfo user={post.author} />
<Dropdown post={post} setPost={setPost} onDelete={onDelete} />
</div>
<div className='relative'>
<MarkdownContent content={content} />
<MarkdownContent content={contentToShow} />
{!isPostPage && isLong && (
<div
onClick={() => setIsExpanded(!isExpanded)}
Expand Down

0 comments on commit c43ea9c

Please sign in to comment.