link-stack/apps/link/app/(main)/_components/ZammadWrapper.tsx

170 lines
4.8 KiB
TypeScript
Raw Normal View History

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";
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);
const [hashCheckComplete, setHashCheckComplete] = useState(false);
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}`;
const id = url.replace(/[^a-zA-Z0-9]/g, "");
2023-05-24 20:27:57 +00:00
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",
});
2023-10-16 09:20:40 +02:00
console.log({ res });
if (res.type === "opaqueredirect") {
setAuthenticated(true);
} else {
setAuthenticated(false);
}
};
checkAuthenticated();
}, [path, hashCheckComplete]);
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) {
console.log("Not authenticated");
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) {
console.log("Session and authenticated");
return (
<Iframe
id={id}
url={url}
width="100%"
height="100%"
frameBorder={0}
styles={{ display }}
onLoad={() => {
const linkElement = document.querySelector(
`#${id}`,
) as HTMLIFrameElement;
console.log({ path });
console.log({ id });
console.log({ linkElement });
2023-08-25 07:11:33 +00:00
if (
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
linkElement.contentDocument.querySelector("#navigation").style =
2023-05-24 20:27:57 +00:00
"display: none";
2024-09-09 10:24:15 +02:00
// @ts-ignore
if (linkElement.contentDocument.querySelector(".content")) {
// If navigation removed, set content margin to 0 to avoid gap.
// @ts-ignore
linkElement.contentDocument.querySelector(".content").style =
"margin-left: 0";
}
// @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
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
setDisplay("inherit");
2023-05-24 20:27:57 +00: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-05-24 20:27:57 +00:00
};