Skip to content

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

feat: show explore challenges completed (#1319)

Raul Rusu4ebfe924aef3
diff --git a/apps/web/src/app/[locale]/explore/_components/explore-challenges-progress.tsx b/apps/web/src/app/[locale]/explore/_components/explore-challenges-progress.tsx
new file mode 100644
index 00000000..f8ab5804
--- /dev/null
+++ b/apps/web/src/app/[locale]/explore/_components/explore-challenges-progress.tsx
@@ -0,0 +1,24 @@
+import { CheckCircle } from '@repo/ui/icons';
+
+interface ExploreChallengesProgressionProps {
+ completed: number;
+ total: number;
+}
+
+const SOLVED = 'Solved';
+
+export async function ExploreChallengesProgression({
+ completed,
+ total,
+}: ExploreChallengesProgressionProps) {
+ return (
+ <div className="flex items-center gap-1">
+ {completed === total && (
+ <CheckCircle className="stroke-green-600 dark:stroke-green-300" size={18} />
+ )}
+ <span className="text-muted-foreground text-sm">
+ {completed}/{total} {SOLVED}
+ </span>
+ </div>
+ );
+}
diff --git a/apps/web/src/app/[locale]/explore/_components/explore-section.tsx b/apps/web/src/app/[locale]/explore/_components/explore-section.tsx
index e5d47c70..33ba4fb1 100644
--- a/apps/web/src/app/[locale]/explore/_components/explore-section.tsx
+++ b/apps/web/src/app/[locale]/explore/_components/explore-section.tsx
@@ -1,10 +1,15 @@
+import { auth } from '@repo/auth/server';
import type { Difficulty, Tags } from '@repo/db/types';
import Link from 'next/link';
+import { ExploreChallengesProgression } from '~/app/[locale]/explore/_components/explore-challenges-progress';
import { Carousel } from '~/components/carousel';
+import { getAllFlags } from '~/utils/feature-flags';
import { ExploreCard } from './explore-card';
-import { getChallengesByTagOrDifficulty } from './explore.action';
+import {
+ getChallengesByTagOrDifficulty,
+ getExploreChallengesLengthByTagOrDifficulty,
+} from './explore.action';
import { ViewMoreButton } from './view-more-button';
-import { getAllFlags } from '~/utils/feature-flags';
interface SectionProps {
title: string;
@@ -58,51 +63,73 @@ export const COLORS_BY_TAGS = {
} as const;
export async function ExploreSection({ title, tag, redirectRoute }: SectionProps) {
+ const session = await auth();
const { enableHolidayEvent } = await getAllFlags();
- const challenges = await getChallengesByTagOrDifficulty(tag.trim().toUpperCase(), 6);
+ const challenges = await getChallengesByTagOrDifficulty(tag.trim().toUpperCase());
+ const challengesLength = await getExploreChallengesLengthByTagOrDifficulty(
+ tag.trim().toUpperCase(),
+ );
+
+ const sortedChallenges = challenges.sort((a, b) => {
+ const aHasSubmission = a?.submission?.length && a.submission.length > 0;
+ const bHasSubmission = b?.submission?.length && b.submission.length > 0;
+
+ if (aHasSubmission && !bHasSubmission) {
+ return 1;
+ }
+
+ if (!aHasSubmission && bHasSubmission) {
+ return -1;
+ }
+
+ return difficultyToNumber[a.difficulty] !== difficultyToNumber[b.difficulty]
+ ? difficultyToNumber[a.difficulty] - difficultyToNumber[b.difficulty]
+ : a.name.localeCompare(b.name);
+ });
+
+ const completedChallenges = sortedChallenges
+ .filter((exploreChallenge) => {
+ return (
+ exploreChallenge.submission.length &&
+ exploreChallenge.submission.some((submission) => submission.isSuccessful)
+ );
+ })
+ .map((exploreChallenge) => exploreChallenge.id);
+
return (
<div>
<div className="container flex items-center justify-between gap-3 px-4 pt-5">
- <h2 className={`relative text-3xl font-bold tracking-tight ${TITLES_BY_TAG[tag]}`}>
- <div
- className={`absolute -left-8 -z-10 h-12 w-32 rounded-full bg-pink-300/50 blur-3xl ${COLORS_BY_TAGS[tag]}`}
- />
- {title}
- </h2>
+ <div className="flex flex-col items-start gap-3 sm:flex-row sm:items-center">
+ <h2 className={`relative text-3xl font-bold tracking-tight ${TITLES_BY_TAG[tag]}`}>
+ <div
+ className={`absolute -left-8 -z-10 h-12 w-32 rounded-full bg-pink-300/50 blur-3xl ${COLORS_BY_TAGS[tag]}`}
+ />
+ {title}
+ </h2>
+ {session ? (
+ <ExploreChallengesProgression
+ completed={completedChallenges.length}
+ total={challengesLength}
+ />
+ ) : null}
+ </div>
<ViewMoreButton redirectRoute={redirectRoute} tag={tag} />
</div>
<section className="relative flex w-full flex-col overflow-hidden rounded-[2.5rem]">
<Carousel>
- {challenges
- .sort((a, b) => {
- const aHasSubmission = a?.submission?.length && a.submission.length > 0;
- const bHasSubmission = b?.submission?.length && b.submission.length > 0;
-
- if (aHasSubmission && !bHasSubmission) {
- return 1;
- }
-
- if (!aHasSubmission && bHasSubmission) {
- return -1;
- }
-
- return difficultyToNumber[a.difficulty] !== difficultyToNumber[b.difficulty]
- ? difficultyToNumber[a.difficulty] - difficultyToNumber[b.difficulty]
- : a.name.localeCompare(b.name);
- })
- .map((challenge) => (
- <Link
- className="group snap-center focus:outline-none sm:w-[330px] xl:w-[333px]"
- href={`/challenge/${challenge.slug}`}
- key={challenge.id}
- >
- <ExploreCard
- challenge={challenge}
- key={`challenge-${challenge.id}`}
- isHolidayEvent={Boolean(enableHolidayEvent)}
- />
- </Link>
- ))}
+ {sortedChallenges.slice(0, 6).map((challenge) => (
+ <Link
+ className="group snap-center focus:outline-none sm:w-[330px] xl:w-[333px]"
+ href={`/challenge/${challenge.slug}`}
+ key={challenge.id}
+ >
+ <ExploreCard
+ challenge={challenge}
+ key={`challenge-${challenge.id}`}
+ isHolidayEvent={Boolean(enableHolidayEvent)}
+ />
+ </Link>
+ ))}
</Carousel>
</section>
</div>
diff --git a/apps/web/src/app/[locale]/explore/_components/explore.action.ts b/apps/web/src/app/[locale]/explore/_components/explore.action.ts
index 31f03cae..76372bfd 100644
--- a/apps/web/src/app/[locale]/explore/_components/explore.action.ts
+++ b/apps/web/src/app/[locale]/explore/_components/explore.action.ts
@@ -4,6 +4,7 @@ import { auth } from '@repo/auth/server';
import { prisma } from '@repo/db';
import { Tags, type Difficulty } from '@repo/db/types';
import { AOT_CHALLENGES } from '../../challenge/[slug]/aot-slugs';
+import { cache } from 'react';
export type ExploreChallengeData = ReturnType<typeof getChallengesByTagOrDifficulty>;
const allTags: Tags[] = Object.values(Tags);
@@ -57,3 +58,24 @@ export async function getChallengesByTagOrDifficulty(str: string, take?: number)
}),
});
}
+
+/**
+ * Fetches the length of how many challenges are in a
+ * difficutly / tag group
+ * @param str difficutly or tag string
+ */
+export const getExploreChallengesLengthByTagOrDifficulty = cache((str: string) => {
+ const formattedStr = str.trim().toUpperCase();
+
+ return prisma.challenge.count({
+ where: {
+ status: 'ACTIVE',
+ user: {
+ NOT: {
+ status: 'BANNED',
+ },
+ },
+ difficulty: { in: [formattedStr as Difficulty] },
+ },
+ });
+});