forked from Epondia/starked-education
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourseStore.ts
More file actions
287 lines (254 loc) · 9.81 KB
/
Copy pathcourseStore.ts
File metadata and controls
287 lines (254 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
interface Course {
id: string;
title: string;
description: string;
content: string;
instructor: string;
category: string;
difficulty: 'beginner' | 'intermediate' | 'advanced';
duration: string;
enrolledAt?: Date;
progress: number;
topics: string[];
materials: {
type: 'video' | 'document' | 'quiz' | 'assignment';
title: string;
url: string;
duration?: string;
}[];
}
interface CourseStore {
// Current course state
currentCourse: Course | null;
enrolledCourses: Course[];
availableCourses: Course[];
// Learning progress
studyStreak: number;
totalStudyTime: number; // in minutes
lastStudyDate: Date | null;
// Actions
setCurrentCourse: (course: Course | null) => void;
enrollInCourse: (courseId: string) => Promise<void>;
unenrollFromCourse: (courseId: string) => Promise<void>;
updateProgress: (courseId: string, progress: number) => void;
// Course management
loadAvailableCourses: () => Promise<void>;
loadEnrolledCourses: () => Promise<void>;
addCourse: (course: Omit<Course, 'id'>) => void;
updateCourse: (courseId: string, updates: Partial<Course>) => void;
// Study tracking
startStudySession: (courseId: string) => void;
endStudySession: (courseId: string, duration: number) => void;
updateStudyStreak: () => void;
// Context for AI assistant
getCourseContext: (courseId?: string) => {
courseTitle: string;
courseContent: string;
currentProgress: number;
topics: string[];
nextTopic?: string;
} | null;
}
export const useCourseStore = create<CourseStore>()(
persist(
(set, get) => ({
// Initial state
currentCourse: null,
enrolledCourses: [],
availableCourses: [],
studyStreak: 0,
totalStudyTime: 0,
lastStudyDate: null,
// Actions
setCurrentCourse: (course) => {
set({ currentCourse: course });
},
enrollInCourse: async (courseId) => {
const state = get();
const course = state.availableCourses.find(c => c.id === courseId);
if (course && !state.enrolledCourses.find(c => c.id === courseId)) {
const enrolledCourse = {
...course,
enrolledAt: new Date(),
progress: 0
};
set((state) => ({
enrolledCourses: [...state.enrolledCourses, enrolledCourse]
}));
// Track course enrollment
if (typeof window !== 'undefined' && (window as any).plausible) {
(window as any).plausible('Course Enrollment', {
props: { courseId: course.id, courseTitle: course.title }
});
}
}
},
unenrollFromCourse: async (courseId) => {
set((state) => ({
enrolledCourses: state.enrolledCourses.filter(c => c.id !== courseId),
currentCourse: state.currentCourse?.id === courseId ? null : state.currentCourse
}));
},
updateProgress: (courseId, progress) => {
set((state) => ({
enrolledCourses: state.enrolledCourses.map(course =>
course.id === courseId ? { ...course, progress } : course
),
currentCourse: state.currentCourse?.id === courseId
? { ...state.currentCourse, progress }
: state.currentCourse
}));
},
// Course management
loadAvailableCourses: async () => {
// Mock data - replace with API call
const mockCourses: Course[] = [
{
id: 'course-1',
title: 'Introduction to React',
description: 'Learn the fundamentals of React.js and build modern web applications',
content: 'React is a JavaScript library for building user interfaces. It was developed by Facebook and is widely used for creating interactive web applications.',
instructor: 'John Doe',
category: 'Web Development',
difficulty: 'beginner',
duration: '8 weeks',
progress: 0,
topics: ['Components', 'State Management', 'Hooks', 'Routing', 'Context API'],
materials: [
{ type: 'video', title: 'React Introduction', url: '/videos/react-intro', duration: '45 min' },
{ type: 'document', title: 'React Documentation', url: '/docs/react-basics' },
{ type: 'quiz', title: 'React Basics Quiz', url: '/quiz/react-basics' }
]
},
{
id: 'course-2',
title: 'Advanced TypeScript',
description: 'Master advanced TypeScript concepts and patterns for large-scale applications',
content: 'TypeScript is a strongly typed programming language that builds on JavaScript. It provides static typing, classes, interfaces, and more.',
instructor: 'Jane Smith',
category: 'Programming',
difficulty: 'advanced',
duration: '12 weeks',
progress: 0,
topics: ['Generics', 'Decorators', 'Advanced Types', 'Modules', 'Performance'],
materials: [
{ type: 'video', title: 'TypeScript Generics', url: '/videos/ts-generics', duration: '60 min' },
{ type: 'document', title: 'TypeScript Handbook', url: '/docs/ts-handbook' },
{ type: 'assignment', title: 'TypeScript Project', url: '/assignments/ts-project' }
]
},
{
id: 'course-3',
title: 'Blockchain Fundamentals',
description: 'Understanding blockchain technology and its applications in modern software',
content: 'Blockchain is a distributed ledger technology that maintains a secure and decentralized record of transactions.',
instructor: 'Mike Johnson',
category: 'Blockchain',
difficulty: 'intermediate',
duration: '10 weeks',
progress: 0,
topics: ['Cryptography', 'Consensus', 'Smart Contracts', 'DeFi', 'NFTs'],
materials: [
{ type: 'video', title: 'Blockchain Basics', url: '/videos/bc-basics', duration: '50 min' },
{ type: 'document', title: 'Whitepaper Analysis', url: '/docs/bc-whitepaper' },
{ type: 'quiz', title: 'Blockchain Concepts', url: '/quiz/bc-concepts' }
]
}
];
set({ availableCourses: mockCourses });
},
loadEnrolledCourses: async () => {
// This would typically load from API
// For now, return empty array
set({ enrolledCourses: [] });
},
addCourse: (course) => {
const newCourse: Course = {
...course,
id: Date.now().toString(),
progress: 0
};
set((state) => ({
availableCourses: [...state.availableCourses, newCourse]
}));
},
updateCourse: (courseId, updates) => {
set((state) => ({
availableCourses: state.availableCourses.map(course =>
course.id === courseId ? { ...course, ...updates } : course
),
enrolledCourses: state.enrolledCourses.map(course =>
course.id === courseId ? { ...course, ...updates } : course
),
currentCourse: state.currentCourse?.id === courseId
? { ...state.currentCourse, ...updates }
: state.currentCourse
}));
},
// Study tracking
startStudySession: (courseId) => {
// This would typically log to analytics
console.log(`Started study session for course: ${courseId}`);
},
endStudySession: (courseId, duration) => {
const state = get();
const newTotalTime = state.totalStudyTime + duration;
set({
totalStudyTime: newTotalTime,
lastStudyDate: new Date()
});
// Update progress based on study time
const course = state.enrolledCourses.find(c => c.id === courseId);
if (course) {
const progressIncrease = Math.min((duration / 60) * 5, 10); // 5% per hour, max 10%
const newProgress = Math.min(course.progress + progressIncrease, 100);
get().updateProgress(courseId, newProgress);
}
},
updateStudyStreak: () => {
const state = get();
const today = new Date();
const lastStudy = state.lastStudyDate;
if (!lastStudy) {
set({ studyStreak: 1 });
return;
}
const daysDiff = Math.floor((today.getTime() - lastStudy.getTime()) / (1000 * 60 * 60 * 24));
if (daysDiff === 1) {
set((state) => ({ studyStreak: state.studyStreak + 1 }));
} else if (daysDiff > 1) {
set({ studyStreak: 1 });
}
// If daysDiff === 0, same day, don't change streak
},
// Context for AI assistant
getCourseContext: (courseId) => {
const state = get();
const course = courseId
? state.enrolledCourses.find(c => c.id === courseId) || state.availableCourses.find(c => c.id === courseId)
: state.currentCourse;
if (!course) return null;
const currentTopicIndex = Math.floor((course.progress / 100) * course.topics.length);
const nextTopic = course.topics[currentTopicIndex + 1];
return {
courseTitle: course.title,
courseContent: course.content,
currentProgress: course.progress,
topics: course.topics,
nextTopic
};
}
}),
{
name: 'course-store',
partialize: (state) => ({
enrolledCourses: state.enrolledCourses,
studyStreak: state.studyStreak,
totalStudyTime: state.totalStudyTime,
lastStudyDate: state.lastStudyDate
})
}
)
);