|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useContext } from "react"; |
| 4 | +import { |
| 5 | + formattedLocaleDate, |
| 6 | + isEnrolled, |
| 7 | + isLessonCompleted, |
| 8 | +} from "@ui-lib/utils"; |
| 9 | +import { CheckCircled, Circle, Lock } from "@courselit/icons"; |
| 10 | +import { SIDEBAR_TEXT_COURSE_ABOUT } from "@ui-config/strings"; |
| 11 | +import { Profile, Constants } from "@courselit/common-models"; |
| 12 | +import { |
| 13 | + ComponentScaffoldMenuItem, |
| 14 | + ComponentScaffold, |
| 15 | + Divider, |
| 16 | +} from "@components/public/scaffold"; |
| 17 | +import { ProfileContext, SiteInfoContext } from "@components/contexts"; |
| 18 | +import { CourseFrontend, GroupWithLessons } from "./helpers"; |
| 19 | + |
| 20 | +export default function ProductPage({ |
| 21 | + product, |
| 22 | + children, |
| 23 | +}: { |
| 24 | + product: CourseFrontend; |
| 25 | + children: React.ReactNode; |
| 26 | +}) { |
| 27 | + const { profile } = useContext(ProfileContext); |
| 28 | + const siteInfo = useContext(SiteInfoContext); |
| 29 | + |
| 30 | + if (!profile) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <ComponentScaffold |
| 36 | + items={generateSideBarItems(product, profile as Profile)} |
| 37 | + drawerWidth={360} |
| 38 | + showCourseLitBranding={true} |
| 39 | + siteinfo={siteInfo} |
| 40 | + > |
| 41 | + {children} |
| 42 | + </ComponentScaffold> |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +export function generateSideBarItems( |
| 47 | + course: CourseFrontend, |
| 48 | + profile: Profile, |
| 49 | +): (ComponentScaffoldMenuItem | Divider)[] { |
| 50 | + if (!course) return []; |
| 51 | + |
| 52 | + const items: (ComponentScaffoldMenuItem | Divider)[] = [ |
| 53 | + { |
| 54 | + label: SIDEBAR_TEXT_COURSE_ABOUT, |
| 55 | + href: `/course/${course.slug}/${course.courseId}`, |
| 56 | + }, |
| 57 | + ]; |
| 58 | + |
| 59 | + let lastGroupDripDateInMillis = Date.now(); |
| 60 | + |
| 61 | + for (const group of course.groups) { |
| 62 | + let availableLabel = ""; |
| 63 | + if (group.drip && group.drip.status) { |
| 64 | + if ( |
| 65 | + group.drip.type === |
| 66 | + Constants.dripType[0].split("-")[0].toUpperCase() |
| 67 | + ) { |
| 68 | + const delayInMillis = |
| 69 | + (group?.drip?.delayInMillis ?? 0) + |
| 70 | + lastGroupDripDateInMillis; |
| 71 | + const daysUntilAvailable = Math.ceil( |
| 72 | + (delayInMillis - Date.now()) / 86400000, |
| 73 | + ); |
| 74 | + availableLabel = |
| 75 | + daysUntilAvailable && |
| 76 | + !isGroupAccessibleToUser(course, profile as Profile, group) |
| 77 | + ? isEnrolled(course.courseId, profile) |
| 78 | + ? `Available in ${daysUntilAvailable} days` |
| 79 | + : `Available ${daysUntilAvailable} days after enrollment` |
| 80 | + : ""; |
| 81 | + } else { |
| 82 | + const today = new Date(); |
| 83 | + const dripDate = new Date(group?.drip?.dateInUTC ?? ""); |
| 84 | + const timeDiff = dripDate.getTime() - today.getTime(); |
| 85 | + const daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24)); |
| 86 | + |
| 87 | + availableLabel = |
| 88 | + daysDiff > 0 && |
| 89 | + !isGroupAccessibleToUser(course, profile, group) |
| 90 | + ? `Available on ${formattedLocaleDate(dripDate)}` |
| 91 | + : ""; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Update lastGroupDripDateInMillis for relative drip types |
| 96 | + if ( |
| 97 | + group.drip && |
| 98 | + group.drip.status && |
| 99 | + group.drip.type === |
| 100 | + Constants.dripType[0].split("-")[0].toUpperCase() |
| 101 | + ) { |
| 102 | + lastGroupDripDateInMillis += group?.drip?.delayInMillis ?? 0; |
| 103 | + } |
| 104 | + |
| 105 | + items.push({ |
| 106 | + badge: availableLabel, |
| 107 | + label: group.name, |
| 108 | + }); |
| 109 | + |
| 110 | + for (const lesson of group.lessons) { |
| 111 | + items.push({ |
| 112 | + label: lesson.title, |
| 113 | + href: `/course/${course.slug}/${course.courseId}/${lesson.lessonId}`, |
| 114 | + icon: |
| 115 | + profile && profile.userId ? ( |
| 116 | + isEnrolled(course.courseId, profile) ? ( |
| 117 | + isLessonCompleted({ |
| 118 | + courseId: course.courseId, |
| 119 | + lessonId: lesson.lessonId, |
| 120 | + profile, |
| 121 | + }) ? ( |
| 122 | + <CheckCircled /> |
| 123 | + ) : ( |
| 124 | + <Circle /> |
| 125 | + ) |
| 126 | + ) : lesson.requiresEnrollment ? ( |
| 127 | + <Lock /> |
| 128 | + ) : undefined |
| 129 | + ) : lesson.requiresEnrollment ? ( |
| 130 | + <Lock /> |
| 131 | + ) : undefined, |
| 132 | + iconPlacementRight: true, |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return items; |
| 138 | +} |
| 139 | + |
| 140 | +export function isGroupAccessibleToUser( |
| 141 | + course: CourseFrontend, |
| 142 | + profile: Profile, |
| 143 | + group: GroupWithLessons, |
| 144 | +): boolean { |
| 145 | + if (!group.drip || !group.drip.status) return true; |
| 146 | + |
| 147 | + if (!Array.isArray(profile.purchases)) return false; |
| 148 | + |
| 149 | + for (const purchase of profile.purchases) { |
| 150 | + if (purchase.courseId === course.courseId) { |
| 151 | + if (Array.isArray(purchase.accessibleGroups)) { |
| 152 | + if (purchase.accessibleGroups.includes(group.id)) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return false; |
| 160 | +} |
0 commit comments