2023-06-26 10:07:12 +00:00
|
|
|
"use client";
|
|
|
|
|
|
2023-10-16 09:20:40 +02:00
|
|
|
import { FC, useState, useEffect, useRef } from "react";
|
2023-06-26 10:07:12 +00:00
|
|
|
import { useRouter } from "next/navigation";
|
2023-05-24 20:27:57 +00:00
|
|
|
import Iframe from "react-iframe";
|
2023-09-08 16:34:13 +02:00
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
|
import { Box, Grid, CircularProgress } from "@mui/material";
|
2023-05-24 20:27:57 +00:00
|
|
|
|
2023-06-26 10:07:12 +00:00
|
|
|
type ZammadWrapperProps = {
|
2023-05-24 20:27:57 +00:00
|
|
|
path: string;
|
|
|
|
|
hideSidebar?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-26 10:07:12 +00:00
|
|
|
export const ZammadWrapper: FC<ZammadWrapperProps> = ({
|
2023-05-24 20:27:57 +00:00
|
|
|
path,
|
|
|
|
|
hideSidebar = true,
|
|
|
|
|
}) => {
|
|
|
|
|
const router = useRouter();
|
2023-10-16 09:20:40 +02:00
|
|
|
const { data: session } = useSession({ required: true });
|
|
|
|
|
const timeoutRef = useRef(null);
|
2023-11-10 14:17:09 +01:00
|
|
|
const [hashCheckComplete, setHashCheckComplete] = useState(false);
|
2023-09-08 16:34:13 +02:00
|
|
|
const [authenticated, setAuthenticated] = useState(false);
|
2023-07-11 10:05:52 +00:00
|
|
|
const [display, setDisplay] = useState("none");
|
2023-08-25 07:11:33 +00:00
|
|
|
const url = `/zammad${path}`;
|
2023-09-08 16:34:13 +02:00
|
|
|
const id = url.replace(/[^a-zA-Z0-9]/g, "");
|
2023-05-24 20:27:57 +00:00
|
|
|
|
2023-09-08 16:34:13 +02:00
|
|
|
useEffect(() => {
|
2023-11-10 14:17:09 +01:00
|
|
|
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;
|
|
|
|
|
|
2023-09-08 16:34:13 +02:00
|
|
|
const checkAuthenticated = async () => {
|
|
|
|
|
const res = await fetch("/zammad/auth/sso", {
|
|
|
|
|
method: "GET",
|
|
|
|
|
redirect: "manual",
|
|
|
|
|
});
|
|
|
|
|
if (res.type === "opaqueredirect") {
|
|
|
|
|
setAuthenticated(true);
|
|
|
|
|
} else {
|
|
|
|
|
setAuthenticated(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
checkAuthenticated();
|
2023-11-10 14:17:09 +01:00
|
|
|
}, [path, hashCheckComplete]);
|
2023-09-08 16:34:13 +02:00
|
|
|
|
2023-10-16 09:20:40 +02:00
|
|
|
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) {
|
2023-09-08 16:34:13 +02:00
|
|
|
return (
|
|
|
|
|
<Box sx={{ width: "100%" }}>
|
|
|
|
|
<Grid
|
|
|
|
|
container
|
|
|
|
|
direction="column"
|
|
|
|
|
sx={{ height: 500 }}
|
|
|
|
|
justifyContent="center"
|
|
|
|
|
alignContent="center"
|
|
|
|
|
alignItems="center"
|
|
|
|
|
>
|
|
|
|
|
<Grid item>
|
|
|
|
|
<CircularProgress size={80} color="success" />
|
|
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (session && authenticated) {
|
|
|
|
|
return (
|
|
|
|
|
<Iframe
|
|
|
|
|
id={id}
|
|
|
|
|
url={url}
|
|
|
|
|
width="100%"
|
|
|
|
|
height="100%"
|
|
|
|
|
frameBorder={0}
|
|
|
|
|
styles={{ display }}
|
|
|
|
|
onLoad={() => {
|
|
|
|
|
const linkElement = document.querySelector(
|
|
|
|
|
`#${id}`,
|
|
|
|
|
) as HTMLIFrameElement;
|
2023-08-25 07:11:33 +00:00
|
|
|
if (
|
2023-09-08 16:34:13 +02:00
|
|
|
linkElement.contentDocument &&
|
|
|
|
|
linkElement.contentDocument?.querySelector &&
|
|
|
|
|
linkElement.contentDocument.querySelector("#navigation") &&
|
|
|
|
|
linkElement.contentDocument.querySelector("body")
|
2023-08-25 07:11:33 +00:00
|
|
|
) {
|
2023-05-24 20:27:57 +00:00
|
|
|
// @ts-ignore
|
2023-09-08 16:34:13 +02:00
|
|
|
linkElement.contentDocument.querySelector("#navigation").style =
|
2023-05-24 20:27:57 +00:00
|
|
|
"display: none";
|
2023-09-08 16:34:13 +02:00
|
|
|
// @ts-ignore
|
|
|
|
|
linkElement.contentDocument.querySelector("body").style =
|
|
|
|
|
"font-family: Arial";
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
hideSidebar &&
|
|
|
|
|
linkElement.contentDocument.querySelector(".sidebar")
|
|
|
|
|
) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
linkElement.contentDocument.querySelector(".sidebar").style =
|
|
|
|
|
"display: none";
|
|
|
|
|
}
|
2023-05-24 20:27:57 +00:00
|
|
|
|
|
|
|
|
// @ts-ignore
|
2023-09-08 16:34:13 +02:00
|
|
|
if (linkElement.contentDocument.querySelector(".overview-header")) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
(
|
|
|
|
|
linkElement.contentDocument.querySelector(
|
|
|
|
|
".overview-header",
|
|
|
|
|
) as any
|
|
|
|
|
).style = "display: none";
|
|
|
|
|
}
|
2023-05-24 20:27:57 +00:00
|
|
|
|
2023-09-08 16:34:13 +02:00
|
|
|
setDisplay("inherit");
|
2023-05-24 20:27:57 +00:00
|
|
|
|
2023-09-08 16:34:13 +02:00
|
|
|
if (linkElement.contentWindow) {
|
|
|
|
|
linkElement.contentWindow.addEventListener("hashchange", () => {
|
|
|
|
|
const hash = linkElement.contentWindow?.location?.hash ?? "";
|
|
|
|
|
if (hash.startsWith("#ticket/zoom/")) {
|
|
|
|
|
setDisplay("none");
|
|
|
|
|
const ticketID = hash.split("/").pop();
|
|
|
|
|
router.push(`/tickets/${ticketID}`);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setDisplay("inherit");
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-05-24 20:27:57 +00:00
|
|
|
}
|
2023-09-08 16:34:13 +02:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-05-24 20:27:57 +00:00
|
|
|
};
|