Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

fix: read more based on height now content

bautistaaa73541d7125ae
diff --git a/src/components/challenge/comments/comment-input.tsx b/src/components/challenge/comments/comment-input.tsx
index 71b3203d..8c5ab648 100644
--- a/src/components/challenge/comments/comment-input.tsx
+++ b/src/components/challenge/comments/comment-input.tsx
@@ -80,6 +80,7 @@ export function CommentInput({ mode, onCancel, onChange, onKeyDown, value, onSub
console.error(e);
} finally {
setIsSubmitting(false);
+ setCommentMode('editor');
}
}}
className="h-8 w-[5.5rem] rounded-lg rounded-br-sm bg-emerald-600 hover:bg-emerald-500 dark:bg-emerald-400 dark:hover:bg-emerald-300"
diff --git a/src/components/challenge/comments/comment.tsx b/src/components/challenge/comments/comment.tsx
index a11efb86..6422d02c 100644
--- a/src/components/challenge/comments/comment.tsx
+++ b/src/components/challenge/comments/comment.tsx
@@ -3,7 +3,7 @@
import clsx from 'clsx';
import { Pencil, Reply, Share, Trash2 } from 'lucide-react';
import { useSession } from 'next-auth/react';
-import { useEffect, useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
import { z } from 'zod';
import type { ChallengeRouteData } from '~/app/challenge/[id]/getChallengeRouteData';
import ReportDialog from '~/components/report';
@@ -43,16 +43,10 @@ const commentReportSchema = z
export type CommentReportSchemaType = z.infer<typeof commentReportSchema>;
-const Comment = ({ comment, readonly = false }: CommentProps) => {
+export const Comment = ({ comment, readonly = false }: CommentProps) => {
const queryClient = useQueryClient();
const [text, setText] = useState(comment.text);
const [isEditing, setIsEditing] = useState(false);
- const isTooManyLines = comment.text.split('\n').length > 15;
- const [showReadMore, setShowReadMore] = useState(isTooManyLines); // take some default value from the calculation of characters
-
- useEffect(() => {
- setShowReadMore(isTooManyLines);
- }, [isTooManyLines]);
async function copyPathNotifyUser() {
try {
@@ -173,26 +167,7 @@ const Comment = ({ comment, readonly = false }: CommentProps) => {
</div>
</div>
<div>
- {!isEditing && (
- <p
- className={clsx(
- { 'h-full': !showReadMore, 'max-h-[300px]': showReadMore },
- 'relative w-full overflow-hidden break-words pl-[1px] text-sm',
- )}
- >
- <Markdown>{comment.text}</Markdown>
- {showReadMore && (
- <div
- className="absolute top-0 flex h-full w-full cursor-pointer items-end bg-gradient-to-b from-transparent to-white dark:to-zinc-800"
- onClick={() => setShowReadMore(false)}
- >
- <div className="text-md text-label-1 dark:text-dark-label-1 flex w-full items-center justify-center hover:bg-transparent">
- Read more
- </div>
- </div>
- )}
- </p>
- )}
+ {!isEditing && <ExpandableContent content={comment.text} />}
{isEditing && (
<div className="my-2">
<CommentInput
@@ -215,4 +190,44 @@ const Comment = ({ comment, readonly = false }: CommentProps) => {
);
};
-export default Comment;
+const ExpandableContent = ({ content }: { content: string }) => {
+ const [expanded, setExpanded] = useState(true);
+ const contentWrapperRef = useRef<HTMLDivElement>(null);
+
+ useEffect(() => {
+ const handleResize = () => {
+ if ((contentWrapperRef.current?.clientHeight ?? 0) > 300) {
+ setExpanded(false);
+ }
+ };
+
+ window.addEventListener('resize', handleResize);
+ handleResize();
+
+ return () => {
+ window.removeEventListener('resize', handleResize);
+ };
+ }, [content]);
+
+ return (
+ <div
+ className={clsx(
+ { 'h-full': expanded, 'max-h-[300px]': !expanded },
+ 'relative w-full overflow-hidden break-words pl-[1px] text-sm',
+ )}
+ ref={contentWrapperRef}
+ >
+ <Markdown>{content}</Markdown>
+ {!expanded && (
+ <div
+ className="absolute top-0 flex h-full w-full cursor-pointer items-end bg-gradient-to-b from-transparent to-white dark:to-zinc-800"
+ onClick={() => setExpanded(true)}
+ >
+ <div className="text-md text-label-1 dark:text-dark-label-1 flex w-full items-center justify-center hover:bg-transparent">
+ Read more
+ </div>
+ </div>
+ )}
+ </div>
+ );
+};
diff --git a/src/components/challenge/comments/index.tsx b/src/components/challenge/comments/index.tsx
index 681b8882..639d4d70 100644
--- a/src/components/challenge/comments/index.tsx
+++ b/src/components/challenge/comments/index.tsx
@@ -6,7 +6,7 @@ import clsx from 'clsx';
import { ChevronDown, ChevronLeft, ChevronRight, MessageCircle } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { useRef, useState } from 'react';
-import Comment from '~/components/challenge/comments/comment';
+import { Comment } from '~/components/challenge/comments/comment';
import { Button } from '~/components/ui/button';
import { toast } from '~/components/ui/use-toast';
import { CommentInput } from './comment-input';
diff --git a/src/components/landing/community/community.tsx b/src/components/landing/community/community.tsx
index 52102807..46b85899 100644
--- a/src/components/landing/community/community.tsx
+++ b/src/components/landing/community/community.tsx
@@ -1,6 +1,3 @@
-// eslint-disable-next-line no-restricted-imports
-// import { Octokit } from 'octokit';
-
// eslint-disable-next-line no-restricted-imports
import { contributors } from '../../../../public/contributors';
@@ -11,34 +8,6 @@ import { clsx } from 'clsx';
import { type CSSProperties } from 'react';
const Community = async function () {
- // async function getOcto() {
- // if (!process.env.PROD) {
- // console.log('You are not in production mode. Mocking data.');
- // return Array(13).fill({
- // login: 'PickleNik',
- // avatar_url: 'https://avatars.githubusercontent.com/u/31113245?v=4',
- // html_url: 'https://github.com/PickleNik',
- // });
- // }
- // if (!process.env.GITHUB_TOKEN) {
- // console.error('No GitHub token provided. Please set GITHUB_TOKEN env var.');
- // return [];
- // }
- //
- // // TODO: hacksore already thinking about making the Graphql api work here
- // const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
- // const response = await octokit.request('GET /repos/{owner}/{repo}/contributors', {
- // owner: 'bautistaa',
- // repo: 'typehero',
- // headers: {
- // 'X-GitHub-Api-Version': '2022-11-28',
- // },
- // });
- // // put all avatar urls in a list
- // return response.data.map((contributor) => contributor);
- // }
- // const avatars = await getOcto();
- //
type WrapperStyle = CSSProperties & {
'--bottom': string;
};
diff --git a/src/components/report/types/comment.report.tsx b/src/components/report/types/comment.report.tsx
index 19ac9d46..aefa9862 100644
--- a/src/components/report/types/comment.report.tsx
+++ b/src/components/report/types/comment.report.tsx
@@ -1,6 +1,6 @@
import Link from 'next/link';
import { type ReportWithInfo } from '~/app/admin/report/[id]/report.action';
-import Comment from '~/components/challenge/comments/comment';
+import { Comment } from '~/components/challenge/comments/comment';
export default function CommentReport({ report }: { report: NonNullable<ReportWithInfo> }) {
if (report.type !== 'COMMENT' || report.commentId === null || !report.comment) return null;
diff --git a/src/components/report/types/user.report.tsx b/src/components/report/types/user.report.tsx
index 6469d58c..a312ed64 100644
--- a/src/components/report/types/user.report.tsx
+++ b/src/components/report/types/user.report.tsx
@@ -1,7 +1,7 @@
'use server';
import { type ReportWithInfo } from '~/app/admin/report/[id]/report.action';
-import Comment from '~/components/challenge/comments/comment';
+import { Comment } from '~/components/challenge/comments/comment';
import { Expandable } from '~/components/ui/expandable';
import { Markdown } from '~/components/ui/markdown';
import Text from '~/components/ui/typography/typography';
diff --git a/src/components/ui/markdown.tsx b/src/components/ui/markdown.tsx
index bdbc9dce..e9dcdb05 100644
--- a/src/components/ui/markdown.tsx
+++ b/src/components/ui/markdown.tsx
@@ -46,6 +46,9 @@ export function Markdown({ children, className }: { children: string; className?
className={className}
remarkPlugins={[removeHtmlComments, remarkGfm]}
components={{
+ a: ({ className, ...props }) => (
+ <a className={clsx(className, 'text-blue-500')} {...props} />
+ ),
ul: ({ className, ...props }) => (
<ul className={clsx(className, 'list-disc ps-10')} {...props} />
),