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:
131
context/CatalogContext.tsx
Normal file
131
context/CatalogContext.tsx
Normal file
@@ -0,0 +1,131 @@
|
||||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
||||
import { Course } from '../types';
|
||||
import { getCourses, getLearningPaths, PublicLearningPath } from '../services/db';
|
||||
import { courseApi, InstructorSummary, profileApi } from '../services/api';
|
||||
import { useAuth } from './AuthContext';
|
||||
|
||||
/** The three area tones from the design; every category is shown in one of them. */
|
||||
export type AreaTone = 'midia' | 'conv' | 'oper';
|
||||
const TONES: AreaTone[] = ['midia', 'conv', 'oper'];
|
||||
|
||||
export interface Professor {
|
||||
id: string;
|
||||
name: string;
|
||||
courses: Course[];
|
||||
headline?: string;
|
||||
avatarImageUrl?: string | null;
|
||||
}
|
||||
|
||||
interface CatalogContextType {
|
||||
courses: Course[];
|
||||
paths: PublicLearningPath[];
|
||||
/** Categories that have published courses, in the admin-defined order. */
|
||||
areas: string[];
|
||||
professors: Professor[];
|
||||
isLoading: boolean;
|
||||
failed: boolean;
|
||||
reload: () => Promise<void>;
|
||||
courseById: (courseId: string) => Course | undefined;
|
||||
toneOf: (category: string) => AreaTone;
|
||||
pathTone: (path: PublicLearningPath) => AreaTone;
|
||||
}
|
||||
|
||||
const CatalogContext = createContext<CatalogContextType | undefined>(undefined);
|
||||
|
||||
export const CatalogProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const { user, isLoading: authLoading } = useAuth();
|
||||
const [courses, setCourses] = useState<Course[]>([]);
|
||||
const [paths, setPaths] = useState<PublicLearningPath[]>([]);
|
||||
const [instructorProfiles, setInstructorProfiles] = useState<InstructorSummary[]>([]);
|
||||
const [categoryOrder, setCategoryOrder] = useState<string[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [failed, setFailed] = useState(false);
|
||||
|
||||
const reload = useCallback(async () => {
|
||||
setFailed(false);
|
||||
try {
|
||||
const [loadedCourses, loadedPaths, categories, instructors] = await Promise.all([
|
||||
getCourses(),
|
||||
getLearningPaths().catch(() => [] as PublicLearningPath[]),
|
||||
courseApi.categories().then((response) => response.data).catch(() => [] as string[]),
|
||||
profileApi.instructors().then((response) => response.data).catch(() => [] as InstructorSummary[]),
|
||||
]);
|
||||
setCourses(loadedCourses);
|
||||
setPaths(loadedPaths);
|
||||
setCategoryOrder(categories);
|
||||
setInstructorProfiles(instructors);
|
||||
} catch {
|
||||
// Never replace a failed API with fake catalogue data: the screens use the
|
||||
// `failed` flag to show an honest loading error instead.
|
||||
setFailed(true);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Progress on each course belongs to the signed-in learner, so reload when the session changes.
|
||||
useEffect(() => {
|
||||
if (authLoading) return;
|
||||
void reload();
|
||||
}, [authLoading, reload, user?.id]);
|
||||
|
||||
const value = useMemo<CatalogContextType>(() => {
|
||||
const present: string[] = Array.from(new Set(courses.map((course) => course.category).filter(Boolean)));
|
||||
const ordered = [
|
||||
...categoryOrder.filter((category) => present.includes(category)),
|
||||
...present.filter((category) => !categoryOrder.includes(category)).sort((a, b) => a.localeCompare(b, 'pt-BR')),
|
||||
];
|
||||
const toneIndex = new Map(ordered.map((category, index) => [category, index]));
|
||||
const toneOf = (category: string): AreaTone => TONES[(toneIndex.get(category) ?? 0) % TONES.length];
|
||||
const byId = new Map(courses.map((course) => [course.id, course]));
|
||||
|
||||
const professorMap = new Map<string, Professor>();
|
||||
instructorProfiles.forEach((professor) => {
|
||||
professorMap.set(professor.id, {
|
||||
id: professor.id,
|
||||
name: professor.name,
|
||||
headline: professor.headline || undefined,
|
||||
avatarImageUrl: professor.avatarImageUrl,
|
||||
courses: [],
|
||||
});
|
||||
});
|
||||
courses.forEach((course) => {
|
||||
if (!course.instructorId) return;
|
||||
const entry = professorMap.get(course.instructorId) || {
|
||||
id: course.instructorId,
|
||||
name: course.instructor || 'Professor',
|
||||
headline: undefined,
|
||||
avatarImageUrl: null,
|
||||
courses: [],
|
||||
};
|
||||
entry.courses.push(course);
|
||||
professorMap.set(course.instructorId, entry);
|
||||
});
|
||||
|
||||
return {
|
||||
courses,
|
||||
paths,
|
||||
areas: ordered,
|
||||
professors: Array.from(professorMap.values()).sort((a, b) => a.name.localeCompare(b.name, 'pt-BR')),
|
||||
isLoading,
|
||||
failed,
|
||||
reload,
|
||||
courseById: (courseId: string) => byId.get(courseId),
|
||||
toneOf,
|
||||
pathTone: (path: PublicLearningPath) => {
|
||||
const counts = new Map<string, number>();
|
||||
path.courses.forEach((course) => counts.set(course.category, (counts.get(course.category) || 0) + 1));
|
||||
const main = [...counts.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
|
||||
return main ? toneOf(main) : 'midia';
|
||||
},
|
||||
};
|
||||
}, [categoryOrder, courses, failed, isLoading, instructorProfiles, paths, reload]);
|
||||
|
||||
return <CatalogContext.Provider value={value}>{children}</CatalogContext.Provider>;
|
||||
};
|
||||
|
||||
export const useCatalog = () => {
|
||||
const context = useContext(CatalogContext);
|
||||
if (!context) throw new Error('useCatalog must be used within CatalogProvider');
|
||||
return context;
|
||||
};
|
||||
39
context/ThemeContext.tsx
Normal file
39
context/ThemeContext.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
||||
import { readPreference, writePreference } from '../lib/storage';
|
||||
|
||||
type Theme = 'light' | 'dark';
|
||||
|
||||
interface ThemeContextType {
|
||||
theme: Theme;
|
||||
toggleTheme: () => void;
|
||||
}
|
||||
|
||||
const THEME_KEY = 'ca-theme';
|
||||
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
|
||||
|
||||
// Light by default; the choice is saved and the system preference is not followed.
|
||||
const initialTheme = (): Theme => (readPreference(THEME_KEY) === 'dark' ? 'dark' : 'light');
|
||||
|
||||
export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [theme, setTheme] = useState<Theme>(initialTheme);
|
||||
|
||||
useEffect(() => {
|
||||
document.documentElement.classList.toggle('dark', theme === 'dark');
|
||||
}, [theme]);
|
||||
|
||||
const toggleTheme = useCallback(() => {
|
||||
setTheme((current) => {
|
||||
const next = current === 'dark' ? 'light' : 'dark';
|
||||
writePreference(THEME_KEY, next);
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
return <ThemeContext.Provider value={{ theme, toggleTheme }}>{children}</ThemeContext.Provider>;
|
||||
};
|
||||
|
||||
export const useTheme = () => {
|
||||
const context = useContext(ThemeContext);
|
||||
if (!context) throw new Error('useTheme must be used within ThemeProvider');
|
||||
return context;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import React, { createContext, useContext, useState, useCallback } from 'react';
|
||||
import { CheckCircle2, XCircle, Info, Download, X, FileText, Check } from 'lucide-react';
|
||||
import { CheckCircle, DownloadSimple, Info, WarningCircle, X } from '@phosphor-icons/react';
|
||||
|
||||
export type ToastType = 'success' | 'error' | 'info' | 'download';
|
||||
|
||||
@@ -18,99 +18,47 @@ export interface ToastContextType {
|
||||
|
||||
const ToastContext = createContext<ToastContextType | undefined>(undefined);
|
||||
|
||||
const icons: Record<ToastType, React.ReactNode> = {
|
||||
success: <CheckCircle className="ph" />,
|
||||
error: <WarningCircle className="ph" />,
|
||||
info: <Info className="ph" />,
|
||||
download: <DownloadSimple className="ph" />,
|
||||
};
|
||||
|
||||
export const ToastProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
|
||||
const showToast = useCallback((message: string, type: ToastType = 'success', fileName?: string, fileSize?: string) => {
|
||||
const push = useCallback((toast: Omit<Toast, 'id'>, duration: number) => {
|
||||
const id = Date.now().toString() + Math.random().toString(36).substring(2, 5);
|
||||
setToasts((prev) => [...prev, { id, message, type, fileName, fileSize }]);
|
||||
setTimeout(() => {
|
||||
setToasts((prev) => prev.filter((t) => t.id !== id));
|
||||
}, 4000);
|
||||
setToasts((prev) => [...prev, { ...toast, id }]);
|
||||
setTimeout(() => setToasts((prev) => prev.filter((t) => t.id !== id)), duration);
|
||||
}, []);
|
||||
|
||||
const showToast = useCallback((message: string, type: ToastType = 'success', fileName?: string, fileSize?: string) => {
|
||||
push({ message, type, fileName, fileSize }, 4000);
|
||||
}, [push]);
|
||||
|
||||
const showDownloadToast = useCallback((fileName: string, fileSize?: string) => {
|
||||
const id = Date.now().toString() + Math.random().toString(36).substring(2, 5);
|
||||
setToasts((prev) => [...prev, {
|
||||
id,
|
||||
message: `O material "${fileName}" foi baixado com sucesso.`,
|
||||
type: 'download',
|
||||
fileName,
|
||||
fileSize
|
||||
}]);
|
||||
setTimeout(() => {
|
||||
setToasts((prev) => prev.filter((t) => t.id !== id));
|
||||
}, 4500);
|
||||
}, []);
|
||||
push({ message: `O material "${fileName}" foi baixado com sucesso.`, type: 'download', fileName, fileSize }, 4500);
|
||||
}, [push]);
|
||||
|
||||
const removeToast = (id: string) => {
|
||||
setToasts((prev) => prev.filter((t) => t.id !== id));
|
||||
};
|
||||
const removeToast = (id: string) => setToasts((prev) => prev.filter((t) => t.id !== id));
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{ showToast, showDownloadToast }}>
|
||||
{children}
|
||||
<div className="fixed bottom-6 right-6 z-[300] flex flex-col gap-3 pointer-events-none">
|
||||
{toasts.map((toast) => {
|
||||
if (toast.type === 'download') {
|
||||
return (
|
||||
<div
|
||||
key={toast.id}
|
||||
className="pointer-events-auto relative overflow-hidden flex items-center gap-3.5 bg-[#0d0e12]/95 backdrop-blur-2xl border border-emerald-500/30 text-white p-4 rounded-2xl shadow-2xl shadow-emerald-500/15 animate-fade-in-up min-w-[320px] max-w-[420px]"
|
||||
>
|
||||
{/* Emerald vertical bar */}
|
||||
<div className="absolute left-0 top-0 bottom-0 w-1 bg-gradient-to-b from-emerald-400 to-teal-500"></div>
|
||||
|
||||
<div className="w-10 h-10 rounded-xl bg-emerald-500/15 border border-emerald-500/30 flex items-center justify-center shrink-0 text-emerald-400 shadow-inner">
|
||||
<Download className="w-5 h-5 animate-bounce" style={{ animationIterationCount: 2 }} />
|
||||
</div>
|
||||
|
||||
<div className="flex-1 min-w-0 pr-1">
|
||||
<div className="flex items-center gap-2 mb-0.5">
|
||||
<span className="text-[10px] font-extrabold tracking-wider uppercase text-emerald-400 bg-emerald-500/15 px-2 py-0.5 rounded-full border border-emerald-500/30 flex items-center gap-1">
|
||||
<CheckCircle2 className="w-3 h-3 text-emerald-400" />
|
||||
Download Concluído
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-sm font-bold text-white truncate leading-tight" title={toast.fileName || toast.message}>
|
||||
{toast.fileName || toast.message}
|
||||
</p>
|
||||
<div className="flex items-center gap-2 text-xs text-slate-400 mt-1">
|
||||
<span>Salvo no seu dispositivo</span>
|
||||
{toast.fileSize && (
|
||||
<>
|
||||
<span>•</span>
|
||||
<span className="font-mono text-emerald-400/90 font-medium">{toast.fileSize}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={() => removeToast(toast.id)}
|
||||
className="text-slate-500 hover:text-white p-1 rounded-lg hover:bg-white/10 transition-colors shrink-0"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
key={toast.id}
|
||||
className="pointer-events-auto flex items-center gap-3 bg-[#121318]/95 backdrop-blur-2xl border border-white/10 text-white px-5 py-4 rounded-2xl shadow-2xl animate-fade-in-up min-w-[300px]"
|
||||
>
|
||||
{toast.type === 'success' && <CheckCircle2 className="w-5 h-5 text-emerald-400 shrink-0" />}
|
||||
{toast.type === 'error' && <XCircle className="w-5 h-5 text-rose-400 shrink-0" />}
|
||||
{toast.type === 'info' && <Info className="w-5 h-5 text-sky-400 shrink-0" />}
|
||||
<p className="flex-1 text-sm font-medium">{toast.message}</p>
|
||||
<button onClick={() => removeToast(toast.id)} className="text-slate-500 hover:text-white shrink-0">
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
<div className="toasts" role="status" aria-live="polite">
|
||||
{toasts.map((toast) => (
|
||||
<div key={toast.id} className={`toast ${toast.type}`}>
|
||||
{icons[toast.type]}
|
||||
{toast.type === 'download' ? (
|
||||
<p>{toast.fileName}<small>Baixado{toast.fileSize ? ` · ${toast.fileSize}` : ''}</small></p>
|
||||
) : (
|
||||
<p>{toast.message}</p>
|
||||
)}
|
||||
<button type="button" className="iconbtn" onClick={() => removeToast(toast.id)} aria-label="Fechar aviso"><X className="ph" /></button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
@@ -121,4 +69,3 @@ export const useToast = () => {
|
||||
if (!context) throw new Error('useToast must be used within ToastProvider');
|
||||
return context;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user