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

84 lines
2.5 KiB
TypeScript
Raw Normal View History

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-05-24 20:27:57 +00:00
import { useRouter } from "next/router";
import Iframe from "react-iframe";
type InternalZammadWrapperProps = {
path: string;
hideSidebar?: boolean;
};
export const InternalZammadWrapper: FC<InternalZammadWrapperProps> = ({
path,
hideSidebar = true,
}) => {
const router = useRouter();
const [display, setDisplay] = useState("none");
2023-05-30 09:05:40 +00:00
const {
publicRuntimeConfig: { linkURL },
} = getConfig();
const url = `${linkURL}/proxy/zammad${path}`;
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");
2023-05-30 09:05:40 +00:00
// const baseElement = linkElement.contentDocument.createElement("base");
// baseElement.href = `${linkURL}/proxy/zammad`;
2023-05-24 20:27:57 +00:00
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:05:40 +00:00
linkElement.contentDocument.querySelector(
".overview-header"
).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);
}
});
}
}
}}
/>
);
};