2024-05-08 23:52:08 -07:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import {
|
|
|
|
ChevronLeft,
|
|
|
|
ChevronRight,
|
2024-05-11 19:28:37 -07:00
|
|
|
Globe,
|
2024-05-13 14:38:14 -07:00
|
|
|
Link,
|
2024-05-08 23:52:08 -07:00
|
|
|
RotateCw,
|
|
|
|
TerminalSquare,
|
2024-05-09 22:16:56 -07:00
|
|
|
UnfoldVertical,
|
2024-05-08 23:52:08 -07:00
|
|
|
} from "lucide-react";
|
2024-05-25 20:13:31 -07:00
|
|
|
import { useRef, useState } from "react";
|
2024-05-13 14:38:14 -07:00
|
|
|
import { toast } from "sonner";
|
2024-05-08 23:52:08 -07:00
|
|
|
|
2024-05-09 22:16:56 -07:00
|
|
|
export default function PreviewWindow({
|
|
|
|
collapsed,
|
|
|
|
open,
|
2024-05-23 23:05:01 -07:00
|
|
|
ip,
|
2024-05-09 22:16:56 -07:00
|
|
|
}: {
|
|
|
|
collapsed: boolean;
|
|
|
|
open: () => void;
|
2024-05-23 23:05:01 -07:00
|
|
|
ip: string;
|
2024-05-09 22:16:56 -07:00
|
|
|
}) {
|
2024-05-13 14:38:14 -07:00
|
|
|
const ref = useRef<HTMLIFrameElement>(null);
|
2024-05-25 20:13:31 -07:00
|
|
|
const [iframeKey, setIframeKey] = useState(0);
|
2024-05-13 14:38:14 -07:00
|
|
|
|
2024-05-08 23:52:08 -07:00
|
|
|
return (
|
|
|
|
<>
|
2024-05-09 22:16:56 -07:00
|
|
|
<div
|
|
|
|
className={`${
|
|
|
|
collapsed ? "h-full" : "h-10"
|
|
|
|
} select-none w-full flex gap-2`}
|
|
|
|
>
|
2024-05-11 19:28:37 -07:00
|
|
|
<div className="h-8 rounded-md px-3 bg-secondary flex items-center w-full justify-between">
|
|
|
|
<div className="text-xs">
|
|
|
|
Preview
|
2024-05-25 20:13:31 -07:00
|
|
|
{/* <span className="inline-block ml-2 items-center font-mono text-muted-foreground">
|
2024-05-24 01:28:50 -07:00
|
|
|
localhost:8000
|
2024-05-25 20:13:31 -07:00
|
|
|
</span> */}
|
2024-05-11 19:28:37 -07:00
|
|
|
</div>
|
2024-05-08 23:52:08 -07:00
|
|
|
<div className="flex space-x-1 translate-x-1">
|
2024-05-09 22:16:56 -07:00
|
|
|
{collapsed ? (
|
|
|
|
<PreviewButton onClick={open}>
|
|
|
|
<UnfoldVertical className="w-4 h-4" />
|
|
|
|
</PreviewButton>
|
|
|
|
) : (
|
|
|
|
<>
|
2024-05-13 14:38:14 -07:00
|
|
|
{/* Todo, make this open inspector */}
|
2024-05-25 20:13:31 -07:00
|
|
|
{/* <PreviewButton disabled onClick={() => {}}>
|
2024-05-09 22:16:56 -07:00
|
|
|
<TerminalSquare className="w-4 h-4" />
|
2024-05-25 20:13:31 -07:00
|
|
|
</PreviewButton> */}
|
2024-05-13 14:38:14 -07:00
|
|
|
|
|
|
|
<PreviewButton
|
|
|
|
onClick={() => {
|
2024-05-25 20:13:31 -07:00
|
|
|
navigator.clipboard.writeText(`http://${ip}:3000`);
|
2024-05-13 14:38:14 -07:00
|
|
|
toast.info("Copied preview link to clipboard");
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Link className="w-4 h-4" />
|
2024-05-09 22:16:56 -07:00
|
|
|
</PreviewButton>
|
2024-05-13 14:38:14 -07:00
|
|
|
<PreviewButton
|
|
|
|
onClick={() => {
|
2024-05-25 20:13:31 -07:00
|
|
|
// if (ref.current) {
|
|
|
|
// ref.current.contentWindow?.location.reload();
|
|
|
|
// }
|
|
|
|
setIframeKey((prev) => prev + 1);
|
2024-05-13 14:38:14 -07:00
|
|
|
}}
|
|
|
|
>
|
2024-05-09 22:16:56 -07:00
|
|
|
<RotateCw className="w-3 h-3" />
|
|
|
|
</PreviewButton>
|
|
|
|
</>
|
|
|
|
)}
|
2024-05-08 23:52:08 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-05-09 22:16:56 -07:00
|
|
|
{collapsed ? null : (
|
2024-05-12 22:06:11 -07:00
|
|
|
<div className="w-full grow rounded-md overflow-hidden bg-foreground">
|
|
|
|
<iframe
|
2024-05-25 20:13:31 -07:00
|
|
|
key={iframeKey}
|
2024-05-13 14:38:14 -07:00
|
|
|
ref={ref}
|
2024-05-12 22:06:11 -07:00
|
|
|
width={"100%"}
|
|
|
|
height={"100%"}
|
2024-05-25 20:13:31 -07:00
|
|
|
src={`http://${ip}:3000`}
|
2024-05-12 22:06:11 -07:00
|
|
|
/>
|
|
|
|
</div>
|
2024-05-09 22:16:56 -07:00
|
|
|
)}
|
2024-05-08 23:52:08 -07:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2024-05-09 22:16:56 -07:00
|
|
|
|
|
|
|
function PreviewButton({
|
|
|
|
children,
|
2024-05-13 14:38:14 -07:00
|
|
|
disabled = false,
|
2024-05-09 22:16:56 -07:00
|
|
|
onClick,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode;
|
2024-05-13 14:38:14 -07:00
|
|
|
disabled?: boolean;
|
2024-05-09 22:16:56 -07:00
|
|
|
onClick: () => void;
|
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<div
|
2024-05-13 14:38:14 -07:00
|
|
|
className={`${
|
|
|
|
disabled ? "pointer-events-none opacity-50" : ""
|
|
|
|
} p-0.5 h-5 w-5 ml-0.5 flex items-center justify-center transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm`}
|
2024-05-09 22:16:56 -07:00
|
|
|
onClick={onClick}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|