infoalloggi-monorepo/apps/infoalloggi/src/components/allegato-iframe.tsx

58 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { useEffect, useRef, useState } from "react";
import { LoadingPage } from "~/components/loading";
2025-08-04 17:45:44 +02:00
import { cn } from "~/lib/utils";
import type { TempTokensToken } from "~/schemas/public/TempTokens";
import { api } from "~/utils/api";
export const AllegatoIframe = ({
2025-08-28 18:27:07 +02:00
className,
allegato,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
className?: string;
allegato: string;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
const if_ref = useRef<HTMLIFrameElement>(null);
const { mutateAsync: getToken } = api.storage.getStorageToken.useMutation();
const [token, setToken] = useState<TempTokensToken | null>(null);
const handleIframeLoad = () => {
if (if_ref.current?.contentWindow) {
const img = if_ref.current.contentWindow.document.querySelector("img");
if (img) {
img.style.width = "100%";
img.style.height = "100%";
img.style.objectFit = "contain";
}
}
};
useEffect(() => {
async function set() {
const token = await getToken();
setToken(token);
}
set().catch((e) => console.error(e));
}, []);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (!token) return <LoadingPage />;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const requestUrl = `/go-api/storage/get/${allegato}?token=${String(token)}`;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<div
className={cn(
"flex h-full w-full items-center justify-center object-contain",
className,
)}
>
{/** biome-ignore lint/a11y/noNoninteractiveElementInteractions: <need interaction> */}
<iframe
2025-08-29 16:18:32 +02:00
allowFullScreen
2025-08-28 18:27:07 +02:00
className="h-full w-full"
onLoad={handleIframeLoad}
2025-08-29 16:18:32 +02:00
ref={if_ref}
src={requestUrl}
title="Allegato"
2025-08-28 18:27:07 +02:00
/>
</div>
);
2025-08-04 17:45:44 +02:00
};