@@ -16,13 +16,14 @@ import { useMutation } from "@tanstack/react-query";
1616import clsx from "clsx" ;
1717import { motion } from "framer-motion" ;
1818import { Check , Globe2 , Search } from "lucide-react" ;
19- import { useCallback , useEffect , useState } from "react" ;
19+ import { useCallback , useEffect , useMemo , useState } from "react" ;
2020import { toast } from "sonner" ;
2121import { shareCap } from "@/actions/caps/share" ;
2222import { useDashboardContext } from "@/app/(org)/dashboard/Contexts" ;
2323import type { Spaces } from "@/app/(org)/dashboard/dashboard-data" ;
2424import { SignedImageUrl } from "@/components/SignedImageUrl" ;
2525import { Tooltip } from "@/components/Tooltip" ;
26+ import { usePublicEnv } from "@/utils/public-env" ;
2627
2728interface SharingDialogProps {
2829isOpen : boolean ;
@@ -182,13 +183,9 @@ export const SharingDialog: React.FC<SharingDialogProps> = ({
182183} ) ;
183184} ;
184185
185- const handleCopyEmbedCode = async ( ) => {
186- const embedCode = `<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="${
187- process . env . NODE_ENV === "development"
188- ? process . env . NEXT_PUBLIC_WEB_URL
189- : "https://cap.so"
190- } /embed/${ capId } " frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>`;
186+ const embedCode = useEmbedCode ( capId ) ;
191187
188+ const handleCopyEmbedCode = async ( ) => {
192189try {
193190await navigator . clipboard . writeText ( embedCode ) ;
194191toast . success ( "Embed code copied to clipboard" ) ;
@@ -325,11 +322,7 @@ export const SharingDialog: React.FC<SharingDialogProps> = ({
325322< div className = "space-y-4" >
326323< div className = "p-3 rounded-lg border bg-gray-3 border-gray-4" >
327324< code className = "font-mono text-xs break-all text-gray-11" >
328- { `<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="${
329- process . env . NODE_ENV === "development"
330- ? process . env . NEXT_PUBLIC_WEB_URL
331- : "https://cap.so"
332- } /embed/${ capId } " frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>`}
325+ { embedCode }
333326</ code >
334327</ div >
335328< Button
@@ -447,3 +440,28 @@ const SpaceCard = ({
447440</ Tooltip >
448441) ;
449442} ;
443+
444+ function useEmbedCode ( capId : Video . VideoId ) {
445+ const publicEnv = usePublicEnv ( ) ;
446+
447+ return useMemo (
448+ ( ) =>
449+ `
450+ <div style="position: relative; padding-bottom: 56.25%; height: 0;">
451+ <iframe
452+ src="${ publicEnv . webUrl } /embed/${ capId } "
453+ frameborder="0"
454+ webkitallowfullscreen
455+ mozallowfullscreen
456+ allowfullscreen
457+ style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"
458+ ></iframe>
459+ </div>
460+ `
461+ . trim ( )
462+ . replace ( / [ \n \t ] + / g, " " )
463+ . replace ( / > \s + < / g, "><" )
464+ . replace ( / " \s + > / g, '">' ) ,
465+ [ publicEnv . webUrl , capId ] ,
466+ ) ;
467+ }
0 commit comments