diff --git a/src/app/dashboard/[subdomain]/settings/css/page-component.tsx b/src/app/dashboard/[subdomain]/settings/css/page-component.tsx index c26c55bf..83e52a9a 100644 --- a/src/app/dashboard/[subdomain]/settings/css/page-component.tsx +++ b/src/app/dashboard/[subdomain]/settings/css/page-component.tsx @@ -1,13 +1,17 @@ "use client" import { useParams } from "next/navigation" -import { useEffect, useState } from "react" +import { FC, useEffect, useRef, useState } from "react" import toast from "react-hot-toast" +import { useQuery } from "@tanstack/react-query" + import { MonacoEditor } from "~/components/common/Monaco" import { SettingsLayout } from "~/components/dashboard/SettingsLayout" import { Button } from "~/components/ui/Button" import { FieldLabel } from "~/components/ui/FieldLabel" +import { useGetState } from "~/hooks/useGetState" +import { getSiteLink } from "~/lib/helpers" import { useTranslation } from "~/lib/i18n/client" import { useGetSite, useUpdateSite } from "~/queries/site" @@ -18,7 +22,6 @@ export default function SettingsCSSPage() { const updateSite = useUpdateSite() const site = useGetSite(subdomain) const { t } = useTranslation("dashboard") - const [css, setCss] = useState("") const handleSubmit = (e: any) => { e.preventDefault() @@ -106,7 +109,12 @@ export default function SettingsCSSPage() { }} /> -
+
+ @@ -115,3 +123,98 @@ export default function SettingsCSSPage() { ) } + +const PreviewButton: FC<{ + css: string + subdomain: string + custom_domain?: string +}> = ({ css, subdomain, custom_domain }) => { + const { t } = useTranslation("dashboard") + + const { data: siteLink } = useQuery({ + queryKey: ["site_link", subdomain], + + queryFn: async () => { + return getSiteLink({ + subdomain, + domain: custom_domain, + }) + }, + }) + + const datasetRef = useRef({ + previewWindowOrigin: "", + previewWindow: null as null | Window, + }).current + const [isInPreview, setIsPreview] = useState(false) + + const handlePreview = () => { + if (!siteLink) { + return + } + let url: URL = new URL(siteLink) + + url.searchParams.set("origin", location.origin) + url.searchParams.set("css-preview", "true") + + const finalUrl = url.toString() + + const forkWindow = window.open(finalUrl) + if (!forkWindow) { + toast.error("Failed to open preview window") + return + } + + datasetRef.previewWindowOrigin = url.origin + datasetRef.previewWindow = forkWindow + setIsPreview(true) + } + + const getCSS = useGetState(css) + + useEffect(() => { + if (!isInPreview) return + + const handler = (e: MessageEvent): void => { + const { previewWindowOrigin, previewWindow } = datasetRef + + if (!isInPreview) return + if (e.origin !== previewWindowOrigin) return + + if (!previewWindow) return + previewWindow.postMessage( + JSON.stringify({ + type: "preview", + data: { css: getCSS() }, + }), + previewWindowOrigin, + ) + } + window.addEventListener("message", handler) + + return () => { + window.removeEventListener("message", handler) + } + }, [isInPreview]) + + const getIsPreview = useGetState(isInPreview) + useEffect(() => { + if (!getIsPreview()) return + + if (!datasetRef.previewWindow) return + + datasetRef.previewWindow.postMessage( + JSON.stringify({ + type: "preview", + data: { css: getCSS() }, + }), + datasetRef.previewWindowOrigin, + ) + }, [css]) + + return ( + + ) +} diff --git a/src/app/site/[site]/layout.tsx b/src/app/site/[site]/layout.tsx index 77286ab7..5f0a7dea 100644 --- a/src/app/site/[site]/layout.tsx +++ b/src/app/site/[site]/layout.tsx @@ -6,8 +6,8 @@ import { Hydrate, dehydrate } from "@tanstack/react-query" import { BlockchainInfo } from "~/components/common/BlockchainInfo" import { SitePlayerContainer } from "~/components/common/SitePlayer" -import Style from "~/components/common/Style" import { BackToTopFAB } from "~/components/site/BackToTopFAB" +import { CustomSiteStyle } from "~/components/site/CustomSiteStyle" import SiteFooter from "~/components/site/SiteFooter" import { SiteHeader } from "~/components/site/SiteHeader" import { FABContainer } from "~/components/ui/FAB" @@ -169,7 +169,7 @@ export default async function SiteLayout({
-