"use client";
import { Box, Stack, Text, Title } from "@mantine/core";
import { IconBrowserOff, IconProtocol } from "@tabler/icons-react";
import { objectEntries } from "@homarr/common";
import { useI18n } from "@homarr/translation/client";
import type { WidgetComponentProps } from "../definition";
import classes from "./component.module.css";
export default function IFrameWidget({ options, isEditMode }: WidgetComponentProps<"iframe">) {
const t = useI18n();
const { embedUrl, ...permissions } = options;
const allowedPermissions = getAllowedPermissions(permissions);
if (embedUrl.trim() === "") return ;
if (!isSupportedProtocol(embedUrl)) {
return ;
}
return (
);
}
const supportedProtocols = ["http", "https"];
const isSupportedProtocol = (url: string) => {
try {
const parsedUrl = new URL(url);
return supportedProtocols.map((protocol) => `${protocol}:`).includes(`${parsedUrl.protocol}`);
} catch {
return false;
}
};
const NoUrl = () => {
const t = useI18n();
return (
{t("widget.iframe.error.noUrl")}
);
};
const UnsupportedProtocol = () => {
const t = useI18n();
return (
{t("widget.iframe.error.unsupportedProtocol", {
supportedProtocols: supportedProtocols.map((protocol) => protocol).join(", "),
})}
);
};
const getAllowedPermissions = (permissions: Omit["options"], "embedUrl">) => {
return objectEntries(permissions)
.filter(([_key, value]) => value)
.map(([key]) => permissionMapping[key]);
};
const permissionMapping = {
allowAutoPlay: "autoplay",
allowCamera: "camera",
allowFullScreen: "fullscreen",
allowGeolocation: "geolocation",
allowMicrophone: "microphone",
allowPayment: "payment",
allowScrolling: "scrolling",
allowTransparency: "transparency",
} satisfies Record["options"], "embedUrl">, string>;