|
|
@ -9,26 +9,33 @@ import { NextLessonAlertModal } from './NextLessonAlertModal'; |
|
|
|
import { useStore } from '@nanostores/react'; |
|
|
|
import { useStore } from '@nanostores/react'; |
|
|
|
import { currentLesson } from '../../stores/course'; |
|
|
|
import { currentLesson } from '../../stores/course'; |
|
|
|
import { getPercentage } from '../../helper/number'; |
|
|
|
import { getPercentage } from '../../helper/number'; |
|
|
|
|
|
|
|
import { cn } from '../../lib/classname'; |
|
|
|
|
|
|
|
|
|
|
|
type CourseLayoutProps = { |
|
|
|
type CourseLayoutProps = { |
|
|
|
children: React.ReactNode; |
|
|
|
children: React.ReactNode; |
|
|
|
} & CourseSidebarProps; |
|
|
|
} & Omit<CourseSidebarProps, 'completedPercentage'>; |
|
|
|
|
|
|
|
|
|
|
|
export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
const { children, ...sidebarProps } = props; |
|
|
|
const { children, ...sidebarProps } = props; |
|
|
|
const { chapters, courseId, chapterId, lessonId, lesson } = sidebarProps; |
|
|
|
const { |
|
|
|
|
|
|
|
chapters, |
|
|
|
|
|
|
|
currentCourseId, |
|
|
|
|
|
|
|
currentChapterId, |
|
|
|
|
|
|
|
currentLessonId, |
|
|
|
|
|
|
|
lesson, |
|
|
|
|
|
|
|
} = sidebarProps; |
|
|
|
|
|
|
|
|
|
|
|
const $currentLesson = useStore(currentLesson); |
|
|
|
const $currentLesson = useStore(currentLesson); |
|
|
|
const [showNextWarning, setShowNextWarning] = useState(false); |
|
|
|
const [showNextWarning, setShowNextWarning] = useState(false); |
|
|
|
|
|
|
|
|
|
|
|
const { data: courseProgress } = useCourseProgress(courseId); |
|
|
|
const { data: courseProgress } = useCourseProgress(currentCourseId); |
|
|
|
const completeLesson = useCompleteLessonMutation(courseId); |
|
|
|
const completeLesson = useCompleteLessonMutation(currentCourseId); |
|
|
|
|
|
|
|
|
|
|
|
const completeLessonSet = useMemo( |
|
|
|
const completeLessonSet = useMemo( |
|
|
|
() => |
|
|
|
() => |
|
|
|
new Set( |
|
|
|
new Set( |
|
|
|
(courseProgress?.completed || []).map( |
|
|
|
(courseProgress?.completed || []).map( |
|
|
|
(l) => `/learn/${courseId}/${l.chapterId}/${l.lessonId}`, |
|
|
|
(l) => `/learn/${currentCourseId}/${l.chapterId}/${l.lessonId}`, |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
), |
|
|
|
[courseProgress], |
|
|
|
[courseProgress], |
|
|
@ -38,7 +45,7 @@ export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
const lessons: string[] = []; |
|
|
|
const lessons: string[] = []; |
|
|
|
for (const chapter of chapters) { |
|
|
|
for (const chapter of chapters) { |
|
|
|
for (const lesson of chapter.lessons) { |
|
|
|
for (const lesson of chapter.lessons) { |
|
|
|
lessons.push(`/learn/${courseId}/${chapter.id}/${lesson.id}`); |
|
|
|
lessons.push(`/learn/${currentCourseId}/${chapter.id}/${lesson.id}`); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -53,7 +60,7 @@ export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
return getPercentage(completedCount, allLessonLinks.length); |
|
|
|
return getPercentage(completedCount, allLessonLinks.length); |
|
|
|
}, [allLessonLinks, completeLessonSet]); |
|
|
|
}, [allLessonLinks, completeLessonSet]); |
|
|
|
|
|
|
|
|
|
|
|
const currentLessonUrl = `/learn/${courseId}/${chapterId}/${lessonId}`; |
|
|
|
const currentLessonUrl = `/learn/${currentCourseId}/${currentChapterId}/${currentLessonId}`; |
|
|
|
const isCurrentLessonCompleted = completeLessonSet.has(currentLessonUrl); |
|
|
|
const isCurrentLessonCompleted = completeLessonSet.has(currentLessonUrl); |
|
|
|
|
|
|
|
|
|
|
|
const currentLessonIndex = allLessonLinks.indexOf(currentLessonUrl); |
|
|
|
const currentLessonIndex = allLessonLinks.indexOf(currentLessonUrl); |
|
|
@ -68,10 +75,14 @@ export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!currentChapterId || !currentLessonId) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
completeLesson.mutate( |
|
|
|
completeLesson.mutate( |
|
|
|
{ |
|
|
|
{ |
|
|
|
chapterId, |
|
|
|
chapterId: currentChapterId, |
|
|
|
lessonId, |
|
|
|
lessonId: currentLessonId, |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
onSuccess: () => { |
|
|
|
onSuccess: () => { |
|
|
@ -91,10 +102,10 @@ export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentLesson.set({ |
|
|
|
currentLesson.set({ |
|
|
|
courseId, |
|
|
|
courseId: currentCourseId, |
|
|
|
chapterId, |
|
|
|
chapterId: currentChapterId, |
|
|
|
lessonId, |
|
|
|
lessonId: currentLessonId, |
|
|
|
lessonType: lesson.frontmatter.type, |
|
|
|
lessonType: lesson?.frontmatter?.type, |
|
|
|
challengeStatus: 'pending', |
|
|
|
challengeStatus: 'pending', |
|
|
|
quizStatus: 'pending', |
|
|
|
quizStatus: 'pending', |
|
|
|
}); |
|
|
|
}); |
|
|
@ -112,7 +123,14 @@ export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
/> |
|
|
|
/> |
|
|
|
)} |
|
|
|
)} |
|
|
|
|
|
|
|
|
|
|
|
<section className="grid h-screen grid-rows-[1fr_60px] overflow-hidden bg-zinc-900 text-zinc-50"> |
|
|
|
<section |
|
|
|
|
|
|
|
className={cn( |
|
|
|
|
|
|
|
'grid h-screen grid-rows-[1fr_60px] overflow-hidden bg-zinc-900 text-zinc-50', |
|
|
|
|
|
|
|
currentChapterId && currentLessonId |
|
|
|
|
|
|
|
? 'grid-rows-[1fr_60px]' |
|
|
|
|
|
|
|
: 'grid-rows-1', |
|
|
|
|
|
|
|
)} |
|
|
|
|
|
|
|
> |
|
|
|
<div className="grid grid-cols-[240px_1fr] overflow-hidden"> |
|
|
|
<div className="grid grid-cols-[240px_1fr] overflow-hidden"> |
|
|
|
<CourseSidebar |
|
|
|
<CourseSidebar |
|
|
|
{...sidebarProps} |
|
|
|
{...sidebarProps} |
|
|
@ -122,53 +140,55 @@ export function CourseLayout(props: CourseLayoutProps) { |
|
|
|
{children} |
|
|
|
{children} |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
|
|
|
|
|
|
|
|
<footer className="flex items-center justify-end border-t border-zinc-800 px-4"> |
|
|
|
{currentChapterId && currentLessonId && ( |
|
|
|
<div className="flex items-center gap-2"> |
|
|
|
<footer className="flex items-center justify-end border-t border-zinc-800 px-4"> |
|
|
|
<button |
|
|
|
<div className="flex items-center gap-2"> |
|
|
|
className="flex items-center gap-1 rounded-lg border border-zinc-800 px-2 py-1.5 text-sm leading-none disabled:opacity-60" |
|
|
|
<button |
|
|
|
onClick={() => { |
|
|
|
className="flex items-center gap-1 rounded-lg border border-zinc-800 px-2 py-1.5 text-sm leading-none disabled:opacity-60" |
|
|
|
window.location.href = prevLessonLink; |
|
|
|
onClick={() => { |
|
|
|
}} |
|
|
|
window.location.href = prevLessonLink; |
|
|
|
disabled={!prevLessonLink || completeLesson.isPending} |
|
|
|
}} |
|
|
|
> |
|
|
|
disabled={!prevLessonLink || completeLesson.isPending} |
|
|
|
<ChevronLeft className="size-4 stroke-[3]" /> |
|
|
|
> |
|
|
|
Prev |
|
|
|
<ChevronLeft className="size-4 stroke-[3]" /> |
|
|
|
</button> |
|
|
|
Prev |
|
|
|
|
|
|
|
</button> |
|
|
|
<button |
|
|
|
|
|
|
|
className="flex items-center gap-1 rounded-lg border border-zinc-800 px-2 py-1.5 text-sm leading-none disabled:opacity-60" |
|
|
|
<button |
|
|
|
onClick={() => { |
|
|
|
className="flex items-center gap-1 rounded-lg border border-zinc-800 px-2 py-1.5 text-sm leading-none disabled:opacity-60" |
|
|
|
const isQuizPending = |
|
|
|
onClick={() => { |
|
|
|
($currentLesson?.lessonType === 'lesson-quiz' || |
|
|
|
const isQuizPending = |
|
|
|
$currentLesson?.lessonType === 'quiz') && |
|
|
|
($currentLesson?.lessonType === 'lesson-quiz' || |
|
|
|
$currentLesson?.quizStatus === 'pending'; |
|
|
|
$currentLesson?.lessonType === 'quiz') && |
|
|
|
|
|
|
|
$currentLesson?.quizStatus === 'pending'; |
|
|
|
const isChallengePending = |
|
|
|
|
|
|
|
($currentLesson?.lessonType === 'lesson-challenge' || |
|
|
|
const isChallengePending = |
|
|
|
$currentLesson?.lessonType === 'challenge') && |
|
|
|
($currentLesson?.lessonType === 'lesson-challenge' || |
|
|
|
$currentLesson?.challengeStatus === 'pending'; |
|
|
|
$currentLesson?.lessonType === 'challenge') && |
|
|
|
|
|
|
|
$currentLesson?.challengeStatus === 'pending'; |
|
|
|
if ( |
|
|
|
|
|
|
|
(isQuizPending || isChallengePending) && |
|
|
|
if ( |
|
|
|
!isCurrentLessonCompleted |
|
|
|
(isQuizPending || isChallengePending) && |
|
|
|
) { |
|
|
|
!isCurrentLessonCompleted |
|
|
|
setShowNextWarning(true); |
|
|
|
) { |
|
|
|
return; |
|
|
|
setShowNextWarning(true); |
|
|
|
} |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
handleCompleteLesson(); |
|
|
|
|
|
|
|
}} |
|
|
|
handleCompleteLesson(); |
|
|
|
disabled={completeLesson.isPending} |
|
|
|
}} |
|
|
|
> |
|
|
|
disabled={completeLesson.isPending} |
|
|
|
Next |
|
|
|
> |
|
|
|
{completeLesson.isPending ? ( |
|
|
|
Next |
|
|
|
<Loader2 className="size-4 animate-spin stroke-[3]" /> |
|
|
|
{completeLesson.isPending ? ( |
|
|
|
) : ( |
|
|
|
<Loader2 className="size-4 animate-spin stroke-[3]" /> |
|
|
|
<ChevronRight className="size-4 stroke-[3]" /> |
|
|
|
) : ( |
|
|
|
)} |
|
|
|
<ChevronRight className="size-4 stroke-[3]" /> |
|
|
|
</button> |
|
|
|
)} |
|
|
|
</div> |
|
|
|
</button> |
|
|
|
</footer> |
|
|
|
</div> |
|
|
|
|
|
|
|
</footer> |
|
|
|
|
|
|
|
)} |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
</> |
|
|
|
</> |
|
|
|
); |
|
|
|
); |
|
|
|