import { useEffect, useState } from "react" import { Link, useParams } from "react-router-dom" import { BookOpen, Mic, Briefcase, HelpCircle, ArrowLeft, ArrowRight, ChevronRight } from "lucide-react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "../../components/ui/card" import { Button } from "../../components/ui/button" import { getCourseCategories } from "../../api/courses.api" import type { CourseCategory } from "../../types/course.types" const contentSections = [ { key: "courses", pathFn: (categoryId: string | undefined) => `/content/category/${categoryId}/courses`, icon: BookOpen, title: "Courses", description: "Manage sub-categories, course videos and educational content", action: "Manage Courses", count: 12, countLabel: "courses", gradient: "from-brand-500/10 via-brand-400/5 to-transparent", accentBorder: "group-hover:border-brand-400", }, { key: "speaking", pathFn: () => "/content/speaking", icon: Mic, title: "Speaking", description: "Manage speaking practice sessions and exercises", action: "Manage Speaking", count: 8, countLabel: "sessions", gradient: "from-purple-500/10 via-purple-400/5 to-transparent", accentBorder: "group-hover:border-purple-400", }, { key: "practices", pathFn: () => "/content/practices", icon: Briefcase, title: "Practice", description: "Manage practice details, members, and leadership", action: "Manage Practice", count: 5, countLabel: "practices", gradient: "from-indigo-500/10 via-indigo-400/5 to-transparent", accentBorder: "group-hover:border-indigo-400", }, { key: "questions", pathFn: () => "/content/questions", icon: HelpCircle, title: "Questions", description: "Manage questions, quizzes, and assessments", action: "Manage Questions", count: 34, countLabel: "questions", gradient: "from-rose-500/10 via-rose-400/5 to-transparent", accentBorder: "group-hover:border-rose-400", }, ] as const type ContentSection = (typeof contentSections)[number] export function ContentOverviewPage() { const { categoryId } = useParams<{ categoryId: string }>() const [category, setCategory] = useState(null) const [sections, setSections] = useState(() => [...contentSections]) const [dragKey, setDragKey] = useState(null) const [flowSteps, setFlowSteps] = useState< { id: string type: "lesson" | "practice" | "exam" | "feedback" | "course" | "speaking" | "new_course" title: string description?: string }[] >([]) useEffect(() => { const fetchCategory = async () => { try { const res = await getCourseCategories() const found = res.data.data.categories.find((c) => c.id === Number(categoryId)) setCategory(found ?? null) } catch (err) { console.error("Failed to fetch category:", err) } } if (categoryId) { fetchCategory() } }, [categoryId]) // Load category-level flow sequence (if any) from localStorage useEffect(() => { if (!categoryId) { setFlowSteps([]) return } const key = `category_flow_${categoryId}` try { const raw = window.localStorage.getItem(key) if (raw) { const parsed = JSON.parse(raw) if (Array.isArray(parsed)) { setFlowSteps(parsed) return } } } catch { // ignore and fall back to default } // No explicit flow saved; fall back to an empty sequence setFlowSteps([]) }, [categoryId]) // Load persisted section order from localStorage useEffect(() => { try { const raw = window.localStorage.getItem("content_sections_order") if (!raw) return const savedKeys: string[] = JSON.parse(raw) const byKey = new Map(contentSections.map((s) => [s.key, s])) const reordered: ContentSection[] = [] savedKeys.forEach((k) => { const item = byKey.get(k as ContentSection["key"]) if (item) { reordered.push(item) byKey.delete(k as ContentSection["key"]) } }) // Append any new sections that weren't in saved order byKey.forEach((item) => reordered.push(item)) if (reordered.length) { setSections(reordered) } } catch { // ignore corrupted localStorage } }, []) // Persist order whenever it changes useEffect(() => { const keys = sections.map((s) => s.key) window.localStorage.setItem("content_sections_order", JSON.stringify(keys)) }, [sections]) const handleDropOn = (targetKey: string) => { if (!dragKey || dragKey === targetKey) return setSections((prev) => { const currentIndex = prev.findIndex((s) => s.key === dragKey) const targetIndex = prev.findIndex((s) => s.key === targetKey) if (currentIndex === -1 || targetIndex === -1) return prev const copy = [...prev] const [moved] = copy.splice(currentIndex, 1) copy.splice(targetIndex, 0, moved) return copy }) setDragKey(null) } return (
{/* Header & Breadcrumb */}
Content {category?.name ?? "Overview"}

{category?.name ?? "Content Management"}

{/* Gradient Divider */}