Skip to content

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

feat: add in-progress challenges to profiles (#717)

Anthony Morrisa03bf318659f
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 52b3d062..2d4c1ebf 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,42 +1,80 @@
+import { prisma } from '@repo/db';
import {
Table,
TableBody,
- TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@repo/ui/components/table';
+import Link from 'next/link';
+import { getRelativeTime } from '~/utils/relativeTime';
+import { withUnstableCache } from '~/utils/withUnstableCache';
-const challengesList = [
- {
- challengeTitle: 'Extreme Challenge 1',
- challengeStatus: 'In-Progress',
- },
- {
- challengeTitle: 'Extreme Challenge 4',
- challengeStatus: 'In-Progress',
- },
-];
+export async function InProgressTab({ userId }: { userId: string }) {
+ const challenges = await withUnstableCache({
+ fn: getInProgressChallenges,
+ args: [userId],
+ keys: ['in-progress-challenges'],
+ tags: ['challenges'],
+ });
-export function InProgressTab() {
return (
<Table>
- <TableCaption>A list of your recent challenges.</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Challenge</TableHead>
- <TableHead>Status</TableHead>
+ <TableHead>Last Submission</TableHead>
</TableRow>
</TableHeader>
<TableBody>
- {challengesList.map((challengeTitle) => (
- <TableRow key={challengeTitle.challengeTitle}>
- <TableCell className="font-medium">{challengeTitle.challengeTitle}</TableCell>
- <TableCell>{challengeTitle.challengeStatus}</TableCell>
+ {challenges.map((challenge) => (
+ <TableRow key={challenge.id}>
+ <TableCell className="font-medium underline">
+ <Link href={`/challenge/${challenge.id}`}>{challenge.name}</Link>
+ </TableCell>
+ <TableCell>{getRelativeTime(challenge.submission[0]!.createdAt)}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}
+
+async function getInProgressChallenges(id: string) {
+ const challenges = await prisma.challenge.findMany({
+ where: {
+ AND: [
+ {
+ submission: {
+ none: {
+ userId: id,
+ isSuccessful: true,
+ },
+ },
+ },
+ // Make sure there is at least one submission
+ { submission: { some: { userId: id, isSuccessful: false } } },
+ ],
+ },
+ select: {
+ id: 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/index.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/index.tsx
index 80fcf263..803d8342 100644
--- a/apps/web/src/app/[locale]/[username]/_components/dashboard/index.tsx
+++ b/apps/web/src/app/[locale]/[username]/_components/dashboard/index.tsx
@@ -1,7 +1,12 @@
import { getServerAuthSession } from '@repo/auth/server';
-import { prisma } from '@repo/db';
import type { User } from '@repo/db/types';
-import { Card, CardContent, CardHeader, CardTitle } from '@repo/ui/components/card';
+import {
+ Card,
+ CardContent,
+ CardHeader,
+ CardTitle,
+ CardDescription,
+} from '@repo/ui/components/card';
import { MagicIcon } from '@repo/ui/components/magic-icon';
import {
Tabs,
@@ -16,9 +21,8 @@ import { getRelativeTime } from '~/utils/relativeTime';
import { stripProtocolAndWWW } from '~/utils/stringUtils';
import { InProgressTab } from './in-progress-tab';
import { Overview } from './overview';
-import { SolutionsTab } from './solutions-tab';
+import { SubmissionsTab } from './submissions-tab';
import UserHeader from './user-header';
-import { cache } from 'react';
interface Props {
// TODO: how do do this union type with just letting prisma halp
@@ -27,42 +31,9 @@ interface Props {
};
}
-export type UserData = NonNullable<Awaited<ReturnType<typeof getUserdata>>>;
-const getUserdata = cache(async (id: string) => {
- const userData = await prisma.user.findFirst({
- where: {
- id,
- },
- select: {
- submission: {
- where: {
- userId: id,
- },
- orderBy: [
- {
- createdAt: 'desc',
- },
- ],
- take: 10,
- include: {
- challenge: true,
- },
- },
- },
- });
-
- return userData;
-});
-
export async function Dashboard({ user }: Props) {
- const userData = await getUserdata(user.id);
const session = await getServerAuthSession();
- // TODO: this seems sus
- if (!userData) {
- return null;
- }
-
return (
<div className="container">
{/* // TODO: GFI: make each page a subroute, put settings into this same layout */}
@@ -114,10 +85,10 @@ export async function Dashboard({ user }: Props) {
</VerticalTabsTrigger>
<VerticalTabsTrigger
className="flex items-center justify-center gap-3 px-2 md:justify-normal md:px-3"
- value="solutions"
+ value="submissions"
>
<ChevronRightSquare className="h-4 w-4" />
- <span className="hidden md:block">Solutions</span>
+ <span className="hidden md:block">Submissions</span>
</VerticalTabsTrigger>
<VerticalTabsTrigger
className="flex items-center justify-center gap-3 px-2 md:justify-normal md:px-3"
@@ -162,19 +133,25 @@ export async function Dashboard({ user }: Props) {
<Card className="col-span-4 min-h-[calc(100vh_-_56px_-_6rem)]">
<CardHeader>
<CardTitle>In-Progress</CardTitle>
+ <CardDescription className="text-muted-foreground mb-4 text-sm">
+ These are all your in-progress challenges.
+ </CardDescription>
</CardHeader>
<CardContent className="pl-2">
- <InProgressTab />
+ <InProgressTab userId={user.id} />
</CardContent>
</Card>
</VerticalTabsContent>
- <VerticalTabsContent className="shrink grow space-y-4" value="solutions">
+ <VerticalTabsContent className="shrink grow space-y-4" value="submissions">
<Card className="col-span-4 min-h-[calc(100vh_-_56px_-_6rem)]">
<CardHeader>
- <CardTitle>Solutions</CardTitle>
+ <CardTitle>Submissions</CardTitle>
+ <CardDescription className="text-muted-foreground mb-4 text-sm">
+ This is your entire submission history.
+ </CardDescription>
</CardHeader>
<CardContent className="pl-2">
- <SolutionsTab submissions={userData.submission} />
+ <SubmissionsTab userId={user.id} />
</CardContent>
</Card>
</VerticalTabsContent>
diff --git a/apps/web/src/app/[locale]/[username]/_components/dashboard/solutions-tab.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/solutions-tab.tsx
deleted file mode 100644
index 4b4777ab..00000000
--- a/apps/web/src/app/[locale]/[username]/_components/dashboard/solutions-tab.tsx
+++ /dev/null
@@ -1,37 +0,0 @@
-import Link from 'next/link';
-import {
- Table,
- TableBody,
- TableCaption,
- TableCell,
- TableHead,
- TableHeader,
- TableRow,
-} from '@repo/ui/components/table';
-import type { UserData } from '.';
-
-export function SolutionsTab({ submissions = [] }: { submissions: UserData['submission'] }) {
- return (
- <Table>
- <TableCaption>A list of your recent challenges.</TableCaption>
- <TableHeader>
- <TableRow>
- <TableHead>Challenge</TableHead>
- <TableHead>Status</TableHead>
- </TableRow>
- </TableHeader>
- <TableBody>
- {submissions.map((submission) => (
- <TableRow key={submission.id}>
- <TableCell className="font-medium underline">
- <Link href={`/challenge/${submission.challenge.id}`}>
- {submission.challenge.name}
- </Link>
- </TableCell>
- <TableCell>{submission.isSuccessful ? 'Accepted' : 'Rejected'}</TableCell>
- </TableRow>
- ))}
- </TableBody>
- </Table>
- );
-}
diff --git a/apps/web/src/app/[locale]/[username]/_components/dashboard/submissions-tab.tsx b/apps/web/src/app/[locale]/[username]/_components/dashboard/submissions-tab.tsx
new file mode 100644
index 00000000..89e3bd0c
--- /dev/null
+++ b/apps/web/src/app/[locale]/[username]/_components/dashboard/submissions-tab.tsx
@@ -0,0 +1,72 @@
+import { prisma } from '@repo/db';
+import { Badge } from '@repo/ui/components/badge';
+import {
+ Table,
+ TableBody,
+ TableCell,
+ TableHead,
+ TableHeader,
+ TableRow,
+} from '@repo/ui/components/table';
+import Link from 'next/link';
+import { withUnstableCache } from '~/utils/withUnstableCache';
+
+export async function SubmissionsTab({ userId }: { userId: string }) {
+ const submissions = await withUnstableCache({
+ fn: getRecentSubmissions,
+ args: [userId],
+ keys: ['all-submissions'],
+ tags: ['submissions'],
+ });
+
+ return (
+ <Table>
+ <TableHeader>
+ <TableRow>
+ <TableHead>Challenge</TableHead>
+ <TableHead>Status</TableHead>
+ </TableRow>
+ </TableHeader>
+
+ <TableBody>
+ {submissions.map((submission) => (
+ <TableRow key={submission.id}>
+ <TableCell className="font-medium underline">
+ <Link href={`/challenge/${submission.challenge.id}`}>
+ {submission.challenge.name}
+ </Link>
+ </TableCell>
+ <TableCell>
+ {submission.isSuccessful ? (
+ <Badge className="dark:bg-difficulty-easy-dark bg-difficulty-easy text-white duration-300 dark:text-black">
+ Accepted
+ </Badge>
+ ) : (
+ <Badge className="dark:bg-difficulty-hard-dark bg-difficulty-hard text-white duration-300 dark:text-black">
+ Rejected
+ </Badge>
+ )}
+ </TableCell>
+ </TableRow>
+ ))}
+ </TableBody>
+ </Table>
+ );
+}
+
+async function getRecentSubmissions(id: string) {
+ return await prisma.submission.findMany({
+ where: {
+ userId: id,
+ },
+ orderBy: [
+ {
+ createdAt: 'desc',
+ },
+ ],
+ take: 25,
+ include: {
+ challenge: true,
+ },
+ });
+}
diff --git a/apps/web/src/components/navigation.tsx b/apps/web/src/components/navigation.tsx
index 3770df89..c6e09138 100644
--- a/apps/web/src/components/navigation.tsx
+++ b/apps/web/src/components/navigation.tsx
@@ -51,7 +51,7 @@ export function Navigation() {
>
<div className="flex w-full items-center justify-between">
<div className="relative flex items-center gap-3">
- <a
+ <Link
className="flex items-center space-x-2 focus:outline-none focus-visible:ring-2"
href="/"
>
@@ -88,7 +88,7 @@ export function Navigation() {
<br />
hero
</span>
- </a>
+ </Link>
{featureFlags?.exploreButton ? (
<Link href="/explore" className="ml-4">
<div
diff --git a/apps/web/src/utils/withUnstableCache.ts b/apps/web/src/utils/withUnstableCache.ts
new file mode 100644
index 00000000..2a4e13cf
--- /dev/null
+++ b/apps/web/src/utils/withUnstableCache.ts
@@ -0,0 +1,20 @@
+import { unstable_cache } from 'next/cache';
+
+// eslint-disable-next-line
+export async function withUnstableCache<T extends (...args: any[]) => Promise<any>>(opts: {
+ fn: T;
+ args: Parameters<T>;
+ keys: string[];
+ tags: string[];
+}) {
+ const cachedResult = await unstable_cache(
+ async (...args) => {
+ const result = await opts.fn(...args);
+ return result;
+ },
+ [...opts.keys],
+ { tags: opts.tags },
+ )(...opts.args);
+
+ return cachedResult as Awaited<ReturnType<T>>;
+}