import { useState, useMemo, useEffect } from 'react'; import { motion } from 'framer-motion'; import { Search, Users, Filter, ArrowUpDown, BookOpen, ChevronRight, Sparkles, X } from 'lucide-react'; import FacultyCard from '@/components/FacultyCard/FacultyCard'; import { StaggerContainer, StaggerItem } from '@/components/AnimatedContainer/AnimatedContainer'; import { FACULTY_DATA, DEPARTMENTS } from '@/constants'; import { Link, useSearchParams } from 'react-router-dom'; import { getBackendUrl } from '@/lib/utils'; const DEPT_MAP: Record = { 'Computer Science & Engineering': { abrv: 'CSE', full: 'Computer Science & Engineering' }, 'Computer Science & Business Systems': { abrv: 'CSBS', full: 'Computer Science & Business Systems' }, 'Artificial Intelligence & Machine Learning': { abrv: 'AIML', full: 'Artificial Intelligence & Machine Learning' }, 'Electronics & Communication Engineering': { abrv: 'ECE', full: 'Electronics & Communication Engineering' }, 'Mechanical Engineering': { abrv: 'MECH', full: 'Mechanical Engineering' }, 'Civil Engineering': { abrv: 'CIVIL', full: 'Civil Engineering' }, 'Artificial Intelligence & Data Science': { abrv: 'AI & DS', full: 'Artificial Intelligence & Data Science' }, 'Electrical & Electronics Engineering': { abrv: 'EEE', full: 'Electrical & Electronics Engineering' }, 'Communication and Computer Engineering': { abrv: 'CCE', full: 'Communication and Computer Engineering' }, 'Electronics Engineering VLSI (Design and Technology)': { abrv: 'EE VLSI (D&T)', full: 'Electronics Engineering VLSI (Design and Technology)' }, 'Humanities & Sciences': { abrv: 'H&S', full: 'Humanities & Sciences' }, 'M.Tech (Data Science)': { abrv: 'M.TECH-DS', full: 'M.Tech (Data Science)' }, 'Mathematics': { abrv: 'MATHS', full: 'Mathematics' }, 'M.E. (VLSI Design)': { abrv: 'MEVLSI', full: 'M.E. (VLSI Design)' }, }; function normalizeDepartmentName(input?: string | null): string | null { if (!input) return null; const cleaned = input.trim(); // Exact match if (DEPARTMENTS.includes(cleaned)) return cleaned; // Search by abbreviation or substring const upper = cleaned.toUpperCase(); for (const [full, info] of Object.entries(DEPT_MAP)) { if (info.abrv.toUpperCase() === upper || full.toUpperCase() === upper || full.toLowerCase().includes(cleaned.toLowerCase())) { return full; } } return null; } export default function Faculty() { const [searchParams] = useSearchParams(); const [searchFaculty, setSearchFaculty] = useState(''); const [sortBy, setSortBy] = useState('Name A-Z'); // Initial department detection (from URL query string or logged-in IMS student session) const initialDepartment = useMemo(() => { const queryDept = searchParams.get('dept'); const matchedQuery = normalizeDepartmentName(queryDept); if (matchedQuery) return matchedQuery; const session = getStoredImsSession(); if (session?.student?.department) { const matchedSession = normalizeDepartmentName(session.student.department); if (matchedSession) return matchedSession; } return 'All Departments'; }, [searchParams]); const [selectedDept, setSelectedDept] = useState(initialDepartment); // Update selectedDept if URL parameter changes useEffect(() => { setSelectedDept(initialDepartment); }, [initialDepartment]); const studentSession = useMemo(() => getStoredImsSession(), []); const isStudentDeptAutoFiltered = selectedDept !== 'All Departments' && ( selectedDept === studentSession?.student?.department || searchParams.has('dept') ); const [facultyList, setFacultyList] = useState(() => { try { const saved = localStorage.getItem('RIT_LOCAL_FACULTY'); if (saved) { const parsed = JSON.parse(saved); if (Array.isArray(parsed) && parsed.length > 0) return parsed; } } catch {} return FACULTY_DATA; }); useEffect(() => { fetch(getBackendUrl('/api/faculty')) .then((res) => { if (!res.ok) throw new Error(); return res.json(); }) .then((data) => { if (Array.isArray(data) && data.length > 0) { setFacultyList(data); localStorage.setItem('RIT_LOCAL_FACULTY', JSON.stringify(data)); } }) .catch(() => {}); }, []); const filteredAndSortedFaculty = useMemo(() => { const filtered = facultyList.filter((f) => { const searchLower = searchFaculty.trim().toLowerCase(); const matchSearch = (f.name && f.name.toLowerCase().includes(searchLower)) || (f.department && f.department.toLowerCase().includes(searchLower)) || (f.designation && f.designation.toLowerCase().includes(searchLower)) || (f.specialization && f.specialization.toLowerCase().includes(searchLower)) || (f.interest && f.interest.toLowerCase().includes(searchLower)); const matchDept = selectedDept === 'All Departments' || f.department === selectedDept; return matchSearch && matchDept; }); return filtered.sort((a, b) => { if (sortBy === 'Name A-Z') return (a.name || '').localeCompare(b.name || ''); if (sortBy === 'Department') return (a.department || '').localeCompare(b.department || ''); if (sortBy === 'Designation') return (a.designation || '').localeCompare(b.designation || ''); return 0; }); }, [facultyList, searchFaculty, selectedDept, sortBy]); const groupedFaculty = useMemo(() => { const groups: Record = {}; filteredAndSortedFaculty.forEach((faculty) => { if (!groups[faculty.department]) { groups[faculty.department] = []; } groups[faculty.department].push(faculty); }); return groups; }, [filteredAndSortedFaculty]); const departmentOrder = useMemo(() => { const presentDepts = Object.keys(groupedFaculty); const sorted = DEPARTMENTS.filter(d => d !== 'All Departments'); presentDepts.forEach(d => { if (!sorted.includes(d)) sorted.push(d); }); return sorted; }, [groupedFaculty]); return (
{/* Background decorations */}
{/* Header */}
Home Faculty Directory

Faculty{' '} Directory

Browse and connect with experienced faculty members across all departments.

{/* Active Student Department Filter Badge */} {isStudentDeptAutoFiltered && (
Student Department Auto-Filter
Showing faculty for your department: {selectedDept}
)} {/* Faculty Hero Section */}

Faculty Directory

Browse and connect with experienced faculty members. Search by name, department, or specialization.

Department Overview

{[ { abrv: 'CSE', full: 'Computer Science & Engineering', color: 'blue' }, { abrv: 'CSBS', full: 'Computer Science & Business Systems', color: 'indigo' }, { abrv: 'AIML', full: 'Artificial Intelligence & Machine Learning', color: 'sky' }, { abrv: 'ECE', full: 'Electronics & Communication Engineering', color: 'purple' }, { abrv: 'MECH', full: 'Mechanical Engineering', color: 'orange' }, { abrv: 'CIVIL', full: 'Civil Engineering', color: 'emerald' }, { abrv: 'AI & DS', full: 'Artificial Intelligence & Data Science', color: 'pink' }, { abrv: 'EEE', full: 'Electrical & Electronics Engineering', color: 'yellow' }, { abrv: 'CCE', full: 'Communication and Computer Engineering', color: 'rose' }, { abrv: 'EE VLSI (D&T)', full: 'Electronics Engineering VLSI (Design and Technology)', color: 'teal' }, { abrv: 'H&S', full: 'Humanities & Sciences', color: 'indigo' }, { abrv: 'M.TECH-DS', full: 'M.Tech (Data Science)', color: 'fuchsia' }, { abrv: 'MATHS', full: 'Mathematics', color: 'blue' }, { abrv: 'MEVLSI', full: 'M.E. (VLSI Design)', color: 'rose' }, ].filter(d => FACULTY_DATA.filter(f => f.department === d.full).length > 0).map(d => { const count = FACULTY_DATA.filter(f => f.department === d.full).length; const isSelected = selectedDept === d.full; return ( ); })}
{/* Search Section */}
setSearchFaculty(e.target.value)} className="w-full pl-12 pr-4 py-3.5 rounded-2xl bg-[#F8FAFC] border-none text-sm text-[#1E293B] placeholder-[#94A3B8] focus:ring-2 focus:ring-[#FF7A00]/20 focus:outline-none transition-all" style={{ fontFamily: 'Inter, sans-serif' }} />
{/* Grouped Grid */} {filteredAndSortedFaculty.length > 0 ? (
{departmentOrder.map((deptName) => { const facultyInDept = groupedFaculty[deptName]; if (!facultyInDept || facultyInDept.length === 0) return null; const deptInfo = DEPT_MAP[deptName] || { abrv: deptName, full: deptName }; return (
{/* Department Header */}

{deptInfo.abrv}

{deptInfo.full}

{/* Department Grid */} {facultyInDept.map((faculty) => ( ))}
); })}
) : (

No faculty found

Try adjusting your search or filters.

)} {/* Bottom Status */}
👥 Showing {filteredAndSortedFaculty.length} Faculty Members
); }