"use client"; import { FC, useState, useEffect, useRef } from "react"; import { useRouter } from "next/navigation"; import Iframe from "react-iframe"; import { useSession } from "next-auth/react"; import { Box, Grid, CircularProgress } from "@mui/material"; type ZammadWrapperProps = { path: string; hideSidebar?: boolean; }; export const ZammadWrapper: FC = ({ path, hideSidebar = true, }) => { const router = useRouter(); const { data: session } = useSession({ required: true }); const timeoutRef = useRef(null); const [hashCheckComplete, setHashCheckComplete] = useState(false); const [authenticated, setAuthenticated] = useState(false); const [display, setDisplay] = useState("none"); const url = `/zammad${path}`; const id = url.replace(/[^a-zA-Z0-9]/g, ""); useEffect(() => { const hash = window?.location?.hash; if (hash && hash.startsWith("#ticket/zoom/")) { const ticketID = hash.split("/").pop(); router.push(`/tickets/${ticketID}`); } setHashCheckComplete(true); }); useEffect(() => { if (!hashCheckComplete) return; const checkAuthenticated = async () => { const res = await fetch("/zammad/auth/sso", { method: "GET", redirect: "manual", }); console.log({ res }); if (res.type === "opaqueredirect") { setAuthenticated(true); } else { setAuthenticated(false); } }; checkAuthenticated(); }, [path, hashCheckComplete]); useEffect(() => { if (session === null) { timeoutRef.current = setTimeout(() => { if (session === null) { router.push("/login"); } }, 3000); } if (session !== null) { clearTimeout(timeoutRef.current); } return () => clearTimeout(timeoutRef.current); }, [session]); if (!session || !authenticated) { console.log("Not authenticated"); return ( ); } if (session && authenticated) { console.log("Session and authenticated"); return (