Skip to content

Commit

Permalink
feat(aih): enhance recommendations component with progress tracking a…
Browse files Browse the repository at this point in the history
…nd user session handling

- Integrated progress tracking for lessons, allowing users to mark resources as completed.
- Included a login prompt for users to save their progress if not authenticated.
  • Loading branch information
vojtaholik committed Jan 24, 2025
1 parent 3edb7c8 commit 8b1ec6e
Showing 1 changed file with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use client'

import Link from 'next/link'
import { setProgressForResource } from '@/lib/progress'
import { api } from '@/trpc/react'
import { cn } from '@/utils/cn'
import { ArrowRight } from 'lucide-react'
import { useSession } from 'next-auth/react'

import { Button, Skeleton } from '@coursebuilder/ui'

import { useProgress } from './progress-provider'

export default function Recommendations({
postId,
className,
Expand All @@ -22,7 +26,11 @@ export default function Recommendations({
refetchOnWindowFocus: false,
},
)

const { progress, addLessonProgress } = useProgress()
const { data: session } = useSession()
const isCompleted = progress?.completedLessons.some(
(lesson) => lesson.resourceId === postId,
)
if (!post && status !== 'pending') return null

return (
Expand All @@ -36,19 +44,40 @@ export default function Recommendations({
<h2 className="fluid-2xl mb-3 font-semibold">Recommended Next</h2>
<ul className="w-full">
<li className="flex w-full flex-col">
<Button
className="text-primary flex w-full items-center gap-2 text-lg lg:text-xl"
asChild
variant="link"
>
{status === 'pending' ? (
<Skeleton className="mx-auto mt-2 flex h-8 w-full max-w-sm" />
) : post ? (
{status === 'pending' ? (
<Skeleton className="mx-auto mt-2 flex h-8 w-full max-w-sm" />
) : post ? (
<Button
className="text-primary flex w-full items-center gap-2 text-lg lg:text-xl"
asChild
variant="link"
onClick={async () => {
if (!isCompleted) {
addLessonProgress(postId)
await setProgressForResource({
resourceId: postId,
isCompleted: true,
})
}
}}
>
<Link href={`/${post.slug}`}>
{post.title} <ArrowRight className="w-4" />
</Link>
) : null}
</Button>
</Button>
) : null}
{!session?.user && (
<span className="text-muted-foreground mt-4">
<Link
href="/login"
target="_blank"
className="hover:text-foreground text-center underline"
>
Log in
</Link>{' '}
to save progress
</span>
)}
</li>
</ul>
</nav>
Expand Down

0 comments on commit 8b1ec6e

Please sign in to comment.