Connect, collaborate, and grow with a community of TypeScript developers
feat: Add tab for challenge comments (#142)
Ben Asokanthan0f1092bb3394
diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 8a7aff15..68b16b93 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -171,6 +171,7 @@ model Solution {model ChallengeComment {id Int @id @default(autoincrement())+ createdAt DateTime @default(now())user User @relation(fields: [userId], references: [id])userId Stringchallenge Challenge @relation(fields: [challengeId], references: [id])@@ -184,6 +185,7 @@ model ChallengeComment {model ChallengeReply {id Int @id @default(autoincrement())+ createdAt DateTime @default(now())user User @relation(fields: [userId], references: [id])userId Stringcomment ChallengeComment @relation(fields: [commentId], references: [id])@@ -196,6 +198,7 @@ model ChallengeReply {model SolutionComment {id Int @id @default(autoincrement())+ createdAt DateTime @default(now())user User @relation(fields: [userId], references: [id])userId Stringsolution SharedSolution @relation(fields: [solutionId], references: [id])@@ -209,6 +212,7 @@ model SolutionComment {model SolutionReply {id Int @id @default(autoincrement())+ createdAt DateTime @default(now())user User @relation(fields: [userId], references: [id])userId Stringsolution SolutionComment @relation(fields: [solutionId], references: [id])diff --git a/src/components/challenge/comments.tsx b/src/components/challenge/comments.tsxnew file mode 100644index 00000000..f3b47482--- /dev/null+++ b/src/components/challenge/comments.tsx@@ -0,0 +1,27 @@+import { Button } from '~/components/ui/button';+import Comment from '~/components/ui/comment';+import type { Challenge } from '.';++interface Props {+ challenge: NonNullable<Challenge>;+}++const Comments = ({ challenge }: Props) => {+ return (+ <div className="flex h-full w-full flex-col py-4">+ <div className="flex-1 flex-grow">+ {challenge.comment.map((comment) => (+ <Comment+ key={comment.id}+ userId={comment.user.name ?? ''}+ text={comment.text}+ createdAt={comment.createdAt}+ />+ ))}+ </div>+ <Button className="sticky bottom-4 right-4 max-w-fit self-end">Comment</Button>+ </div>+ );+};++export default Comments;diff --git a/src/components/challenge/description-panel-skeleton.tsx b/src/components/challenge/description-panel-skeleton.tsxindex dd564992..6f4fb443 100644--- a/src/components/challenge/description-panel-skeleton.tsx+++ b/src/components/challenge/description-panel-skeleton.tsx@@ -4,7 +4,7 @@ export function DescriptionPanelSkeleton() {return (<><Tabs defaultValue="description" className="w-full">- <TabsList className="sticky top-0 grid h-auto w-full grid-cols-3 rounded-none border-b border-zinc-300 bg-background dark:border-zinc-700 dark:bg-muted">+ <TabsList className="sticky top-0 grid h-auto w-full grid-cols-4 rounded-none border-b border-zinc-300 bg-background dark:border-zinc-700 dark:bg-muted"><TabsTriggervalue="description"className="rounded-md rounded-tl-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"@@ -23,6 +23,12 @@ export function DescriptionPanelSkeleton() {>Submissions</TabsTrigger>+ <TabsTrigger+ value="comments"+ className="rounded-md rounded-tr-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"+ >+ Comments+ </TabsTrigger></TabsList><TabsContent value="description" className="mt-0 dark:bg-muted"><div className="h-full p-5">@@ -90,6 +96,25 @@ export function DescriptionPanelSkeleton() {</div></div></TabsContent>+ <TabsContent className="flex h-full flex-col space-y-2 p-5" value="comments">+ <div className="flex flex-col space-y-10">+ <div className="prose-invert flex flex-col space-y-2 prose-h3:text-xl">+ <div className="h-4 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ <div className="h-4 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ <div className="h-4 w-1/2 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ </div>+ <div className="prose-invert flex flex-col space-y-2 prose-h3:text-xl">+ <div className="h-4 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ <div className="h-4 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ <div className="h-4 w-1/2 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ </div>+ <div className="prose-invert flex flex-col space-y-2 prose-h3:text-xl">+ <div className="h-4 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ <div className="h-4 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ <div className="h-4 w-1/2 rounded-lg bg-zinc-300 dark:bg-zinc-700"></div>+ </div>+ </div>+ </TabsContent></Tabs></>);diff --git a/src/components/challenge/description-panel.tsx b/src/components/challenge/description-panel.tsxindex 7578d45b..cedfd766 100644--- a/src/components/challenge/description-panel.tsx+++ b/src/components/challenge/description-panel.tsx@@ -30,6 +30,7 @@ import { Textarea } from '../ui/textarea';import { TypographyLarge } from '../ui/typography/large';import { toast } from '../ui/use-toast';import { addOrRemoveBookmark } from './bookmark.action';+import Comments from './comments';import { incrementOrDecrementUpvote } from './increment.action';import { addReport } from './report.action';import { ShareForm } from './share-form';@@ -125,7 +126,7 @@ export function DescriptionPanel({ challenge }: Props) {return (<><Tabs defaultValue="description" className="flex h-full w-full flex-col">- <TabsList className="sticky top-0 z-10 grid h-auto w-full grid-cols-3 rounded-none border-b border-zinc-300 bg-background/90 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90">+ <TabsList className="sticky top-0 z-10 grid h-auto w-full grid-cols-4 rounded-none border-b border-zinc-300 bg-background/90 backdrop-blur-sm dark:border-zinc-700 dark:bg-muted/90"><TabsTriggervalue="description"className="rounded-md rounded-tl-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"@@ -144,6 +145,12 @@ export function DescriptionPanel({ challenge }: Props) {>Submissions</TabsTrigger>+ <TabsTrigger+ value="comments"+ className="rounded-md rounded-tr-lg duration-300 data-[state=active]:bg-neutral-200 dark:data-[state=active]:bg-neutral-700"+ >+ Comments+ </TabsTrigger></TabsList><TabsContent value="description" className="mt-0 flex-1 dark:bg-muted"><div className="h-full px-4 py-3">@@ -303,6 +310,9 @@ export function DescriptionPanel({ challenge }: Props) {<DialogContent className="w-[200px]"><DialogHeader><DialogTitle>Share this challenge</DialogTitle>+ <div className="pt-4">+ <ShareForm />+ </div></DialogHeader><div className="pt-4"><ShareForm />@@ -379,6 +389,9 @@ export function DescriptionPanel({ challenge }: Props) {<TabsContent value="submissions" className="mt-0 flex-1 dark:bg-muted"><Submissions challenge={challenge} /></TabsContent>+ <TabsContent value="comments" className="mt-0 h-full dark:bg-muted">+ <Comments challenge={challenge} />+ </TabsContent></Tabs></>);diff --git a/src/components/challenge/index.tsx b/src/components/challenge/index.tsxindex 566cefd5..702c4b5f 100644--- a/src/components/challenge/index.tsx+++ b/src/components/challenge/index.tsx@@ -70,6 +70,17 @@ async function getChallenge(id: string, session: Session | null) {},},},+ comment: {+ take: 10,+ orderBy: [+ {+ createdAt: 'desc',+ },+ ],+ include: {+ user: true,+ },+ },},});diff --git a/src/components/ui/comment.tsx b/src/components/ui/comment.tsxnew file mode 100644index 00000000..f2b52977--- /dev/null+++ b/src/components/ui/comment.tsx@@ -0,0 +1,23 @@+import { getRelativeTime } from '~/utils/relativeTime';++type Props = {+ userId: string;+ text: string;+ createdAt: Date;+};++const Comment = ({ userId, text, createdAt }: Props) => {+ return (+ <div className="flex cursor-pointer flex-col gap-2 p-4 duration-300 hover:bg-neutral-100 dark:rounded-none dark:hover:bg-neutral-700">+ <p>{text}</p>+ <div className="space-x-2">+ <span className="max-w-fit rounded-lg bg-neutral-200 pl-1 pr-2 font-bold dark:bg-neutral-700 dark:text-neutral-300">+ @{userId}+ </span>+ <span>{getRelativeTime(createdAt)}</span>+ </div>+ </div>+ );+};++export default Comment;