Connect, collaborate, and grow with a community of TypeScript developers
chore: slightly better tracks. (#695)
Ayush7a5346e8f625
diff --git a/apps/web/src/app/[locale]/_components/feature-card.tsx b/apps/web/src/app/[locale]/_components/feature-card.tsxindex daa3b827..b7a6ab5c 100644--- a/apps/web/src/app/[locale]/_components/feature-card.tsx+++ b/apps/web/src/app/[locale]/_components/feature-card.tsx@@ -1,6 +1,9 @@'use client';import type { Challenge, Difficulty } from '@repo/db/types';+import { Button } from '@repo/ui/components/button';+import { Markdown } from '@repo/ui/components/markdown';+import { Reply } from '@repo/ui/icons';import clsx from 'clsx';import {motion,@@ -9,16 +12,13 @@ import {type MotionStyle,type MotionValue,} from 'framer-motion';-import { Reply } from '@repo/ui/icons';import { useTheme } from 'next-themes';import Image, { type StaticImageData } from 'next/image';import { useEffect, useState, type MouseEvent } from 'react';import { useInView } from 'react-intersection-observer';import { Steps } from '~/components/wizard/Steps';import { useIsMobile } from '~/utils/useIsMobile';-import { TrackChallenge } from '../tracks/_components/track-challenge-card';-import { Button } from '@repo/ui/components/button';-import { Markdown } from '@repo/ui/components/markdown';+import { MockTrackChallenge } from '../tracks/_components/track-challenge-card';type WrapperStyle = MotionStyle & {'--x': MotionValue<string>;@@ -462,10 +462,9 @@ export function CuratedTracksCard(props: CardProps) {</div><div className="flex w-[69%] flex-col">{mockChallenges.map((mockChallenge) => (- <TrackChallenge+ <MockTrackChallengekey={`mock-${mockChallenge.id}`}challenge={mockChallenge as Challenge}- mock/>))}</div>diff --git a/apps/web/src/app/[locale]/tracks/_components/enroll-button.tsx b/apps/web/src/app/[locale]/tracks/_components/enroll-button.tsxindex d6beb12a..8099ca51 100644--- a/apps/web/src/app/[locale]/tracks/_components/enroll-button.tsx+++ b/apps/web/src/app/[locale]/tracks/_components/enroll-button.tsx@@ -1,9 +1,9 @@'use client';-import { useRouter } from 'next/navigation';-import type { enrollUserInTrack, unenrollUserFromTrack } from './track.action';import { Button } from '@repo/ui/components/button';import { toast } from '@repo/ui/components/use-toast';+import { useRouter } from 'next/navigation';+import type { enrollUserInTrack, unenrollUserFromTrack } from './track.action';interface EnrollButtonProps {action: typeof enrollUserInTrack | typeof unenrollUserFromTrack;@@ -11,10 +11,11 @@ interface EnrollButtonProps {trackId: number;}-export function EnrollButton({ action, text, trackId }: EnrollButtonProps) {+export function ActionButton({ action, text, trackId }: EnrollButtonProps) {const router = useRouter();return (<Button+ variant="secondary"onClick={async () => {try {await action(trackId);diff --git a/apps/web/src/app/[locale]/tracks/_components/track-card.tsx b/apps/web/src/app/[locale]/tracks/_components/track-card.tsxindex ad2b7b96..2a02e3c4 100644--- a/apps/web/src/app/[locale]/tracks/_components/track-card.tsx+++ b/apps/web/src/app/[locale]/tracks/_components/track-card.tsx@@ -1,29 +1,25 @@'use client';-import type { Tracks } from './track-grid';-import { Card, CardContent } from '@repo/ui/components/card';import { Badge } from '@repo/ui/components/badge';-import Link from 'next/link';-import { clsx } from 'clsx';+import { Card, CardContent } from '@repo/ui/components/card';import { Swords } from '@repo/ui/icons';+import { clsx } from 'clsx';+import Link from 'next/link';+import type { Tracks } from './track-grid';interface TrackProps {- index: number;track: Tracks[number];}-const BGS_BY_TRACK: Record<number, string> = {+// The color for track.+export const BGS_BY_TRACK: Record<number, string> = {0: 'to-difficulty-beginner/30 dark:to-difficulty-beginner-dark/20',1: 'to-difficulty-easy/30 dark:to-difficulty-easy-dark/20',2: 'to-difficulty-medium/30 dark:to-difficulty-medium-dark/20',3: 'to-difficulty-hard/30 dark:to-difficulty-hard-dark/20',4: 'to-difficulty-extreme/30 dark:to-difficulty-extreme-dark/20',- 5: 'to-difficulty-hard/10 dark:to-difficulty-hard-dark/10',- 6: 'to-difficulty-medium/10 dark:to-difficulty-medium-dark/10',- 7: 'to-difficulty-easy/10 dark:to-difficulty-easy-dark/10',- 8: 'to-difficulty-beginner/10 dark:to-difficulty-beginner-dark/10',- 9: 'to-difficulty-extreme/10 dark:to-difficulty-extreme-dark/10',} as const;+export const bgsArray = Object.values(BGS_BY_TRACK);const EnrolledBadge = ({ text = 'Enrolled' }: { text?: string }) => (<div@@ -36,7 +32,7 @@ const EnrolledBadge = ({ text = 'Enrolled' }: { text?: string }) => (</div>);-export function TrackCard({ track, index }: TrackProps) {+export function TrackCard({ track }: TrackProps) {const isEnrolled = Boolean(track.enrolledUsers?.length);return (@@ -53,7 +49,9 @@ export function TrackCard({ track, index }: TrackProps) {<CardContent className="flex items-center gap-5 p-2 pr-4"><divclassName={clsx(- `bg-gradient-to-r from-neutral-500/10 from-10% ${BGS_BY_TRACK[index]} relative to-100% dark:from-neutral-500/20`,+ `bg-gradient-to-r from-neutral-500/10 from-10% ${+ BGS_BY_TRACK[track.id % bgsArray.length]+ } relative to-100% dark:from-neutral-500/20`,'flex h-24 w-24 flex-none items-center justify-center rounded-2xl',)}>diff --git a/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsx b/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsxindex 84501822..e2d994cf 100644--- a/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsx+++ b/apps/web/src/app/[locale]/tracks/_components/track-challenge-card.tsx@@ -4,14 +4,17 @@ import { Check } from '@repo/ui/icons';import clsx from 'clsx';import { useIsMobile } from '~/utils/useIsMobile';-import type { Challenge } from '@repo/db/types';+import type { Challenge, Submission } from '@repo/db/types';+import { DifficultyBadge } from '@repo/ui/components/difficulty-badge';+import RelativeTime from '../../explore/_components/relative-time';+import { Badge } from '@repo/ui/components/badge';interface TrackChallengeProps {- challenge: Challenge;- challengeInProgress?: boolean;- challengeCompleted?: boolean;- className?: string;- mock?: boolean;+ challenge: Challenge & {+ submission: Submission[];+ };+ isInProgress: boolean;+ isCompleted: boolean;}const COLORS_BY_DIFFICULTY = {@@ -30,13 +33,7 @@ const BGS_BY_DIFFICULTY = {EXTREME: 'to-difficulty-extreme/20 dark:to-difficulty-extreme-dark/20',} as const;-export function TrackChallenge({- challenge,- className,- mock,- challengeInProgress = false,- challengeCompleted = false,-}: TrackChallengeProps) {+export function TrackChallenge({ challenge, isInProgress, isCompleted }: TrackChallengeProps) {const isMobile = useIsMobile();return (@@ -51,48 +48,74 @@ export function TrackChallenge({BGS_BY_DIFFICULTY[challenge.difficulty]} to-100% dark:from-neutral-500/20`,` p-4 py-2 text-black/90 duration-300 group-active/challenge:bg-neutral-500/40 group-active/challenge:duration-75 dark:text-white/90 sm:py-4`,- className,!isMobile &&'group-hover/challenge:scale-105 group-hover/challenge:rounded-xl group-hover/challenge:bg-neutral-500/20',)}>- <div className="relative flex flex-col gap-3 text-xs sm:text-base">- <div className="relative flex items-center gap-3 text-xs sm:text-base">- {mock == true && (- <>+ <div className="w-full flex-col space-y-2">+ <div className="flex flex-row justify-between">+ <div className="flex flex-row gap-3">+ <div className="relative hidden items-center gap-3 md:flex md:flex-row "><inputclassName="peer hidden appearance-none"type="checkbox"id={challenge.id.toString()}+ checked={isCompleted}+ readOnly/><div className="h-5 w-5 rounded-full border border-black/70 bg-black/10 duration-75 peer-checked:border-transparent peer-checked:bg-green-600/80 dark:border-white/50 dark:bg-white/10 peer-checked:dark:bg-green-300/80" /><Check className="absolute left-1 my-auto h-3 w-3 scale-0 stroke-[4] text-white duration-300 peer-checked:scale-100 dark:text-black" />- </>- )}- {challenge.name}- {challengeCompleted ? (- <p className="text-xs">- {' '}- - <span className="text-green-600/80 dark:text-green-300/80">Done</span>- </p>- ) : null}+ </div>+ <div className="flex flex-col items-start gap-3 md:flex-row">+ <div className="flex flex-col gap-2">+ <span>{challenge.name}</span>+ <div className="flex flex-row gap-3 md:hidden">+ <DifficultyBadge difficulty={challenge.difficulty} />+ {isCompleted && isMobile ? <Badge variant="outline">Completed</Badge> : null}+ </div>+ </div>+ <span className="text-muted-foreground">+ {challenge.shortDescription.substring(0, 60).concat('...')}+ </span>+ </div>+ </div>+ <div className="hidden md:block">+ <DifficultyBadge difficulty={challenge.difficulty} />+ </div></div></div>- <div- className={`relative text-xs font-medium tracking-wide ${- COLORS_BY_DIFFICULTY[challenge.difficulty]- } text-background`}- >- <div- className={`absolute right-0 top-1/2 h-12 w-12 -translate-y-1/2 blur-3xl ${- BGS_BY_DIFFICULTY[challenge.difficulty]- }`}+ </div>+ </label>+ );+}++export function MockTrackChallenge({ challenge }: { challenge: Challenge }) {+ const isMobile = useIsMobile();+ return (+ <label+ htmlFor={challenge.id.toString()}+ className="group/challenge flex cursor-pointer flex-col items-center pt-2"+ >+ <div+ className={clsx(+ `flex w-full items-center justify-between gap-3 overflow-hidden rounded-lg`,+ `bg-gradient-to-r from-neutral-500/10 from-70% ${+ BGS_BY_DIFFICULTY[challenge.difficulty]+ } to-100% dark:from-neutral-500/20`,+ `p-4 py-2 text-black/90 duration-300 group-active/challenge:bg-neutral-500/40 group-active/challenge:duration-75 dark:text-white/90 sm:py-4`,+ !isMobile &&+ 'group-hover/challenge:scale-105 group-hover/challenge:rounded-xl group-hover/challenge:bg-neutral-500/20',+ )}+ >+ <div className="relative flex flex-row gap-3 text-xs sm:text-base">+ <input+ className="peer hidden appearance-none"+ type="checkbox"+ id={challenge.id.toString()}/>- {challengeInProgress- ? 'In Progress'- : `${challenge.difficulty[0]}${challenge.difficulty- .substring(1, challenge.difficulty.length)- .toLowerCase()}`}+ <div className="h-5 w-5 rounded-full border border-black/70 bg-black/10 duration-75 peer-checked:border-transparent peer-checked:bg-green-600/80 dark:border-white/50 dark:bg-white/10 peer-checked:dark:bg-green-300/80" />+ <Check className="absolute left-1 my-auto h-3 w-3 scale-0 stroke-[4] text-white duration-300 peer-checked:scale-100 dark:text-black" />+ {challenge.name}</div></div></label>diff --git a/apps/web/src/app/[locale]/tracks/_components/track-details.tsx b/apps/web/src/app/[locale]/tracks/_components/track-details.tsxindex 7c6aed06..e12a65a4 100644--- a/apps/web/src/app/[locale]/tracks/_components/track-details.tsx+++ b/apps/web/src/app/[locale]/tracks/_components/track-details.tsx@@ -1,81 +1,116 @@import { getServerAuthSession } from '@repo/auth/server';import Link from 'next/link';-import { EnrollButton } from './enroll-button';+import { Swords } from '@repo/ui/icons';+import { clsx } from 'clsx';+import { ActionButton } from './enroll-button';import { TrackChallenge } from './track-challenge-card';import { TrackProgress } from './track-progress';import { enrollUserInTrack, getTrackDetails, unenrollUserFromTrack } from './track.action';+import { TypographyH3 } from '@repo/ui/components/typography/h3';+import { Markdown } from '@repo/ui/components/markdown';interface TrackDetailProps {// trackidslug: string;}+// The color for track.+export const BGS_BY_TRACK: Record<number, string> = {+ 0: 'to-difficulty-beginner/30 dark:to-difficulty-beginner-dark/20',+ 1: 'to-difficulty-easy/30 dark:to-difficulty-easy-dark/20',+ 2: 'to-difficulty-medium/30 dark:to-difficulty-medium-dark/20',+ 3: 'to-difficulty-hard/30 dark:to-difficulty-hard-dark/20',+ 4: 'to-difficulty-extreme/30 dark:to-difficulty-extreme-dark/20',+} as const;+export const bgsArray = Object.values(BGS_BY_TRACK);+export async function TrackDetail({ slug }: TrackDetailProps) {const session = await getServerAuthSession();const trackId = parseInt(slug);const track = await getTrackDetails(trackId);+const trackChallenges = track?.trackChallenges ?? [];const isEnrolled = track?.enrolledUsers.findIndex((user) => user.id === session?.user.id);- // Calculates the total number of successful challenges.- function calulcateCompletedChallenges(): number {- let completedChallenges = 0;- for (const trackChallenge of trackChallenges) {- for (const submission of trackChallenge.challenge.submission) {- if (submission.isSuccessful) {- completedChallenges++;- }++ const completedTrackChallengeId: number[] = [];+ const inProgressTrackChallengeId: number[] = [];+ const unattemptedTrackChallengeId: number[] = [];++ for (const trackChallenge of trackChallenges) {+ const submissions = trackChallenge.challenge.submission;+ if (submissions.length > 0) {+ if (submissions.some((submission) => !submission.isSuccessful)) {+ inProgressTrackChallengeId.push(trackChallenge.id);}+ if (submissions.some((submission) => submission.isSuccessful)) {+ completedTrackChallengeId.push(trackChallenge.id);+ }+ } else {+ unattemptedTrackChallengeId.push(trackChallenge.id);}- return completedChallenges;}return (- <div className="container flex min-h-screen flex-col items-center gap-8 py-5 md:gap-16 md:pb-20">- <div className="container">- <h3 className="mb-1 text-2xl font-bold tracking-wide text-neutral-900/40 dark:text-white/40">- Track- </h3>- <h1 className="mb-8 text-4xl font-bold tracking-tight text-neutral-900 dark:text-white">- {track?.title}- </h1>- <p className=" max-w-[69ch] text-lg leading-10 text-neutral-600 dark:text-white/50">- {track?.description}- </p>- {isEnrolled !== -1 && (- <div className="my-3">- <TrackProgress- completedChallenges={calulcateCompletedChallenges()}- totalChallenges={trackChallenges.length}- />- </div>- )}+ <div className="container flex min-h-screen flex-col items-center gap-4 py-5 md:gap-8 md:pb-20">+ <div className="flex flex-col items-center space-y-4">+ <div+ className={clsx(+ `bg-gradient-to-r from-neutral-500/10 from-10% ${+ BGS_BY_TRACK[trackId % bgsArray.length]+ } relative to-100% dark:from-neutral-500/20`,+ 'flex h-24 w-24 flex-none items-center justify-center rounded-2xl',+ )}+ >+ <Swords+ size={50}+ className={clsx(+ 'transition-opacity duration-300',+ !isEnrolled && 'opacity-50 group-hover:opacity-100 group-focus:opacity-100',+ )}+ />+ </div>+ <div className="flex flex-col items-center space-y-2">+ <h1 className="text-center text-3xl font-bold tracking-tight text-neutral-900 dark:text-white">+ {track?.title}+ </h1>+ <p className="text-md max-w-[69ch] text-center text-neutral-600 dark:text-white/50">+ {track?.description}+ </p>+ </div>{isEnrolled === -1 ? (- <EnrollButton action={enrollUserInTrack} trackId={trackId} text="Enroll" />+ <ActionButton action={enrollUserInTrack} trackId={trackId} text="Enroll" />) : (- <EnrollButton action={unenrollUserFromTrack} trackId={trackId} text="Unenroll" />+ <ActionButton action={unenrollUserFromTrack} trackId={trackId} text="Unenroll" />)}</div>- <div className="grid-col grid grid-cols-1 gap-x-4 self-stretch md:grid-cols-2">- {trackChallenges- .sort((a, b) => a.orderId - b.orderId)- .map((x) => x.challenge)- .map((challenge) => (- <Link key={challenge.id} href={`/challenge/${challenge.id}`}>- <TrackChallenge- className="max-w-lg"- challenge={challenge}- challengeCompleted={- challenge.submission.length > 0 &&- challenge.submission.some((submission) => submission.isSuccessful)- }- challengeInProgress={- challenge.submission.length > 0 &&- !challenge.submission.some((submission) => submission.isSuccessful)- }- />- </Link>- ))}+ {isEnrolled !== -1 && (+ <div className="w-full md:w-3/4 ">+ <TrackProgress+ completedChallenges={completedTrackChallengeId.length}+ totalChallenges={trackChallenges.length}+ />+ </div>+ )}+ <div className="flex w-full flex-row justify-around gap-8">+ <div className="flex flex-col space-y-2 md:w-3/4">+ <div className="grid-col grid grid-cols-1 gap-2 self-stretch">+ {trackChallenges+ .sort((a, b) => a.orderId - b.orderId)+ .map((trackChallenge) => (+ <Link+ key={trackChallenge.challenge.id}+ href={`/challenge/${trackChallenge.challenge.id}`}+ >+ <TrackChallenge+ challenge={trackChallenge.challenge}+ isCompleted={completedTrackChallengeId.includes(trackChallenge.id)}+ isInProgress={inProgressTrackChallengeId.includes(trackChallenge.id)}+ />+ </Link>+ ))}+ </div>+ </div></div></div>);diff --git a/apps/web/src/app/[locale]/tracks/_components/track-grid.tsx b/apps/web/src/app/[locale]/tracks/_components/track-grid.tsxindex 1516684c..71d97468 100644--- a/apps/web/src/app/[locale]/tracks/_components/track-grid.tsx+++ b/apps/web/src/app/[locale]/tracks/_components/track-grid.tsx@@ -1,13 +1,17 @@+import { getServerAuthSession } from '@repo/auth/server';import { prisma } from '@repo/db';import { TrackCard } from './track-card';-import { getServerAuthSession } from '@repo/auth/server';export async function TrackGrid() {const tracks = await getTracks();return (- <div className="container flex items-center justify-between gap-3">- <section className="grid w-full grid-flow-row grid-cols-1 gap-6 lg:grid-cols-2 xl:grid-cols-3">- {tracks?.map((t, i) => <TrackCard key={`track-${t.id}`} index={i} track={t} />)}+ <div className="flex flex-col justify-between gap-4">+ <div className="container 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" />+ <h3>All Tracks ✨</h3>+ </div>+ <section className="container grid w-full grid-flow-row grid-cols-1 gap-6 lg:grid-cols-2 xl:grid-cols-3">+ {tracks?.map((t) => <TrackCard key={`track-${t.id}`} track={t} />)}</section></div>);@@ -20,8 +24,6 @@ export type Tracks = Awaited<ReturnType<typeof getTracks>>;*/async function getTracks() {const session = await getServerAuthSession();- console.log(session?.user.id);-return prisma.track.findMany({include: {_count: {diff --git a/apps/web/src/app/[locale]/tracks/_components/track-progress.tsx b/apps/web/src/app/[locale]/tracks/_components/track-progress.tsxindex afeb8db1..720c6360 100644--- a/apps/web/src/app/[locale]/tracks/_components/track-progress.tsx+++ b/apps/web/src/app/[locale]/tracks/_components/track-progress.tsx@@ -14,7 +14,7 @@ export function TrackProgress({ completedChallenges, totalChallenges }: TrackPro{completedChallenges}/{totalChallenges}</span></div>- <Progress value={(completedChallenges / totalChallenges) * 100} />+ <Progress className="h-[8px]" value={(completedChallenges / totalChallenges) * 100} /></div>);}diff --git a/apps/web/src/app/[locale]/tracks/_components/track.action.ts b/apps/web/src/app/[locale]/tracks/_components/track.action.tsindex fe95208b..7b85da5b 100644--- a/apps/web/src/app/[locale]/tracks/_components/track.action.ts+++ b/apps/web/src/app/[locale]/tracks/_components/track.action.ts@@ -50,7 +50,6 @@ export async function unenrollUserFromTrack(id: number) {*/export const getTrackDetails = cache(async (id: number) => {const session = await getServerAuthSession();-return prisma.track.findUnique({where: {id,@@ -61,7 +60,20 @@ export const getTrackDetails = cache(async (id: number) => {enrolledUsers: true,},},- trackChallenges: { include: { challenge: { include: { submission: true } } } },+ trackChallenges: {+ include: {+ challenge: {+ include: {+ submission: true,+ user: {+ select: {+ name: true,+ },+ },+ },+ },+ },+ },enrolledUsers: {where: {id: session?.user.id,diff --git a/packages/ui/src/icons.ts b/packages/ui/src/icons.tsindex 06624591..db9d414d 100644--- a/packages/ui/src/icons.ts+++ b/packages/ui/src/icons.ts@@ -54,4 +54,5 @@ export {ChevronRightSquare,MessagesSquare,Swords,+ Sword,} from 'lucide-react';