Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 6x 6x 1x 1x 5x 5x 5x 5x 1x 1x 4x 5x 5x 16x 16x 6x 16x 4x 4x 16x 1x 15x 16x 1x 10x 10x 10x 10x 4x 4x 3x 3x 3x 3x 1x 3x 3x 4x 10x | import { useEffect, useState } from "react"; import { StudentStatus, UserClass } from "../api/types/user"; import { useAuth } from "./useAuth"; import { getUserClasses } from "../api/user"; import { supabase } from "../supabaseClient"; /** * UserClassInfo interface represents the structure of user class information. * It contains the user class and the student's status in that class. */ export interface UserClassInfo { userClass: UserClass; studentStatus: StudentStatus; } /** * Custom hook to fetch user classes based on user ID or authenticated user. * @param {string} userID - Optional user ID to fetch specific user classes. * @returns {Object} - Contains classes, loading state, error message, and selected class information. */ export const useUserClasses = (userID?: string | null) => { const { user } = useAuth(); const [classes, setClasses] = useState<UserClassInfo[]>([]); const [selectedClassId, setSelectedClassId] = useState<string | null>(null); const [selectedClassType, setSelectedClassType] = useState< "all" | "class" | "non-class" >("all"); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); useEffect(() => { const currentUserId = userID ?? user?.id; if (!currentUserId) { setLoading(false); return; } const fetchClasses = async () => { try { const { data, error } = await getUserClasses(currentUserId); if (error) { setError(error); return; } setClasses(data || []); } catch (err) { setError(err instanceof Error ? err.message : "Unknown error occurred"); } finally { setLoading(false); } }; fetchClasses(); }, [userID, user?.id]); const specialClasses = [ { id: "all", classTitle: "All", classCode: "", classHexColor: "#e5e5e5", }, { id: "non-class", classTitle: "Non-class Activities", classCode: "", classHexColor: "#404040", }, ]; const modifiedClasses = [ specialClasses[0], ...classes.map((c) => c.userClass), specialClasses[1], ]; const handleClassSelect = (selection: { id: string | null; type: "all" | "class" | "non-class"; }) => { setSelectedClassId( selection.type === "class" ? selection.id : selection.type ); setSelectedClassType(selection.type); }; const selectedClass = selectedClassType === "class" ? classes.find((c) => c.userClass.id === selectedClassId) || null : { userClass: specialClasses.find((c) => c.id === selectedClassType)!, studentStatus: null, }; return { classes: modifiedClasses, originalClasses: classes, selectedClassId: selectedClassType === "class" ? selectedClassId : selectedClassType, selectedClassType, selectedClass, loading, error, handleClassSelect, }; }; export const useUserClassStatus = ( studentId: string | null, classId: string | null ) => { const [studentStatus, setStudentStatus] = useState<StudentStatus | null>( null ); const [loading, setLoading] = useState(false); const [error, setError] = useState<string | null>(null); useEffect(() => { const fetchStatus = async () => { if (!studentId || !classId) return; setLoading(true); setError(null); const { data, error } = await supabase .from("class_users") .select("user_class_status") .eq("student_id", studentId) .eq("class_id", classId) .single(); if (error && error.code !== "PGRST116") { setError(error.message); } setStudentStatus(data?.user_class_status || null); setLoading(false); }; fetchStatus(); }, [studentId, classId]); return { studentStatus, loading, error }; }; |