link-stack/apps/link/components/ZammadWrapper.tsx

81 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-12-02 17:45:14 +01:00
import { FC, useState } from "react";
2023-03-29 14:43:27 +02:00
import { useRouter } from "next/router";
2022-12-02 17:45:14 +01:00
import Iframe from "react-iframe";
type ZammadWrapperProps = {
2023-02-22 13:05:52 +00:00
path: string;
2022-12-02 17:45:14 +01:00
hideSidebar?: boolean;
};
export const ZammadWrapper: FC<ZammadWrapperProps> = ({
2023-02-22 13:05:52 +00:00
path,
2022-12-02 17:45:14 +01:00
hideSidebar = true,
}) => {
2023-03-29 14:43:27 +02:00
const router = useRouter();
2023-03-13 22:14:52 +00:00
const origin =
typeof window !== "undefined" && window.location.origin
? window.location.origin
: "";
2023-03-29 14:43:27 +02:00
const [display, setDisplay] = useState("none");
2023-03-01 11:02:15 +00:00
const url = `${origin}/zammad${path}`;
console.log({ origin, path, url });
2022-12-02 17:45:14 +01:00
return (
2023-03-13 22:14:52 +00:00
// @ts-ignore
<Iframe
2022-12-02 17:45:14 +01:00
id="link"
url={url}
width="100%"
height="100%"
frameBorder={0}
2023-03-01 11:02:15 +00:00
styles={{ display }}
2022-12-02 17:45:14 +01:00
onLoad={() => {
const linkElement = document.querySelector("iframe");
if (
linkElement.contentDocument &&
2023-01-11 16:18:56 +01:00
linkElement.contentDocument?.querySelector &&
linkElement.contentDocument.querySelector("#navigation") &&
linkElement.contentDocument.querySelector("body") &&
linkElement.contentDocument.querySelector(".sidebar")
2022-12-02 17:45:14 +01:00
) {
// @ts-ignore
linkElement.contentDocument.querySelector("#navigation").style =
"display: none";
2022-12-14 13:24:50 +01:00
// @ts-ignore
linkElement.contentDocument.querySelector("body").style =
"font-family: Arial";
2022-12-02 17:45:14 +01:00
if (hideSidebar) {
// @ts-ignore
linkElement.contentDocument.querySelector(".sidebar").style =
"display: none";
}
2023-03-01 11:02:15 +00:00
2023-03-29 14:43:27 +02:00
// @ts-ignore
if (linkElement.contentDocument.querySelector(".overview-header")) {
// @ts-ignore
linkElement.contentDocument.querySelector(".overview-header").style =
"display: none";
}
2023-03-01 11:02:15 +00:00
setDisplay("inherit");
2023-03-29 14:43:27 +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);
}
});
}
2022-12-02 17:45:14 +01:00
}
}}
/>
);
};