Skip to content

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

Showing Completed Challenges on Profile Dashboard (#1014)

Jaime Abbariaoe1ca6279ee00
diff --git a/apps/web/src/app/[locale]/[username]/_components/dashboard/challenge-history-columns.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/challenge-history-columns.tsx
new file mode 100644
index 00000000..ed64264a
--- /dev/null
+++ b/apps/web/src/app/[locale]/[username]/_components/dashboard/challenge-history-columns.tsx
@@ -0,0 +1,27 @@
+'use client';
+
+import { type ColumnDef } from '@tanstack/react-table';
+import Link from 'next/link';
+import { getRelativeTime } from '~/utils/relativeTime';
+
+export interface Challenge {
+ id: number;
+ name: string;
+ slug: string;
+ submission: {
+ createdAt: Date;
+ }[];
+}
+
+export const columns: ColumnDef<Challenge>[] = [
+ {
+ accessorKey: 'challenge',
+ header: 'Challenge',
+ cell: ({ row }) => <Link href={`/challenge/${row.original.slug}`}>{row.original.name}</Link>,
+ },
+ {
+ accessorKey: 'lastSubmission',
+ header: 'Last Submission',
+ cell: ({ row }) => getRelativeTime(row.original.submission[0]!.createdAt),
+ },
+];
diff --git a/apps/web/src/app/[locale]/[username]/_components/dashboard/challenge-history.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/challenge-history.tsx
new file mode 100644
index 00000000..3a145148
--- /dev/null
+++ b/apps/web/src/app/[locale]/[username]/_components/dashboard/challenge-history.tsx
@@ -0,0 +1,10 @@
+import { DataTable } from '@repo/ui/components/data-table';
+import { columns, type Challenge } from './challenge-history-columns';
+
+interface Props {
+ challenges: Challenge[];
+}
+
+export default function ChallengeHistory({ challenges }: Props) {
+ return <DataTable data={challenges} columns={columns} />;
+}
diff --git a/apps/web/src/app/[locale]/[username]/_components/dashboard/completed-tab.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/completed-tab.tsx
new file mode 100644
index 00000000..8d1d1d2d
--- /dev/null
+++ b/apps/web/src/app/[locale]/[username]/_components/dashboard/completed-tab.tsx
@@ -0,0 +1,48 @@
+import { prisma } from '@repo/db';
+import { createCompletedSubmissionCacheKey } from '~/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/save-submission.action';
+import { withUnstableCache } from '~/utils/withUnstableCache';
+import ChallengeHistory from './challenge-history';
+
+export async function CompletedTab({ userId }: { userId: string }) {
+ const challenges = await withUnstableCache({
+ fn: getCompletedChallenges,
+ args: [userId],
+ keys: [`completed-challenges`],
+ tags: [createCompletedSubmissionCacheKey(userId)],
+ });
+
+ return <ChallengeHistory challenges={challenges} />;
+}
+
+async function getCompletedChallenges(userId: string) {
+ const challenges = await prisma.challenge.findMany({
+ where: {
+ submission: {
+ some: {
+ userId,
+ isSuccessful: true,
+ },
+ },
+ },
+ select: {
+ id: true,
+ slug: true,
+ name: true,
+ submission: {
+ orderBy: {
+ createdAt: 'desc',
+ },
+ take: 1,
+ select: {
+ createdAt: true,
+ },
+ },
+ },
+ });
+
+ return challenges.sort(
+ (challengeA, challengeB) =>
+ new Date(challengeB.submission[0]!.createdAt).getTime() -
+ new Date(challengeA.submission[0]!.createdAt).getTime(),
+ );
+}
diff --git a/apps/web/src/app/[locale]/[username]/_components/dashboard/in-progress-tab.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/in-progress-tab.tsx
index 96e537e8..40d0d837 100644
--- a/apps/web/src/app/[locale]/[username]/_components/dashboard/in-progress-tab.tsx
+++ b/apps/web/src/app/[locale]/[username]/_components/dashboard/in-progress-tab.tsx
@@ -1,16 +1,7 @@
import { prisma } from '@repo/db';
-import {
- Table,
- TableBody,
- TableCell,
- TableHead,
- TableHeader,
- TableRow,
-} from '@repo/ui/components/table';
-import Link from 'next/link';
import { createInProgressSubmissionCacheKey } from '~/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/save-submission.action';
-import { getRelativeTime } from '~/utils/relativeTime';
import { withUnstableCache } from '~/utils/withUnstableCache';
+import ChallengeHistory from './challenge-history';
export async function InProgressTab({ userId }: { userId: string }) {
const challenges = await withUnstableCache({
@@ -20,26 +11,7 @@ export async function InProgressTab({ userId }: { userId: string }) {
tags: [createInProgressSubmissionCacheKey(userId)],
});
- return (
- <Table>
- <TableHeader>
- <TableRow>
- <TableHead>Challenge</TableHead>
- <TableHead>Last Submission</TableHead>
- </TableRow>
- </TableHeader>
- <TableBody>
- {challenges.map((challenge) => (
- <TableRow key={challenge.id}>
- <TableCell className="font-medium underline">
- <Link href={`/challenge/${challenge.slug}`}>{challenge.name}</Link>
- </TableCell>
- <TableCell>{getRelativeTime(challenge.submission[0]!.createdAt)}</TableCell>
- </TableRow>
- ))}
- </TableBody>
- </Table>
- );
+ return <ChallengeHistory challenges={challenges} />;
}
async function getInProgressChallenges(userId: string) {
diff --git a/apps/web/src/app/[locale]/[username]/_components/dashboard/index.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/index.tsx
index 271413ab..60c8d9ea 100644
--- a/apps/web/src/app/[locale]/[username]/_components/dashboard/index.tsx
+++ b/apps/web/src/app/[locale]/[username]/_components/dashboard/index.tsx
@@ -9,7 +9,7 @@ import {
VerticalTabsList,
VerticalTabsTrigger,
} from '@repo/ui/components/tabs';
-import { Bookmark, ChevronRightSquare, Play, Settings, Text } from '@repo/ui/icons';
+import { Bookmark, CheckCircle, ChevronRightSquare, Play, Settings, Text } from '@repo/ui/icons';
import { getRelativeTime } from '~/utils/relativeTime';
import { stripProtocolAndWWW } from '~/utils/stringUtils';
@@ -26,7 +26,7 @@ interface Props {
isOwnProfile: boolean;
children: React.ReactNode;
}
-type Tab = 'bookmarks' | 'in-progress' | 'overview' | 'shared-solutions';
+type Tab = 'bookmarks' | 'completed' | 'in-progress' | 'overview' | 'shared-solutions';
export function Dashboard({ user, isOwnProfile, children }: Props) {
const router = useRouter();
@@ -37,6 +37,7 @@ export function Dashboard({ user, isOwnProfile, children }: Props) {
{ name: 'in-progress', route: `/@${user.name}/in-progress` },
{ name: 'shared-solutions', route: `/@${user.name}/shared-solutions` },
{ name: 'bookmarks', route: `/@${user.name}/bookmarks` },
+ { name: 'completed', route: `@${user.name}/completed` },
],
[user.name],
);
@@ -60,6 +61,10 @@ export function Dashboard({ user, isOwnProfile, children }: Props) {
return 'bookmarks';
}
+ if (splitPath.includes('completed')) {
+ return 'completed';
+ }
+
return 'overview';
}, [pathname]);
@@ -119,6 +124,18 @@ export function Dashboard({ user, isOwnProfile, children }: Props) {
<Text className="h-4 w-4" />
<span className="hidden md:block">Overview</span>
</VerticalTabsTrigger>
+ {Boolean(isOwnProfile) && (
+ <VerticalTabsTrigger
+ className="flex items-center justify-center gap-3 px-2 hover:bg-neutral-200/50 dark:hover:bg-neutral-700/50 md:justify-normal md:px-3"
+ value="completed"
+ onClick={() => {
+ router.push(`/@${user.name}/completed`);
+ }}
+ >
+ <CheckCircle className="h-4 w-4" />
+ <span className="hidden md:block">Completed</span>
+ </VerticalTabsTrigger>
+ )}
{Boolean(isOwnProfile) && (
<VerticalTabsTrigger
className="flex items-center justify-center gap-3 px-2 hover:bg-neutral-200/50 dark:hover:bg-neutral-700/50 md:justify-normal md:px-3"
diff --git a/apps/web/src/app/[locale]/[username]/completed/page.tsx b/apps/web/src/app/[locale]/[username]/completed/page.tsx
new file mode 100644
index 00000000..25c8b7d0
--- /dev/null
+++ b/apps/web/src/app/[locale]/[username]/completed/page.tsx
@@ -0,0 +1,54 @@
+import {
+ Card,
+ CardContent,
+ CardDescription,
+ CardHeader,
+ CardTitle,
+} from '@repo/ui/components/card';
+import { CompletedTab } from '../_components/dashboard/completed-tab';
+import { notFound } from 'next/navigation';
+import { prisma } from '@repo/db';
+
+interface Props {
+ params: {
+ username: string;
+ };
+}
+
+export default async function CompletedPage({ params: { username: usernameFromQuery } }: Props) {
+ const [, username] = decodeURIComponent(usernameFromQuery).split('@');
+
+ if (!username) return notFound();
+
+ const user = await prisma.user.findFirst({
+ where: {
+ name: {
+ equals: username,
+ },
+ },
+ select: {
+ id: true,
+ createdAt: true,
+ bio: true,
+ image: true,
+ name: true,
+ userLinks: true,
+ },
+ });
+
+ if (!user) return notFound();
+
+ return (
+ <Card className="col-span-4 md:min-h-[calc(100vh_-_56px_-_6rem)]">
+ <CardHeader>
+ <CardTitle>Completed</CardTitle>
+ <CardDescription className="text-muted-foreground mb-4 text-sm">
+ Your completed challenges.
+ </CardDescription>
+ </CardHeader>
+ <CardContent>
+ <CompletedTab userId={user.id} />
+ </CardContent>
+ </Card>
+ );
+}
diff --git a/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/save-submission.action.ts b/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/save-submission.action.ts
index ef687214..a8acb054 100644
--- a/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/save-submission.action.ts
+++ b/apps/web/src/app/[locale]/challenge/[slug]/submissions/[[...catchAll]]/save-submission.action.ts
@@ -12,6 +12,10 @@ export const createInProgressSubmissionCacheKey = (userId: string) => {
return `${userId}-in-progress-challenges`;
};
+export const createCompletedSubmissionCacheKey = (userId: string) => {
+ return `${userId}-completed-challenges`;
+};
+
interface Args {
challenge: ChallengeRouteData['challenge'];
userId: string;
@@ -30,4 +34,5 @@ export async function saveSubmission({ challenge, userId, code, isSuccessful }:
revalidateTag(createChallengeSubmissionCacheKey(challenge.slug));
revalidateTag(createCacheKeyForSolutions(challenge.slug));
revalidateTag(createInProgressSubmissionCacheKey(userId));
+ revalidateTag(createCompletedSubmissionCacheKey(userId));
}