Skip to content

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

chore: refactored a bit. (#495)

Ayushe06220962bf9
diff --git a/apps/web/src/app/explore/_components/explore-slug.tsx b/apps/web/src/app/explore/_components/explore-slug.tsx
index ba31eed9..982104e1 100644
--- a/apps/web/src/app/explore/_components/explore-slug.tsx
+++ b/apps/web/src/app/explore/_components/explore-slug.tsx
@@ -1,5 +1,5 @@
-import Link from 'next/link';
import { TypographyH3 } from '@repo/ui';
+import Link from 'next/link';
import { ExploreCard } from './explore-card';
import { getChallengesByTagOrDifficulty } from './explore.action';
diff --git a/apps/web/src/app/tracks/_components/index.tsx b/apps/web/src/app/tracks/_components/index.tsx
index 748595a2..cca4d55a 100644
--- a/apps/web/src/app/tracks/_components/index.tsx
+++ b/apps/web/src/app/tracks/_components/index.tsx
@@ -1,14 +1,10 @@
-import { getServerAuthSession, type Session } from '@repo/auth/server';
-import { prisma } from '@repo/db';
+import { getServerAuthSession } from '@repo/auth/server';
import { EnrolledTrackSection } from './track-enrolled-section';
import { PopularTrackSection } from './track-popular-section';
export async function Tracks() {
const session = await getServerAuthSession();
- const enrolledTracks = await getUserEnrolledTracks(session);
- const tracks = await getTracks();
-
return (
<div className="flex flex-col gap-8 py-8 md:gap-10 md:py-10">
<div className="container">
@@ -23,82 +19,8 @@ export async function Tracks() {
tracks, offering a dynamic learning path for developers to enhance their skills.
</p>
</div>
- {session ? (
- <EnrolledTrackSection
- key="enrolled-track-section"
- title="Your Tracks"
- tracks={enrolledTracks}
- />
- ) : null}
- <PopularTrackSection
- key="popular-track-section"
- title="Popular"
- tag="POPULAR"
- tracks={tracks}
- />
+ {session ? <EnrolledTrackSection /> : null}
+ <PopularTrackSection />
</div>
);
}
-
-export type EnrolledTracks = Awaited<ReturnType<typeof getUserEnrolledTracks>>;
-
-/**
- * Fetches user enrolled tracks based on current session.
- */
-function getUserEnrolledTracks(session: Session | null) {
- return prisma.track.findMany({
- where: {
- enrolledUsers: {
- some: {
- id: session?.user.id,
- },
- },
- },
- include: {
- trackChallenges: {
- include: {
- challenge: {
- include: {
- submission: {
- where: {
- userId: session?.user.id,
- },
- },
- },
- },
- },
- },
- _count: {
- select: {
- enrolledUsers: true,
- },
- },
- },
- orderBy: {
- title: 'asc',
- },
- });
-}
-
-export type Tracks = Awaited<ReturnType<typeof getTracks>>;
-
-/**
- * Fetches all tracks.
- */
-function getTracks() {
- return prisma.track.findMany({
- include: {
- trackChallenges: {
- include: {
- challenge: true,
- },
- },
- },
- where: {
- visible: true,
- },
- orderBy: {
- title: 'asc',
- },
- });
-}
diff --git a/apps/web/src/app/tracks/_components/personal-track-card.tsx b/apps/web/src/app/tracks/_components/personal-track-card.tsx
index 04a5c4e9..7e3f03d9 100644
--- a/apps/web/src/app/tracks/_components/personal-track-card.tsx
+++ b/apps/web/src/app/tracks/_components/personal-track-card.tsx
@@ -1,7 +1,7 @@
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@repo/ui';
import { TrendingUpIcon } from '@repo/ui/icons';
import clsx from 'clsx';
-import type { EnrolledTracks } from '.';
+import type { EnrolledTracks } from './track-enrolled-section';
import { TrackProgress } from './track-progress';
interface PersonalTrackCardProps {
diff --git a/apps/web/src/app/tracks/_components/track-card.tsx b/apps/web/src/app/tracks/_components/track-card.tsx
index 469fd006..3a3f0385 100644
--- a/apps/web/src/app/tracks/_components/track-card.tsx
+++ b/apps/web/src/app/tracks/_components/track-card.tsx
@@ -3,7 +3,7 @@
import { Button } from '@repo/ui';
import clsx from 'clsx';
import { useRouter } from 'next/navigation';
-import type { Tracks } from '~/app/tracks/_components';
+import type { Tracks } from '~/app/tracks/_components/track-popular-section';
import { TrackChallenge } from './track-challenge-card';
interface TrackProps {
diff --git a/apps/web/src/app/tracks/_components/track-challenge-card.tsx b/apps/web/src/app/tracks/_components/track-challenge-card.tsx
index 90d831fe..29ea8b8e 100644
--- a/apps/web/src/app/tracks/_components/track-challenge-card.tsx
+++ b/apps/web/src/app/tracks/_components/track-challenge-card.tsx
@@ -2,7 +2,7 @@
import { Check } from '@repo/ui/icons';
import clsx from 'clsx';
-import type { Tracks } from '~/app/tracks/_components';
+import type { Tracks } from '~/app/tracks/_components/track-popular-section';
import { useIsMobile } from '~/utils/useIsMobile';
interface TrackChallengeProps {
diff --git a/apps/web/src/app/tracks/_components/track-enrolled-section.tsx b/apps/web/src/app/tracks/_components/track-enrolled-section.tsx
index 0de88fd2..08d57e0b 100644
--- a/apps/web/src/app/tracks/_components/track-enrolled-section.tsx
+++ b/apps/web/src/app/tracks/_components/track-enrolled-section.tsx
@@ -1,20 +1,17 @@
+import { getServerAuthSession } from '@repo/auth/server';
+import { prisma } from '@repo/db';
import Link from 'next/link';
-import { PersonalTrackCard } from './personal-track-card';
-import type { EnrolledTracks } from '.';
import { Carousel } from '~/components/ui/carousel';
+import { PersonalTrackCard } from './personal-track-card';
-interface EnrolledTrackSectionProps {
- title: string;
- tracks: EnrolledTracks;
-}
-
-export function EnrolledTrackSection({ title, tracks }: EnrolledTrackSectionProps) {
+export async function EnrolledTrackSection() {
+ const tracks = await getUserEnrolledTracks();
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">
<div className="absolute -left-8 -z-10 h-12 w-32 rounded-full bg-blue-300/50 blur-3xl" />
- {title}
+ Your Tracks
</h2>
</div>
<section className="relative flex w-full flex-row gap-4 overflow-hidden rounded-[2.5rem]">
@@ -41,3 +38,44 @@ export function EnrolledTrackSection({ title, tracks }: EnrolledTrackSectionProp
</div>
);
}
+
+export type EnrolledTracks = Awaited<ReturnType<typeof getUserEnrolledTracks>>;
+
+/**
+ * Fetches user enrolled tracks based on current session.
+ */
+async function getUserEnrolledTracks() {
+ const session = await getServerAuthSession();
+ return prisma.track.findMany({
+ where: {
+ enrolledUsers: {
+ some: {
+ id: session?.user.id,
+ },
+ },
+ },
+ include: {
+ trackChallenges: {
+ include: {
+ challenge: {
+ include: {
+ submission: {
+ where: {
+ userId: session?.user.id,
+ },
+ },
+ },
+ },
+ },
+ },
+ _count: {
+ select: {
+ enrolledUsers: true,
+ },
+ },
+ },
+ orderBy: {
+ title: 'asc',
+ },
+ });
+}
diff --git a/apps/web/src/app/tracks/_components/track-popular-section.tsx b/apps/web/src/app/tracks/_components/track-popular-section.tsx
index 368f2381..d0c9df1c 100644
--- a/apps/web/src/app/tracks/_components/track-popular-section.tsx
+++ b/apps/web/src/app/tracks/_components/track-popular-section.tsx
@@ -1,23 +1,17 @@
-import type { Difficulty, Tags } from '@repo/db/types';
+import { prisma } from '@repo/db';
import { COLORS_BY_TAGS } from '~/app/explore/_components/explore-section';
import { TrackCard } from '~/app/tracks/_components/track-card';
-import type { Tracks } from '.';
-interface PopularTrackSectionProps {
- title: string;
- tracks: Tracks;
- tag: Difficulty | Tags;
-}
-
-export function PopularTrackSection({ title, tracks, tag }: PopularTrackSectionProps) {
+export async function PopularTrackSection() {
+ const tracks = await getTracks();
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">
<div
- className={`absolute -left-8 -z-10 h-12 w-32 rounded-full bg-neutral-300/50 blur-3xl ${COLORS_BY_TAGS[tag]}`}
+ className={`absolute -left-8 -z-10 h-12 w-32 rounded-full bg-neutral-300/50 blur-3xl ${COLORS_BY_TAGS.POPULAR}`}
/>
- {title}
+ Popular
</h2>
</div>
<section className="grid w-full grid-flow-row grid-cols-1 gap-4 overflow-hidden rounded-[2.5rem] p-8 md:grid-cols-2 lg:grid-cols-3">
@@ -26,3 +20,26 @@ export function PopularTrackSection({ title, tracks, tag }: PopularTrackSectionP
</div>
);
}
+
+export type Tracks = Awaited<ReturnType<typeof getTracks>>;
+
+/**
+ * Fetches all tracks.
+ */
+function getTracks() {
+ return prisma.track.findMany({
+ include: {
+ trackChallenges: {
+ include: {
+ challenge: true,
+ },
+ },
+ },
+ where: {
+ visible: true,
+ },
+ orderBy: {
+ title: 'asc',
+ },
+ });
+}