"use client"; import { useStagedSettings } from "@/features/settings/store/useStagedSettings"; import { useEffect, useRef, useState } from "react"; import { KeyRound, Library, ShieldOff, SlidersHorizontal, } from "lucide-react"; import { useTranslation } from "react-i18next"; import { GuardianReadingReport } from "@/features/multi-user/components/GuardianReadingReport"; import { ConfirmDialog } from "@/components/ui/ConfirmDialog"; import { getGuardianMaterials, getGuardianReport, getGuardianRestrictions, listGuardianRelationships, resetLearnerCredentials, revokeMyGuardianRelationship, type GuardianExtension, type GuardianMaterial, type GuardianRelationship, type GuardianReport, type GuardianRestrictions, } from "@/lib/guardian-api"; type BusyAction = | "loading" | "materials" | "restrictions" | "credentials" | "revoke" | null; type ConfirmAction = "credentials" | "revoke" | null; const permissionKeys: Record = { assign_materials: "Manage materials", manage_restrictions: "Manage restrictions", reset_credentials: "Reset credentials", view_reports: "View reports", }; export default function GuardianSettingsPage() { const { t } = useTranslation(); const selectionGeneration = useRef(0); const [relationships, setRelationships] = useState( [], ); const [selectedRelationship, setSelectedRelationship] = useState(null); const [report, setReport] = useState(null); const [materials, setMaterials] = useState([]); const [liveMaterialIds, setLiveMaterialIds] = useState([]); const [selectedMaterialIds, setSelectedMaterialIds] = useStagedSettings(`guardian:materials:${selectedRelationship?.learner_user_id ?? ""}`, liveMaterialIds, setLiveMaterialIds); const [liveRestrictions, setLiveRestrictions] = useState( null, ); const [restrictions, setRestrictions] = useStagedSettings(`guardian:restrictions:${selectedRelationship?.learner_user_id ?? ""}`, liveRestrictions, setLiveRestrictions); const [extensions, setExtensions] = useState([]); const [newPassword, setNewPassword] = useState(""); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(null); const [confirmAction, setConfirmAction] = useState(null); const [message, setMessage] = useState(null); const [error, setError] = useState(null); useEffect(() => { let cancelled = false; void listGuardianRelationships() .then((value) => { if (!cancelled) setRelationships(value); }) .catch((reason: unknown) => { if (!cancelled) setError((reason as Error).message); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; selectionGeneration.current += 1; }; }, []); const can = (permission: string) => selectedRelationship?.permissions.includes(permission) ?? false; const selectLearner = async (relationship: GuardianRelationship) => { const generation = ++selectionGeneration.current; setSelectedRelationship(relationship); setReport(null); setMaterials([]); setLiveMaterialIds([]); setLiveRestrictions(null); setExtensions([]); setNewPassword(""); setMessage(null); setError(null); setBusy("loading"); try { const [nextReport, nextMaterials, nextRestrictions] = await Promise.all([ relationship.permissions.includes("view_reports") ? getGuardianReport(relationship.learner_user_id) : Promise.resolve(null), relationship.permissions.includes("assign_materials") ? getGuardianMaterials(relationship.learner_user_id) : Promise.resolve(null), relationship.permissions.includes("manage_restrictions") ? getGuardianRestrictions(relationship.learner_user_id) : Promise.resolve(null), ]); if (generation !== selectionGeneration.current) return; setReport(nextReport); if (nextMaterials) { setMaterials(nextMaterials); setLiveMaterialIds( nextMaterials .filter((item) => item.assigned) .map((item) => item.book_id), ); } if (nextRestrictions) { setLiveRestrictions(nextRestrictions.restrictions); setExtensions(nextRestrictions.available_extensions); } } catch (reason) { if (generation === selectionGeneration.current) setError((reason as Error).message); } finally { if (generation === selectionGeneration.current) setBusy(null); } }; const resetCredentials = async () => { if ( !selectedRelationship || !can("reset_credentials") || newPassword.length < 8 ) { return; } setConfirmAction(null); setBusy("credentials"); setError(null); setMessage(null); try { await resetLearnerCredentials( selectedRelationship.learner_user_id, newPassword, ); setNewPassword(""); setMessage(t("Learner credentials were reset.")); } catch (reason) { setError((reason as Error).message); } finally { setBusy(null); } }; const revokeRelationship = async () => { if (!selectedRelationship) return; setConfirmAction(null); setBusy("revoke"); setError(null); setMessage(null); try { await revokeMyGuardianRelationship(selectedRelationship.id); setRelationships((current) => current.filter((item) => item.id !== selectedRelationship.id), ); setSelectedRelationship(null); setReport(null); setMessage(t("Guardian access revoked.")); } catch (reason) { setError((reason as Error).message); } finally { setBusy(null); } }; return (

{t("Guardian management")}

{t("Review authorized learners and their approved learning materials.")}

{error && (

{error}

)} {message && (

{message}

)} {loading ? (

{t("Loading guardian relationships…")}

) : relationships.length === 0 ? (

{t("No active learner relationships.")}

) : (
{relationships.map((relationship) => ( ))}
)} {selectedRelationship && (

{report?.learner.username ?? selectedRelationship.learner_username}

{busy === "loading" ? (

{t("Loading learner details…")}

) : can("view_reports") && report ? (
{t("Approved materials")}
{report.assigned_materials.length}
{t("Enabled learning resources")}
{report.grant_summary.model_count + report.grant_summary.knowledge_base_count + report.grant_summary.skill_count}
) : !can("view_reports") ? (

{t("You are not authorized to view this learner report.")}

) : null} {busy !== "loading" && can("view_reports") && report?.reading && report.learner.id === selectedRelationship.learner_user_id && ( )} {can("assign_materials") && (

{t("Approved materials")}

{materials.length === 0 ? (

{t("No approved materials are available.")}

) : (
{materials.map((material) => ( ))}
)}
)} {can("manage_restrictions") && restrictions && (

{t("Learning restrictions")}

{extensions.map((extension) => ( ))}
)} {can("reset_credentials") && (
)}
)} setConfirmAction(null)} onConfirm={() => { if (confirmAction === "credentials") void resetCredentials(); if (confirmAction === "revoke") void revokeRelationship(); }} > {confirmAction === "credentials" ? t( "This changes the learner password and revokes every learner device credential.", ) : t("This immediately removes your access to this learner account.")}
); }