This document covers the key data structures used in the application and explains their implementation and use cases.
The trainee knowledge system efficiently tracks and summarizes a trainee's exercise history using modern JavaScript data structures and React optimization patterns.
function useTraineeSessions(sessions: Session[], traineeId?: string) {
const traineeSessionList = useMemo(() => {
if (!traineeId) return [];
return sessions
.filter((session) => session.traineeId === traineeId)
.sort((a, b) => b.timestamp - a.timestamp);
}, [sessions, traineeId]);
const getTraineeKnowledge = useMemo(() => {
const categorizedExercises = new Map();
// ... implementation
return categorizedExercises;
}, [traineeSessionList]);
return {
traineeSessionList,
getLastCategories,
getTraineeKnowledge,
};
}
-
Map for Category-Exercise Relationships
- Purpose: Store exercises by category
- Benefits:
- O(1) lookups
- Maintains insertion order
- Dynamic keys
- Example:
const exercises = new Map(); exercises.set("mat-work", new Set(["roll-up", "hundred"])); exercises.set("reformer", new Set(["footwork", "short-box"]));
-
Set for Unique Exercises
- Purpose: Store unique exercises within each category
- Benefits:
- Automatic deduplication
- O(1) operations
- Built-in set operations
- Example:
const matExercises = new Set(); matExercises.add("roll-up"); // Added matExercises.add("roll-up"); // Ignored (duplicate)
-
Memoization Strategy
const memoizedValue = useMemo( () => { // Expensive computation here }, [ /* dependencies */ ] );
-
Time Complexity
- Session filtering: O(n)
- Exercise categorization: O(n * m)
- n = number of sessions
- m = average exercises per session
-
Adding New Exercises
function addExercise(category: string, exercise: string) { if (!categorizedExercises.has(category)) { categorizedExercises.set(category, new Set()); } categorizedExercises.get(category).add(exercise); }
-
Retrieving Category Exercises
function getCategoryExercises(category: string): Set<string> { return categorizedExercises.get(category) || new Set(); }
-
Type Safety
interface ExerciseMap { categories: Map<string, Set<string>>; lastUsed: string[]; }
-
Error Handling
try { const exercises = getCategoryExercises(category); } catch (error) { console.error("Failed to get exercises:", error); return new Set(); }
-
Observer Pattern
- Track changes in exercise history
- Update UI when new exercises are added
-
Factory Pattern
- Create new exercise categories
- Initialize with default exercises
-
Caching
const cache = new Map<string, Map<string, Set<string>>>();
-
Pagination
function getPagedExercises(category: string, page: number, size: number) { const exercises = Array.from(categorizedExercises.get(category) || []); return exercises.slice(page * size, (page + 1) * size); }
[Link to session management documentation]
[Link to scheduling system documentation]
- Previous: Trainee Availability
- Next: Refactoring Journey
- Back to Home