feat: finalize academy redesign and production readiness
Align admin, community, search, library, learning and course management with the mockup. Add saved courses, download history, public community feed, instructor directory, backend search, course views, admin content tools and community moderation. Harden API and Nginx for production.
This commit is contained in:
63
hooks/useSavedCourses.ts
Normal file
63
hooks/useSavedCourses.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import { savedCourseApi } from '../services/api';
|
||||
|
||||
const legacyKeyFor = (userId: string) => `ca-saved-courses:${userId}`;
|
||||
|
||||
const readLegacy = (userId?: string) => {
|
||||
if (!userId) return [] as string[];
|
||||
try {
|
||||
const value = JSON.parse(localStorage.getItem(legacyKeyFor(userId)) || '[]');
|
||||
return Array.isArray(value)
|
||||
? value.filter((item): item is string => typeof item === 'string' && /^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(item))
|
||||
: [];
|
||||
} catch { return [] as string[]; }
|
||||
};
|
||||
|
||||
/** Saved courses follow the signed-in account and are stored by the API. */
|
||||
export const useSavedCourses = () => {
|
||||
const { user } = useAuth();
|
||||
const [ids, setIds] = useState<string[]>([]);
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
setIds([]);
|
||||
setLoaded(false);
|
||||
if (!user) { setLoaded(true); return; }
|
||||
|
||||
const load = async () => {
|
||||
const legacy = readLegacy(user.id);
|
||||
try {
|
||||
if (legacy.length) {
|
||||
// Move saves created before the API existed, then keep only the server copy.
|
||||
const results = await Promise.allSettled(legacy.map((courseId) => savedCourseApi.save(courseId)));
|
||||
if (results.every((result) => result.status === 'fulfilled')) {
|
||||
localStorage.removeItem(legacyKeyFor(user.id));
|
||||
}
|
||||
}
|
||||
const response = await savedCourseApi.list();
|
||||
if (!cancelled) setIds(response.data.map((item) => item.courseId));
|
||||
} catch {
|
||||
if (!cancelled) setIds(legacy);
|
||||
} finally {
|
||||
if (!cancelled) setLoaded(true);
|
||||
}
|
||||
};
|
||||
|
||||
void load();
|
||||
return () => { cancelled = true; };
|
||||
}, [user?.id]);
|
||||
|
||||
const toggle = useCallback((courseId: string) => {
|
||||
if (!user) return;
|
||||
const previous = ids;
|
||||
const wasSaved = previous.includes(courseId);
|
||||
const next = wasSaved ? previous.filter((id) => id !== courseId) : [...previous, courseId];
|
||||
setIds(next);
|
||||
const request = wasSaved ? savedCourseApi.remove(courseId) : savedCourseApi.save(courseId);
|
||||
request.catch(() => setIds(previous));
|
||||
}, [ids, user]);
|
||||
|
||||
return { ids, loaded, toggle, isSaved: (courseId: string) => ids.includes(courseId) };
|
||||
};
|
||||
Reference in New Issue
Block a user