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

83 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-06-26 10:07:12 +00:00
"use client";
2023-05-24 20:27:57 +00:00
import { FC, useState } from "react";
2023-05-30 09:05:40 +00:00
import getConfig from "next/config";
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-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-07-11 10:05:52 +00:00
const [display, setDisplay] = useState("none");
const url = `/proxy/zammad${path}`;
2023-05-30 09:05:40 +00:00
console.log({ url });
2023-05-24 20:27:57 +00:00
return (
// @ts-ignore
<Iframe
2023-05-30 09:05:40 +00:00
id="zammad"
2023-05-24 20:27:57 +00:00
url={url}
width="100%"
height="100%"
frameBorder={0}
styles={{ display }}
onLoad={() => {
const linkElement = document.querySelector("iframe");
if (
linkElement.contentDocument &&
linkElement.contentDocument?.querySelector &&
linkElement.contentDocument.querySelector("#navigation") &&
linkElement.contentDocument.querySelector("body") &&
linkElement.contentDocument.querySelector(".sidebar")
) {
// @ts-ignore
linkElement.contentDocument.querySelector("#navigation").style =
"display: none";
// @ts-ignore
linkElement.contentDocument.querySelector("body").style =
"font-family: Arial";
if (hideSidebar) {
// @ts-ignore
linkElement.contentDocument.querySelector(".sidebar").style =
"display: none";
}
// @ts-ignore
if (linkElement.contentDocument.querySelector(".overview-header")) {
// @ts-ignore
2023-05-30 09:52:49 +00:00
(
linkElement.contentDocument.querySelector(
".overview-header"
) as any
2023-05-30 09:05:40 +00:00
).style = "display: none";
2023-05-24 20:27:57 +00:00
}
setDisplay("inherit");
if (linkElement.contentWindow) {
2023-05-30 09:05:40 +00:00
linkElement.contentWindow.addEventListener("hashchange", () => {
const hash = linkElement.contentWindow?.location?.hash ?? "";
2023-05-24 20:27:57 +00:00
if (hash.startsWith("#ticket/zoom/")) {
setDisplay("none");
const ticketID = hash.split("/").pop();
router.push(`/tickets/${ticketID}`);
setTimeout(() => {
setDisplay("inherit");
}, 1000);
}
});
}
}
}}
/>
);
};