Skip to content

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

add: upvotes on solutions

bautistaaaf25b885a6658
diff --git a/apps/web/src/app/[locale]/challenge/[id]/getChallengeRouteData.ts b/apps/web/src/app/[locale]/challenge/[id]/getChallengeRouteData.ts
index 10cff08b..ae42b231 100644
--- a/apps/web/src/app/[locale]/challenge/[id]/getChallengeRouteData.ts
+++ b/apps/web/src/app/[locale]/challenge/[id]/getChallengeRouteData.ts
@@ -20,11 +20,7 @@ export const getChallengeRouteData = cache((id: string, session: Session | null)
},
_count: {
select: {
- vote: {
- where: {
- rootType: 'CHALLENGE',
- },
- },
+ vote: true,
},
},
vote: {
diff --git a/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/error.tsx b/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/error.tsx
new file mode 100644
index 00000000..d9ecdc13
--- /dev/null
+++ b/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/error.tsx
@@ -0,0 +1,13 @@
+'use client'; // Error components must be Client Components
+
+import { Text } from '@repo/ui/components/typography/typography';
+
+export default function Error() {
+ return (
+ <div className="container flex h-full flex-col items-center justify-center">
+ <Text className="mb-6" intent="h2">
+ Uh oh! We couldn&apos;t find the solution you were looking for.
+ </Text>
+ </div>
+ );
+}
diff --git a/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx b/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx
index 3c0e1c2d..312a023e 100644
--- a/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx
+++ b/apps/web/src/app/[locale]/challenge/[id]/solutions/[solutionId]/page.tsx
@@ -1,5 +1,6 @@
import { prisma } from '@repo/db';
import { SolutionDetails } from '../_components/solution-detail';
+import { getServerAuthSession, type Session } from '@repo/auth/server';
interface Props {
params: {
@@ -9,15 +10,25 @@ interface Props {
export type ChallengeSolution = NonNullable<Awaited<ReturnType<typeof getSolution>>>;
export default async function SolutionPage({ params: { solutionId } }: Props) {
- const solution = await getSolution(solutionId);
- if (!solution) return null;
+ const session = await getServerAuthSession();
+ const solution = await getSolution(solutionId, session);
return <SolutionDetails solution={solution} />;
}
export async function generateMetadata({ params: { solutionId } }: Props) {
- const solution = await getSolution(solutionId);
- if (!solution) return null;
+ const solution = await prisma.sharedSolution.findFirstOrThrow({
+ where: {
+ id: Number(solutionId),
+ },
+ include: {
+ challenge: {
+ select: {
+ name: true,
+ },
+ },
+ },
+ });
return {
title: `${solution.title}, solution to ${solution.challenge?.name} | TypeHero`,
@@ -25,31 +36,31 @@ export async function generateMetadata({ params: { solutionId } }: Props) {
};
}
-async function getSolution(solutionId: string) {
- const solution = await prisma.sharedSolution.findFirst({
+async function getSolution(solutionId: string, session: Session | null) {
+ const solution = await prisma.sharedSolution.findFirstOrThrow({
where: {
id: Number(solutionId),
},
include: {
- challenge: true,
user: {
select: {
name: true,
image: true,
},
},
+ _count: {
+ select: { vote: true },
+ },
+ vote: {
+ where: {
+ userId: session?.user.id || '',
+ },
+ select: {
+ userId: true,
+ },
+ },
},
});
- const f = await prisma.comment.findMany({
- where: {
- rootType: 'SOLUTION',
- rootSolutionId: Number(solutionId),
- },
- });
-
- return {
- ...solution,
- jimComments: f,
- };
+ return solution;
}
diff --git a/apps/web/src/app/[locale]/challenge/[id]/solutions/_components/solution-detail.tsx b/apps/web/src/app/[locale]/challenge/[id]/solutions/_components/solution-detail.tsx
index 78d6de3a..0830db5d 100644
--- a/apps/web/src/app/[locale]/challenge/[id]/solutions/_components/solution-detail.tsx
+++ b/apps/web/src/app/[locale]/challenge/[id]/solutions/_components/solution-detail.tsx
@@ -13,11 +13,14 @@ import { TypographyLarge } from '@repo/ui/components/typography/large';
import { Tooltip, TooltipContent, TooltipTrigger } from '@repo/ui/components/tooltip';
import { Markdown } from '@repo/ui/components/markdown';
import { UserBadge } from '@repo/ui/components/user-badge';
+import { Vote } from '../../../_components/vote';
+import { useSession } from '@repo/auth/react';
interface Props {
solution: ChallengeSolution;
}
export function SolutionDetails({ solution }: Props) {
+ const { data: session } = useSession();
const handleShareClick = async () => {
if (navigator.clipboard) {
const url = window.location.href;
@@ -41,9 +44,11 @@ export function SolutionDetails({ solution }: Props) {
<div className="mb-5 flex flex-col gap-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
- <Avatar>
+ <Avatar className="h-7 w-7">
<AvatarImage alt="github profile picture" src={solution.user?.image ?? ''} />
- <AvatarFallback>{solution.user?.name}</AvatarFallback>
+ <AvatarFallback className="border border-zinc-300 dark:border-zinc-600">
+ {solution.user?.name.substring(0, 1)}
+ </AvatarFallback>
</Avatar>
<TypographyLarge>{solution.title}</TypographyLarge>
</div>
@@ -86,6 +91,23 @@ export function SolutionDetails({ solution }: Props) {
{solution.createdAt?.toLocaleString()}
</span>
</div>
+ <Vote
+ voteCount={solution._count.vote}
+ initialHasVoted={solution.vote.length > 0}
+ disabled={!session?.user?.id || solution.userId === session?.user?.id}
+ rootType="SHAREDSOLUTION"
+ rootId={solution.id}
+ onVote={(didUpvote: boolean) => {
+ solution.vote = didUpvote
+ ? [
+ {
+ userId: session?.user?.id ?? '',
+ },
+ ]
+ : [];
+ solution._count.vote += didUpvote ? 1 : -1;
+ }}
+ />
</div>
</div>
<Markdown>{solution.description || ''}</Markdown>
diff --git a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx
index 2cd10a90..3d7f741e 100644
--- a/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx
+++ b/apps/web/src/app/[locale]/challenge/_components/comments/comment.tsx
@@ -317,7 +317,7 @@ function SingleComment({
</div>
)}
{isEditing ? (
- <div className=" mb-2">
+ <div className="mb-2">
<CommentInput
mode="edit"
onCancel={() => {
@@ -332,7 +332,7 @@ function SingleComment({
/>
</div>
) : null}
- <div className="my-auto flex items-center gap-4">
+ <div className="my-auto mt-1 flex items-center gap-4">
{!readonly && (
<>
<Vote